summaryrefslogtreecommitdiffstats
path: root/embedding/java/src/org/uscxml/tests/invoker/factory/TestCustomInvoker.java
blob: 637a6566716aacc6cff6f5d9251e9abca43b97e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package org.uscxml.tests.invoker.factory;

import org.uscxml.Data;
import org.uscxml.Event;
import org.uscxml.Factory;
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 TestCustomInvoker 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")); // 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")); // enqueue an external event
		}
	}

	@Override
	public void uninvoke() {
		System.out.println("uninvoke");
	}

	@Override
	public Invoker create(Interpreter interpreter) {
		TestCustomInvoker invoker = new TestCustomInvoker();
		invoker.swigReleaseOwnership();
		return invoker;
	}

	/**
	 * @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");

		TestCustomInvoker invoker = new TestCustomInvoker();
		// just register prototype at global factory
		Factory.getInstance().registerInvoker(invoker);

		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>";

		// parse and interpret
		Interpreter interpreter = Interpreter.fromXML(xml);
		interpreter.interpret();
	}

}