Skip to content

Commit

Permalink
Moved java binding
Browse files Browse the repository at this point in the history
  • Loading branch information
sradomski committed Nov 28, 2016
1 parent 5c09b82 commit 044fefa
Show file tree
Hide file tree
Showing 19 changed files with 794 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -8,7 +8,6 @@ contrib/prebuilt/linux*
contrib/prebuilt/windows*
contrib/prebuilt/ios*
contrib/prebuilt/android*
contrib/java/bin*
contrib/java/*.log

package/linux*
Expand Down
9 changes: 9 additions & 0 deletions contrib/java/bindings/build.properties
@@ -0,0 +1,9 @@
# Default path on Windows (maybe with an '(x86)' in there for good measure)
#umundo.jar=C:\\Program Files\\uMundo\\share\\umundo\\lib\\umundo.jar

# Default path on MacOSX and Linux
uscxml.library.path=/usr/local/share/uscxml/bindings/java
uscxml.jar=/usr/local/share/uscxml/bindings/uscxml.jar

jexl.jar=libs/commons-jexl3-3.0.jar
logging.jar=libs/commons-logging-1.2.jar
42 changes: 42 additions & 0 deletions contrib/java/bindings/build.xml
@@ -0,0 +1,42 @@
<project name="java-tests" default="test-w3c">

<property environment="env"/>

<property file="build.properties" />
<property name="src" value="." />
<property name="bin" value="bin" />
<!-- <property name="test.file" value="..." /> -->

<!-- Allow to override actual value with environment .. yes ant sucks -->
<condition property="real.uscxml.jar" value="${env.USCXML_JAVA_JAR}" else="${uscxml.jar}">
<isset property="env.USCXML_JAVA_JAR" />
</condition>

<target name="clean">
<delete dir="${bin}" />
</target>

<target name="compile">
<mkdir dir="${bin}" />
<javac destdir="${bin}" debuglevel="lines,vars,source" debug="on"
encoding="utf-8" includeantruntime="false">
<src path="${src}" />
<classpath>
<pathelement location="${real.uscxml.jar}" />
<pathelement location="${jexl.jar}" />
<pathelement location="${logging.jar}" />
</classpath>
<include name="**/*.java" />
</javac>
</target>

<target name="test-w3c">
<antcall target="test-w3c-jexl" />
</target>

<target name="test-w3c-jexl" depends="compile">
<java classpath="${real.uscxml.jar}:${jexl.jar}:${logging.jar}:${bin}" classname="org.uscxml.tests.JexlDataModelTest">
<arg value="${test.file}"/>
</java>
</target>
</project>
@@ -0,0 +1,7 @@
package org.uscxml.apache.commons.scxml2;

import org.uscxml.DataModel;

public class Context {
public DataModel dm = null;
}
@@ -0,0 +1,7 @@
package org.uscxml.apache.commons.scxml2;

public abstract class Evaluator {

public abstract Context newContext(Object object);

}
@@ -0,0 +1,19 @@
package org.uscxml.apache.commons.scxml2;

import org.uscxml.Factory;
import org.uscxml.dm.jexl.JexlDataModel;

public class JexlEvaluator extends Evaluator {

public JexlEvaluator() {

}

@Override
public Context newContext(Object object) {
// TODO Auto-generated method stub
Context ctx = new Context();
ctx.dm = new JexlDataModel();
return ctx;
}
}
@@ -0,0 +1,8 @@
package org.uscxml.apache.commons.scxml2;

import java.net.URL;

public class SCXML {
public URL url = null;

}
@@ -0,0 +1,49 @@
package org.uscxml.apache.commons.scxml2;

import java.net.URL;

import org.uscxml.ActionLanguage;
import org.uscxml.Factory;
import org.uscxml.Interpreter;
import org.uscxml.InterpreterException;
import org.uscxml.InterpreterState;
import org.uscxml.helper.TestMonitor;

