commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ra...@apache.org
Subject svn commit: r391133 - in /jakarta/commons/sandbox/scxml/trunk/src: main/java/org/apache/commons/scxml/ test/java/org/apache/commons/scxml/ test/java/org/apache/commons/scxml/env/jexl/
Date Mon, 03 Apr 2006 20:13:59 GMT
Author: rahul
Date: Mon Apr  3 13:13:56 2006
New Revision: 391133

URL: http://svn.apache.org/viewcvs?rev=391133&view=rev
Log:
Applying additions between Sep '05 and Jan '06 SCXML WDs related to events [part 1].

Event payload must be available to the transition condition expression and the executable
content on transition under the special variable "_eventdata".

Added a couple of test cases with sample SCXML documents illustrating the usage.

Added:
    jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/EventDataTest.java
  (with props)
    jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/jexl/eventdata-01.xml
  (with props)
    jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/jexl/eventdata-02.xml
  (with props)
Modified:
    jakarta/commons/sandbox/scxml/trunk/src/main/java/org/apache/commons/scxml/SCXMLExecutor.java
    jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/SCXMLTestHelper.java
    jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/SCXMLTestSuite.java

Modified: jakarta/commons/sandbox/scxml/trunk/src/main/java/org/apache/commons/scxml/SCXMLExecutor.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/scxml/trunk/src/main/java/org/apache/commons/scxml/SCXMLExecutor.java?rev=391133&r1=391132&r2=391133&view=diff
==============================================================================
--- jakarta/commons/sandbox/scxml/trunk/src/main/java/org/apache/commons/scxml/SCXMLExecutor.java
(original)
+++ jakarta/commons/sandbox/scxml/trunk/src/main/java/org/apache/commons/scxml/SCXMLExecutor.java
Mon Apr  3 13:13:56 2006
@@ -19,7 +19,9 @@
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.Iterator;
+import java.util.Map;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -96,6 +98,8 @@
      */
     public void triggerEvents(final TriggerEvent[] evts)
             throws ModelException {
+        // Set event data, saving old values
+        Object[] oldData = setEventData(evts);
         ArrayList evs = new ArrayList(Arrays.asList(evts));
         do {
             // CreateStep
@@ -119,10 +123,26 @@
                 evs.clear();
             }
         } while(superStep && currentStatus.getEvents().size() > 0);
