commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject svn commit: r1559097 - in /commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2: SCInstance.java SCXMLExecutor.java io/SCXMLReader.java model/SCXML.java
Date Fri, 17 Jan 2014 13:23:06 GMT
Author: ate
Date: Fri Jan 17 13:23:05 2014
New Revision: 1559097

URL: http://svn.apache.org/r1559097
Log:
SCXML-187: Add support for initial SCXML script (SCXML spec 3.2.2: <scxml> children)
See: https://issues.apache.org/jira/browse/SCXML-187

Modified:
    commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/SCInstance.java
    commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/SCXMLExecutor.java
    commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/SCXMLReader.java
    commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/SCXML.java

Modified: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/SCInstance.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/SCInstance.java?rev=1559097&r1=1559096&r2=1559097&view=diff
==============================================================================
--- commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/SCInstance.java (original)
+++ commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/SCInstance.java Fri
Jan 17 13:23:05 2014
@@ -87,6 +87,11 @@ public class SCInstance implements Seria
     private Context rootContext;
 
     /**
+     * The initial script context
+     */
+    private Context initialScriptContext;
+
+    /**
      * The owning state machine executor.
      */
     private SCXMLExecutor executor;
@@ -147,6 +152,16 @@ public class SCInstance implements Seria
         this.rootContext = context;
     }
 
