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