Author: ate
Date: Fri Mar 14 01:26:24 2014
New Revision: 1577405

URL: http://svn.apache.org/r1577405
Log:
SCXML-196: rename initialScript to globalScript and no need to wrap the 
globalScript in an artificial Transition and State

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/env/SimpleErrorReporter.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/io/SCXMLWriter.java
    
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/Action.java
    
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/SCXML.java
    
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/Script.java
    
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/semantics/SCXMLSemanticsImpl.java
    
commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/io/SCXMLReaderTest.java
    
commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/io/SCXMLWriterTest.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=1577405&r1=1577404&r2=1577405&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 Mar 14 01:26:24 2014
@@ -89,7 +89,7 @@ public class SCInstance implements Seria
     /**
      * The initial script context
      */
-    private Context initialScriptContext;
+    private Context globalScriptContext;
 
     /**
      * The owning state machine executor.
@@ -152,14 +152,14 @@ public class SCInstance implements Seria
         this.rootContext = context;
     }
 
-    Context getInitialScriptContext() {
-        if (initialScriptContext == null) {
+    public Context getGlobalScriptContext() {
+        if (globalScriptContext == null) {
             Context rootContext = getRootContext();
             if (rootContext != null) {
-                initialScriptContext = evaluator.newContext(getRootContext());
+                globalScriptContext = evaluator.newContext(getRootContext());
             }
         }
-        return initialScriptContext;
+        return globalScriptContext;
     }
 
     /**
@@ -192,15 +192,8 @@ public class SCInstance implements Seria
         if (context == null) {
             TransitionTarget parent = transitionTarget.getParent();
             if (parent == null) {
-                if (executor != null && 
executor.getStateMachine().getInitialScript() != null &&
-                        transitionTarget == 
executor.getStateMachine().getInitialScript().getParent().getParent()) {
-                    // initialScript
-                    return getInitialScriptContext();
-                }
-                else {
-                    // docroot
-                    context = evaluator.newContext(getInitialScriptContext());
-                }
+                // docroot
+                context = evaluator.newContext(getGlobalScriptContext());
             } 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=1577405&r1=1577404&r2=1577405&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 Mar 14 01:26:24 2014
@@ -32,7 +32,6 @@ 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;
@@ -412,11 +411,8 @@ 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();
-            }
+        if (scInstance.getGlobalScriptContext() != null) {
+            scInstance.getGlobalScriptContext().reset();
         }
         // all states and parallels, only states have variable contexts
         for (TransitionTarget tt : stateMachine.getTargets().values()) {
@@ -437,7 +433,7 @@ public class SCXMLExecutor implements Se
         // CreateEmptyStatus
         currentStatus = new Status();
         Step step = new Step(null, currentStatus);
-        // execute initial script if defined
+        // execute global script if defined
         semantics.executeGlobalScript(step, stateMachine, eventdispatcher, 
errorReporter, scInstance);
         // DetermineInitialStates
         semantics.determineInitialStates(step, stateMachine, errorReporter, 
scInstance);

Modified: 
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/SimpleErrorReporter.java
URL: 
http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/SimpleErrorReporter.java?rev=1577405&r1=1577404&r2=1577405&view=diff
==============================================================================
--- 
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/SimpleErrorReporter.java
 (original)
+++ 
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/SimpleErrorReporter.java
 Fri Mar 14 01:26:24 2014
@@ -105,6 +105,10 @@ public class SimpleErrorReporter impleme
                 TransitionTarget parent = ((Executable) errCtx).getParent();
                 msg.append("Expression error inside " + 
LogUtils.getTTPath(parent));
             }
+            else if (errCtx instanceof SCXML) {
+                // Global Script
+                msg.append("Expression error inside the global script");
+            }
         }
         handleErrorMessage(errorCode, errDetail, errCtx, msg);
     }

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=1577405&r1=1577404&r2=1577405&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 Mar 14 01:26:24 2014
@@ -268,8 +268,6 @@ 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
@@ -616,7 +614,7 @@ public final class SCXMLReader {
         }
         readNamespaces(configuration, scxml);
 
-        boolean hasInitialScript = false;
+        boolean hasGlobalScript = false;
 
         loop : while (reader.hasNext()) {
             String name, nsURI;
@@ -634,9 +632,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 if (ELEM_SCRIPT.equals(name) && 
!hasGlobalScript) {
+                            readGlobalScript(reader, configuration, scxml);
+                            hasGlobalScript = true;
                         } else {
                             reportIgnoredElement(reader, configuration, 
ELEM_SCXML, nsURI, name);
                         }
@@ -1850,21 +1848,15 @@ public final class SCXMLReader {
      *
      * @throws XMLStreamException An exception processing the underlying 
{@link XMLStreamReader}.
      */