+    Context getInitialScriptContext() {
+        if (initialScriptContext == null) {
+            Context rootContext = getRootContext();
+            if (rootContext != null) {
+                initialScriptContext = evaluator.newContext(getRootContext());
+            }
+        }
+        return initialScriptContext;
+    }
+
     /**
      * Get the notification registry.
      *
@@ -177,8 +192,15 @@ public class SCInstance implements Seria
         if (context == null) {
             TransitionTarget parent = transitionTarget.getParent();
             if (parent == null) {
-                // docroot
-                context = evaluator.newContext(getRootContext());
+                if (executor != null && executor.getStateMachine().getInitialScript()
!= null &&
+                        transitionTarget == executor.getStateMachine().getInitialScript().getParent().getParent())
{
+                    // initialScript
+                    return getInitialScriptContext();
+                }
+                else {
+                    // docroot
+                    context = evaluator.newContext(getInitialScriptContext());
+                }
             } else {
                 context = evaluator.newContext(getContext(parent));
             }

Modified: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/SCXMLExecutor.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/SCXMLExecutor.java?rev=1559097&r1=1559096&r2=1559097&view=diff
==============================================================================
--- commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/SCXMLExecutor.java
(original)
+++ commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/SCXMLExecutor.java
Fri Jan 17 13:23:05 2014
@@ -32,6 +32,7 @@ import org.apache.commons.scxml2.model.M
 import org.apache.commons.scxml2.model.Observable;
 import org.apache.commons.scxml2.model.SCXML;
 import org.apache.commons.scxml2.model.State;
+import org.apache.commons.scxml2.model.Transition;
 import org.apache.commons.scxml2.model.TransitionTarget;
 import org.apache.commons.scxml2.semantics.SCXMLSemanticsImpl;
 import org.apache.commons.scxml2.system.EventVariable;
@@ -224,6 +225,12 @@ public class SCXMLExecutor implements Se
             SCXMLHelper.cloneDatamodel(rootdm, rootCtx,
                 scInstance.getEvaluator(), log);
         }
+        if (stateMachine.getInitialScript() != null) {
+            Context initialScriptCtx = scInstance.getContext(stateMachine.getInitialScript().getParentTransitionTarget());
+            if (initialScriptCtx != null) {
+                initialScriptCtx.reset();
+            }
+        }
         // all states and parallels, only states have variable contexts
         for (TransitionTarget tt : stateMachine.getTargets().values()) {
             if (tt instanceof State) {
@@ -247,6 +254,10 @@ public class SCXMLExecutor implements Se
         semantics.determineInitialStates(stateMachine,
                 step.getAfterStatus().getStates(),
                 step.getEntryList(), errorReporter, scInstance);
+        // execute initial script if defined configured as transition so as to not trigger
events
+        if (stateMachine.getInitialScript() != null) {
+            step.getTransitList().add((Transition)stateMachine.getInitialScript().getParent());
+        }
         // ExecuteActions
         semantics.executeActions(step, stateMachine, eventdispatcher,
                 errorReporter, scInstance);

Modified: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/SCXMLReader.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/SCXMLReader.java?rev=1559097&r1=1559096&r2=1559097&view=diff
==============================================================================
--- commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/SCXMLReader.java
(original)
+++ commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/SCXMLReader.java
Fri Jan 17 13:23:05 2014
@@ -242,6 +242,8 @@ public final class SCXMLReader {
     private static final String ATTR_TYPE = "type";
     private static final String ATTR_VERSION = "version";
 
+    private static final String ID_INITIAL_SCRIPT = "_initialScript";
+
     //------------------------- PUBLIC API METHODS -------------------------//
     /*
      * Public methods
@@ -585,6 +587,8 @@ public final class SCXMLReader {
         scxml.setVersion(readAV(reader, ATTR_VERSION));
         readNamespaces(configuration, scxml);
 
+        boolean hasInitialScript = false;
+
         loop : while (reader.hasNext()) {
             String name, nsURI;
             switch (reader.next()) {
@@ -601,6 +605,9 @@ public final class SCXMLReader {
                             readFinal(reader, configuration, scxml, null);
                         } else if (ELEM_DATAMODEL.equals(name)) {
                             readDatamodel(reader, configuration, scxml, null);
+                        } else if (ELEM_SCRIPT.equals(name) && !hasInitialScript)
{
+                            readInitialScript(reader, configuration, scxml);
+                            hasInitialScript = true;
                         } else {
                             reportIgnoredElement(reader, configuration, ELEM_SCXML, nsURI,
name);
                         }
@@ -1750,6 +1757,33 @@ public final class SCXMLReader {
     }
 
     /**
+     * Read the contents of the initial &lt;script&gt; element.
+     * @see <a href="http://www.w3.org/TR/2013/WD-scxml-20130801/#scxml">http://www.w3.org/TR/2013/WD-scxml-20130801/#scxml<a>
section 3.2.2
+     *
+     * @param reader The {@link XMLStreamReader} providing the SCXML document to parse.
+     * @param configuration The {@link Configuration} to use while parsing.
+     * @param scxml The root of the object model being parsed.
+     *
+     * @throws XMLStreamException An exception processing the underlying {@link XMLStreamReader}.
+     */
+    private static void readInitialScript(final XMLStreamReader reader, final Configuration
configuration,
+                                   final SCXML scxml)
+            throws XMLStreamException {
+
+        Script initialScript = new Script();
+        State initialScriptState = new State();
+        initialScriptState.setId(ID_INITIAL_SCRIPT);
+        Transition initialScriptTransition = new Transition();
+        initialScript.setParent(initialScriptTransition);
+        initialScriptTransition.getActions().add(initialScript);
+        initialScriptState.addTransition(initialScriptTransition);
+
+        readNamespaces(configuration, initialScript);
+        initialScript.setBody(readBody(reader, configuration, XMLNS_SCXML, ELEM_SCRIPT));
+        scxml.setInitialScript(initialScript);
+    }
+
+    /**
      * Read the contents of this &lt;var&gt; element.
      *
      * @param reader The {@link XMLStreamReader} providing the SCXML document to parse.

Modified: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/SCXML.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/SCXML.java?rev=1559097&r1=1559096&r2=1559097&view=diff
==============================================================================
--- commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/SCXML.java (original)
+++ commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/SCXML.java Fri
Jan 17 13:23:05 2014
@@ -86,6 +86,11 @@ public class SCXML implements Serializab
     private Datamodel datamodel;
 
     /**
+     * Optional property holding the initial script for this SCXML document.
+     */
+    private Script initialScript;
+
+    /**
      * The immediate child targets of this SCXML document root.
      */
     private Map<String, TransitionTarget> children;
@@ -110,6 +115,14 @@ public class SCXML implements Serializab
         this.targets = new HashMap<String, TransitionTarget>();
     }
 
+    public Script getInitialScript() {
+        return initialScript;
+    }
+
+    public void setInitialScript(Script script) {
+        this.initialScript = script;
+    }
+
     /**
      * Get the initial TransitionTarget.
      *



Mime
View raw message