From 0da799e220877b538a492f89f1dd26049d904c12 Mon Sep 17 00:00:00 2001 From: Stefan Radomski Date: Tue, 13 May 2014 19:57:36 +0200 Subject: More work on Rhino datamodel --- .../java/src/org/uscxml/ECMAScriptDataModel.java | 169 ------------ .../org/uscxml/datamodel/ecmascript/ECMAEvent.java | 69 +++++ .../datamodel/ecmascript/ECMAScriptDataModel.java | 294 +++++++++++++++++++++ contrib/java/src/org/uscxml/tests/TestData.java | 2 +- .../java/src/org/uscxml/tests/TestDataModel.java | 65 ++--- contrib/java/src/org/uscxml/tests/TestInvoker.java | 14 +- .../org/uscxml/tests/TestJavaScriptDataModel.java | 13 +- contrib/java/src/org/uscxml/tests/TestW3CECMA.java | 35 +++ src/bindings/swig/java/JavaDataModel.h | 16 +- src/bindings/swig/java/org/uscxml/Data.java | 2 +- 10 files changed, 460 insertions(+), 219 deletions(-) delete mode 100644 contrib/java/src/org/uscxml/ECMAScriptDataModel.java create mode 100644 contrib/java/src/org/uscxml/datamodel/ecmascript/ECMAEvent.java create mode 100644 contrib/java/src/org/uscxml/datamodel/ecmascript/ECMAScriptDataModel.java create mode 100644 contrib/java/src/org/uscxml/tests/TestW3CECMA.java diff --git a/contrib/java/src/org/uscxml/ECMAScriptDataModel.java b/contrib/java/src/org/uscxml/ECMAScriptDataModel.java deleted file mode 100644 index 25f1afe..0000000 --- a/contrib/java/src/org/uscxml/ECMAScriptDataModel.java +++ /dev/null @@ -1,169 +0,0 @@ -package org.uscxml; - -import org.mozilla.javascript.Context; -import org.mozilla.javascript.Scriptable; - - -public class ECMAScriptDataModel extends JavaDataModel { - protected Context ctx; - protected Scriptable scope; - - @Override - public JavaDataModel create(Interpreter interpreter) { - /** - * Called when an SCXML interpreter wants an instance of this datamodel - * Be careful to instantiate attributes of instance returned and not *this* - */ - System.out.println("create"); - ECMAScriptDataModel newDM = new ECMAScriptDataModel(); - - newDM.ctx = Context.enter(); - try { - newDM.scope = newDM.ctx.initStandardObjects(); - } catch(Exception e) { - System.err.println(e); - } - - return newDM; - } - - @Override - public StringSet getNames() { - /** - * Register with the following names for the datamodel attribute - * at the scxml element. - */ - System.out.println("getNames"); - StringSet ss = new StringSet(); - ss.insert("ecmascript"); - return ss; - } - - @Override - public boolean validate(String location, String schema) { - /** - * Validate the datamodel. This make more sense for XML datamodels - * and is pretty much unused but required as per draft. - */ - System.out.println("validate " + location + " " + schema); - return true; - } - - @Override - public void setEvent(Event event) { - /** - * Make the current event available as the variable _event - * in the datamodel. - */ - System.out.println("setEvent " + event); - } - - @Override - public DataNative getStringAsData(String content) { - /** - * Evaluate the string as a value expression and - * transform it into a JSON-like Data structure - */ - System.out.println("getStringAsData " + content); - Data data = new Data(); - - // TODO: populate data object - - return Data.toNative(data); - } - - @Override - public long getLength(String expr) { - /** - * Return the length of the expression if it were an - * array, used by foreach element. - */ - System.out.println("getLength " + expr); - - Object result = ctx.evaluateString(scope, expr, "uscxml", 1, null); - return (long) Context.toNumber(result); - } - - @Override - public void setForeach(String item, String array, String index, long iteration) { - /** - * Prepare an iteration of the foreach element, by setting the variable in index - * to the current iteration and setting the variable in item to the current item - * from array. - */ - System.out.println("setForeach " + item + " " + index + " " + iteration); - - - } - - @Override - public void eval(String scriptElem, String expr) { - /** - * Evaluate the given expression in the datamodel. - * This is used foremost with script elements. - */ - System.out.println("eval " + scriptElem + " " + expr); - - ctx.evaluateString(scope, expr, "uscxml", 1, null); - - } - - @Override - public String evalAsString(String expr) { - /** - * Evaluate the expression as a string e.g. for the log element. - */ - System.out.println("evalAsString " + expr); - - Object result = ctx.evaluateString(scope, expr, "uscxml", 1, null); - return Context.toString(result); - } - - @Override - public boolean evalAsBool(String expr) { - /** - * Evaluate the expression as a boolean for cond attributes in - * if and transition elements. - */ - System.out.println("evalAsBool " + expr); - - Object result = ctx.evaluateString(scope, expr, "uscxml", 1, null); - return Context.toBoolean(result); - } - - @Override - public boolean isDeclared(String expr) { - /** - * The interpreter is supposed to raise an error if we assign - * to an undeclared variable. This method is used to check whether - * a location from assign is declared. - */ - System.out.println("isDeclared " + expr); - - Object x = scope.get(expr, scope); - return x != Scriptable.NOT_FOUND; - } - - @Override - public void init(String dataElem, String location, String content) { - /** - * Called when we pass data elements. - */ - System.out.println("init " + dataElem + " " + location + " " + content); - - String expr = location + "=" + content; - ctx.evaluateString(scope, expr, "uscxml", 1, null); - } - - @Override - public void assign(String assignElem, String location, String content) { - /** - * Called when we evaluate assign elements - */ - System.out.println("assign " + assignElem + " " + location + " " + content); - - String expr = location + "=" + content; - ctx.evaluateString(scope, expr, "uscxml", 1, null); - } - -} diff --git a/contrib/java/src/org/uscxml/datamodel/ecmascript/ECMAEvent.java b/contrib/java/src/org/uscxml/datamodel/ecmascript/ECMAEvent.java new file mode 100644 index 0000000..e876d80 --- /dev/null +++ b/contrib/java/src/org/uscxml/datamodel/ecmascript/ECMAEvent.java @@ -0,0 +1,69 @@ +package org.uscxml.datamodel.ecmascript; + +import org.mozilla.javascript.ScriptableObject; +import org.mozilla.javascript.annotations.JSConstructor; +import org.mozilla.javascript.annotations.JSGetter; +import org.uscxml.Data; +import org.uscxml.DataMap; +import org.uscxml.Event; +import org.uscxml.Event.Type; + +public class ECMAEvent { + + private Event event; + + private static final long serialVersionUID = -5241020609349204355L; + + public ECMAEvent(Event event) { + this.event = event; + } + + public String getName() { + return event.getName(); + } + + public Type getEventType() { + return event.getEventType(); + } + + public String getOrigin() { + return event.getOrigin(); + } + + public String getOriginType() { + return event.getOriginType(); + } + + public String getContent() { + return event.getContent(); + } + + public String getXML() { + return event.getXML(); + } + + public String getSendId() { + return event.getSendId(); + } + + public String getInvokeId() { + return event.getInvokeId(); + } + + public Data getData() { + return new Data(event.getData()); + } + + public DataMap getNameList() { + return event.getNameList(); + } + + /* + @JSGetter + public SWIGTYPE_p_std__multimapT_std__string_uscxml__Data_t getParams() { + // TODO Auto-generated method stub + return super.getParams(); + } +*/ + +} diff --git a/contrib/java/src/org/uscxml/datamodel/ecmascript/ECMAScriptDataModel.java b/contrib/java/src/org/uscxml/datamodel/ecmascript/ECMAScriptDataModel.java new file mode 100644 index 0000000..6fee8ec --- /dev/null +++ b/contrib/java/src/org/uscxml/datamodel/ecmascript/ECMAScriptDataModel.java @@ -0,0 +1,294 @@ +package org.uscxml.datamodel.ecmascript; + +import org.mozilla.javascript.Callable; +import org.mozilla.javascript.Context; +import org.mozilla.javascript.NativeJSON; +import org.mozilla.javascript.NativeJavaObject; +import org.mozilla.javascript.Scriptable; +import org.mozilla.javascript.ScriptableObject; +import org.mozilla.javascript.Undefined; +import org.uscxml.Data; +import org.uscxml.DataNative; +import org.uscxml.Event; +import org.uscxml.Interpreter; +import org.uscxml.JavaDataModel; +import org.uscxml.StringSet; + +public class ECMAScriptDataModel extends JavaDataModel { + + private class NullCallable implements Callable { + @Override + public Object call(Context context, Scriptable scope, + Scriptable holdable, Object[] objects) { + return objects[1]; + } + } + + public Context ctx; + public Scriptable scope; + + public Data getScriptableAsData(Object object) { + Data data = new Data(); + + Scriptable s; + try { + s = (Scriptable) object; + String className = s.getClassName(); // ECMA class name +// System.out.println("Scriptable: " + className); + if (className.toLowerCase().equals("object")) { + ScriptableObject obj = (ScriptableObject) Context.toObject(s, + scope); + for (Object key : obj.getIds()) { + data.compound.put(Context.toString(key), + getScriptableAsData(obj.get(key))); + } + } + } catch (ClassCastException e) { + if (object instanceof Boolean) { + data.atom = (Context.toBoolean(object) ? "true" : "false"); + data.type = Data.Type.INTERPRETED; + } else if (object instanceof String) { + data.atom = (String) object; + data.type = Data.Type.VERBATIM; + } else if (object instanceof Integer) { + data.atom = ((Integer) object).toString(); + data.type = Data.Type.INTERPRETED; + } else { + throw new RuntimeException("Unhandled ECMA type " + + object.getClass().getName()); + } + } + + return data; + } + + public ScriptableObject getDataAsScriptable(Data data) { + // TODO! + throw new UnsupportedOperationException("Not implemented"); + } + + @Override + public JavaDataModel create(Interpreter interpreter) { + /** + * Called when an SCXML interpreter wants an instance of this datamodel + * Be careful to instantiate attributes of instance returned and not + * *this* + */ + ECMAScriptDataModel newDM = new ECMAScriptDataModel(); + + newDM.ctx = Context.enter(); + try { + newDM.scope = newDM.ctx.initStandardObjects(); +// ScriptableObject.defineClass(newDM.scope, ECMAEvent.class); + } catch (Exception e) { + System.err.println(e); + } + + return newDM; + } + + @Override + public StringSet getNames() { + /** + * Register with the following names for the datamodel attribute at the + * scxml element. + */ + StringSet ss = new StringSet(); + ss.insert("ecmascript"); + return ss; + } + + @Override + public boolean validate(String location, String schema) { + /** + * Validate the datamodel. This make more sense for XML datamodels and + * is pretty much unused but required as per draft. + */ + return true; + } + + @Override + public void setEvent(Event event) { + /** + * Make the current event available as the variable _event in the + * datamodel. + */ + +// Object[] args = { event }; +// Scriptable ecmaEvent = ctx.newObject(scope, "Event", args); + + ECMAEvent ecmaEvent = new ECMAEvent(event); + NativeJavaObject njo = new NativeJavaObject(scope, ecmaEvent, ECMAEvent.class, true); + +// for (Object key : njo.getIds()) { +// System.out.println(key); +// } + + scope.put("_event", scope, njo); + Data data = new Data(getStringAsData("_event")); + +// System.out.println(data); + } + + @Override + public DataNative getStringAsData(String content) { + /** + * Evaluate the string as a value expression and transform it into a + * JSON-like Data structure + */ + if (content.length() == 0) { + return Data.toNative(new Data()); + } + + // is it a json expression? + try { + Object json = NativeJSON.parse(ctx, scope, content, + new NullCallable()); + if (json != NativeJSON.NOT_FOUND) { + return Data.toNative(getScriptableAsData(json)); + } + } catch (org.mozilla.javascript.EcmaError e) { + } + + // is it a function call or variable? + Object x = ctx.evaluateString(scope, content, "uscxml", 0, null); + if (x == Undefined.instance) { + // maybe a literal string? + x = ctx.evaluateString(scope, '"' + content + '"', "uscxml", 0, + null); + } + return Data.toNative(getScriptableAsData(x)); + } + + @Override + public long getLength(String expr) { + /** + * Return the length of the expression if it were an array, used by + * foreach element. + */ + + Object x = scope.get(expr, scope); + if (x == Undefined.instance) { + return 0; + } + + Scriptable result = Context.toObject(x, scope); + if (result.has("length", result)) { + return (long) Context.toNumber(result.get("length", result)); + } + return 0; + } + + @Override + public void setForeach(String item, String array, String index, + long iteration) { + /** + * Prepare an iteration of the foreach element, by setting the variable + * in index to the current iteration and setting the variable in item to + * the current item from array. + */ + + try { + // get the array object + Scriptable arr = (Scriptable) scope.get(array, scope); + + if (arr.has((int) iteration, arr)) { + ctx.evaluateString(scope, item + '=' + array + '[' + iteration + + ']', "uscxml", 1, null); + if (index.length() > 0) { + ctx.evaluateString(scope, index + '=' + iteration, + "uscxml", 1, null); + } + } else { + // we ought to throw a error.execution + } + + } catch (ClassCastException e) { + System.err.println(e); + } + } + + @Override + public void eval(String scriptElem, String expr) { + /** + * Evaluate the given expression in the datamodel. This is used foremost + * with script elements. + */ + ctx.evaluateString(scope, expr, "uscxml", 1, null); + + } + + @Override + public String evalAsString(String expr) { + /** + * Evaluate the expression as a string e.g. for the log element. + */ + Object result = ctx.evaluateString(scope, expr, "uscxml", 1, null); + return Context.toString(result); + } + + @Override + public boolean evalAsBool(String elem, String expr) { + /** + * Evaluate the expression as a boolean for cond attributes in if and + * transition elements. + */ + Object result = ctx.evaluateString(scope, expr, "uscxml", 1, null); + return Context.toBoolean(result); + } + + @Override + public boolean isDeclared(String expr) { + /** + * The interpreter is supposed to raise an error if we assign to an + * undeclared variable. This method is used to check whether a location + * from assign is declared. + */ + Object x = scope.get(expr, scope); + return x != Scriptable.NOT_FOUND; + } + + @Override + public void init(String dataElem, String location, String content) { + /** + * Called when we pass data elements. + */ + if (("null").equals(location)) + return; + + if (("null").equals(content) || content.length() == 0) { + scope.put(location, scope, Context.getUndefinedValue()); + return; + } + + try { + Object json = NativeJSON.parse(ctx, scope, content, + new NullCallable()); + if (json != NativeJSON.NOT_FOUND) { + scope.put(location, scope, json); + } else { + scope.put(location, scope, content); + } + } catch (org.mozilla.javascript.EcmaError e) { + scope.put(location, scope, content); + } + } + + @Override + public void assign(String assignElem, String location, String content) { + /** + * Called when we evaluate assign elements + */ + if (("null").equals(location)) + return; + + if (("null").equals(content) || content.length() == 0) { + scope.put(location, scope, Context.getUndefinedValue()); + return; + } + + String expr = location + "=" + content; + ctx.evaluateString(scope, expr, "uscxml", 1, null); + } + +} diff --git a/contrib/java/src/org/uscxml/tests/TestData.java b/contrib/java/src/org/uscxml/tests/TestData.java index cb2fc0b..fc8deda 100644 --- a/contrib/java/src/org/uscxml/tests/TestData.java +++ b/contrib/java/src/org/uscxml/tests/TestData.java @@ -4,7 +4,7 @@ import org.uscxml.Data; import org.uscxml.DataNative; public class TestData { - + public static void main(String[] args) { System.load("/Users/sradomski/Documents/TK/Code/uscxml/build/cli/lib/libuscxmlNativeJava64_d.jnilib"); { diff --git a/contrib/java/src/org/uscxml/tests/TestDataModel.java b/contrib/java/src/org/uscxml/tests/TestDataModel.java index 90f4a7a..e813d66 100644 --- a/contrib/java/src/org/uscxml/tests/TestDataModel.java +++ b/contrib/java/src/org/uscxml/tests/TestDataModel.java @@ -8,9 +8,8 @@ import org.uscxml.Interpreter; import org.uscxml.JavaDataModel; import org.uscxml.StringSet; - public class TestDataModel extends JavaDataModel { - + @Override public JavaDataModel create(Interpreter interpreter) { /** @@ -23,8 +22,8 @@ public class TestDataModel extends JavaDataModel { @Override public StringSet getNames() { /** - * Register with the following names for the datamodel attribute - * at the scxml element. + * Register with the following names for the datamodel attribute at the + * scxml element. */ System.out.println("getNames"); StringSet ss = new StringSet(); @@ -35,8 +34,8 @@ public class TestDataModel extends JavaDataModel { @Override public boolean validate(String location, String schema) { /** - * Validate the datamodel. This make more sense for XML datamodels - * and is pretty much unused but required as per draft. + * Validate the datamodel. This make more sense for XML datamodels and + * is pretty much unused but required as per draft. */ System.out.println("validate " + location + " " + schema); return true; @@ -45,8 +44,8 @@ public class TestDataModel extends JavaDataModel { @Override public void setEvent(Event event) { /** - * Make the current event available as the variable _event - * in the datamodel. + * Make the current event available as the variable _event in the + * datamodel. */ System.out.println("setEvent " + event); } @@ -54,8 +53,8 @@ public class TestDataModel extends JavaDataModel { @Override public DataNative getStringAsData(String content) { /** - * Evaluate the string as a value expression and - * transform it into a JSON-like Data structure + * Evaluate the string as a value expression and transform it into a + * JSON-like Data structure */ System.out.println("getStringAsData " + content); Data data = new Data(); @@ -65,28 +64,30 @@ public class TestDataModel extends JavaDataModel { @Override public long getLength(String expr) { /** - * Return the length of the expression if it were an - * array, used by foreach element. + * Return the length of the expression if it were an array, used by + * foreach element. */ System.out.println("getLength " + expr); return 0; } @Override - public void setForeach(String item, String array, String index, long iteration) { + public void setForeach(String item, String array, String index, + long iteration) { /** - * Prepare an iteration of the foreach element, by setting the variable in index - * to the current iteration and setting the variable in item to the current item - * from array. + * Prepare an iteration of the foreach element, by setting the variable + * in index to the current iteration and setting the variable in item to + * the current item from array. */ - System.out.println("setForeach " + item + " " + index + " " + iteration); + System.out + .println("setForeach " + item + " " + index + " " + iteration); } @Override public void eval(String scriptElem, String expr) { /** - * Evaluate the given expression in the datamodel. - * This is used foremost with script elements. + * Evaluate the given expression in the datamodel. This is used foremost + * with script elements. */ System.out.println("eval " + scriptElem + " " + expr); } @@ -94,7 +95,7 @@ public class TestDataModel extends JavaDataModel { @Override public String evalAsString(String expr) { /** - * Evaluate the expression as a string e.g. for the log element. + * Evaluate the expression as a string e.g. for the log element. */ System.out.println("evalAsString " + expr); return ""; @@ -103,8 +104,8 @@ public class TestDataModel extends JavaDataModel { @Override public boolean evalAsBool(String expr) { /** - * Evaluate the expression as a boolean for cond attributes in - * if and transition elements. + * Evaluate the expression as a boolean for cond attributes in if and + * transition elements. */ System.out.println("evalAsBool " + expr); return true; @@ -113,9 +114,9 @@ public class TestDataModel extends JavaDataModel { @Override public boolean isDeclared(String expr) { /** - * The interpreter is supposed to raise an error if we assign - * to an undeclared variable. This method is used to check whether - * a location from assign is declared. + * The interpreter is supposed to raise an error if we assign to an + * undeclared variable. This method is used to check whether a location + * from assign is declared. */ System.out.println("isDeclared " + expr); return true; @@ -134,7 +135,8 @@ public class TestDataModel extends JavaDataModel { /** * Called when we evaluate assign elements */ - System.out.println("assign " + assignElem + " " + location + " " + content); + System.out.println("assign " + assignElem + " " + location + " " + + content); } /** @@ -143,16 +145,17 @@ public class TestDataModel extends JavaDataModel { public static void main(String[] args) { // load JNI library from build directory System.load("/Users/sradomski/Documents/TK/Code/uscxml/build/cli/lib/libuscxmlNativeJava64.jnilib"); - + // register java datamodel at factory - TestDataModel datamodel = new TestDataModel(); + TestDataModel datamodel = new TestDataModel(); Factory.getInstance().registerDataModel(datamodel); - + // instantiate interpreter with document from file - Interpreter interpreter = Interpreter.fromURI("/Users/sradomski/Documents/TK/Code/uscxml/test/uscxml/java/test-java-datamodel.scxml"); + Interpreter interpreter = Interpreter + .fromURI("/Users/sradomski/Documents/TK/Code/uscxml/test/uscxml/java/test-java-datamodel.scxml"); // wait until interpreter has finished - while(true) + while (true) interpreter.interpret(); } diff --git a/contrib/java/src/org/uscxml/tests/TestInvoker.java b/contrib/java/src/org/uscxml/tests/TestInvoker.java index 8f68c6b..7807cda 100644 --- a/contrib/java/src/org/uscxml/tests/TestInvoker.java +++ b/contrib/java/src/org/uscxml/tests/TestInvoker.java @@ -10,7 +10,6 @@ import org.uscxml.JavaInvoker; import org.uscxml.SendRequest; import org.uscxml.StringSet; - public class TestInvoker extends JavaInvoker { @Override @@ -38,7 +37,7 @@ public class TestInvoker extends JavaInvoker { System.out.println(req.getData()); System.out.println(req.getXML()); - + Event ev = new Event(); ev.setName("foo"); returnEvent(ev); @@ -54,12 +53,13 @@ public class TestInvoker extends JavaInvoker { */ public static void main(String[] args) { System.load("/Users/sradomski/Documents/TK/Code/uscxml/build/cli/lib/libuscxmlNativeJava64_d.jnilib"); - - TestInvoker invoker = new TestInvoker(); + + TestInvoker invoker = new TestInvoker(); Factory.getInstance().registerInvoker(invoker); - - Interpreter interpreter = Interpreter.fromURI("/Users/sradomski/Documents/TK/Code/uscxml/test/samples/uscxml/test-java-invoker.scxml"); - while(true) + + Interpreter interpreter = Interpreter + .fromURI("/Users/sradomski/Documents/TK/Code/uscxml/test/samples/uscxml/test-java-invoker.scxml"); + while (true) interpreter.interpret(); } diff --git a/contrib/java/src/org/uscxml/tests/TestJavaScriptDataModel.java b/contrib/java/src/org/uscxml/tests/TestJavaScriptDataModel.java index 3621a19..7399c94 100644 --- a/contrib/java/src/org/uscxml/tests/TestJavaScriptDataModel.java +++ b/contrib/java/src/org/uscxml/tests/TestJavaScriptDataModel.java @@ -2,23 +2,24 @@ package org.uscxml.tests; import org.uscxml.Factory; import org.uscxml.Interpreter; -import org.uscxml.ECMAScriptDataModel; +import org.uscxml.datamodel.ecmascript.ECMAScriptDataModel; public class TestJavaScriptDataModel { public static void main(String[] args) { // load JNI library from build directory System.load("/Users/sradomski/Documents/TK/Code/uscxml/build/cli/lib/libuscxmlNativeJava64.jnilib"); - + // register java datamodel at factory - ECMAScriptDataModel datamodel = new ECMAScriptDataModel(); + ECMAScriptDataModel datamodel = new ECMAScriptDataModel(); Factory.getInstance().registerDataModel(datamodel); - + // instantiate interpreter with document from file - Interpreter interpreter = Interpreter.fromURI("/Users/sradomski/Documents/TK/Code/uscxml/test/uscxml/java/test-ecmascript-datamodel.scxml"); + Interpreter interpreter = Interpreter + .fromURI("/Users/sradomski/Documents/TK/Code/uscxml/test/uscxml/java/test-ecmascript-datamodel.scxml"); // wait until interpreter has finished - while(true) + while (true) interpreter.interpret(); } diff --git a/contrib/java/src/org/uscxml/tests/TestW3CECMA.java b/contrib/java/src/org/uscxml/tests/TestW3CECMA.java new file mode 100644 index 0000000..2b2a36e --- /dev/null +++ b/contrib/java/src/org/uscxml/tests/TestW3CECMA.java @@ -0,0 +1,35 @@ +package org.uscxml.tests; + +import java.io.File; + +import org.uscxml.Factory; +import org.uscxml.Interpreter; +import org.uscxml.datamodel.ecmascript.ECMAScriptDataModel; + +public class TestW3CECMA { + + public static String testDir = "/Users/sradomski/Documents/TK/Code/uscxml/test/w3c/ecma"; + + public static void main(String[] args) { + System.load("/Users/sradomski/Documents/TK/Code/uscxml/build/cli/lib/libuscxmlNativeJava64.jnilib"); + + ECMAScriptDataModel datamodel = new ECMAScriptDataModel(); + Factory.getInstance().registerDataModel(datamodel); + + Interpreter interpreter = Interpreter.fromURI("/Users/sradomski/Documents/TK/Code/uscxml/test/w3c/ecma/test176.scxml"); + interpreter.interpret(); + System.exit(0); + +// File dir = new File(testDir); +// File[] filesList = dir.listFiles(); +// for (File file : filesList) { +// if (file.isFile() && file.getName().endsWith(".scxml")) { +// System.out.println("### " + file.getName() + " #####"); +// Interpreter interpreter = Interpreter.fromURI(file.getAbsolutePath()); +// interpreter.interpret(); +// } +// } + + } + +} diff --git a/src/bindings/swig/java/JavaDataModel.h b/src/bindings/swig/java/JavaDataModel.h index ef8daa6..fcfb665 100644 --- a/src/bindings/swig/java/JavaDataModel.h +++ b/src/bindings/swig/java/JavaDataModel.h @@ -54,11 +54,13 @@ public: return ""; } virtual bool evalAsBool(const std::string& expr) { - return false; + return evalAsBool("", expr); } virtual bool evalAsBool(const Arabica::DOM::Node& node, const std::string& expr) { - return false; + std::ostringstream ssNode; + ssNode << node; + return evalAsBool(ssNode.str(), expr); } virtual bool isDeclared(const std::string& expr) { @@ -73,6 +75,7 @@ public: if (assignElem.hasAttribute("location")) { location = assignElem.getAttribute("location"); } + std::ostringstream ssAssign; ssAssign << assignElem; std::string tmp; @@ -80,10 +83,12 @@ public: std::ostringstream ssContent; ssContent << node; tmp = ssContent.str(); + } else if (assignElem.hasAttribute("expr")) { + tmp = assignElem.getAttribute("expr"); } else { tmp = content; } - assign(location, ssAssign.str(), tmp); + assign(ssAssign.str(), location, tmp); } virtual void assign(const std::string& location, const Data& data) { @@ -108,10 +113,12 @@ public: std::ostringstream ssContent; ssContent << node; tmp = ssContent.str(); + } else if (dataElem.hasAttribute("expr")) { + tmp = dataElem.getAttribute("expr"); } else { tmp = content; } - init(location, ssData.str(), tmp); + init(ssData.str(), location, tmp); } virtual void init(const std::string& location, const Data& data) { @@ -119,6 +126,7 @@ public: } // these functions are exposed to java + virtual bool evalAsBool(const std::string& elem, const std::string& content) { return false; } virtual void init(const std::string& dataElem, const std::string& location, const std::string& content) {} virtual void assign(const std::string& assignElem, const std::string& location, const std::string& content) {} virtual void eval(const std::string& scriptElem, const std::string& expr) {} diff --git a/src/bindings/swig/java/org/uscxml/Data.java b/src/bindings/swig/java/org/uscxml/Data.java index ce295af..f3f21f3 100644 --- a/src/bindings/swig/java/org/uscxml/Data.java +++ b/src/bindings/swig/java/org/uscxml/Data.java @@ -74,7 +74,7 @@ public class Data { nativeDataList.add(toNative(item)); } nativeData.setArray(nativeDataList); - } else { + } else if (data.atom != null) { nativeData.setAtom(data.atom); if (data.type == Type.INTERPRETED) { nativeData.setType(DataNative.Type.INTERPRETED); -- cgit v0.12