-    private static void readInitialScript(final XMLStreamReader reader, final 
Configuration configuration,
-                                   final SCXML scxml)
+    private static void readGlobalScript(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);
+        Script globalScript = new Script();
+        globalScript.setGlobalScript(true);
+        readNamespaces(configuration, globalScript);
+        globalScript.setBody(readBody(reader, configuration, XMLNS_SCXML, 
ELEM_SCRIPT));
+        scxml.setGlobalScript(globalScript);
     }
 
     /**

Modified: 
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/SCXMLWriter.java
URL: 
http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/SCXMLWriter.java?rev=1577405&r1=1577404&r2=1577405&view=diff
==============================================================================
--- 
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/SCXMLWriter.java
 (original)
+++ 
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/SCXMLWriter.java
 Fri Mar 14 01:26:24 2014
@@ -449,9 +449,12 @@ public class SCXMLWriter {
         // Marker to indicate generated document
         writer.writeComment(XMLNS_COMMONS_SCXML);
 
-        // Write initial script if defined
-        if (scxml.getInitialScript() != null) {
-            writeExecutableContent(writer, 
scxml.getInitialScript().getParent().getActions());
+        // Write global script if defined
+        if (scxml.getGlobalScript() != null) {
+            Script s = scxml.getGlobalScript();
+            writer.writeStartElement(XMLNS_SCXML, ELEM_SCRIPT);
+            writer.writeCData(s.getScript());
+            writer.writeEndElement();
         }
 
         // Children

Modified: 
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/Action.java
URL: 
http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/Action.java?rev=1577405&r1=1577404&r2=1577405&view=diff
==============================================================================
--- 
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/Action.java
 (original)
+++ 
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/Action.java
 Fri Mar 14 01:26:24 2014
@@ -108,6 +108,10 @@ public abstract class Action implements 
      */
     public final TransitionTarget getParentTransitionTarget()
     throws ModelException {
+        if (parent == null && this instanceof Script && 
((Script)this).isGlobalScript()) {
+            // global script doesn't have a TransitionTarget
+            return null;
+        }
         TransitionTarget tt = parent.getParent();
         if (tt instanceof State || tt instanceof Parallel) {
             return tt;

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=1577405&r1=1577404&r2=1577405&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 Mar 14 01:26:24 2014
@@ -88,7 +88,7 @@ public class SCXML implements Serializab
     /**
      * Optional property holding the initial script for this SCXML document.
      */
-    private Script initialScript;
+    private Script globalScript;
 
     /**
      * The immediate child targets of this SCXML document root.
@@ -115,12 +115,12 @@ public class SCXML implements Serializab
         this.targets = new HashMap<String, TransitionTarget>();
     }
 
-    public Script getInitialScript() {
-        return initialScript;
+    public Script getGlobalScript() {
+        return globalScript;
     }
 
-    public void setInitialScript(Script script) {
-        this.initialScript = script;
+    public void setGlobalScript(Script script) {
+        this.globalScript = script;
     }
 
     /**

Modified: 
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/Script.java
URL: 
http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/Script.java?rev=1577405&r1=1577404&r2=1577405&view=diff
==============================================================================
--- 
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/Script.java
 (original)
+++ 
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/Script.java
 Fri Mar 14 01:26:24 2014
@@ -39,7 +39,8 @@ public class Script extends Action imple
      * Serial version UID.
      */
     private static final long serialVersionUID = 1L;
-    
+
+    private boolean globalScript;
     private String body;
 
     /**
@@ -49,6 +50,14 @@ public class Script extends Action imple
         super();
     }
 
+    public boolean isGlobalScript() {
+        return globalScript;
+    }
+
+    public void setGlobalScript(final boolean globalScript) {
+        this.globalScript = globalScript;
+    }
+
     @Override
     public String getBody() {
         return body;
@@ -76,7 +85,8 @@ public class Script extends Action imple
             final ErrorReporter errRep, final SCInstance scInstance,
             final Log appLog, final Collection<TriggerEvent> derivedEvents)
     throws ModelException, SCXMLExpressionException {
-        Context ctx = scInstance.getContext(getParentTransitionTarget());
+        Context ctx = isGlobalScript() ? scInstance.getGlobalScriptContext() :
+                scInstance.getContext(getParentTransitionTarget());
         ctx.setLocal(getNamespacesKey(), getNamespaces());
         Evaluator eval = scInstance.getEvaluator();
         eval.evalScript(ctx, getScript());

Modified: 
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/semantics/SCXMLSemanticsImpl.java
URL: 
http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/semantics/SCXMLSemanticsImpl.java?rev=1577405&r1=1577404&r2=1577405&view=diff
==============================================================================
--- 
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/semantics/SCXMLSemanticsImpl.java
 (original)
+++ 
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/semantics/SCXMLSemanticsImpl.java
 Fri Mar 14 01:26:24 2014
@@ -214,9 +214,9 @@ public class SCXMLSemanticsImpl implemen
 
     public void executeGlobalScript(final Step step, final SCXML stateMachine, 
final EventDispatcher evtDispatcher,
                                     final ErrorReporter errRep, final 
SCInstance scInstance) throws ModelException {
-        if (stateMachine.getInitialScript() != null) {
+        if (stateMachine.getGlobalScript() != null) {
             try {
-                stateMachine.getInitialScript().execute(evtDispatcher, errRep, 
scInstance, appLog,
+                stateMachine.getGlobalScript().execute(evtDispatcher, errRep, 
scInstance, appLog,
                         step.getAfterStatus().getEvents());
             } catch (SCXMLExpressionException e) {
                 errRep.onError(ErrorConstants.EXPRESSION_ERROR, 
e.getMessage(), stateMachine);

Modified: 
commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/io/SCXMLReaderTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/io/SCXMLReaderTest.java?rev=1577405&r1=1577404&r2=1577405&view=diff
==============================================================================
--- 
commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/io/SCXMLReaderTest.java
 (original)
+++ 
commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/io/SCXMLReaderTest.java
 Fri Mar 14 01:26:24 2014
@@ -297,12 +297,12 @@ public class SCXMLReaderTest {
     public void testSCXMLReaderGroovyClosure() throws Exception {
         scxml = SCXMLTestHelper.parse(groovyClosure);
         Assert.assertNotNull(scxml);
-        Assert.assertNotNull(scxml.getInitialScript());
+        Assert.assertNotNull(scxml.getGlobalScript());
         scxmlAsString = serialize(scxml);
         Assert.assertNotNull(scxmlAsString);
         scxml = SCXMLTestHelper.parse(new StringReader(scxmlAsString), null);
         Assert.assertNotNull(scxml);
-        Assert.assertNotNull(scxml.getInitialScript());
+        Assert.assertNotNull(scxml.getGlobalScript());
     }
 
     private String serialize(final SCXML scxml) throws IOException, 
XMLStreamException {

Modified: 
commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/io/SCXMLWriterTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/io/SCXMLWriterTest.java?rev=1577405&r1=1577404&r2=1577405&view=diff
==============================================================================
--- 
commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/io/SCXMLWriterTest.java
 (original)
+++ 
commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/io/SCXMLWriterTest.java
 Fri Mar 14 01:26:24 2014
@@ -22,6 +22,7 @@ import java.util.Map;
 
 import org.apache.commons.scxml2.model.Parallel;
 import org.apache.commons.scxml2.model.SCXML;
+import org.apache.commons.scxml2.model.Script;
 import org.apache.commons.scxml2.model.State;
 import org.junit.Assert;
 import org.junit.Test;
@@ -123,4 +124,29 @@ public class SCXMLWriterTest {
         Assert.assertEquals(assertValue, SCXMLWriter.write(scxml, new 
SCXMLWriter.Configuration(true, false)));
      }
 
+    @Test
+    public void testSerializeGlobalScript() throws IOException, 
XMLStreamException {
+        SCXML scxml = new SCXML();
+        Map<String, String> namespaces = new LinkedHashMap<String, String>();
+        scxml.setNamespaces(namespaces);
+        scxml.setVersion("1.0");
+        scxml.setInitial("S1");
+
+        Script script = new Script();
+        script.setGlobalScript(true);
+        script.setBody("foo=\"abc\"");
+        scxml.setGlobalScript(script);
+
+        State s1 = new State();
+        s1.setId("S1");
+
+        scxml.addChild(s1);
+
+        String assertValue = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+                + "<scxml xmlns=\"http://www.w3.org/2005/07/scxml\"; "
+                + "xmlns:cs=\"http://commons.apache.org/scxml\"; 
version=\"1.0\" initial=\"S1\">"
+                + 
"<!--http://commons.apache.org/scxml--><script><![CDATA[foo=\"abc\"]]></script><state
 id=\"S1\"></state></scxml>";
+
+        Assert.assertEquals(assertValue, SCXMLWriter.write(scxml, new 
SCXMLWriter.Configuration(true, false)));
+    }
 }


Reply via email to