commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Joseph Olson" <olso...@dhfs.state.wi.us>
Subject Re: How to install Commons SCXML? How to use it?
Date Tue, 10 Jan 2006 13:47:48 GMT


>>> beaperez@unizar.es 01/10/06 05:35AM >>>
Hello, I am starting to study the use of Commons scxml and I would like
to know how I can install and use it.

Thanks

This a very, very small of amount of sample code that uses SCXML
The application needs to update objects/classes in a certain  seqeunce
I "attach" a listener for a particular state. With the State engine the
objects/classes
are updated in the desired seqeunce

CODE (look for the other text CODE)


This is wrapper class for SCXML
Constructor for this class
	public SCXMLEngines(Configuration lconfig,String pathtoConfig)
{

Configuration comes from jconfig
pathtoConfig points is a category in a jconfig application config file
  <category name="SCXML">
     <category name="GIS">
        <filename>${Project_Directory}\SCXML-Gis.xml</filename>
       
<Events>evGISUpdate1,evGISUpdate2,evGISUpdate3,evGISUpdate4,evGISUpdate5,evGISUpdate6,evGISUpdate7,evGISUpdate8,evGISUpdate9,evGISUpdate10,evGISUpdate11,evGISUpdate12</Events>
     </category>
  </category>

the file SCXML-Gis is below this class


package codetable;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.scxml.Context;
import org.apache.commons.scxml.Evaluator;
import org.apache.commons.scxml.EventDispatcher;
import org.apache.commons.scxml.SCXMLDigester;
import org.apache.commons.scxml.SCXMLExecutor;
import org.apache.commons.scxml.SCXMLListener;
import org.apache.commons.scxml.TriggerEvent;
import org.apache.commons.scxml.env.SimpleDispatcher;
import org.apache.commons.scxml.env.Tracer;
import org.apache.commons.scxml.env.jsp.ELEvaluator;
import org.apache.commons.scxml.model.ModelException;
import org.apache.commons.scxml.model.SCXML;
import org.apache.commons.scxml.model.State;
import org.apache.log4j.Logger;
import org.jconfig.Configuration;


public class SCXMLEngines {

	private final transient Configuration lconfig;
	static final Logger logger =
Logger.getLogger(SCXMLEngines.class);

	private transient String uri=null;
	private transient SCXML doc=null;
	private transient SCXMLExecutor exec=null;
	private transient String triggers[]=null;	
	
	private static Locale bLocale = new Locale("en", "US");

	private transient String pathtoConfig = null;
	private transient Evaluator engine=null;
	private transient Context rootCtx=null;
	private transient EventDispatcher dispatcher=null;
	private transient Tracer trc=null;
	
/* States used for updating J Drew map numbers update panels
	public static String AllState ="AllStates";
	public static String stGISUpdate ="stGISUpdate";
	public static String stUpdateMatrix ="stUpdateMatrix";
	public static String stUpdateImportPanel
="stUpdateImportPanel";
	D public static String stUpdateEditAbTable
="stUpdateEditAbTable";
	public static String stUpdateEditJTable ="stUpdateEditJTable";
	D public static String stUpdateEditPanel ="stUpdateEditPanel";
	public static String stUpdateLocationMapHandler
="stUpdateLocationMapHandler";
	public static String stUpdateLocationMapLayer
="stUpdateLocationMapLayer";
	public static String stUpdateShapeMapHandler
="stUpdateShapeMapHandler";
	public static String stUpdateShapeMapLayer
="stUpdateShapeMapLayer";
	D public static String stUpdateMapPanel ="stUpdateMapPanel";  
	public static String stGISFinished ="stGISFinished";
	public static String stFinal ="stFinal";
*/
	
	public SCXMLEngines(Configuration lconfig,String pathtoConfig)
{
		super();

		this.lconfig = lconfig;
		this.pathtoConfig = pathtoConfig;
		
		try {
			uri =
this.getCanonicalURI(this.lconfig.getProperty("filename","None",this.pathtoConfig));
			
			triggers = this.lconfig.getArray("Events",
													new
String[]{"No","No","No","No","No","No","No","No","No","No","No","No"},
													this.pathtoConfig);
			
			engine = new ELEvaluator();
			rootCtx = engine.newContext(null);
			dispatcher = new SimpleDispatcher();
			trc = new Tracer();
			doc = SCXMLDigester.digest(new URL(uri), 
											trc,

											rootCtx,
											engine);
			if (doc == null) {
				logger.info("The SCXML document " + uri
									+
" can not be parsed!");
				System.exit(-1);
			}
			logger.info(SCXMLDigester.serializeSCXML(doc));
			exec = new SCXMLExecutor(engine, dispatcher,
trc);
			doc.addListener(trc);
			exec.setSuperStep(true);
			exec.setStateMachine(doc);
		} catch (IOException e) {
			final  EtoLogger etologger = (EtoLogger)
CodeTable.appContainer.getInstanceOfType( EtoLogger.class );
			etologger.captureTrace(logger, e);
		} catch (ModelException e) {
			final  EtoLogger etologger = (EtoLogger)
CodeTable.appContainer.getInstanceOfType( EtoLogger.class );
			etologger.captureTrace(logger, e);
		}
logger.info(uri.toString()+"   "+triggers);
	}

	public SCXML getDoc() {
		return doc;
	}

	public Map getStates(){
		return doc.getStates();
	}
	
	public void addListener(final SCXMLListener listener, final
String statename) {
		final Map states = this.getStates();
		final State state = (State)states.get(statename);
		state.addListener(listener);
	}
	public void removeListener(final SCXMLListener listener, final
String statename) {
		final Map states = this.getStates();
		final State state = (State)states.get(statename);
		state.removeListener(listener);
	}

	public SCXMLExecutor getExec() {
		return exec;
	}

	public void execGISStates(){
		try {
			for(int i=0; i<triggers.length; ++i){
		        TriggerEvent[] evts = {new
TriggerEvent(triggers[i], TriggerEvent.SIGNAL_EVENT, null)};
				exec.triggerEvents(evts);
			}
		} catch (ModelException e) {
			final  EtoLogger etologger = (EtoLogger)
CodeTable.appContainer.getInstanceOfType( EtoLogger.class );
			etologger.captureTrace(logger, e);
		}
	}

    /***
     * @param uri an absolute or relative URL
     * @return java.lang.String canonical URL (absolute)
     * @throws java.io.IOException if a relative URL can not be
resolved
     *         to a local file
     */
    private String getCanonicalURI(final String uri)
    throws IOException {
        if (uri.toLowerCase(bLocale).startsWith("http://")
            || uri.toLowerCase(bLocale).startsWith("file://")) {
                return uri;
        }
        final File inFile = new File(uri);
        return "file:///" + inFile.getCanonicalPath();
    }
	
	
}


CODE
State Transition File SCXML-Gis


<?xml version="1.0"?>

<scxml        xmlns="http://www.w3.org/2005/01/SCXML"
              version="1.0"
              initialstate="AllStates">

  <state      id="AllStates">  
    <transition  event="evGISUpdate1">  
       <target   next="stGISUpdate"/> 
    </transition>
  </state> 

  <state      id="stGISUpdate">  
    <transition  event="evGISUpdate2">  
       <target   next="stUpdateMatrix"/>
    </transition>
  </state> 

  <state      id="stUpdateMatrix">     
    <transition  event="evGISUpdate3">  
       <target   next="stUpdateImportPanel"/>
    </transition>
  </state>

  <state      id="stUpdateImportPanel">     
    <transition  event="evGISUpdate4">  
       <target   next="stUpdateEditAbTable"/>
    </transition>
  </state>

  <state      id="stUpdateEditAbTable">     
    <transition  event="evGISUpdate5">  
       <target   next="stUpdateEditJTable"/>
    </transition>
  </state>

  <state      id="stUpdateEditJTable">     
    <transition  event="evGISUpdate6">  
       <target   next="stUpdateEditPanel"/>
    </transition>
  </state>

  <state      id="stUpdateEditPanel">     
    <transition  event="evGISUpdate7">  
       <target   next="stUpdateLocationMapHandler"/>
    </transition>
  </state>

  <state      id="stUpdateLocationMapHandler">     
    <transition  event="evGISUpdate8">  
       <target   next="stUpdateLocationMapLayer"/>
    </transition>
  </state>

  <state      id="stUpdateLocationMapLayer">     
    <transition  event="evGISUpdate9">  
       <target   next="stUpdateShapeMapHandler"/>
    </transition>
  </state>

  <state      id="stUpdateShapeMapHandler">     
    <transition  event="evGISUpdate10">
       <target   next="stUpdateShapeMapLayer"/>
    </transition>
  </state>

  <state      id="stUpdateShapeMapLayer">     
    <transition  event="evGISUpdate11">
       <target   next="stUpdateMapPanel"/>
    </transition>
  </state>

  <state      id="stUpdateMapPanel">     
    <transition  event="evGISUpdate12">
       <target   next="stGISFinished"/>
    </transition>
  </state>

  <state      id="stGISFinished">     
    <transition  event="evGISUpdate13">
       <target   next="stFinal"/> 
    </transition>
  </state> 

  <state      id="stFinal"      final="true"/> 

</scxml>

CODE
this is how I create/store references to this class using YAN(IOC
Container)
			GISDisplayContainer.registerValue("lconfig",
lconfig);
			ITextMapper places = new PlacetoRowMap(lDAO);
			ITextMapper interests = new
InteresttoColumnMap();
			GISDisplayContainer.registerValue("places",
places);
			GISDisplayContainer.registerValue("interests",
interests);

			GISDisplayContainer.registerComponent(Components.ctor(SCXMLEngines.class)
													.withArgument(1,Components.value("SCXML/GIS")));
			GISDisplayContainer.registerComponent(Components.ctor(GIS_Data_Mediator.class)
													.withArgument(3,
Components.useKey("places"))
													.withArgument(4,
Components.useKey("interests"))
													.singleton().label("GIS
Data Mediator")
													);

CODE
Constructor within GIS_Data_Mediator
	public GIS_Data_Mediator(Configuration lconfig,
								DAO
pvDAO,
								SCXMLEngines
stateEngine,
								ITextMapper
placeMap,
								ITextMapper
interestMap) {
		super();
		this.lconfig = lconfig;
		this.pDAO = pvDAO;
		this.stateEngine = stateEngine; 
		this.placeMap = placeMap;
		this.interestMap = interestMap;
	}


CODE
Code within GIS_Data_Mediator that attaches listeners to the state
Engine
		stateEngine.addListener(gPanel, "stUpdateEditPanel");
		stateEngine.addListener(gTable, "stUpdateEditAbTable");

CODE
Code used by GIS_Data_Grid_JPanel.
public class GIS_Data_Grid_JPanel extends JPanel implements
SCXMLListener{


	public void onEntry(TransitionTarget arg0) {
logger.info("Started by SCML in grid jpanel");		
	}
	public void onExit(TransitionTarget arg0) {
	}
	public void onTransition(TransitionTarget arg0, TransitionTarget
arg1, Transition arg2) {
	}


CODE
Part of build.xml with references to the files required by SCXML
       <pathelement
location="D:/Commons-Scxml/apache-tomcat-5.5.12-embed/lib/servlets-default.jar"/>
        <pathelement
location="D:/Commons-Scxml/apache-tomcat-5.5.12-embed/lib/jsp-api.jar"/>
        <pathelement
location="D:/Commons-Scxml/apache-tomcat-5.5.12-embed/lib/servlet-api.jar"/>
        <pathelement
location="D:/Commons-Scxml/commons-scxml-1.0-SNAPSHOT.jar"/>
        <pathelement
location="D:/commons-digester-1.7/commons-digester-1.7.jar"/>
        <pathelement
location="D:/commons-beanutils-1.7.0/commons-beanutils-core.jar"/>
        <pathelement
location="D:/commons-beanutils-1.7.0/commons-beanutils.jar"/>
        <pathelement
location="D:/commons-beanutils-1.7.0/commons-beanutils-bean-collections.jar"/>
        <pathelement location="D:/commons-el-1.0/commons-el.jar"/>


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message