+        // Restore event data
+        restoreEventData(oldData);
         logState();
     }
 
     /**
+     * Convenience method when only one event needs to be triggered.
+     *
+     * @param evt
+     *            the external events which triggered during the last
+     *            time quantum
+     * @throws ModelException in case there is a fatal SCXML object
+     *            model problem.
+     */
+    public void triggerEvent(final TriggerEvent evt)
+            throws ModelException {
+        triggerEvents(new TriggerEvent[] {evt});
+    }
+
+    /**
      * Constructor.
      *
      * @param expEvaluator The expression evaluator
@@ -455,6 +475,49 @@
         scInstance.getRootContext().setLocal("_ALL_STATES",
             SCXMLHelper.getAncestorClosure(currentStatus.getStates(), null));
     }
+
+    /**
+     * @param evts The events being triggered.
+     * @return Object[] Previous values.
+     */
+    private Object[] setEventData(final TriggerEvent[] evts) {
+        Context rootCtx = scInstance.getRootContext();
+        Object[] oldData = {rootCtx.get(EVENT_DATA),
+            rootCtx.get(EVENT_DATA_MAP)};
+        Object eventData = null;
+        Map payloadMap = new HashMap();
+        int len = evts.length;
+        for (int i = 0; i < len; i++) {
+            TriggerEvent te = evts[i];
+            payloadMap.put(te.getName(), te.getPayload());
+        }
+        if (len == 1) {
+            // we have only one event
+            eventData = evts[0].getPayload();
+        }
+        rootCtx.setLocal(EVENT_DATA, eventData);
+        rootCtx.setLocal(EVENT_DATA_MAP, payloadMap);
+        return oldData;
+    }
+
+    /**
+     * @param oldData The old values to restore to.
+     */
+    private void restoreEventData(final Object[] oldData) {
+        scInstance.getRootContext().setLocal(EVENT_DATA, oldData[0]);
+        scInstance.getRootContext().setLocal(EVENT_DATA_MAP, oldData[1]);
+    }
+
+    /**
+     * The special variable for storing single event data / payload.
+     */
+    private static final String EVENT_DATA = "_eventdata";
+
+    /**
+     * The special variable for storing event data / payload,
+     * when multiple events are triggered, keyed by event name.
+     */
+    private static final String EVENT_DATA_MAP = "_eventdatamap";
 
     /**
      * SCXMLExecutor put into motion without setting a model (state machine).

Added: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/EventDataTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/EventDataTest.java?rev=391133&view=auto
==============================================================================
--- jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/EventDataTest.java
(added)
+++ jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/EventDataTest.java
Mon Apr  3 13:13:56 2006
@@ -0,0 +1,144 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.scxml;
+
+import java.net.URL;
+import java.util.Set;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+import org.apache.commons.scxml.model.State;
+/**
+ * Unit tests {@link org.apache.commons.scxml.SCXMLExecutor}.
+ * Testing special variable "_eventdata"
+ */
+public class EventDataTest extends TestCase {
+    /**
+     * Construct a new instance of SCXMLExecutorTest with
+     * the specified name
+     */
+    public EventDataTest(String name) {
+        super(name);
+    }
+
+    public static Test suite() {
+        TestSuite suite = new TestSuite(EventDataTest.class);
+        suite.setName("SCXML Executor Tests, _eventdata special variable");
+        return suite;
+    }
+
+    // Test data
+    private URL eventdata01, eventdata02;
+    private SCXMLExecutor exec;
+
+    /**
+     * Set up instance variables required by this test case.
+     */
+    public void setUp() {
+        eventdata01 = this.getClass().getClassLoader().
+            getResource("org/apache/commons/scxml/env/jexl/eventdata-01.xml");
+        eventdata02 = this.getClass().getClassLoader().
+            getResource("org/apache/commons/scxml/env/jexl/eventdata-02.xml");
+    }
+
+    /**
+     * Tear down instance variables required by this test case.
+     */
+    public void tearDown() {
+        eventdata01 = eventdata02 = null;
+    }
+
+    /**
+     * Test the SCXML documents, usage of "_eventdata"
+     */
+    public void testEventdata01Sample() {
+    	exec = SCXMLTestHelper.getExecutor(eventdata01);
+        assertNotNull(exec);
+        try {
+            Set currentStates = exec.getCurrentStatus().getStates();
+            assertEquals(1, currentStates.size());
+            assertEquals("state1", ((State)currentStates.iterator().
+                next()).getId());
+            TriggerEvent te = new TriggerEvent("event.foo",
+                TriggerEvent.SIGNAL_EVENT, new Integer(3));
+            currentStates = SCXMLTestHelper.fireEvent(exec, te);
+            assertEquals(1, currentStates.size());
+            assertEquals("state3", ((State)currentStates.iterator().
+                next()).getId());
+            TriggerEvent[] evts = new TriggerEvent[] { te,
+                new TriggerEvent("event.bar", TriggerEvent.SIGNAL_EVENT,
+                new Integer(6))};
+            currentStates = SCXMLTestHelper.fireEvent(exec, evts);
+            assertEquals(1, currentStates.size());
+            assertEquals("state6", ((State)currentStates.iterator().
+                next()).getId());
+            te = new TriggerEvent("event.baz",
+                TriggerEvent.SIGNAL_EVENT, new Integer(7));
+            currentStates = SCXMLTestHelper.fireEvent(exec, te);
+            assertEquals(1, currentStates.size());
+            assertEquals("state7", ((State)currentStates.iterator().
+                next()).getId());
+        } catch (Exception e) {
+            fail(e.getMessage());
+        }
+    }
+
+    public void testEventdata02Sample() {
+    	exec = SCXMLTestHelper.getExecutor(eventdata02);
+        assertNotNull(exec);
+        try {
+            Set currentStates = exec.getCurrentStatus().getStates();
+            assertEquals(1, currentStates.size());
+            assertEquals("state0", ((State)currentStates.iterator().
+                next()).getId());
+            TriggerEvent te1 = new TriggerEvent("connection.alerting",
+                TriggerEvent.SIGNAL_EVENT, "line2");
+            currentStates = SCXMLTestHelper.fireEvent(exec, te1);
+            assertEquals(1, currentStates.size());
+            assertEquals("state2", ((State)currentStates.iterator().
+                next()).getId());
+            TriggerEvent te2 = new TriggerEvent("connection.alerting",
+                TriggerEvent.SIGNAL_EVENT,
+                new ConnectionAlertingPayload(4));
+            currentStates = SCXMLTestHelper.fireEvent(exec, te2);
+            assertEquals(1, currentStates.size());
+            assertEquals("state4", ((State)currentStates.iterator().
+                next()).getId());
+        } catch (Exception e) {
+            fail(e.getMessage());
+        }
+    }
+
+    public static class ConnectionAlertingPayload {
+        private int line;
+        public ConnectionAlertingPayload(int line) {
+            this.line = line;
+        }
+        public void setLine(int line) {
+            this.line = line;
+        }
+        public int getLine() {
+            return line;
+        }
+    }
+
+    public static void main(String args[]) {
+        TestRunner.run(suite());
+    }
+}

Propchange: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/EventDataTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/EventDataTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/SCXMLTestHelper.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/SCXMLTestHelper.java?rev=391133&r1=391132&r2=391133&view=diff
==============================================================================
--- jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/SCXMLTestHelper.java
(original)
+++ jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/SCXMLTestHelper.java
Mon Apr  3 13:13:56 2006
@@ -16,6 +16,7 @@
 package org.apache.commons.scxml;
 
 import java.net.URL;