public class SCXMLExecutor {

public Interpreter interpreter = null;
public URL sourceURL = null;
public ActionLanguage al = new ActionLanguage();

public SCXMLExecutor(Evaluator evaluator, Object object, SimpleErrorReporter simpleErrorReporter) {
// TODO Auto-generated constructor stub
}

public void setStateMachine(SCXML scxml) {
sourceURL = scxml.url;
}

public void setRootContext(Context rootContext) {
al.setDataModel(rootContext.dm);
}

public void go() {
try {
interpreter = Interpreter.fromURL(sourceURL.toString());
interpreter.setActionLanguage(al);

TestMonitor tm = new TestMonitor();
interpreter.addMonitor(tm);

InterpreterState state = InterpreterState.USCXML_UNDEF;
while(state != InterpreterState.USCXML_FINISHED) {
interpreter.step();
}

} catch (InterpreterException e) {
e.printStackTrace();
}

}

}
@@ -0,0 +1,13 @@
package org.uscxml.apache.commons.scxml2;

import java.net.URL;

public class SCXMLReader {

public static SCXML read(URL scxml) {
SCXML foo = new SCXML();
foo.url = scxml;
return foo;
}

}
@@ -0,0 +1,5 @@
package org.uscxml.apache.commons.scxml2;

public class SimpleErrorReporter {

}
45 changes: 45 additions & 0 deletions contrib/java/bindings/org/uscxml/examples/ApacheCommonsAPI.java
@@ -0,0 +1,45 @@
package org.uscxml.examples;

import java.net.URL;

//import org.uscxml.apache.commons.scxml2.*;
import org.apache.commons.scxml2.*;
import org.apache.commons.scxml2.env.SimpleErrorReporter;
import org.apache.commons.scxml2.env.jexl.JexlEvaluator;
import org.apache.commons.scxml2.io.SCXMLReader;
import org.apache.commons.scxml2.model.SCXML;

public class ApacheCommonsAPI {

// SCXML model source URL
private static final URL SCXML = ApacheCommonsAPI.class.getResource("hello-world.xml");

public static void main(String [] args) throws Exception {
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);


// evaluator instance which is used by SCXML engine to evaluate expressions in SCXML
Evaluator evaluator = new JexlEvaluator();
// engine to execute the scxml instance
SCXMLExecutor executor = new SCXMLExecutor(evaluator, null, new SimpleErrorReporter());

// parse SCXML URL into SCXML model
SCXML scxml = SCXMLReader.read(SCXML);
// set state machine (scxml instance) to execute
executor.setStateMachine(scxml);

// create root context storing variables and being used by evaluator
Context rootContext = evaluator.newContext(null);
// set the root context for the engine
executor.setRootContext(rootContext);

// initiate the execution of the state machine
executor.go();
}

}
46 changes: 46 additions & 0 deletions contrib/java/bindings/org/uscxml/examples/BasicExample.java
@@ -0,0 +1,46 @@
package org.uscxml.examples;

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();
System.exit(-1);
}
System.exit(0);

}

}
60 changes: 60 additions & 0 deletions contrib/java/bindings/org/uscxml/examples/DataModelExample.java
@@ -0,0 +1,60 @@
package org.uscxml.examples;

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.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();
System.exit(-1);
}
System.exit(0);

}

}
55 changes: 55 additions & 0 deletions contrib/java/bindings/org/uscxml/examples/MonitorExample.java
@@ -0,0 +1,55 @@
package org.uscxml.examples;

import org.uscxml.Interpreter;
import org.uscxml.InterpreterException;
import org.uscxml.InterpreterState;
import org.uscxml.StringVector;
import org.uscxml.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.addMonitor(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:
break;
case USCXML_MICROSTEPPED:
case USCXML_MACROSTEPPED:
StringVector states = scxml.getConfiguration();
for (int i = 0; i < states.size(); i++) {
System.out.print(states.get(i) + " ");
}
System.out.println();
case USCXML_CANCELLED:
break;
default:
break;
}
}

} catch (InterpreterException e) {
e.printStackTrace();
System.exit(-1);
}
System.exit(0);
}

}

0 comments on commit 044fefa

Please sign in to comment.