1 | #!/usr/bin/env node
|
2 |
|
3 | var scxml = require('..'),
|
4 | repl = require('repl');
|
5 |
|
6 | var pathToScxml = process.argv[2];
|
7 |
|
8 | if(!pathToScxml){
|
9 | console.log('Usage: scxml foo.scxml');
|
10 | process.exit(1);
|
11 | }
|
12 |
|
13 | function customSend(event, options) {
|
14 | console.log('SEND: ' +
|
15 | JSON.stringify(event) +
|
16 | ', options: ' +
|
17 | JSON.stringify(options));
|
18 | }
|
19 |
|
20 | var interpOpts = {
|
21 | customSend: customSend
|
22 | }
|
23 |
|
24 | var 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 |
|
40 | scxml.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 |
|
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 |
|
72 | repl.start('#',process.stdin,processEvent);
|
73 | })
|
74 |
|
75 | });
|
76 |
|