diff options
author | Stefan Radomski <radomski@tk.informatik.tu-darmstadt.de> | 2014-11-12 18:56:38 (GMT) |
---|---|---|
committer | Stefan Radomski <radomski@tk.informatik.tu-darmstadt.de> | 2014-11-12 18:56:38 (GMT) |
commit | c95be52b02e921d4ba8b3e707cd2f6239ce9c99e (patch) | |
tree | f30012410b30788f37df1122fde18b75316a4417 /embedding/java/src/org/uscxml/tests/datamodel/TestDataModelAccess.java | |
parent | ba6950d9968f151247d873b414c3ff0fd513f4ca (diff) | |
download | uscxml-c95be52b02e921d4ba8b3e707cd2f6239ce9c99e.zip uscxml-c95be52b02e921d4ba8b3e707cd2f6239ce9c99e.tar.gz uscxml-c95be52b02e921d4ba8b3e707cd2f6239ce9c99e.tar.bz2 |
Data model access example in Java
Diffstat (limited to 'embedding/java/src/org/uscxml/tests/datamodel/TestDataModelAccess.java')
-rw-r--r-- | embedding/java/src/org/uscxml/tests/datamodel/TestDataModelAccess.java | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/embedding/java/src/org/uscxml/tests/datamodel/TestDataModelAccess.java b/embedding/java/src/org/uscxml/tests/datamodel/TestDataModelAccess.java new file mode 100644 index 0000000..fcad186 --- /dev/null +++ b/embedding/java/src/org/uscxml/tests/datamodel/TestDataModelAccess.java @@ -0,0 +1,84 @@ +package org.uscxml.tests.datamodel; + +import org.uscxml.Interpreter; +import org.uscxml.InterpreterException; +import org.uscxml.InterpreterState; + +public class TestDataModelAccess { + public static void main(String[] args) throws InterpreterException { + // load JNI library from build directory + System.load("/Users/sradomski/Documents/TK/Code/uscxml/build/cli/lib/libuscxmlNativeJava64.jnilib"); + + { + // initializing and accessing complex data + String xml = + "<scxml datamodel=\"ecmascript\">" + + " <script src=\"http://uscxml.tk.informatik.tu-darmstadt.de/scripts/dump.js\" />" + + " <datamodel>" + + " <data id=\"cmplx1\"><![CDATA[" + + " { foo: \"bar\", baz: 12 }" + + " ]]></data>" + + " </datamodel>" + + " <state id=\"s1\">" + + " <onentry>" + + " <log label=\"cmplx1\" expr=\"cmplx1.foo\" />" + + " <log label=\"cmplx1\" expr=\"cmplx1.baz\" />" + + " <script>dump(cmplx1)</script>" + + " </onentry>" + + " <transition target=\"done\" />" + + " </state>" + + " <final id=\"done\" />" + + "</scxml>"; + + Interpreter interpreter = Interpreter.fromXML(xml); + InterpreterState state; + do { + state = interpreter.step(); + // after first microstep, data model is initialized and we can access cmplx1 + if (state == InterpreterState.USCXML_MICROSTEPPED) + System.out.println(interpreter.getDataModel().getStringAsData("cmplx1").toString()); + + } while (state != InterpreterState.USCXML_FINISHED && + state != InterpreterState.USCXML_DESTROYED); + } + + { + // initializing and accessing complex data via data.src + String xml = + "<scxml datamodel=\"ecmascript\">" + + " <script src=\"http://uscxml.tk.informatik.tu-darmstadt.de/scripts/dump.js\" />" + + " <datamodel>" + + " <data id=\"cmplx1\" src=\"TestData.json\" />" + + " <data id=\"cmplx2\" src=\"TestData.xml\" />" + + " </datamodel>" + + " <state id=\"s1\">" + + " <onentry>" + + " <!-- BUG script>" + + " var node = document.evaluate('//to', cmplx2);" + + " dump(node);" + + " </script -->" + + " <log label=\"cmplx1\" expr=\"cmplx1.name\" />" + + " <log label=\"cmplx1\" expr=\"cmplx1.price\" />" + + " <script>dump(cmplx1)</script>" + + " </onentry>" + + " <transition target=\"done\" />" + + " </state>" + + " <final id=\"done\" />" + + "</scxml>"; + + Interpreter interpreter = Interpreter.fromXML(xml); + interpreter.setSourceURI(TestDataModelAccess.class.getResource("").toString()); + InterpreterState state; + do { + state = interpreter.step(); + // after first microstep, data model is initialized and we can access cmplx1 + if (state == InterpreterState.USCXML_MICROSTEPPED) + System.out.println(interpreter.getDataModel().getStringAsData("cmplx1").toString()); + + } while (state != InterpreterState.USCXML_FINISHED && + state != InterpreterState.USCXML_DESTROYED); + + } + } + +} |