diff options
Diffstat (limited to 'embedding')
-rw-r--r-- | embedding/java/src/org/uscxml/tests/invoker/adhoc/TestAdhocInvoker.java | 86 | ||||
-rw-r--r-- | embedding/java/src/org/uscxml/tests/invoker/factory/TestCustomInvoker.java (renamed from embedding/java/src/org/uscxml/tests/invoker/TestCustomInvoker.java) | 2 | ||||
-rw-r--r-- | embedding/java/src/org/uscxml/tests/ioprocessor/adhoc/TestAdhocIOProc.java | 142 | ||||
-rw-r--r-- | embedding/java/src/org/uscxml/tests/ioprocessor/adhoc/console/ConsoleFrame.java | 77 | ||||
-rw-r--r-- | embedding/java/src/org/uscxml/tests/ioprocessor/adhoc/console/ConsoleIOProc.java (renamed from embedding/java/src/org/uscxml/tests/ioprocessor/console/ConsoleIOProc.java) | 2 | ||||
-rw-r--r-- | embedding/java/src/org/uscxml/tests/ioprocessor/console/ConsoleFrame.java | 74 | ||||
-rw-r--r-- | embedding/java/src/org/uscxml/tests/ioprocessor/factory/TestCustomIOProc.java (renamed from embedding/java/src/org/uscxml/tests/ioprocessor/TestCustomIOProc.java) | 2 |
7 files changed, 308 insertions, 77 deletions
diff --git a/embedding/java/src/org/uscxml/tests/invoker/adhoc/TestAdhocInvoker.java b/embedding/java/src/org/uscxml/tests/invoker/adhoc/TestAdhocInvoker.java new file mode 100644 index 0000000..9092910 --- /dev/null +++ b/embedding/java/src/org/uscxml/tests/invoker/adhoc/TestAdhocInvoker.java @@ -0,0 +1,86 @@ +package org.uscxml.tests.invoker.adhoc; + +import org.uscxml.Data; +import org.uscxml.Event; +import org.uscxml.Interpreter; +import org.uscxml.InterpreterException; +import org.uscxml.InvokeRequest; +import org.uscxml.Invoker; +import org.uscxml.SendRequest; +import org.uscxml.StringList; + +public class TestAdhocInvoker extends Invoker { + + @Override + public StringList getNames() { + StringList ss = new StringList(); + ss.add("java"); + return ss; + } + + @Override + public Data getDataModelVariables() { + Data data = new Data(); + return data; + } + + @Override + public void send(SendRequest req) { + System.out.println(req); + if ("foo".equals(req.getName())) + returnEvent(new Event("received2"), true); // enqueue an external event + } + + @Override + public void invoke(InvokeRequest req) { + System.out.println(req); + if ("Some string content".equals(req.getContent())) { + returnEvent(new Event("received1"), true); // enqueue an external event + } + } + + @Override + public void uninvoke() { + System.out.println("uninvoke"); + } + + /** + * @param args + * @throws InterpreterException + */ + public static void main(String[] args) throws InterpreterException { + System.load("/Users/sradomski/Documents/TK/Code/uscxml/build/cli/lib/libuscxmlNativeJava64.jnilib"); + + String xml = + "<scxml>" + + " <state id=\"s1\">" + + " <invoke type=\"java\" id=\"javainvoker1\">" + + " <content>Some string content</content>" + + " </invoke>" + + " <invoke type=\"java\" id=\"javainvoker2\" />" + + " <state id=\"s11\">" + + " <transition event=\"received1\" target=\"s12\" />" + + " </state>" + + " <state id=\"s12\">" + + " <onentry>" + + " <log label=\"label\" expr=\"foo\" />" + + " <send target=\"#_javainvoker2\" event=\"foo\" />" + + " </onentry>" + + " <transition event=\"received2\" target=\"done\" />" + + " </state>" + + " </state>" + + " <final id=\"done\" />" + + "</scxml>"; + + TestAdhocInvoker javainvoker1 = new TestAdhocInvoker(); + TestAdhocInvoker javainvoker2 = new TestAdhocInvoker(); + + // parse and interpret + Interpreter interpreter = Interpreter.fromXML(xml); + interpreter.setInvoker("javainvoker1", javainvoker1); + interpreter.setInvoker("javainvoker2", javainvoker2); + interpreter.interpret(); + + } + +} diff --git a/embedding/java/src/org/uscxml/tests/invoker/TestCustomInvoker.java b/embedding/java/src/org/uscxml/tests/invoker/factory/TestCustomInvoker.java index ecbff9b..c9cf6bc 100644 --- a/embedding/java/src/org/uscxml/tests/invoker/TestCustomInvoker.java +++ b/embedding/java/src/org/uscxml/tests/invoker/factory/TestCustomInvoker.java @@ -1,4 +1,4 @@ -package org.uscxml.tests.invoker; +package org.uscxml.tests.invoker.factory; import org.uscxml.Data; import org.uscxml.Event; diff --git a/embedding/java/src/org/uscxml/tests/ioprocessor/adhoc/TestAdhocIOProc.java b/embedding/java/src/org/uscxml/tests/ioprocessor/adhoc/TestAdhocIOProc.java new file mode 100644 index 0000000..fcb9688 --- /dev/null +++ b/embedding/java/src/org/uscxml/tests/ioprocessor/adhoc/TestAdhocIOProc.java @@ -0,0 +1,142 @@ +package org.uscxml.tests.ioprocessor.adhoc; + +import java.io.IOException; +import java.io.StringReader; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.uscxml.Data; +import org.uscxml.Event; +import org.uscxml.Factory; +import org.uscxml.IOProcessor; +import org.uscxml.Interpreter; +import org.uscxml.InterpreterException; +import org.uscxml.SendRequest; +import org.uscxml.StringList; +import org.w3c.dom.Document; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +public class TestAdhocIOProc extends IOProcessor { + + public Interpreter interpreter; + + /** + * The types we will handle on a <send> element + */ + @Override + public StringList getNames() { + StringList ss = new StringList(); + ss.add("java"); + return ss; + } + + /** + * Optional data we want to make available at + * _ioprocessors[this.getNames.front()] in the datamodel + */ + @Override + public Data getDataModelVariables() { + return new Data(); + } + + /** + * Send from the SCXML interpreter to this ioprocessor + */ + @Override + public void send(SendRequest req) { + System.out.println(req); + // send in s1.onentry + if ("This is some content!".equals(req.getContent())) { + returnEvent(new Event("received1")); + return; + } + // send in s2.onentry + if (req.getParams().containsKey("foo") + && "bar".equals(req.getParams().get("foo").get(0).getAtom())) { + returnEvent(new Event("received2")); + return; + } + // send in s3 + if (req.getXML().length() > 0) { + try { + DocumentBuilderFactory factory = DocumentBuilderFactory + .newInstance(); + Document doc = factory.newDocumentBuilder().parse( + new InputSource(new StringReader(req.getXML()))); + System.out.println("Root element :" + + doc.getDocumentElement().getNodeName()); + if ("this".equals(doc.getDocumentElement().getNodeName())) { + returnEvent(new Event("received3")); + return; + } + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + } + + /** + * Create a new instance of this + */ + @Override + public IOProcessor create(Interpreter interpreter) { + TestAdhocIOProc ioProc = new TestAdhocIOProc(); + ioProc.interpreter = interpreter; + ioProc.swigReleaseOwnership(); + return ioProc; + } + + /** + * @param args + * @throws InterpreterException + */ + public static void main(String[] args) throws InterpreterException { + System.load("/Users/sradomski/Documents/TK/Code/uscxml/build/cli/lib/libuscxmlNativeJava64.jnilib"); + + String xml = + "<scxml>" + + " <state id=\"s1\">" + + " <onentry>" + + " <send type=\"java\">" + + " <content>This is some content!</content>" + + " </send>" + + " </onentry>" + + " <transition event=\"received1\" target=\"s2\" />" + + " </state>" + + " <state id=\"s2\">" + + " <onentry>" + + " <send type=\"java\">" + + " <param name=\"foo\" expr=\"bar\" />" + + " </send>" + + " </onentry>" + + " <transition event=\"received2\" target=\"s3\" />" + + " </state>" + + " <state id=\"s3\">" + + " <onentry>" + + " <send type=\"java\">" + + " <content>" + + " <this><is><xml/></is></this>" + + " </content>" + + " </send>" + + " </onentry>" + + " <transition event=\"received3\" target=\"done\" />" + + " </state>" + + " <final id=\"done\" />" + + "</scxml>"; + + TestAdhocIOProc ioProc = new TestAdhocIOProc(); + + // parse and interpret + Interpreter interpreter = Interpreter.fromXML(xml); + interpreter.addIOProcessor(ioProc); + interpreter.interpret(); + } + +} diff --git a/embedding/java/src/org/uscxml/tests/ioprocessor/adhoc/console/ConsoleFrame.java b/embedding/java/src/org/uscxml/tests/ioprocessor/adhoc/console/ConsoleFrame.java new file mode 100644 index 0000000..0dbe528 --- /dev/null +++ b/embedding/java/src/org/uscxml/tests/ioprocessor/adhoc/console/ConsoleFrame.java @@ -0,0 +1,77 @@ +package org.uscxml.tests.ioprocessor.adhoc.console; + +import java.awt.Frame; + +import javax.swing.JLabel; +import javax.swing.JPanel; + +import org.uscxml.Interpreter; +import org.uscxml.InterpreterException; + +public class ConsoleFrame extends Frame { + private static final long serialVersionUID = 3682378173372160680L; + private ConsoleIOProc ioProc; + + public ConsoleFrame() throws InterpreterException { + super("Input Frame"); + JPanel p = new JPanel(); + JLabel label = new JLabel("Key Listener!"); + p.add(label); + add(p); + setSize(200, 100); + + // instantiate SCXML interpreter + final Interpreter interpreter = Interpreter.fromXML( + "<scxml datamodel=\"ecmascript\">" + + " <script src=\"http://uscxml.tk.informatik.tu-darmstadt.de/scripts/dump.js\" />" + + " <script>var charSeq = \"\";</script>" + + " <state id=\"idle\">" + + " <transition event=\"error\" target=\"quit\" />" + + " <transition event=\"key.released\" cond=\"_event.data.keyChar == 10\">" + + " <send type=\"console\">" + + " <param name=\"foo\" expr=\"charSeq\" />" + + " </send>" + + " <script>" + + " charSeq = \"\"; // reset string" + + " </script>" + + " </transition>" + + " <transition event=\"*\">" + + " <log label=\"event\" expr=\"dump(_event.data)\" />" + + " <script>charSeq += String.fromCharCode(_event.data.keyChar);</script>" + + " </transition>" + + " </state>" + + " <final id=\"quit\" />" + + "</scxml>"); + + /** + * ConsoleIOProc will register as a keyboard listener and send events to the interpreter. + * + * This circumvents the factory and registers instances directly. You will have to promise + * that the ioProc instance exists for the complete lifetime of the interpreter, otherwise + * finalize() will call the C++ destructor and SegFaults will occur. + */ + ioProc = new ConsoleIOProc(this); + interpreter.addIOProcessor(ioProc); + + // Start the interpreter in a separate thread + Thread intrerpreterThread = new Thread(new Runnable() { + @Override + public void run() { + try { + interpreter.interpret(); + } catch (InterpreterException e) { + e.printStackTrace(); + } + } + }); + intrerpreterThread.start(); + + // show the user interface + setVisible(true); + } + + public static void main(String[] args) throws InterpreterException { + System.load("/Users/sradomski/Documents/TK/Code/uscxml/build/cli/lib/libuscxmlNativeJava64.jnilib"); + ConsoleFrame frame = new ConsoleFrame(); + } +} diff --git a/embedding/java/src/org/uscxml/tests/ioprocessor/console/ConsoleIOProc.java b/embedding/java/src/org/uscxml/tests/ioprocessor/adhoc/console/ConsoleIOProc.java index a95f6cb..41d4702 100644 --- a/embedding/java/src/org/uscxml/tests/ioprocessor/console/ConsoleIOProc.java +++ b/embedding/java/src/org/uscxml/tests/ioprocessor/adhoc/console/ConsoleIOProc.java @@ -1,4 +1,4 @@ -package org.uscxml.tests.ioprocessor.console; +package org.uscxml.tests.ioprocessor.adhoc.console; import java.awt.Frame; import java.awt.event.KeyEvent; diff --git a/embedding/java/src/org/uscxml/tests/ioprocessor/console/ConsoleFrame.java b/embedding/java/src/org/uscxml/tests/ioprocessor/console/ConsoleFrame.java deleted file mode 100644 index a88cce4..0000000 --- a/embedding/java/src/org/uscxml/tests/ioprocessor/console/ConsoleFrame.java +++ /dev/null @@ -1,74 +0,0 @@ -package org.uscxml.tests.ioprocessor.console; - -import java.awt.Frame; -import java.util.HashMap; -import java.util.Map; - -import javax.swing.JLabel; -import javax.swing.JPanel; - -import org.uscxml.Factory; -import org.uscxml.Interpreter; -import org.uscxml.InterpreterException; - -public class ConsoleFrame extends Frame { - - private static final long serialVersionUID = 3682378173372160680L; - private ConsoleIOProc ioProc; - - public ConsoleFrame() throws InterpreterException { - super("Input Frame"); - JPanel p = new JPanel(); - JLabel label = new JLabel("Key Listener!"); - p.add(label); - add(p); - setSize(200, 100); - - final Interpreter interpreter = Interpreter.fromXML( - "<scxml datamodel=\"ecmascript\">" - + " <script src=\"http://uscxml.tk.informatik.tu-darmstadt.de/scripts/dump.js\" />" - + " <script>var charSeq = \"\";</script>" - + " <state id=\"idle\">" - + " <transition event=\"error\" target=\"quit\" />" - + " <transition event=\"key.released\" cond=\"_event.data.keyChar == 10\">" - + " <send type=\"console\">" - + " <param name=\"foo\" expr=\"charSeq\" />" - + " </send>" - + " <script>" - + " charSeq = \"\"; // reset string" - + " </script>" - + " </transition>" - + " <transition event=\"*\">" - + " <log label=\"event\" expr=\"dump(_event.data)\" />" - + " <script>charSeq += String.fromCharCode(_event.data.keyChar);</script>" - + " </transition>" - + " </state>" - + " <final id=\"quit\" />" - + "</scxml>"); - - ioProc = new ConsoleIOProc(this); - interpreter.addIOProcessor(ioProc); - - Thread intrerpreterThread = new Thread(new Runnable() { - @Override - public void run() { - try { - interpreter.interpret(); - } catch (InterpreterException e) { - e.printStackTrace(); - } - } - }); - intrerpreterThread.start(); - - setVisible(true); - } - - public static void main(String[] args) throws InterpreterException { - System.load("/Users/sradomski/Documents/TK/Code/uscxml/build/cli/lib/libuscxmlNativeJava64.jnilib"); - ConsoleFrame frame = new ConsoleFrame(); - - } - -} - diff --git a/embedding/java/src/org/uscxml/tests/ioprocessor/TestCustomIOProc.java b/embedding/java/src/org/uscxml/tests/ioprocessor/factory/TestCustomIOProc.java index 8a72abe..c37cdb4 100644 --- a/embedding/java/src/org/uscxml/tests/ioprocessor/TestCustomIOProc.java +++ b/embedding/java/src/org/uscxml/tests/ioprocessor/factory/TestCustomIOProc.java @@ -1,4 +1,4 @@ -package org.uscxml.tests.ioprocessor; +package org.uscxml.tests.ioprocessor.factory; import java.io.IOException; import java.io.StringReader; |