1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.scxml2.test;
18
19 import java.io.BufferedReader;
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
23 import java.net.URL;
24 import java.util.StringTokenizer;
25
26 import javax.xml.stream.XMLStreamException;
27
28 import org.apache.commons.scxml2.Context;
29 import org.apache.commons.scxml2.Evaluator;
30 import org.apache.commons.scxml2.SCXMLExecutor;
31 import org.apache.commons.scxml2.TriggerEvent;
32 import org.apache.commons.scxml2.env.Tracer;
33 import org.apache.commons.scxml2.invoke.SimpleSCXMLInvoker;
34 import org.apache.commons.scxml2.io.SCXMLReader;
35 import org.apache.commons.scxml2.io.SCXMLWriter;
36 import org.apache.commons.scxml2.model.ModelException;
37 import org.apache.commons.scxml2.model.SCXML;
38
39
40
41
42
43
44
45
46
47
48
49
50 public final class StandaloneUtils {
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 public static void execute(final String uri, final Evaluator evaluator) {
71 try {
72 String documentURI = getCanonicalURI(uri);
73 Context rootCtx = evaluator.newContext(null);
74 Tracer trc = new Tracer();
75 SCXML doc = SCXMLReader.read(new URL(documentURI));
76 if (doc == null) {
77 System.err.println("The SCXML document " + uri
78 + " can not be parsed!");
79 System.exit(-1);
80 }
81 System.out.println(SCXMLWriter.write(doc));
82 SCXMLExecutor exec = new SCXMLExecutor(evaluator, null, trc);
83 exec.setStateMachine(doc);
84 exec.addListener(doc, trc);
85 exec.registerInvokerClass("scxml", SimpleSCXMLInvoker.class);
86 exec.setRootContext(rootCtx);
87 exec.go();
88 BufferedReader br = new BufferedReader(new
89 InputStreamReader(System.in));
90 String event;
91 while ((event = br.readLine()) != null) {
92 event = event.trim();
93 if (event.equalsIgnoreCase("help") || event.equals("?")) {
94 System.out.println("Enter a space-separated list of "
95 + "events");
96 System.out.println("To populate a variable in the "
97 + "current context, type \"name=value\"");
98 System.out.println("To quit, enter \"quit\"");
99 System.out.println("To reset state machine, enter "
100 + "\"reset\"");
101 } else if (event.equalsIgnoreCase("quit")) {
102 break;
103 } else if (event.equalsIgnoreCase("reset")) {
104 exec.reset();
105 } else if (event.indexOf('=') != -1) {
106 int marker = event.indexOf('=');
107 String name = event.substring(0, marker);
108 String value = event.substring(marker + 1);
109 rootCtx.setLocal(name, value);
110 System.out.println("Set variable " + name + " to "
111 + value);
112 } else if (event.trim().length() == 0
113 || event.equalsIgnoreCase("null")) {
114 TriggerEvent[] evts = {new TriggerEvent(null,
115 TriggerEvent.SIGNAL_EVENT, null)};
116 exec.triggerEvents(evts);
117 if (exec.getStatus().isFinal()) {
118 System.out.println("A final configuration reached.");
119 }
120 } else {
121 StringTokenizer st = new StringTokenizer(event);
122 int tkns = st.countTokens();
123 TriggerEvent[] evts = new TriggerEvent[tkns];
124 for (int i = 0; i < tkns; i++) {
125 evts[i] = new TriggerEvent(st.nextToken(),
126 TriggerEvent.SIGNAL_EVENT, null);
127 }
128 exec.triggerEvents(evts);
129 if (exec.getStatus().isFinal()) {
130 System.out.println("A final configuration reached.");
131 }
132 }
133 }
134 } catch (IOException e) {
135 e.printStackTrace();
136 } catch (ModelException e) {
137 e.printStackTrace();
138 } catch (XMLStreamException e) {
139 e.printStackTrace();
140 }
141 }
142
143
144
145
146
147
148
149 private static String getCanonicalURI(final String uri)
150 throws IOException {
151 if (uri.toLowerCase().startsWith("http://")
152 || uri.toLowerCase().startsWith("file://")) {
153 return uri;
154 }
155 File in = new File(uri);
156 return "file:///" + in.getCanonicalPath();
157 }
158
159
160
161
162 private StandaloneUtils() {
163 super();
164 }
165
166 }
167