UNPKG

2.13 kBJavaScriptView Raw
1#!/usr/bin/env node
2//A simple scxml interactive simulation environment
3var scxml = require('..'),
4 repl = require('repl');
5
6var pathToScxml = process.argv[2];
7
8if(!pathToScxml){
9 console.log('Usage: scxml foo.scxml');
10 process.exit(1);
11}
12
13function customSend(event, options) {
14 console.log('SEND: ' +
15 JSON.stringify(event) +
16 ', options: ' +
17 JSON.stringify(options));
18}
19
20var interpOpts = {
21 customSend: customSend
22}
23
24var listeners = {
25 onEntry: function(stateId) { console.log('entering state ' + stateId); },
26 onExit: function(stateId) { console.log('exiting state ' + stateId); },
27 onTransition: function(sourceStateId, targetIds) {
28 if (targetIds && targetIds.length) {
29 console.log('transitioning from ' + sourceStateId + ' to ' + targetIds.join(','));
30 } else {
31 console.log('executing target-less transition in ' + sourceStateId);
32 }
33 },
34 onError: function(err) {
35 console.log('ERROR:' + JSON.stringify(err));
36 }
37};
38
39//1 - 2. get the xml file and convert it to jsonml
40scxml.pathToModel(pathToScxml,function(err,model){
41
42 if(err){
43 console.error(err);
44 process.exit(1);
45 }
46
47 model.prepare(function(err, fnModel) {
48 if (err) {
49 console.error(err);
50 process.exit(1);
51 }
52
53 //Use the statechart object model to instantiate an instance of the statechart interpreter. Optionally, we can pass to the construct an object to be used as the context object (the 'this' object) in script evaluation. Lots of other parameters are available.
54 var interpreter = new scxml.scion.Statechart(fnModel, interpOpts);
55
56
57 interpreter.registerListener(listeners);
58
59
60 interpreter.start();
61
62 console.log(interpreter.getConfiguration());
63
64 function processEvent(cmd,dontKnow,alsoDontKnow,callback){
65 cmd = cmd.trim();
66 interpreter.gen({name : cmd});
67 var conf = interpreter.getConfiguration();
68 callback(null,conf);
69 }
70
71 //start
72 repl.start('#',process.stdin,processEvent);
73 })
74
75});
76