+import java.util.Set;
 
 import junit.framework.Assert;
 
@@ -153,6 +154,36 @@
             return null;
         }
         return exec.getSCInstance().lookupContext(tt);
+    }
+
+    public static Set fireEvent(SCXMLExecutor exec, String name) {
+        TriggerEvent[] evts = {new TriggerEvent(name,
+                TriggerEvent.SIGNAL_EVENT, null)};
+        try {
+            exec.triggerEvents(evts);
+        } catch (Exception e) {
+            Assert.fail(e.getMessage());
+        }
+        return exec.getCurrentStatus().getStates();
+    }
+
+    public static Set fireEvent(SCXMLExecutor exec, TriggerEvent te) {
+        TriggerEvent[] evts = { te };
+        try {
+            exec.triggerEvents(evts);
+        } catch (Exception e) {
+            Assert.fail(e.getMessage());
+        }
+        return exec.getCurrentStatus().getStates();
+    }
+
+    public static Set fireEvent(SCXMLExecutor exec, TriggerEvent[] evts) {
+        try {
+            exec.triggerEvents(evts);
+        } catch (Exception e) {
+            Assert.fail(e.getMessage());
+        }
+        return exec.getCurrentStatus().getStates();
     }
 
     /**

Modified: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/SCXMLTestSuite.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/SCXMLTestSuite.java?rev=391133&r1=391132&r2=391133&view=diff
==============================================================================
--- jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/SCXMLTestSuite.java
(original)
+++ jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/SCXMLTestSuite.java
Mon Apr  3 13:13:56 2006
@@ -47,6 +47,7 @@
         TestSuite suite = new TestSuite();
         suite.setName("Commons-SCXML Tests");
         suite.addTest(BuiltinTest.suite());
+        suite.addTest(EventDataTest.suite());
         suite.addTest(SCInstanceTest.suite());
         suite.addTest(SCXMLExecutorTest.suite());
         suite.addTest(SCXMLHelperTest.suite());

Added: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/jexl/eventdata-01.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/jexl/eventdata-01.xml?rev=391133&view=auto
==============================================================================
--- jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/jexl/eventdata-01.xml
(added)
+++ jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/jexl/eventdata-01.xml
Mon Apr  3 13:13:56 2006
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+   Copyright 2006 The Apache Software Foundation
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+-->
+<scxml xmlns="http://www.w3.org/2005/07/SCXML"
+      version="1.0"
+      initialstate="state1">
+     <state id="state1">
+           <transition event="event.foo" cond="_eventdata eq 2"
+                       target="state2"/>
+           <transition event="event.foo" cond="_eventdata eq 3"
+                       target="state3"/>
+     </state>
+     <state id="state2" final="true"/>
+     <state id="state3">
+           <transition event="event.bar" target="state4"
+                       cond="_eventdatamap['event.bar'] eq 4"/>
+           <transition event="event.bar" target="state5"
+                       cond="_eventdatamap['event.bar'] eq 5"/>
+           <transition event="event.bar" target="state6"
+                       cond="_eventdatamap['event.bar'] eq 6"/>
+     </state>
+     <state id="state4" final="true"/>
+     <state id="state5" final="true"/>
+     <state id="state6">
+           <transition event="event.baz" target="state7"
+                       cond="_eventdatamap['event.baz'] eq 7"/>
+     </state>
+     <state id="state7" final="true"/>
+</scxml>

Propchange: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/jexl/eventdata-01.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/jexl/eventdata-01.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/jexl/eventdata-02.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/jexl/eventdata-02.xml?rev=391133&view=auto
==============================================================================
--- jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/jexl/eventdata-02.xml
(added)
+++ jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/jexl/eventdata-02.xml
Mon Apr  3 13:13:56 2006
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+   Copyright 2006 The Apache Software Foundation
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+-->
+<scxml xmlns="http://www.w3.org/2005/07/SCXML"
+      version="1.0"
+      initialstate="state0">
+     <state id="state0">
+           <!-- Payload can be any object, such as a String ... -->
+           <transition event="connection.alerting"
+                       cond="_eventdata eq 'line1'" target="state1"/>
+           <transition event="connection.alerting"
+                       cond="_eventdata eq 'line2'" target="state2"/>
+     </state>
+     <state id="state1" final="true"/>
+     <state id="state2">
+           <!-- ... or an arbitrary, user defined object. -->
+           <transition event="connection.alerting"
+                       cond="_eventdata.line eq 3" target="state3"/>
+           <transition event="connection.alerting"
+                       cond="_eventdata.line eq 4" target="state4"/>
+     </state>
+     <state id="state3" final="true"/>
+     <state id="state4" final="true"/>
+</scxml>

Propchange: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/jexl/eventdata-02.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/jexl/eventdata-02.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL



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


Mime
View raw message