This repository has been archived by the owner on Sep 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
SCXML.js
652 lines (522 loc) · 24.6 KB
/
SCXML.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
// Copyright 2011-2012 Jacob Beard, INFICON, and other SCION contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
"use strict";
var ArraySet = require('./set/ArraySet'),
stateKinds = require('./state-kinds-enum'),
setupDefaultOpts = require('./setup-default-opts'),
scxmlPrefixTransitionSelector = require('./scxml-dynamic-name-match-transition-selector'),
pm = require('../../platform');
function getTransitionWithHigherSourceChildPriority(model) {
return function(_arg) {
var t1 = _arg[0], t2 = _arg[1];
//compare transitions based first on depth, then based on document order
if (t1.source.depth < t2.source.depth) {
return t2;
} else if (t2.source.depth < t1.source.depth) {
return t1;
} else {
if (t1.documentOrder < t2.documentOrder) {
return t1;
} else {
return t2;
}
}
};
}
/** @const */
var printTrace = false;
/** @constructor */
function SCXMLInterpreter(model, opts){
this.model = model;
this.opts = opts;
this.opts.log = this.opts.log || pm.platform.log; //rely on global console if this console is undefined
this.opts.StateIdSet = this.opts.StateIdSet || ArraySet;
this.opts.EventSet = this.opts.EventSet || ArraySet;
this.opts.TransitionPairSet = this.opts.TransitionPairSet || ArraySet;
this.opts.priorityComparisonFn = this.opts.priorityComparisonFn || getTransitionWithHigherSourceChildPriority(this.opts.model);
this._sessionid = this.opts.sessionid || "";
this._platformvars = this.opts.platformvars || {};
this._ioprocessors = this.opts.ioprocessors || {};
this._configuration = new this.opts.BasicStateSet();
this._historyValue = {};
this._innerEventQueue = [];
this._isInFinalState = false;
this._timeoutMap = {};
this._listeners = [];
}
SCXMLInterpreter.prototype = {
/** @expose */
start : function() {
//perform big step without events to take all default transitions and reach stable initial state
if (printTrace) pm.platform.log("performing initial big step");
this._configuration.add(this.model.root.initial);
//figure out which require to use when evaluating action code, in the following order:
//the one specified when instantiating the interpreter
//the require of the module importing SCION
//the require of the main module
//this module's require
var actionCodeRequire =
this.opts.require ||
(module.parent &&
module.parent.parent &&
module.parent.parent.require &&
module.parent.parent.require.bind(module.parent.parent)) ||
(require.main &&
require.main.require &&
require.main.require.bind(require.main)) ||
require;
//set up scope for action code embedded in the document
var tmp = this.model.actionFactory(
this.opts.log,
this._cancel.bind(this),
this._send.bind(this),
this.opts.origin,
this.isIn.bind(this),
actionCodeRequire,
pm.platform.parseDocumentFromString,
this._sessionid,
this._ioprocessors,
this._platformvars);
this._actions = tmp.actions;
this._datamodel = tmp.datamodel;
this._performBigStep();
return this.getConfiguration();
},
_getOrSetData : function(fnName,name,value){
var data = this._datamodel[name];
if(!data) throw new Error("Variable " + name + " not declared in datamodel.");
return data[fnName](value);
},
_getData : function(name){
return this._getOrSetData("get",name);
},
_setData : function(name,value){
return this._getOrSetData("set",name,value);
},
/** @expose */
getConfiguration : function() {
return this._configuration.iter().map(function(s){return s.id;});
},
/** @expose */
getFullConfiguration : function() {
return this._configuration.iter().
map(function(s){ return [s].concat(this.opts.model.getAncestors(s));},this).
reduce(function(a,b){return a.concat(b);},[]). //flatten
map(function(s){return s.id;}).
reduce(function(a,b){return a.indexOf(b) > -1 ? a : a.concat(b);},[]); //uniq
},
/** @expose */
isIn : function(stateName) {
return this.getFullConfiguration().indexOf(stateName) > -1;
},
/** @expose */
isFinal : function(stateName) {
return this._isInFinalState;
},
/** @private */
_performBigStep : function(e) {
if (e) this._innerEventQueue.push(new this.opts.EventSet([e]));
var keepGoing = true;
while (keepGoing) {
var eventSet = this._innerEventQueue.length ? this._innerEventQueue.shift() : new this.opts.EventSet();
//create new datamodel cache for the next small step
var datamodelForNextStep = {};
var selectedTransitions = this._performSmallStep(eventSet, datamodelForNextStep);
keepGoing = !selectedTransitions.isEmpty();
}
this._isInFinalState = this._configuration.iter().every(function(s){ return s.kind === stateKinds.FINAL; });
},
/** @private */
_performSmallStep : function(eventSet, datamodelForNextStep) {
if (printTrace) pm.platform.log("selecting transitions with eventSet: ", eventSet);
var selectedTransitions = this._selectTransitions(eventSet, datamodelForNextStep);
if (printTrace) pm.platform.log("selected transitions: ", selectedTransitions);
if (!selectedTransitions.isEmpty()) {
if (printTrace) pm.platform.log("sorted transitions: ", selectedTransitions);
//we only want to enter and exit states from transitions with targets
//filter out targetless transitions here - we will only use these to execute transition actions
var selectedTransitionsWithTargets = new this.opts.TransitionSet(selectedTransitions.iter().filter(function(t){return t.targets;}));
var exitedTuple = this._getStatesExited(selectedTransitionsWithTargets),
basicStatesExited = exitedTuple[0],
statesExited = exitedTuple[1];
var enteredTuple = this._getStatesEntered(selectedTransitionsWithTargets),
basicStatesEntered = enteredTuple[0],
statesEntered = enteredTuple[1];
if (printTrace) pm.platform.log("basicStatesExited ", basicStatesExited);
if (printTrace) pm.platform.log("basicStatesEntered ", basicStatesEntered);
if (printTrace) pm.platform.log("statesExited ", statesExited);
if (printTrace) pm.platform.log("statesEntered ", statesEntered);
var eventsToAddToInnerQueue = new this.opts.EventSet();
//update history states
if (printTrace) pm.platform.log("executing state exit actions");
statesExited.forEach(function(state){
if (printTrace || this.opts.logStatesEnteredAndExited) pm.platform.log("exiting ", state.id);
//invoke listeners
this._listeners.forEach(function(l){
if(l.onExit) l.onExit(state.id);
});
if(state.onexit !== undefined) this._evaluateAction(state.onexit,eventSet, datamodelForNextStep, eventsToAddToInnerQueue);
var f;
if (state.history) {
if (state.history.isDeep) {
f = function(s0) {
return s0.kind === stateKinds.BASIC && state.descendants.indexOf(s0) > -1;
};
} else {
f = function(s0) {
return s0.parent === state;
};
}
//update history
this._historyValue[state.history.id] = statesExited.filter(f);
}
},this);
// -> Concurrency: Number of transitions: Multiple
// -> Concurrency: Order of transitions: Explicitly defined
var sortedTransitions = selectedTransitions.iter().sort(function(t1, t2) {
return t1.documentOrder - t2.documentOrder;
});
if (printTrace) pm.platform.log("executing transitition actions");
sortedTransitions.forEach(function(transition){
var targetIds = transition.targets && transition.targets.map(function(target){return target.id;});
this._listeners.forEach(function(l){
if(l.onTransition) l.onTransition(transition.source.id,targetIds);
});
if(transition.actions !== undefined) this._evaluateAction(transition.actions,eventSet, datamodelForNextStep, eventsToAddToInnerQueue);
},this);
if (printTrace) pm.platform.log("executing state enter actions");
statesEntered.forEach(function(state){
if (printTrace || this.opts.logStatesEnteredAndExited) pm.platform.log("entering", state.id);
this._listeners.forEach(function(l){
if(l.onEntry) l.onEntry(state.id);
});
if(state.onentry !== undefined) this._evaluateAction(state.onentry, eventSet, datamodelForNextStep, eventsToAddToInnerQueue);
},this);
if (printTrace) pm.platform.log("updating configuration ");
if (printTrace) pm.platform.log("old configuration ", this._configuration);
//update configuration by removing basic states exited, and adding basic states entered
this._configuration.difference(basicStatesExited);
this._configuration.union(basicStatesEntered);
if (printTrace) pm.platform.log("new configuration ", this._configuration);
//add set of generated events to the innerEventQueue -> Event Lifelines: Next small-step
if (!eventsToAddToInnerQueue.isEmpty()) {
if (printTrace) pm.platform.log("adding triggered events to inner queue ", eventsToAddToInnerQueue);
this._innerEventQueue.push(eventsToAddToInnerQueue);
}
if (printTrace) pm.platform.log("updating datamodel for next small step :");
//update the datamodel
for (var key in datamodelForNextStep) {
this._setData(key,datamodelForNextStep[key]);
}
}
//if selectedTransitions is empty, we have reached a stable state, and the big-step will stop, otherwise will continue -> Maximality: Take-Many
return selectedTransitions;
},
/** @private */
_evaluateAction : function(actionRef, eventSet, datamodelForNextStep, eventsToAddToInnerQueue) {
function $raise(event){
eventsToAddToInnerQueue.add(event);
}
var n = this._getScriptingInterface(datamodelForNextStep, eventSet, true);
return this._actions[actionRef].call(this.opts.evaluationContext, n.getData, n.setData, n.events, $raise);
},
/** @private */
_getScriptingInterface : function(datamodelForNextStep, eventSet, allowWrite) {
return {
setData: allowWrite ? function(name, value) {
return datamodelForNextStep[name] = value;
} : function() {},
getData: this._getData.bind(this),
events: eventSet.iter()
};
},
/** @private */
_getStatesExited : function(transitions) {
var statesExited = new this.opts.StateSet();
var basicStatesExited = new this.opts.BasicStateSet();
//States exited are defined to be active states that are
//descendants of the scope of each priority-enabled transition.
//Here, we iterate through the transitions, and collect states
//that match this condition.
transitions.iter().forEach(function(transition){
var lca = transition.lca,
scope = transition.scope,
desc = scope.descendants;
//For each state in the configuration
//is that state a descendant of the transition scope?
//Store ancestors of that state up to but not including the lca.
this._configuration.iter().forEach(function(state){
if(desc.indexOf(state) > -1){
basicStatesExited.add(state);
statesExited.add(state);
this.opts.model.getAncestors(state,lca).forEach(function(anc){
statesExited.add(anc);
});
}
},this);
},this);
var sortedStatesExited = statesExited.iter().sort(function(s1, s2) {
return s2.depth - s1.depth;
});
return [basicStatesExited, sortedStatesExited];
},
/** @private */
_getStatesEntered : function(transitions) {
var statesToEnter = new this.opts.StateSet();
var basicStatesToEnter = new this.opts.BasicStateSet();
var statesProcessed = new this.opts.StateSet();
var statesToProcess = [];
var processTransitionSourceAndTarget = (function(source,target){
//process each target
processState(target);
//and process ancestors of targets up to LCA, but according to special rules
var lca = this.opts.model.getLCA(source,target);
this.opts.model.getAncestors(target,lca).forEach(function(s){
if (s.kind === stateKinds.COMPOSITE) {
//just add him to statesToEnter, and declare him processed
//this is to prevent adding his initial state later on
statesToEnter.add(s);
statesProcessed.add(s);
}else{
//everything else can just be passed through as normal
processState(s);
}
});
}).bind(this);
var processState = (function(s){
if(statesProcessed.contains(s)) return;
if (s.kind === stateKinds.HISTORY) {
if (s.id in this._historyValue) {
this._historyValue[s.id].forEach(function(stateFromHistory){
processTransitionSourceAndTarget(s,stateFromHistory);
});
} else {
statesToEnter.add(s);
basicStatesToEnter.add(s);
}
} else {
statesToEnter.add(s);
if (s.kind === stateKinds.PARALLEL) {
statesToProcess.push.apply(statesToProcess,
s.children.filter(function(s){return s.kind !== stateKinds.HISTORY;}));
} else if (s.kind === stateKinds.COMPOSITE) {
statesToProcess.push(s.initial);
} else if (s.kind === stateKinds.INITIAL || s.kind === stateKinds.BASIC || s.kind === stateKinds.FINAL) {
basicStatesToEnter.add(s);
}
}
statesProcessed.add(s);
}).bind(this);
//do the initial setup
transitions.iter().forEach(function(transition){
transition.targets.forEach(function(target){
processTransitionSourceAndTarget(transition.source,target);
});
});
//loop and add states until there are no more to add (we reach a stable state)
var s;
/*jsl:ignore*/
while(s = statesToProcess.pop()){
/*jsl:end*/
processState(s);
}
//sort based on depth
var sortedStatesEntered = statesToEnter.iter().sort(function(s1, s2) {
return s1.depth - s2.depth;
});
return [basicStatesToEnter, sortedStatesEntered];
},
/** @private */
_selectTransitions : function(eventSet, datamodelForNextStep) {
if (this.opts.onlySelectFromBasicStates) {
var states = this._configuration.iter();
} else {
var statesAndParents = new this.opts.StateSet;
//get full configuration, unordered
//this means we may select transitions from parents before children
this._configuration.iter().forEach(function(basicState){
statesAndParents.add(basicState);
this.opts.model.getAncestors(basicState).forEach(function(ancestor){
statesAndParents.add(ancestor);
});
},this);
states = statesAndParents.iter();
}
var n = this._getScriptingInterface(datamodelForNextStep, eventSet);
var e = (function(t) {
return this._actions[t.conditionActionRef].call(this.opts.evaluationContext, n.getData, n.setData, n.events);
}).bind(this);
var eventNames = eventSet.iter().map(function(event){return event.name;});
var usePrefixMatchingAlgorithm = eventNames.filter(function(name){return name.search(".") != -1;}).length;
var transitionSelector = usePrefixMatchingAlgorithm ? scxmlPrefixTransitionSelector : this.opts.transitionSelector;
var enabledTransitions = new this.opts.TransitionSet();
states.forEach(function(state){
transitionSelector(state,eventNames,e).forEach(function(t){
enabledTransitions.add(t);
});
});
var priorityEnabledTransitions = this._selectPriorityEnabledTransitions(enabledTransitions);
if (printTrace) pm.platform.log("priorityEnabledTransitions", priorityEnabledTransitions);
return priorityEnabledTransitions;
},
/** @private */
_selectPriorityEnabledTransitions : function(enabledTransitions) {
var priorityEnabledTransitions = new this.opts.TransitionSet();
var tuple = this._getInconsistentTransitions(enabledTransitions),
consistentTransitions = tuple[0],
inconsistentTransitionsPairs = tuple[1];
priorityEnabledTransitions.union(consistentTransitions);
if (printTrace) pm.platform.log("enabledTransitions", enabledTransitions);
if (printTrace) pm.platform.log("consistentTransitions", consistentTransitions);
if (printTrace) pm.platform.log("inconsistentTransitionsPairs", inconsistentTransitionsPairs);
if (printTrace) pm.platform.log("priorityEnabledTransitions", priorityEnabledTransitions);
while (!inconsistentTransitionsPairs.isEmpty()) {
enabledTransitions = new this.opts.TransitionSet(
inconsistentTransitionsPairs.iter().map(function(t){return this.opts.priorityComparisonFn(t);},this));
tuple = this._getInconsistentTransitions(enabledTransitions);
consistentTransitions = tuple[0];
inconsistentTransitionsPairs = tuple[1];
priorityEnabledTransitions.union(consistentTransitions);
if (printTrace) pm.platform.log("enabledTransitions", enabledTransitions);
if (printTrace) pm.platform.log("consistentTransitions", consistentTransitions);
if (printTrace) pm.platform.log("inconsistentTransitionsPairs", inconsistentTransitionsPairs);
if (printTrace) pm.platform.log("priorityEnabledTransitions", priorityEnabledTransitions);
}
return priorityEnabledTransitions;
},
/** @private */
_getInconsistentTransitions : function(transitions) {
var allInconsistentTransitions = new this.opts.TransitionSet();
var inconsistentTransitionsPairs = new this.opts.TransitionPairSet();
var transitionList = transitions.iter();
if (printTrace) pm.platform.log("transitions", transitionList);
for(var i = 0; i < transitionList.length; i++){
for(var j = i+1; j < transitionList.length; j++){
var t1 = transitionList[i];
var t2 = transitionList[j];
if (this._conflicts(t1, t2)) {
allInconsistentTransitions.add(t1);
allInconsistentTransitions.add(t2);
inconsistentTransitionsPairs.add([t1, t2]);
}
}
}
var consistentTransitions = transitions.difference(allInconsistentTransitions);
return [consistentTransitions, inconsistentTransitionsPairs];
},
/** @private */
_conflicts : function(t1, t2) {
return !this._isArenaOrthogonal(t1, t2);
},
/** @private */
_isArenaOrthogonal : function(t1, t2) {
var isOrthogonal = this.opts.model.isOrthogonalTo(t1.scope, t2.scope);
if (printTrace) {
pm.platform.log("transition scopes", t1.scope.id, t1.scope.id);
pm.platform.log("transition scopes are orthogonal?", isOrthogonal);
}
return isOrthogonal;
},
/*
registerListener provides a generic mechanism to subscribe to state change notifications.
Can be used for logging and debugging. For example, can attache a logger that simply logs the state changes.
Or can attach a network debugging client that sends state change notifications to a debugging server.
listener is of the form:
{
onEntry : function(stateId){},
onExit : function(stateId){},
onTransition : function(sourceStateId,targetStatesIds[]){}
}
*/
/** @expose */
registerListener : function(listener){
return this._listeners.push(listener);
},
/** @expose */
unregisterListener : function(listener){
return this._listeners.splice(this._listeners.indexOf(listener),1);
}
};
/**
* @constructor
* @extends SCXMLInterpreter
*/
function SimpleInterpreter(model, opts) {
opts = opts || {};
setupDefaultOpts(opts);
this._isStepping = false;
this._send = opts.send || this._send;
this._cancel = opts.cancel || this._cancel;
SCXMLInterpreter.call(this,model,opts); //call super constructor
}
SimpleInterpreter.prototype = Object.create(SCXMLInterpreter.prototype);
/** @expose */
SimpleInterpreter.prototype.gen = function(evtObjOrName,optionalData) {
var e;
switch(typeof evtObjOrName){
case 'string':
e = {name : evtObjOrName, data : optionalData};
break;
case 'object':
if(typeof evtObjOrName.name === 'string'){
e = evtObjOrName;
}else{
throw new Error('Event object must have "name" property of type string.');
}
break;
default:
throw new Error('First argument to gen must be a string or object.');
}
if (this._isStepping) {
throw new Error("gen called before previous call to gen could complete. If executed in single-threaded environment, this means it was called recursively, which is illegal, as it would break SCION step semantics.");
}
this._isStepping = true;
this._performBigStep(e);
this._isStepping = false;
return this.getConfiguration();
};
/** @private */
SimpleInterpreter.prototype._send = function(event, options, fnRaise) {
var callback, timeoutId,
_this = this;
if (pm.platform.setTimeout) {
if (printTrace) {
pm.platform.log("sending event", event.name, "with content", event.data, "after delay", options.delay);
}
callback = function() {
return _this.gen(event);
};
timeoutId = pm.platform.setTimeout(callback, options.delay);
if (options.sendid) return this._timeoutMap[options.sendid] = timeoutId;
} else {
throw new Error("setTimeout function not set");
}
};
/** @private */
SimpleInterpreter.prototype._cancel = function(sendid){
if (pm.platform.clearTimeout) {
if (sendid in this._timeoutMap) {
if (printTrace) {
pm.platform.log("cancelling ", sendid, " with timeout id ", this._timeoutMap[sendid]);
}
return pm.platform.clearTimeout(this._timeoutMap[sendid]);
}
} else {
throw new Error("clearTimeout function not set");
}
};
module.exports = {
SCXMLInterpreter: SCXMLInterpreter,
SimpleInterpreter: SimpleInterpreter
};