diff options
author | Stefan Radomski <github@mintwerk.de> | 2016-05-26 10:36:49 (GMT) |
---|---|---|
committer | Stefan Radomski <github@mintwerk.de> | 2016-05-26 10:36:49 (GMT) |
commit | 6e13c7b6e0888323223afd5d2e36e86243df57af (patch) | |
tree | f558fd45fa499c8bc95041554ecad6be1bf788c1 /test/bindings/java | |
parent | f6714b1484b641ea61053350b7d156d2da760b8b (diff) | |
download | uscxml-6e13c7b6e0888323223afd5d2e36e86243df57af.zip uscxml-6e13c7b6e0888323223afd5d2e36e86243df57af.tar.gz uscxml-6e13c7b6e0888323223afd5d2e36e86243df57af.tar.bz2 |
Minor polishing for Java bindings and first draft of JEXL datamodel
Diffstat (limited to 'test/bindings/java')
5 files changed, 484 insertions, 0 deletions
diff --git a/test/bindings/java/org/uscxml/dm/jexl/JEXLDataModel.java b/test/bindings/java/org/uscxml/dm/jexl/JEXLDataModel.java new file mode 100644 index 0000000..33da96c --- /dev/null +++ b/test/bindings/java/org/uscxml/dm/jexl/JEXLDataModel.java @@ -0,0 +1,261 @@ +package org.uscxml.dm.jexl; + +import java.lang.reflect.Array; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.jexl3.JexlBuilder; +import org.apache.commons.jexl3.JexlContext; +import org.apache.commons.jexl3.JexlEngine; +import org.apache.commons.jexl3.JexlException; +import org.apache.commons.jexl3.JexlExpression; +import org.apache.commons.jexl3.MapContext; +import org.uscxml.Data; +import org.uscxml.DataList; +import org.uscxml.DataMap; +import org.uscxml.DataModel; +import org.uscxml.ErrorEvent; +import org.uscxml.Event; +import org.uscxml.StringList; +import org.uscxml.StringVector; + +public class JEXLDataModel extends DataModel { + + protected static final JexlEngine jexl = new JexlBuilder().cache(512).strict(true).silent(false).create(); + protected JexlContext ctx; + + + + @Override + public StringList getNames() { + StringList names = new StringList(); + names.add("jexl"); + return names; + } + + @Override + public DataModel create() { + JEXLDataModel dm = new JEXLDataModel(); + dm.ctx = new MapContext(); + return dm; + } + + @Override + public boolean isValidSyntax(String expr) { + try { + jexl.createExpression(expr); + return true; + } catch(JexlException e) { + return false; + } + } + + @Override + public void setEvent(Event event) { + ctx.set("_event", event); + } + + @Override + public Data getAsData(String content) { + try { + JexlExpression ex = jexl.createExpression(content); + return getJexlObjectAsData(ex.evaluate(ctx)); + } catch(Exception e) { + } + return null; + } + + @Override + public Data evalAsData(String content) { + return new Data(); + } + + @Override + public boolean evalAsBool(String expr) { + try { + JexlExpression ex = jexl.createExpression("!!" + expr); + Object result = ex.evaluate(ctx); + return (Boolean) result; + } catch(Exception e) { + e.printStackTrace(); + } + return false; + } + + @Override + public long getLength(String expr) { + try { + JexlExpression ex = jexl.createExpression(expr); + Object res = ex.evaluate(ctx); + + return Array.getLength(res); + + } catch(Exception e) { + throw new ErrorEvent("Cannot evaluate '" + expr + "' as an array: " + e.getMessage()); + } + } + + @Override + public void setForeach(String item, String array, String index, long iteration) { + Object res = null; + try { + JexlExpression ex = jexl.createExpression(array); + res = ex.evaluate(ctx); + } catch(Exception e) { + throw new ErrorEvent("Cannot evaluate '" + array + "' as an array: " + e.getMessage()); + } + try { + JexlExpression ex = jexl.createExpression(item + "==" + item + ";"); + ex.evaluate(ctx); + + ctx.set(item, Array.get(res, (int) iteration)); + } catch(Exception e) { + throw new ErrorEvent("Cannot set item '" + item + "' to current item: " + e.getMessage()); + } + try { + if (index.length() > 0) { + ctx.set(index, iteration); + } + } catch(Exception e) { + throw new ErrorEvent("Cannot set index '" + index + "' to current index: " + e.getMessage()); + } + } + + @Override + public void assign(String location, Data data) { + try { + ctx.set(location, getDataAsJexlObject(data)); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Override + public void init(String location, Data data) { + ctx.set(location, null); + assign(location, data); + } + + @Override + public boolean isDeclared(String expr) { + try { + JexlExpression ex = jexl.createExpression(expr); + Object result = ex.evaluate(ctx); + return (boolean) result; + } catch(JexlException e) { + } + return false; + } + + protected Object evalAsObject(String expr) { + try { + JexlExpression ex = jexl.createExpression(expr); + return (ex.evaluate(ctx)); + } catch(JexlException e) { + } + return null; + } + + protected Data getJexlObjectAsData(Object obj) { + Data d = new Data(); + if (obj.getClass().isArray()) { + int length = Array.getLength(obj); + for (int i = 0; i < length; i ++) { + d.getArray().add(getJexlObjectAsData(Array.get(obj, i))); + } + } else if (obj.getClass().isPrimitive() || isWrapperType(obj.getClass())) { + String val = obj.toString(); + try { + Integer.parseInt(obj.toString()); + d.setAtom(obj.toString()); + d.setType(Data.Type.INTERPRETED); + return d; + } catch(NumberFormatException e) {} + try { + Double.parseDouble(obj.toString()); + d.setAtom(obj.toString()); + d.setType(Data.Type.INTERPRETED); + return d; + } catch(NumberFormatException e) {} + d.setAtom(obj.toString()); + d.setType(Data.Type.VERBATIM); + return d; + } else { + Field[] fields = obj.getClass().getDeclaredFields(); + for (Field field: fields) { + Object newObj = null; + try { + field.get(newObj); + d.getCompound().set(field.getName(), getJexlObjectAsData(newObj)); + } catch (IllegalArgumentException | IllegalAccessException e) { + } + } + return d; + } + + return d; + } + + protected Object getDataAsJexlObject(Data data) { + if (data.getAtom().length() > 0) { + if (data.getType() == Data.Type.INTERPRETED) { + try { + JexlExpression exp = jexl.createExpression(data.getAtom()); + return exp.evaluate(ctx); + } catch (Exception e) { + e.printStackTrace(); + } + return new String(data.getAtom()); + } + try { + JexlExpression exp = jexl.createExpression(data.getAtom()); + return exp.evaluate(ctx); + } catch (Exception e) { + } + return new String("\"" + data.getAtom() + "\""); + + } else if (data.getCompound().size() > 0) { + StringVector keys = data.getCompoundKeys(); + DataMap dataCompound = data.getCompound(); + Map<String, Object> objCompound = new HashMap<String, Object>(); + for (int i = 0; i < keys.size(); i++) { + objCompound.put(keys.get(i), getDataAsJexlObject(dataCompound.get(keys.get(i)))); + } + return objCompound; + } else if (data.getArray().size() > 0) { + DataList dataList = data.getArray(); + List<Object> objList = new ArrayList<Object>((int) data.getArray().size()); + for (int i = 0; i < dataList.size(); i++) { + objList.add(i, getDataAsJexlObject(dataList.get(i))); + } + return objList; + } + return new Object(); + } + + protected static boolean isWrapperType(Class<?> clazz) { + return WRAPPER_TYPES.contains(clazz); + } + + private static final Set<Class<?>> WRAPPER_TYPES = getWrapperTypes(); + + private static Set<Class<?>> getWrapperTypes() { + Set<Class<?>> ret = new HashSet<Class<?>>(); + ret.add(Boolean.class); + ret.add(Character.class); + ret.add(Byte.class); + ret.add(Short.class); + ret.add(Integer.class); + ret.add(Long.class); + ret.add(Float.class); + ret.add(Double.class); + ret.add(Void.class); + return ret; + } + +} diff --git a/test/bindings/java/org/uscxml/tests/BasicExample.java b/test/bindings/java/org/uscxml/tests/BasicExample.java new file mode 100644 index 0000000..32899e5 --- /dev/null +++ b/test/bindings/java/org/uscxml/tests/BasicExample.java @@ -0,0 +1,43 @@ +package org.uscxml.tests; + +import org.uscxml.Interpreter; +import org.uscxml.InterpreterException; +import org.uscxml.InterpreterState; + +public class BasicExample { + + public static void main(String[] args) { + + String uSCXMLLibPath = "/Users/sradomski/Documents/TK/Code/uscxml/build/cli/lib/libuscxmlNativeJava.jnilib"; + if (System.getenv().containsKey("USCXML_JAVA_LIB")) { + uSCXMLLibPath = System.getenv("USCXML_JAVA_LIB"); + } + + System.load(uSCXMLLibPath); + + try { + Interpreter scxml = Interpreter.fromURL("https://raw.githubusercontent.com/tklab-tud/uscxml/master/test/w3c/null/test436.scxml"); + InterpreterState state = InterpreterState.USCXML_UNDEF; + while((state = scxml.step()) != InterpreterState.USCXML_FINISHED) { + switch (state) { + case USCXML_FINISHED: + case USCXML_UNDEF: + case USCXML_IDLE: + case USCXML_INITIALIZED: + case USCXML_INSTANTIATED: + case USCXML_MICROSTEPPED: + case USCXML_MACROSTEPPED: + case USCXML_CANCELLED: + break; + default: + break; + } + } + System.out.println("Machine finished"); + + } catch (InterpreterException e) { + e.printStackTrace(); + } + } + +} diff --git a/test/bindings/java/org/uscxml/tests/DataModelExample.java b/test/bindings/java/org/uscxml/tests/DataModelExample.java new file mode 100644 index 0000000..db46a18 --- /dev/null +++ b/test/bindings/java/org/uscxml/tests/DataModelExample.java @@ -0,0 +1,58 @@ +package org.uscxml.tests; + +import java.io.File; +import java.net.MalformedURLException; + +import org.uscxml.Factory; +import org.uscxml.Interpreter; +import org.uscxml.InterpreterException; +import org.uscxml.InterpreterState; +import org.uscxml.dm.jexl.JEXLDataModel; +import org.uscxml.tests.helper.TestMonitor; + +public class DataModelExample { + + public static void main(String[] args) { + String uSCXMLLibPath = "/Users/sradomski/Documents/TK/Code/uscxml/build/cli/lib/libuscxmlNativeJava.jnilib"; + if (System.getenv().containsKey("USCXML_JAVA_LIB")) { + uSCXMLLibPath = System.getenv("USCXML_JAVA_LIB"); + } + + System.load(uSCXMLLibPath); + + JEXLDataModel jdm = new JEXLDataModel(); + Factory.getInstance().registerDataModel(jdm);; + + TestMonitor tm = new TestMonitor(); + + File folder = new File("/Users/sradomski/Documents/TK/Code/uscxml/test/w3c/jexl"); + File[] listOfFiles = folder.listFiles(); + + try { + for (File file : listOfFiles) { + if (!file.getName().endsWith(".scxml")) + continue; + String testName = file.toURI().toURL().toString(); + System.out.println(testName); + + Interpreter scxml = Interpreter.fromURL(testName); +// scxml.setMonitor(tm); + + while(scxml.step() != InterpreterState.USCXML_FINISHED) {} + + if (!scxml.isInState("pass")) { + System.out.println("FAIL: " + testName); + + throw new RuntimeException(); + } + System.out.println("SUCCESS"); + + } + + } catch (InterpreterException | MalformedURLException e) { + e.printStackTrace(); + } + + } + +} diff --git a/test/bindings/java/org/uscxml/tests/MonitorExample.java b/test/bindings/java/org/uscxml/tests/MonitorExample.java new file mode 100644 index 0000000..2f0689a --- /dev/null +++ b/test/bindings/java/org/uscxml/tests/MonitorExample.java @@ -0,0 +1,46 @@ +package org.uscxml.tests; + +import org.uscxml.Interpreter; +import org.uscxml.InterpreterException; +import org.uscxml.InterpreterState; +import org.uscxml.tests.helper.TestMonitor; + + +public class MonitorExample { + + public static void main(String[] args) { + + String uSCXMLLibPath = "/Users/sradomski/Documents/TK/Code/uscxml/build/cli/lib/libuscxmlNativeJava.jnilib"; + if (System.getenv().containsKey("USCXML_JAVA_LIB")) { + uSCXMLLibPath = System.getenv("USCXML_JAVA_LIB"); + } + + System.load(uSCXMLLibPath); + + try { + TestMonitor tm = new TestMonitor(); + Interpreter scxml = Interpreter.fromURL("https://raw.githubusercontent.com/tklab-tud/uscxml/master/test/w3c/null/test436.scxml"); + scxml.setMonitor(tm); + InterpreterState state = InterpreterState.USCXML_UNDEF; + while((state = scxml.step()) != InterpreterState.USCXML_FINISHED) { + switch (state) { + case USCXML_FINISHED: + case USCXML_UNDEF: + case USCXML_IDLE: + case USCXML_INITIALIZED: + case USCXML_INSTANTIATED: + case USCXML_MICROSTEPPED: + case USCXML_MACROSTEPPED: + case USCXML_CANCELLED: + break; + default: + break; + } + } + + } catch (InterpreterException e) { + e.printStackTrace(); + } + } + +} diff --git a/test/bindings/java/org/uscxml/tests/helper/TestMonitor.java b/test/bindings/java/org/uscxml/tests/helper/TestMonitor.java new file mode 100644 index 0000000..9266847 --- /dev/null +++ b/test/bindings/java/org/uscxml/tests/helper/TestMonitor.java @@ -0,0 +1,76 @@ +package org.uscxml.tests.helper; + +import org.uscxml.InterpreterIssue; +import org.uscxml.InterpreterMonitor; +import org.uscxml.StringList; + +public class TestMonitor extends InterpreterMonitor { + + public TestMonitor() {} + + @Override + public void beforeExitingState(String stateId, String xpath, String stateXML) { + System.out.println("beforeExitingState: " + stateId + " " + xpath + " " + stateXML); + } + + @Override + public void afterExitingState(String stateId, String xpath, String stateXML) { + System.out.println("afterExitingState: " + stateId + " " + xpath + " " + stateXML); + } + + @Override + public void beforeExecutingContent(String tagName, String xpath, String contentXML) { + System.out.println("afterExecutingContent: " + tagName + " " + xpath + " " + contentXML); + } + + @Override + public void afterExecutingContent(String tagName, String xpath, String contentXML) { + System.out.println("afterExecutingContent: " + tagName + " " + xpath + " " + contentXML); + } + + @Override + public void beforeUninvoking(String xpath, String invokeid, String invokerXML) { + System.out.println("beforeUninvoking: " + xpath + " " + invokeid + " " + invokerXML); + } + + @Override + public void afterUninvoking(String xpath, String invokeid, String invokerXML) { + System.out.println("beforeUninvoking: " + xpath + " " + invokeid + " " + invokerXML); + } + + @Override + public void beforeTakingTransition(String xpath, String source, StringList targets, String transitionXML) { + System.out.println("beforeTakingTransition: " + xpath + " " + source + " " + targets + " " + transitionXML); + } + + @Override + public void afterTakingTransition(String xpath, String source, StringList targets, String transitionXML) { + System.out.println("afterTakingTransition: " + xpath + " " + source + " " + targets + " " + transitionXML); + } + + @Override + public void beforeEnteringState(String stateId, String xpath, String stateXML) { + System.out.println("beforeEnteringState: " + stateId + " " + xpath + " " + stateXML); + } + + @Override + public void afterEnteringState(String stateId, String xpath, String stateXML) { + System.out.println("afterEnteringState: " + stateId + " " + xpath + " " + stateXML); + } + + @Override + public void beforeInvoking(String xpath, String invokeid, String invokerXML) { + System.out.println("beforeInvoking: " + xpath + " " + invokeid + " " + invokerXML); + } + + @Override + public void afterInvoking(String xpath, String invokeid, String invokerXML) { + System.out.println("afterInvoking: " + xpath + " " + invokeid + " " + invokerXML); + } + + @Override + public void reportIssue(InterpreterIssue issue) { + System.out.println(issue); + } + +} |