scion-core
Advanced tools
Comparing version 1.3.2 to 1.4.0
{ | ||
"name": "scion-core", | ||
"version": "1.2.2", | ||
"version": "1.4.0", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Jacob Beard", |
@@ -117,4 +117,14 @@ (function (global, factory) { | ||
function transitionToString(sourceState) { | ||
return sourceState + ' -- ' + (this.events ? '(' + this.events.join(',') + ')' : null) + (this.cond ? '[' + this.cond.name + ']' : '') + ' --> ' + (this.targets ? this.targets.join(',') : null); | ||
} | ||
function stateToString() { | ||
return this.id; | ||
} | ||
function traverse(ancestors, state) { | ||
if (printTrace) state.toString = stateToString; | ||
//add to global transition and state id caches | ||
@@ -145,2 +155,3 @@ if (state.transitions) transitions.push.apply(transitions, state.transitions); | ||
transition.source = state; | ||
if (printTrace) transition.toString = transitionToString.bind(transition, state); | ||
}; | ||
@@ -558,3 +569,3 @@ | ||
toString: function toString() { | ||
return "Set(" + Array.from(this.o).toString() + ")"; | ||
return this.o.size === 0 ? '<empty>' : Array.from(this.o).join(',\n'); | ||
} | ||
@@ -589,8 +600,14 @@ }; | ||
function scxmlPrefixTransitionSelector(state, event, evaluator) { | ||
function scxmlPrefixTransitionSelector(state, event, evaluator, selectEventlessTransitions) { | ||
return state.transitions.filter(function (t) { | ||
return (!t.events || event && event.name && isTransitionMatch(t, event.name)) && (!t.cond || evaluator(t.cond)); | ||
return (selectEventlessTransitions ? !t.events : !t.events || event && event.name && isTransitionMatch(t, event.name)) && (!t.cond || evaluator(t.cond)); | ||
}); | ||
} | ||
function eventlessTransitionSelector(state) { | ||
return state.transitions.filter(function (transition) { | ||
return !transition.events || transition.events && transition.events.length === 0; | ||
}); | ||
} | ||
//model accessor functions | ||
@@ -745,3 +762,3 @@ var query = { | ||
//perform big step without events to take all default transitions and reach stable initial state | ||
if (printTrace) this.opts.console.log("performing initial big step"); | ||
this._log("performing initial big step"); | ||
@@ -767,3 +784,3 @@ //We effectively need to figure out states to enter here to populate initial config. assuming root is compound state makes this simple. | ||
if (printTrace) this.opts.console.log("performing initial big step"); | ||
this._log("performing initial big step"); | ||
@@ -813,4 +830,10 @@ this._configuration.add(this._model.initialRef); | ||
var currentEvent = this._internalEventQueue.shift() || null; | ||
var selectedTransitions = this._selectTransitions(currentEvent, true); | ||
if (selectedTransitions.isEmpty()) { | ||
selectedTransitions = this._selectTransitions(currentEvent, false); | ||
} | ||
this.emit('onSmallStepBegin', currentEvent); | ||
var selectedTransitions = this._performSmallStep(currentEvent); | ||
this._performSmallStep(currentEvent, selectedTransitions); | ||
this.emit('onSmallStepEnd', currentEvent); | ||
keepGoing = !selectedTransitions.isEmpty(); | ||
@@ -835,4 +858,9 @@ } | ||
var currentEvent = self._internalEventQueue.shift() || null; | ||
var selectedTransitions = self._selectTransitions(currentEvent, true); | ||
if (selectedTransitions.isEmpty()) { | ||
selectedTransitions = self._selectTransitions(currentEvent, false); | ||
} | ||
self.emit('onSmallStepBegin', currentEvent); | ||
selectedTransitions = self._performSmallStep(currentEvent); | ||
self._performSmallStep(currentEvent, selectedTransitions); | ||
self.emit('onSmallStepEnd', currentEvent); | ||
@@ -863,13 +891,11 @@ } catch (err) { | ||
/** @private */ | ||
_performSmallStep: function _performSmallStep(currentEvent) { | ||
_performSmallStep: function _performSmallStep(currentEvent, selectedTransitions) { | ||
if (printTrace) this.opts.console.log("selecting transitions with currentEvent: ", currentEvent); | ||
this._log("selecting transitions with currentEvent", JSON.stringify(currentEvent)); | ||
var selectedTransitions = this._selectTransitions(currentEvent); | ||
this._log("selected transitions", selectedTransitions); | ||
if (printTrace) this.opts.console.log("selected transitions: ", selectedTransitions); | ||
if (!selectedTransitions.isEmpty()) { | ||
if (printTrace) this.opts.console.log("sorted transitions: ", selectedTransitions); | ||
this._log("sorted transitions", selectedTransitions); | ||
@@ -888,6 +914,6 @@ //we only want to enter and exit states from transitions with targets | ||
if (printTrace) this.opts.console.log("basicStatesExited ", basicStatesExited); | ||
if (printTrace) this.opts.console.log("basicStatesEntered ", basicStatesEntered); | ||
if (printTrace) this.opts.console.log("statesExited ", statesExited); | ||
if (printTrace) this.opts.console.log("statesEntered ", statesEntered); | ||
this._log("basicStatesExited ", basicStatesExited); | ||
this._log("basicStatesEntered ", basicStatesEntered); | ||
this._log("statesExited ", statesExited); | ||
this._log("statesEntered ", statesEntered); | ||
@@ -897,3 +923,3 @@ var eventsToAddToInnerQueue = new this.opts.Set(); | ||
//update history states | ||
if (printTrace) this.opts.console.log("executing state exit actions"); | ||
this._log("executing state exit actions"); | ||
@@ -903,3 +929,3 @@ for (var j = 0, len = statesExited.length; j < len; j++) { | ||
if (printTrace || this.opts.logStatesEnteredAndExited) this.opts.console.log("exiting ", stateExited.id); | ||
this._log("exiting ", stateExited.id); | ||
@@ -935,3 +961,3 @@ //invoke listeners | ||
if (printTrace) this.opts.console.log("executing transitition actions"); | ||
this._log("executing transitition actions"); | ||
@@ -954,3 +980,3 @@ for (var stxIdx = 0, len = sortedTransitions.length; stxIdx < len; stxIdx++) { | ||
if (printTrace) this.opts.console.log("executing state enter actions"); | ||
this._log("executing state enter actions"); | ||
@@ -960,3 +986,3 @@ for (var enterIdx = 0, enterLen = statesEntered.length; enterIdx < enterLen; enterIdx++) { | ||
if (printTrace || this.opts.logStatesEnteredAndExited) this.opts.console.log("entering", stateEntered.id); | ||
this._log("entering", stateEntered.id); | ||
@@ -972,4 +998,4 @@ this.emit('onEntry', stateEntered.id); | ||
if (printTrace) this.opts.console.log("updating configuration "); | ||
if (printTrace) this.opts.console.log("old configuration ", this._configuration); | ||
this._log("updating configuration "); | ||
this._log("old configuration ", this._configuration); | ||
@@ -980,7 +1006,7 @@ //update configuration by removing basic states exited, and adding basic states entered | ||
if (printTrace) this.opts.console.log("new configuration ", this._configuration); | ||
this._log("new configuration ", this._configuration); | ||
//add set of generated events to the innerEventQueue -> Event Lifelines: Next small-step | ||
if (!eventsToAddToInnerQueue.isEmpty()) { | ||
if (printTrace) this.opts.console.log("adding triggered events to inner queue ", eventsToAddToInnerQueue); | ||
this._log("adding triggered events to inner queue ", eventsToAddToInnerQueue); | ||
this._internalEventQueue.push(eventsToAddToInnerQueue); | ||
@@ -1134,3 +1160,3 @@ } | ||
/** @private */ | ||
_selectTransitions: function _selectTransitions(currentEvent) { | ||
_selectTransitions: function _selectTransitions(currentEvent, selectEventlessTransitions) { | ||
if (this.opts.onlySelectFromBasicStates) { | ||
@@ -1162,3 +1188,3 @@ var states = this._configuration.iter(); | ||
for (var stateIdx = 0, stateLen = states.length; stateIdx < stateLen; stateIdx++) { | ||
var transitions = transitionSelector(states[stateIdx], currentEvent, e); | ||
var transitions = transitionSelector(states[stateIdx], currentEvent, e, selectEventlessTransitions); | ||
for (var txIdx = 0, len = transitions.length; txIdx < len; txIdx++) { | ||
@@ -1171,3 +1197,3 @@ enabledTransitions.add(transitions[txIdx]); | ||
if (printTrace) this.opts.console.log("priorityEnabledTransitions", priorityEnabledTransitions); | ||
this._log("priorityEnabledTransitions", priorityEnabledTransitions); | ||
@@ -1187,6 +1213,6 @@ return priorityEnabledTransitions; | ||
if (printTrace) this.opts.console.log("enabledTransitions", enabledTransitions); | ||
if (printTrace) this.opts.console.log("consistentTransitions", consistentTransitions); | ||
if (printTrace) this.opts.console.log("inconsistentTransitionsPairs", inconsistentTransitionsPairs); | ||
if (printTrace) this.opts.console.log("priorityEnabledTransitions", priorityEnabledTransitions); | ||
this._log("enabledTransitions", enabledTransitions); | ||
this._log("consistentTransitions", consistentTransitions); | ||
this._log("inconsistentTransitionsPairs", inconsistentTransitionsPairs); | ||
this._log("priorityEnabledTransitions", priorityEnabledTransitions); | ||
@@ -1204,6 +1230,6 @@ while (!inconsistentTransitionsPairs.isEmpty()) { | ||
if (printTrace) this.opts.console.log("enabledTransitions", enabledTransitions); | ||
if (printTrace) this.opts.console.log("consistentTransitions", consistentTransitions); | ||
if (printTrace) this.opts.console.log("inconsistentTransitionsPairs", inconsistentTransitionsPairs); | ||
if (printTrace) this.opts.console.log("priorityEnabledTransitions", priorityEnabledTransitions); | ||
this._log("enabledTransitions", enabledTransitions); | ||
this._log("consistentTransitions", consistentTransitions); | ||
this._log("inconsistentTransitionsPairs", inconsistentTransitionsPairs); | ||
this._log("priorityEnabledTransitions", priorityEnabledTransitions); | ||
} | ||
@@ -1219,3 +1245,3 @@ return priorityEnabledTransitions; | ||
if (printTrace) this.opts.console.log("transitions", transitionList); | ||
this._log("transitions", transitions); | ||
@@ -1238,2 +1264,11 @@ for (var i = 0; i < transitionList.length; i++) { | ||
_log: function _log() { | ||
if (printTrace) { | ||
var args = Array.from(arguments); | ||
this.opts.console.log(args[0] + ': ' + args.slice(1).map(function (arg) { | ||
return arg === null ? 'null' : arg === undefined ? 'undefined' : arg.toString(); | ||
}).join(', ')); | ||
} | ||
}, | ||
/** @private */ | ||
@@ -1247,7 +1282,7 @@ _conflicts: function _conflicts(t1, t2) { | ||
if (printTrace) this.opts.console.log("transition scopes", t1.scope, t2.scope); | ||
this._log("transition scopes", t1.scope, t2.scope); | ||
var isOrthogonal = query.isOrthogonalTo(t1.scope, t2.scope); | ||
if (printTrace) this.opts.console.log("transition scopes are orthogonal?", isOrthogonal); | ||
this._log("transition scopes are orthogonal?", isOrthogonal); | ||
@@ -1520,3 +1555,3 @@ return isOrthogonal; | ||
if (printTrace) this._interpreter.opts.console.log("sending event", event.name, "with content", event.data, "after delay", options.delay); | ||
this._interpreter._log("sending event", event.name, "with content", event.data, "after delay", options.delay); | ||
@@ -1533,3 +1568,3 @@ validateSend.call(this, event, options, sendFn); | ||
if (sendid in this._timeoutMap) { | ||
if (printTrace) this._interpreter.opts.console.log("cancelling ", sendid, " with timeout id ", this._timeoutMap[sendid]); | ||
this._interpreter._log("cancelling ", sendid, " with timeout id ", this._timeoutMap[sendid]); | ||
clearTimeout(this._timeoutMap[sendid]); | ||
@@ -1536,0 +1571,0 @@ } |
@@ -1,2 +0,2 @@ | ||
/*! scion-core 2017-03-31 */ | ||
!function(a,b){if("function"==typeof define&&define.amd)define(["module"],b);else if("undefined"!=typeof exports)b(module);else{var c={exports:{}};b(c),a.scion=c.exports}}(this,function(a){"use strict";function b(a){return a.targets}function c(a,b){return b.depth-a.depth}function d(a,b){return a.depth-b.depth}function e(a,b){return a.documentOrder-b.documentOrder}function f(a){function b(a){void 0===m[a]&&(m[a]=0);var b=m[a]++;return"$generated-"+a+"-"+b}function c(a){return{$deserializeDatamodel:a.$deserializeDatamodel||function(){},$serializeDatamodel:a.$serializeDatamodel||function(){return null},$idToStateMap:k,states:[{$type:"initial",transitions:[{target:a}]},a]}}function d(a,c){if(c.transitions&&j.push.apply(j,c.transitions),c.id){if(k.has(c.id))throw new Error("Redefinition of state id "+c.id);k.set(c.id,c)}c.$type=c.$type||"state",c.ancestors=a,c.depth=a.length,c.parent=a[0],c.transitions=c.transitions||[];for(var f=0,g=c.transitions.length;f<g;f++){var h=c.transitions[f];h.documentOrder=l++,h.source=c}if(c.states)for(var i=[c].concat(a),f=0,g=c.states.length;f<g;f++)d(i,c.states[f]);switch(c.$type){case"parallel":c.typeEnum=w.PARALLEL;break;case"initial":c.typeEnum=w.INITIAL;break;case"history":c.typeEnum=w.HISTORY;break;case"final":c.typeEnum=w.FINAL;break;case"state":case"scxml":c.states&&c.states.length?c.typeEnum=w.COMPOSITE:c.typeEnum=w.BASIC;break;default:throw new Error("Unknown state type: "+c.$type)}c.states?c.descendants=c.states.concat(c.states.map(function(a){return a.descendants}).reduce(function(a,b){return a.concat(b)},[])):c.descendants=[];var m;if(c.typeEnum===w.COMPOSITE&&("string"==typeof c.initial?n.push(c):(m=c.states.filter(function(a){return"initial"===a.$type}),c.initialRef=m.length?m[0]:c.states[0],e(c))),c.typeEnum===w.COMPOSITE||c.typeEnum===w.PARALLEL){var o=c.states.filter(function(a){return"history"===a.$type});c.historyRef=o[0]}c.id||(c.id=b(c.$type),k.set(c.id,c)),c.onEntry&&!Array.isArray(c.onEntry)&&(c.onEntry=[c.onEntry]),c.onExit&&!Array.isArray(c.onExit)&&(c.onExit=[c.onExit])}function e(a){if(!a.initialRef)throw new Error("Unable to locate initial state for composite state: "+a.id)}function f(){for(var a=0,b=n.length;a<b;a++){var c=n[a];c.initialRef=k.get(c.initial),e(c)}}function g(){for(var a=0,b=j.length;a<b;a++){var c=j[a];if(c.onTransition&&!Array.isArray(c.onTransition)&&(c.onTransition=[c.onTransition]),"string"==typeof c.event&&(c.events=c.event.trim().split(o)),delete c.event,!c.targets&&"undefined"!=typeof c.target)if("string"==typeof c.target){var d=k.get(c.target);if(!d)throw new Error("Unable to find target state with id "+c.target);c.target=d,c.targets=[c.target]}else if(Array.isArray(c.target))c.targets=c.target.map(function(a){if("string"==typeof a){if(a=k.get(a),!a)throw new Error("Unable to find target state with id "+c.target);return a}return a});else{if("object"!==u(c.target))throw new Error("Transition target has unknown type: "+c.target);c.targets=[c.target]}}for(var a=0,b=j.length;a<b;a++){var c=j[a];c.targets&&(c.lcca=i(c.source,c.targets[0])),c.scope=h(c)}}function h(a){var b="internal"===a.type&&a.source.parent&&a.targets&&a.targets.every(function(b){return a.source.descendants.indexOf(b)>-1});return a.targets?b?a.source:a.lcca:a.source}function i(a,b){for(var c=[],d=0,e=a.ancestors.length;d<e;d++){var f=a.ancestors[d];f.typeEnum===w.COMPOSITE&&f.descendants.indexOf(b)>-1&&c.push(f)}if(!c.length)throw new Error("Could not find LCA for states.");return c[0]}var j=[],k=new Map,l=0,m={},n=[],o=/\s+/,p=c(a);return d([],p),g(),f(),p}function g(){this._listeners={},this._listeners["*"]=[]}function h(a){a=a||[],this.o=new Set(a)}function i(a,b){return a=a.replace(y,""),a===b||!(a.length>b.length)&&("."===b.charAt(a.length)&&0===b.indexOf(a))}function j(a,b){return a.events.some(function(a){return"*"===a||i(a,b)})}function k(a,b,c){return a.transitions.filter(function(a){return(!a.events||b&&b.name&&j(a,b.name))&&(!a.cond||c(a.cond))})}function l(a){var b=a[0],c=a[1];return b.source.depth<c.source.depth?c:c.source.depth<b.source.depth?b:b.documentOrder<c.documentOrder?b:c}function m(a,b,c){return b.x=b.x||{},a.call(c,b.x,b.sessionid,b.ioprocessors,c.isIn.bind(c))}function n(a,b){return a.map(function(a){var c=b.get(a);if(!c)throw new Error("Error loading serialized configuration. Unable to locate state with id "+a);return c})}function o(a,b){var c={};return Object.keys(a).forEach(function(d){c[d]=a[d].map(function(a){var c=b.get(a);if(!c)throw new Error("Error loading serialized history. Unable to locate state with id "+a);return c})}),c}function p(a,b){g.call(this),this._scriptingContext=b.interpreterScriptingContext||(b.InterpreterScriptingContext?new b.InterpreterScriptingContext(this):{});var c;c="function"==typeof a?m(a,b,this):"string"==typeof a?JSON.parse(a):a,this._model=f(c),this.opts=b||{},this.opts.console=b.console||("undefined"==typeof console?{log:function(){}}:console),this.opts.Set=this.opts.Set||h,this.opts.priorityComparisonFn=this.opts.priorityComparisonFn||l,this.opts.transitionSelector=this.opts.transitionSelector||k,this._scriptingContext.log=this._scriptingContext.log||function(){this.opts.console.log.apply?this.opts.console.log.apply(this.opts.console,arguments):this.opts.console.log(Array.prototype.slice.apply(arguments).join(","))}.bind(this),this._internalEventQueue=[],b.snapshot?(this._configuration=new this.opts.Set(n(b.snapshot[0],this._model.$idToStateMap)),this._historyValue=o(b.snapshot[1],this._model.$idToStateMap),this._isInFinalState=b.snapshot[2],this._model.$deserializeDatamodel(b.snapshot[3])):(this._configuration=new this.opts.Set,this._historyValue={},this._isInFinalState=!1),this._x={_sessionId:b.sessionId||null,_name:c.name||b.name||null,_ioprocessors:b.ioprocessors||null}}function q(a,b){b=b||{},b.ioprocessors={};for(var c in x)b.ioprocessors[c]=x[c];b.InterpreterScriptingContext=b.InterpreterScriptingContext||t,this._isStepping=!1,p.call(this,a,b)}function r(a){function b(){}return b.prototype=a,new b}function s(){}function t(a){this._interpreter=a,this._timeoutMap={}}var u="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol?"symbol":typeof a},v=Object.assign||function(a,b){return Object.keys(b).forEach(function(c){a[c]=b[c]}),a},w={BASIC:0,COMPOSITE:1,PARALLEL:2,HISTORY:3,INITIAL:4,FINAL:5},x={scxml:{location:"http://www.w3.org/TR/scxml/#SCXMLEventProcessor"},basichttp:{location:"http://www.w3.org/TR/scxml/#BasicHTTPEventProcessor"},dom:{location:"http://www.w3.org/TR/scxml/#DOMEventProcessor"},publish:{location:"https://github.com/jbeard4/SCION#publish"}};g.prototype.on=function(a,b){return Array.isArray(this._listeners[a])||(this._listeners[a]=[]),this._listeners[a].indexOf(b)===-1&&this._listeners[a].push(b),this},g.prototype.once=function(a,b){function c(){for(var e=[],f=0;f<arguments.length;f+=1)e[f]=arguments[f];d.off(a,c),b.apply(d,e)}var d=this;return c.listener=b,this.on(a,c)},g.prototype.off=function(a,b){if(!Array.isArray(this._listeners[a]))return this;if("undefined"==typeof b)return this._listeners[a]=[],this;var c=this._listeners[a].indexOf(b);if(c===-1)for(var d=0;d<this._listeners[a].length;d+=1)if(this._listeners[a][d].listener===b){c=d;break}return this._listeners[a].splice(c,1),this},g.prototype.emit=function(a){var b,c,d=Array.prototype.slice.call(arguments),e=d.slice(1),f=this._listeners[a];if(Array.isArray(f))for(b=0,c=f.length;b<c;b++)f[b].apply(this,e);for(f=this._listeners["*"],b=0,c=f.length;b<c;b++)f[b].apply(this,d);return this},h.prototype={add:function(a){this.o.add(a)},remove:function(a){return this.o["delete"](a)},union:function(a){var b=!0,c=!1,d=void 0;try{for(var e,f=a.o[Symbol.iterator]();!(b=(e=f.next()).done);b=!0){var g=e.value;this.o.add(g)}}catch(h){c=!0,d=h}finally{try{!b&&f["return"]&&f["return"]()}finally{if(c)throw d}}return this},difference:function(a){var b=!0,c=!1,d=void 0;try{for(var e,f=a.o[Symbol.iterator]();!(b=(e=f.next()).done);b=!0){var g=e.value;this.o["delete"](g)}}catch(h){c=!0,d=h}finally{try{!b&&f["return"]&&f["return"]()}finally{if(c)throw d}}return this},contains:function(a){return this.o.has(a)},iter:function(){return Array.from(this.o)},isEmpty:function(){return!this.o.size},size:function(){return this.o.size},equals:function(a){if(this.o.size!==a.size())return!1;var b=!0,c=!1,d=void 0;try{for(var e,f=this.o[Symbol.iterator]();!(b=(e=f.next()).done);b=!0){var g=e.value;if(!a.contains(g))return!1}}catch(h){c=!0,d=h}finally{try{!b&&f["return"]&&f["return"]()}finally{if(c)throw d}}return!0},toString:function(){return"Set("+Array.from(this.o).toString()+")"}};var y=/\.\*$/,z={getAncestors:function(a,b){var c;return c=a.ancestors.indexOf(b),c>-1?a.ancestors.slice(0,c):a.ancestors},getAncestorsOrSelf:function(a,b){return[a].concat(this.getAncestors(a,b))},getDescendantsOrSelf:function(a){return[a].concat(a.descendants)},isOrthogonalTo:function(a,b){return!this.isAncestrallyRelatedTo(a,b)&&this.getLCA(a,b).typeEnum===w.PARALLEL},isAncestrallyRelatedTo:function(a,b){return this.getAncestorsOrSelf(b).indexOf(a)>-1||this.getAncestorsOrSelf(a).indexOf(b)>-1},getLCA:function(a,b){var c=this.getAncestors(a).filter(function(a){return a.descendants.indexOf(b)>-1},this);return c[0]}};p.prototype=v(r(g.prototype),{start:function(){return this._configuration.add(this._model.initialRef),this._performBigStep(),this.getConfiguration()},startAsync:function(a){"function"!=typeof a&&(a=s),this._configuration.add(this._model.initialRef),this._performBigStepAsync(null,a)},getConfiguration:function(){return this._configuration.iter().map(function(a){return a.id})},getFullConfiguration:function(){return this._configuration.iter().map(function(a){return[a].concat(z.getAncestors(a))},this).reduce(function(a,b){return a.concat(b)},[]).map(function(a){return a.id}).reduce(function(a,b){return a.indexOf(b)>-1?a:a.concat(b)},[])},isIn:function(a){return this.getFullConfiguration().indexOf(a)>-1},isFinal:function(a){return this._isInFinalState},_performBigStep:function(a){this.emit("onBigStepBegin"),a&&this._internalEventQueue.push(a);for(var b=!0;b;){var c=this._internalEventQueue.shift()||null;this.emit("onSmallStepBegin",c);var d=this._performSmallStep(c);b=!d.isEmpty()}this._isInFinalState=this._configuration.iter().every(function(a){return a.typeEnum===w.FINAL}),this.emit("onBigStepEnd")},_performBigStepAsync:function(a,b){function c(a){var e;try{d.emit(a);var f=d._internalEventQueue.shift()||null;d.emit("onSmallStepBegin",f),e=d._performSmallStep(f),d.emit("onSmallStepEnd",f)}catch(g){return void b(g)}e.isEmpty()?(d._isInFinalState=d._configuration.iter().every(function(a){return a.typeEnum===w.FINAL}),d.emit("onBigStepEnd"),b(void 0,d.getConfiguration())):(d.emit("onBigStepSuspend"),setImmediate(c,"onBigStepResume"))}a&&this._internalEventQueue.push(a);var d=this;c("onBigStepBegin")},_performSmallStep:function(a){var c=this._selectTransitions(a);if(!c.isEmpty()){for(var d=new this.opts.Set(c.iter().filter(b)),f=this._getStatesExited(d),g=f[0],h=f[1],i=this._getStatesEntered(d),j=i[0],k=i[1],l=new this.opts.Set,m=0,n=h.length;m<n;m++){var o=h[m];if(this.opts.logStatesEnteredAndExited&&this.opts.console.log("exiting ",o.id),this.emit("onExit",o.id),void 0!==o.onExit)for(var p=0,q=o.onExit.length;p<q;p++)this._evaluateAction(a,o.onExit[p]);var r;o.historyRef&&(r=o.historyRef.isDeep?function(a){return a.typeEnum===w.BASIC&&o.descendants.indexOf(a)>-1}:function(a){return a.parent===o},this._historyValue[o.historyRef.id]=h.filter(r))}for(var s=c.iter().sort(e),t=0,n=s.length;t<n;t++){var u=s[t],v=u.targets&&u.targets.map(function(a){return a.id});if(this.emit("onTransition",u.source.id,v,t),void 0!==u.onTransition)for(var x=0,y=u.onTransition.length;x<y;x++)this._evaluateAction(a,u.onTransition[x])}for(var z=0,A=k.length;z<A;z++){var B=k[z];if(this.opts.logStatesEnteredAndExited&&this.opts.console.log("entering",B.id),this.emit("onEntry",B.id),void 0!==B.onEntry)for(var C=0,D=B.onEntry.length;C<D;C++)this._evaluateAction(a,B.onEntry[C])}this._configuration.difference(g),this._configuration.union(j),l.isEmpty()||this._internalEventQueue.push(l)}return c},_evaluateAction:function(a,b){try{return b.call(this._scriptingContext,a)}catch(c){var d={tagname:b.tagname,line:b.line,column:b.column,reason:c.message};this._internalEventQueue.push({name:"error.execution",data:d}),this.emit("onError",d)}},_getStatesExited:function(a){for(var b=new this.opts.Set,d=new this.opts.Set,e=a.iter(),f=0,g=e.length;f<g;f++)for(var h=e[f],i=h.scope,j=i.descendants,k=this._configuration.iter(),l=0,m=k.length;l<m;l++){var n=k[l];if(j.indexOf(n)>-1){d.add(n),b.add(n);for(var o=z.getAncestors(n,i),p=0,q=o.length;p<q;p++)b.add(o[p])}}var r=b.iter().sort(c);return[d,r]},_getStatesEntered:function(a){for(var b={statesToEnter:new this.opts.Set,basicStatesToEnter:new this.opts.Set,statesProcessed:new this.opts.Set,statesToProcess:[]},c=a.iter(),e=0,f=c.length;e<f;e++)for(var g=c[e],h=0,i=g.targets.length;h<i;h++)this._addStateAndAncestors(g.targets[h],g.scope,b);for(var j;j=b.statesToProcess.pop();)this._addStateAndDescendants(j,b);var k=b.statesToEnter.iter().sort(d);return[b.basicStatesToEnter,k]},_addStateAndAncestors:function(a,b,c){this._addStateAndDescendants(a,c);for(var d=z.getAncestors(a,b),e=0,f=d.length;e<f;e++){var g=d[e];g.typeEnum===w.COMPOSITE?(c.statesToEnter.add(g),c.statesProcessed.add(g)):this._addStateAndDescendants(g,c)}},_addStateAndDescendants:function(a,b){b.statesProcessed.contains(a)||(a.typeEnum===w.HISTORY?a.id in this._historyValue?this._historyValue[a.id].forEach(function(c){this._addStateAndAncestors(c,a.parent,b)},this):(b.statesToEnter.add(a),b.basicStatesToEnter.add(a)):(b.statesToEnter.add(a),a.typeEnum===w.PARALLEL?b.statesToProcess.push.apply(b.statesToProcess,a.states.filter(function(a){return a.typeEnum!==w.HISTORY})):a.typeEnum===w.COMPOSITE?b.statesToProcess.push(a.initialRef):a.typeEnum!==w.INITIAL&&a.typeEnum!==w.BASIC&&a.typeEnum!==w.FINAL||b.basicStatesToEnter.add(a)),b.statesProcessed.add(a))},_selectTransitions:function(a){if(this.opts.onlySelectFromBasicStates)var b=this._configuration.iter();else{for(var c=new this.opts.Set,d=this._configuration.iter(),e=0,f=d.length;e<f;e++){var g=d[e];c.add(g);for(var h=z.getAncestors(g),i=0,j=h.length;i<j;i++)c.add(h[i])}b=c.iter()}for(var k=this.opts.transitionSelector,l=new this.opts.Set,m=this._evaluateAction.bind(this,a),n=0,o=b.length;n<o;n++)for(var p=k(b[n],a,m),q=0,f=p.length;q<f;q++)l.add(p[q]);var r=this._selectPriorityEnabledTransitions(l);return r},_selectPriorityEnabledTransitions:function(a){var b=new this.opts.Set,c=this._getInconsistentTransitions(a),d=c[0],e=c[1];for(b.union(d);!e.isEmpty();)a=new this.opts.Set(e.iter().map(function(a){return this.opts.priorityComparisonFn(a)},this)),c=this._getInconsistentTransitions(a),d=c[0],e=c[1],b.union(d);return b},_getInconsistentTransitions:function(a){for(var b=new this.opts.Set,c=new this.opts.Set,d=a.iter(),e=0;e<d.length;e++)for(var f=e+1;f<d.length;f++){var g=d[e],h=d[f];this._conflicts(g,h)&&(b.add(g),b.add(h),c.add([g,h]))}var i=a.difference(b);return[i,c]},_conflicts:function(a,b){return!this._isArenaOrthogonal(a,b)},_isArenaOrthogonal:function(a,b){var c=z.isOrthogonalTo(a.scope,b.scope);return c},registerListener:function(a){a.onEntry&&this.on("onEntry",a.onEntry),a.onExit&&this.on("onExit",a.onExit),a.onTransition&&this.on("onTransition",a.onTransition),a.onError&&this.on("onError",a.onError),a.onBigStepBegin&&this.on("onBigStepBegin",a.onBigStepBegin),a.onBigStepSuspend&&this.on("onBigStepSuspend",a.onBigStepSuspend),a.onBigStepResume&&this.on("onBigStepResume",a.onBigStepResume),a.onSmallStepBegin&&this.on("onSmallStepBegin",a.onSmallStepBegin),a.onSmallStepEnd&&this.on("onSmallStepEnd",a.onSmallStepEnd),a.onBigStepEnd&&this.on("onBigStepEnd",a.onBigStepEnd)},unregisterListener:function(a){a.onEntry&&this.off("onEntry",a.onEntry),a.onExit&&this.off("onExit",a.onExit),a.onTransition&&this.off("onTransition",a.onTransition),a.onError&&this.off("onError",a.onError),a.onBigStepBegin&&this.off("onBigStepBegin",a.onBigStepBegin),a.onBigStepSuspend&&this.off("onBigStepSuspend",a.onBigStepSuspend),a.onBigStepResume&&this.off("onBigStepResume",a.onBigStepResume),a.onSmallStepBegin&&this.off("onSmallStepBegin",a.onSmallStepBegin),a.onSmallStepEnd&&this.off("onSmallStepEnd",a.onSmallStepEnd),a.onBigStepEnd&&this.off("onBigStepEnd",a.onBigStepEnd)},getAllTransitionEvents:function(){function a(c){if(c.transitions)for(var d=0,e=c.transitions.length;d<e;d++)b[c.transitions[d].event]=!0;if(c.states)for(var f=0,g=c.states.length;f<g;f++)a(c.states[f])}var b={};return a(this._model),Object.keys(b)},getSnapshot:function(){if(this._isStepping)throw new Error("getSnapshot cannot be called while interpreter is executing a big-step");return[this.getConfiguration(),this._serializeHistory(),this._isInFinalState,this._model.$serializeDatamodel()]},_serializeHistory:function(){var a={};return Object.keys(this._historyValue).forEach(function(b){a[b]=this._historyValue[b].map(function(a){return a.id})},this),a}}),q.prototype=r(p.prototype),q.prototype.gen=function(a,b){var c;switch("undefined"==typeof a?"undefined":u(a)){case"string":c={name:a,data:b};break;case"object":if("string"!=typeof a.name)throw new Error('Event object must have "name" property of type string.');c=a;break;default:throw new Error("First argument to gen must be a string or object.")}if(this._isStepping)throw new Error("Cannot call gen during a big-step");return this._isStepping=!0,this._performBigStep(c),this._isStepping=!1,this.getConfiguration()},q.prototype.genAsync=function(a,b){if("object"!==("undefined"==typeof a?"undefined":u(a))||!a||"string"!=typeof a.name)throw new Error("expected currentEvent to be an Object with a name");if(this._isStepping)throw new Error("Cannot call gen during a big-step");"function"!=typeof b&&(b=s),this._isStepping=!0;var c=this;this._performBigStepAsync(a,function(a,d){c._isStepping=!1,b(a,d)})};var A=/\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/i;t.prototype={raise:function(a){this._interpreter._internalEventQueue.push(a)},send:function(a,b){function c(a,b,c){if(a.target){var d=A.test(a.target);if(!d)return this.raise({name:"error.execution",data:"Target is not valid URI",sendid:b.sendid})}var e=Object.keys(x).map(function(a){return x[a].location});return e.indexOf(a.type)===-1?this.raise({name:"error.execution",data:"Unsupported event processor type",sendid:b.sendid}):void c.call(this,a,b)}function d(a,b){if("undefined"==typeof setTimeout)throw new Error("Default implementation of Statechart.prototype.send will not work unless setTimeout is defined globally.");var c=setTimeout(this._interpreter.gen.bind(this._interpreter,a),b.delay||0);b.sendid&&(this._timeoutMap[b.sendid]=c)}function e(){this._interpreter.emit(a.name,a.data)}a.type=a.type||x.scxml.location;var f;f="https://github.com/jbeard4/SCION#publish"===a.type?e:this._interpreter.opts.customSend?this._interpreter.opts.customSend:d,b=b||{},c.call(this,a,b,f)},cancel:function(a){if(this._interpreter.opts.customCancel)return this._interpreter.opts.customCancel.apply(this,[a]);if("undefined"==typeof clearTimeout)throw new Error("Default implementation of Statechart.prototype.cancel will not work unless setTimeout is defined globally.");a in this._timeoutMap&&clearTimeout(this._timeoutMap[a])}},a.exports={BaseInterpreter:p,Statechart:q,ArraySet:h,STATE_TYPES:w,initializeModel:f,InterpreterScriptingContext:t,ioProcessorTypes:x}}); | ||
/*! scion-core 2017-04-05 */ | ||
!function(a,b){if("function"==typeof define&&define.amd)define(["module"],b);else if("undefined"!=typeof exports)b(module);else{var c={exports:{}};b(c),a.scion=c.exports}}(this,function(a){"use strict";function b(a){return a.targets}function c(a,b){return b.depth-a.depth}function d(a,b){return a.depth-b.depth}function e(a,b){return a.documentOrder-b.documentOrder}function f(a){function b(a){void 0===m[a]&&(m[a]=0);var b=m[a]++;return"$generated-"+a+"-"+b}function c(a){return{$deserializeDatamodel:a.$deserializeDatamodel||function(){},$serializeDatamodel:a.$serializeDatamodel||function(){return null},$idToStateMap:k,states:[{$type:"initial",transitions:[{target:a}]},a]}}function d(a,c){if(c.transitions&&j.push.apply(j,c.transitions),c.id){if(k.has(c.id))throw new Error("Redefinition of state id "+c.id);k.set(c.id,c)}c.$type=c.$type||"state",c.ancestors=a,c.depth=a.length,c.parent=a[0],c.transitions=c.transitions||[];for(var f=0,g=c.transitions.length;f<g;f++){var h=c.transitions[f];h.documentOrder=l++,h.source=c}if(c.states)for(var i=[c].concat(a),f=0,g=c.states.length;f<g;f++)d(i,c.states[f]);switch(c.$type){case"parallel":c.typeEnum=w.PARALLEL;break;case"initial":c.typeEnum=w.INITIAL;break;case"history":c.typeEnum=w.HISTORY;break;case"final":c.typeEnum=w.FINAL;break;case"state":case"scxml":c.states&&c.states.length?c.typeEnum=w.COMPOSITE:c.typeEnum=w.BASIC;break;default:throw new Error("Unknown state type: "+c.$type)}c.states?c.descendants=c.states.concat(c.states.map(function(a){return a.descendants}).reduce(function(a,b){return a.concat(b)},[])):c.descendants=[];var m;if(c.typeEnum===w.COMPOSITE&&("string"==typeof c.initial?n.push(c):(m=c.states.filter(function(a){return"initial"===a.$type}),c.initialRef=m.length?m[0]:c.states[0],e(c))),c.typeEnum===w.COMPOSITE||c.typeEnum===w.PARALLEL){var o=c.states.filter(function(a){return"history"===a.$type});c.historyRef=o[0]}c.id||(c.id=b(c.$type),k.set(c.id,c)),c.onEntry&&!Array.isArray(c.onEntry)&&(c.onEntry=[c.onEntry]),c.onExit&&!Array.isArray(c.onExit)&&(c.onExit=[c.onExit])}function e(a){if(!a.initialRef)throw new Error("Unable to locate initial state for composite state: "+a.id)}function f(){for(var a=0,b=n.length;a<b;a++){var c=n[a];c.initialRef=k.get(c.initial),e(c)}}function g(){for(var a=0,b=j.length;a<b;a++){var c=j[a];if(c.onTransition&&!Array.isArray(c.onTransition)&&(c.onTransition=[c.onTransition]),"string"==typeof c.event&&(c.events=c.event.trim().split(o)),delete c.event,!c.targets&&"undefined"!=typeof c.target)if("string"==typeof c.target){var d=k.get(c.target);if(!d)throw new Error("Unable to find target state with id "+c.target);c.target=d,c.targets=[c.target]}else if(Array.isArray(c.target))c.targets=c.target.map(function(a){if("string"==typeof a){if(a=k.get(a),!a)throw new Error("Unable to find target state with id "+c.target);return a}return a});else{if("object"!==u(c.target))throw new Error("Transition target has unknown type: "+c.target);c.targets=[c.target]}}for(var a=0,b=j.length;a<b;a++){var c=j[a];c.targets&&(c.lcca=i(c.source,c.targets[0])),c.scope=h(c)}}function h(a){var b="internal"===a.type&&a.source.parent&&a.targets&&a.targets.every(function(b){return a.source.descendants.indexOf(b)>-1});return a.targets?b?a.source:a.lcca:a.source}function i(a,b){for(var c=[],d=0,e=a.ancestors.length;d<e;d++){var f=a.ancestors[d];f.typeEnum===w.COMPOSITE&&f.descendants.indexOf(b)>-1&&c.push(f)}if(!c.length)throw new Error("Could not find LCA for states.");return c[0]}var j=[],k=new Map,l=0,m={},n=[],o=/\s+/,p=c(a);return d([],p),g(),f(),p}function g(){this._listeners={},this._listeners["*"]=[]}function h(a){a=a||[],this.o=new Set(a)}function i(a,b){return a=a.replace(y,""),a===b||!(a.length>b.length)&&("."===b.charAt(a.length)&&0===b.indexOf(a))}function j(a,b){return a.events.some(function(a){return"*"===a||i(a,b)})}function k(a,b,c,d){return a.transitions.filter(function(a){return(d?!a.events:!a.events||b&&b.name&&j(a,b.name))&&(!a.cond||c(a.cond))})}function l(a){var b=a[0],c=a[1];return b.source.depth<c.source.depth?c:c.source.depth<b.source.depth?b:b.documentOrder<c.documentOrder?b:c}function m(a,b,c){return b.x=b.x||{},a.call(c,b.x,b.sessionid,b.ioprocessors,c.isIn.bind(c))}function n(a,b){return a.map(function(a){var c=b.get(a);if(!c)throw new Error("Error loading serialized configuration. Unable to locate state with id "+a);return c})}function o(a,b){var c={};return Object.keys(a).forEach(function(d){c[d]=a[d].map(function(a){var c=b.get(a);if(!c)throw new Error("Error loading serialized history. Unable to locate state with id "+a);return c})}),c}function p(a,b){g.call(this),this._scriptingContext=b.interpreterScriptingContext||(b.InterpreterScriptingContext?new b.InterpreterScriptingContext(this):{});var c;c="function"==typeof a?m(a,b,this):"string"==typeof a?JSON.parse(a):a,this._model=f(c),this.opts=b||{},this.opts.console=b.console||("undefined"==typeof console?{log:function(){}}:console),this.opts.Set=this.opts.Set||h,this.opts.priorityComparisonFn=this.opts.priorityComparisonFn||l,this.opts.transitionSelector=this.opts.transitionSelector||k,this._scriptingContext.log=this._scriptingContext.log||function(){this.opts.console.log.apply?this.opts.console.log.apply(this.opts.console,arguments):this.opts.console.log(Array.prototype.slice.apply(arguments).join(","))}.bind(this),this._internalEventQueue=[],b.snapshot?(this._configuration=new this.opts.Set(n(b.snapshot[0],this._model.$idToStateMap)),this._historyValue=o(b.snapshot[1],this._model.$idToStateMap),this._isInFinalState=b.snapshot[2],this._model.$deserializeDatamodel(b.snapshot[3])):(this._configuration=new this.opts.Set,this._historyValue={},this._isInFinalState=!1),this._x={_sessionId:b.sessionId||null,_name:c.name||b.name||null,_ioprocessors:b.ioprocessors||null}}function q(a,b){b=b||{},b.ioprocessors={};for(var c in x)b.ioprocessors[c]=x[c];b.InterpreterScriptingContext=b.InterpreterScriptingContext||t,this._isStepping=!1,p.call(this,a,b)}function r(a){function b(){}return b.prototype=a,new b}function s(){}function t(a){this._interpreter=a,this._timeoutMap={}}var u="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol?"symbol":typeof a},v=Object.assign||function(a,b){return Object.keys(b).forEach(function(c){a[c]=b[c]}),a},w={BASIC:0,COMPOSITE:1,PARALLEL:2,HISTORY:3,INITIAL:4,FINAL:5},x={scxml:{location:"http://www.w3.org/TR/scxml/#SCXMLEventProcessor"},basichttp:{location:"http://www.w3.org/TR/scxml/#BasicHTTPEventProcessor"},dom:{location:"http://www.w3.org/TR/scxml/#DOMEventProcessor"},publish:{location:"https://github.com/jbeard4/SCION#publish"}};g.prototype.on=function(a,b){return Array.isArray(this._listeners[a])||(this._listeners[a]=[]),this._listeners[a].indexOf(b)===-1&&this._listeners[a].push(b),this},g.prototype.once=function(a,b){function c(){for(var e=[],f=0;f<arguments.length;f+=1)e[f]=arguments[f];d.off(a,c),b.apply(d,e)}var d=this;return c.listener=b,this.on(a,c)},g.prototype.off=function(a,b){if(!Array.isArray(this._listeners[a]))return this;if("undefined"==typeof b)return this._listeners[a]=[],this;var c=this._listeners[a].indexOf(b);if(c===-1)for(var d=0;d<this._listeners[a].length;d+=1)if(this._listeners[a][d].listener===b){c=d;break}return this._listeners[a].splice(c,1),this},g.prototype.emit=function(a){var b,c,d=Array.prototype.slice.call(arguments),e=d.slice(1),f=this._listeners[a];if(Array.isArray(f))for(b=0,c=f.length;b<c;b++)f[b].apply(this,e);for(f=this._listeners["*"],b=0,c=f.length;b<c;b++)f[b].apply(this,d);return this},h.prototype={add:function(a){this.o.add(a)},remove:function(a){return this.o["delete"](a)},union:function(a){var b=!0,c=!1,d=void 0;try{for(var e,f=a.o[Symbol.iterator]();!(b=(e=f.next()).done);b=!0){var g=e.value;this.o.add(g)}}catch(h){c=!0,d=h}finally{try{!b&&f["return"]&&f["return"]()}finally{if(c)throw d}}return this},difference:function(a){var b=!0,c=!1,d=void 0;try{for(var e,f=a.o[Symbol.iterator]();!(b=(e=f.next()).done);b=!0){var g=e.value;this.o["delete"](g)}}catch(h){c=!0,d=h}finally{try{!b&&f["return"]&&f["return"]()}finally{if(c)throw d}}return this},contains:function(a){return this.o.has(a)},iter:function(){return Array.from(this.o)},isEmpty:function(){return!this.o.size},size:function(){return this.o.size},equals:function(a){if(this.o.size!==a.size())return!1;var b=!0,c=!1,d=void 0;try{for(var e,f=this.o[Symbol.iterator]();!(b=(e=f.next()).done);b=!0){var g=e.value;if(!a.contains(g))return!1}}catch(h){c=!0,d=h}finally{try{!b&&f["return"]&&f["return"]()}finally{if(c)throw d}}return!0},toString:function(){return 0===this.o.size?"<empty>":Array.from(this.o).join(",\n")}};var y=/\.\*$/,z={getAncestors:function(a,b){var c;return c=a.ancestors.indexOf(b),c>-1?a.ancestors.slice(0,c):a.ancestors},getAncestorsOrSelf:function(a,b){return[a].concat(this.getAncestors(a,b))},getDescendantsOrSelf:function(a){return[a].concat(a.descendants)},isOrthogonalTo:function(a,b){return!this.isAncestrallyRelatedTo(a,b)&&this.getLCA(a,b).typeEnum===w.PARALLEL},isAncestrallyRelatedTo:function(a,b){return this.getAncestorsOrSelf(b).indexOf(a)>-1||this.getAncestorsOrSelf(a).indexOf(b)>-1},getLCA:function(a,b){var c=this.getAncestors(a).filter(function(a){return a.descendants.indexOf(b)>-1},this);return c[0]}};p.prototype=v(r(g.prototype),{start:function(){return this._log("performing initial big step"),this._configuration.add(this._model.initialRef),this._performBigStep(),this.getConfiguration()},startAsync:function(a){"function"!=typeof a&&(a=s),this._log("performing initial big step"),this._configuration.add(this._model.initialRef),this._performBigStepAsync(null,a)},getConfiguration:function(){return this._configuration.iter().map(function(a){return a.id})},getFullConfiguration:function(){return this._configuration.iter().map(function(a){return[a].concat(z.getAncestors(a))},this).reduce(function(a,b){return a.concat(b)},[]).map(function(a){return a.id}).reduce(function(a,b){return a.indexOf(b)>-1?a:a.concat(b)},[])},isIn:function(a){return this.getFullConfiguration().indexOf(a)>-1},isFinal:function(a){return this._isInFinalState},_performBigStep:function(a){this.emit("onBigStepBegin"),a&&this._internalEventQueue.push(a);for(var b=!0;b;){var c=this._internalEventQueue.shift()||null,d=this._selectTransitions(c,!0);d.isEmpty()&&(d=this._selectTransitions(c,!1)),this.emit("onSmallStepBegin",c),this._performSmallStep(c,d),this.emit("onSmallStepEnd",c),b=!d.isEmpty()}this._isInFinalState=this._configuration.iter().every(function(a){return a.typeEnum===w.FINAL}),this.emit("onBigStepEnd")},_performBigStepAsync:function(a,b){function c(a){var e;try{d.emit(a);var f=d._internalEventQueue.shift()||null,e=d._selectTransitions(f,!0);e.isEmpty()&&(e=d._selectTransitions(f,!1)),d.emit("onSmallStepBegin",f),d._performSmallStep(f,e),d.emit("onSmallStepEnd",f)}catch(g){return void b(g)}e.isEmpty()?(d._isInFinalState=d._configuration.iter().every(function(a){return a.typeEnum===w.FINAL}),d.emit("onBigStepEnd"),b(void 0,d.getConfiguration())):(d.emit("onBigStepSuspend"),setImmediate(c,"onBigStepResume"))}a&&this._internalEventQueue.push(a);var d=this;c("onBigStepBegin")},_performSmallStep:function(a,c){if(this._log("selecting transitions with currentEvent",JSON.stringify(a)),this._log("selected transitions",c),!c.isEmpty()){this._log("sorted transitions",c);var d=new this.opts.Set(c.iter().filter(b)),f=this._getStatesExited(d),g=f[0],h=f[1],i=this._getStatesEntered(d),j=i[0],k=i[1];this._log("basicStatesExited ",g),this._log("basicStatesEntered ",j),this._log("statesExited ",h),this._log("statesEntered ",k);var l=new this.opts.Set;this._log("executing state exit actions");for(var m=0,n=h.length;m<n;m++){var o=h[m];if(this._log("exiting ",o.id),this.emit("onExit",o.id),void 0!==o.onExit)for(var p=0,q=o.onExit.length;p<q;p++)this._evaluateAction(a,o.onExit[p]);var r;o.historyRef&&(r=o.historyRef.isDeep?function(a){return a.typeEnum===w.BASIC&&o.descendants.indexOf(a)>-1}:function(a){return a.parent===o},this._historyValue[o.historyRef.id]=h.filter(r))}var s=c.iter().sort(e);this._log("executing transitition actions");for(var t=0,n=s.length;t<n;t++){var u=s[t],v=u.targets&&u.targets.map(function(a){return a.id});if(this.emit("onTransition",u.source.id,v,t),void 0!==u.onTransition)for(var x=0,y=u.onTransition.length;x<y;x++)this._evaluateAction(a,u.onTransition[x])}this._log("executing state enter actions");for(var z=0,A=k.length;z<A;z++){var B=k[z];if(this._log("entering",B.id),this.emit("onEntry",B.id),void 0!==B.onEntry)for(var C=0,D=B.onEntry.length;C<D;C++)this._evaluateAction(a,B.onEntry[C])}this._log("updating configuration "),this._log("old configuration ",this._configuration),this._configuration.difference(g),this._configuration.union(j),this._log("new configuration ",this._configuration),l.isEmpty()||(this._log("adding triggered events to inner queue ",l),this._internalEventQueue.push(l))}return c},_evaluateAction:function(a,b){try{return b.call(this._scriptingContext,a)}catch(c){var d={tagname:b.tagname,line:b.line,column:b.column,reason:c.message};this._internalEventQueue.push({name:"error.execution",data:d}),this.emit("onError",d)}},_getStatesExited:function(a){for(var b=new this.opts.Set,d=new this.opts.Set,e=a.iter(),f=0,g=e.length;f<g;f++)for(var h=e[f],i=h.scope,j=i.descendants,k=this._configuration.iter(),l=0,m=k.length;l<m;l++){var n=k[l];if(j.indexOf(n)>-1){d.add(n),b.add(n);for(var o=z.getAncestors(n,i),p=0,q=o.length;p<q;p++)b.add(o[p])}}var r=b.iter().sort(c);return[d,r]},_getStatesEntered:function(a){for(var b={statesToEnter:new this.opts.Set,basicStatesToEnter:new this.opts.Set,statesProcessed:new this.opts.Set,statesToProcess:[]},c=a.iter(),e=0,f=c.length;e<f;e++)for(var g=c[e],h=0,i=g.targets.length;h<i;h++)this._addStateAndAncestors(g.targets[h],g.scope,b);for(var j;j=b.statesToProcess.pop();)this._addStateAndDescendants(j,b);var k=b.statesToEnter.iter().sort(d);return[b.basicStatesToEnter,k]},_addStateAndAncestors:function(a,b,c){this._addStateAndDescendants(a,c);for(var d=z.getAncestors(a,b),e=0,f=d.length;e<f;e++){var g=d[e];g.typeEnum===w.COMPOSITE?(c.statesToEnter.add(g),c.statesProcessed.add(g)):this._addStateAndDescendants(g,c)}},_addStateAndDescendants:function(a,b){b.statesProcessed.contains(a)||(a.typeEnum===w.HISTORY?a.id in this._historyValue?this._historyValue[a.id].forEach(function(c){this._addStateAndAncestors(c,a.parent,b)},this):(b.statesToEnter.add(a),b.basicStatesToEnter.add(a)):(b.statesToEnter.add(a),a.typeEnum===w.PARALLEL?b.statesToProcess.push.apply(b.statesToProcess,a.states.filter(function(a){return a.typeEnum!==w.HISTORY})):a.typeEnum===w.COMPOSITE?b.statesToProcess.push(a.initialRef):a.typeEnum!==w.INITIAL&&a.typeEnum!==w.BASIC&&a.typeEnum!==w.FINAL||b.basicStatesToEnter.add(a)),b.statesProcessed.add(a))},_selectTransitions:function(a,b){if(this.opts.onlySelectFromBasicStates)var c=this._configuration.iter();else{for(var d=new this.opts.Set,e=this._configuration.iter(),f=0,g=e.length;f<g;f++){var h=e[f];d.add(h);for(var i=z.getAncestors(h),j=0,k=i.length;j<k;j++)d.add(i[j])}c=d.iter()}for(var l=this.opts.transitionSelector,m=new this.opts.Set,n=this._evaluateAction.bind(this,a),o=0,p=c.length;o<p;o++)for(var q=l(c[o],a,n,b),r=0,g=q.length;r<g;r++)m.add(q[r]);var s=this._selectPriorityEnabledTransitions(m);return this._log("priorityEnabledTransitions",s),s},_selectPriorityEnabledTransitions:function(a){var b=new this.opts.Set,c=this._getInconsistentTransitions(a),d=c[0],e=c[1];for(b.union(d),this._log("enabledTransitions",a),this._log("consistentTransitions",d),this._log("inconsistentTransitionsPairs",e),this._log("priorityEnabledTransitions",b);!e.isEmpty();)a=new this.opts.Set(e.iter().map(function(a){return this.opts.priorityComparisonFn(a)},this)),c=this._getInconsistentTransitions(a),d=c[0],e=c[1],b.union(d),this._log("enabledTransitions",a),this._log("consistentTransitions",d),this._log("inconsistentTransitionsPairs",e),this._log("priorityEnabledTransitions",b);return b},_getInconsistentTransitions:function(a){var b=new this.opts.Set,c=new this.opts.Set,d=a.iter();this._log("transitions",a);for(var e=0;e<d.length;e++)for(var f=e+1;f<d.length;f++){var g=d[e],h=d[f];this._conflicts(g,h)&&(b.add(g),b.add(h),c.add([g,h]))}var i=a.difference(b);return[i,c]},_log:function(){},_conflicts:function(a,b){return!this._isArenaOrthogonal(a,b)},_isArenaOrthogonal:function(a,b){this._log("transition scopes",a.scope,b.scope);var c=z.isOrthogonalTo(a.scope,b.scope);return this._log("transition scopes are orthogonal?",c),c},registerListener:function(a){a.onEntry&&this.on("onEntry",a.onEntry),a.onExit&&this.on("onExit",a.onExit),a.onTransition&&this.on("onTransition",a.onTransition),a.onError&&this.on("onError",a.onError),a.onBigStepBegin&&this.on("onBigStepBegin",a.onBigStepBegin),a.onBigStepSuspend&&this.on("onBigStepSuspend",a.onBigStepSuspend),a.onBigStepResume&&this.on("onBigStepResume",a.onBigStepResume),a.onSmallStepBegin&&this.on("onSmallStepBegin",a.onSmallStepBegin),a.onSmallStepEnd&&this.on("onSmallStepEnd",a.onSmallStepEnd),a.onBigStepEnd&&this.on("onBigStepEnd",a.onBigStepEnd)},unregisterListener:function(a){a.onEntry&&this.off("onEntry",a.onEntry),a.onExit&&this.off("onExit",a.onExit),a.onTransition&&this.off("onTransition",a.onTransition),a.onError&&this.off("onError",a.onError),a.onBigStepBegin&&this.off("onBigStepBegin",a.onBigStepBegin),a.onBigStepSuspend&&this.off("onBigStepSuspend",a.onBigStepSuspend),a.onBigStepResume&&this.off("onBigStepResume",a.onBigStepResume),a.onSmallStepBegin&&this.off("onSmallStepBegin",a.onSmallStepBegin),a.onSmallStepEnd&&this.off("onSmallStepEnd",a.onSmallStepEnd),a.onBigStepEnd&&this.off("onBigStepEnd",a.onBigStepEnd)},getAllTransitionEvents:function(){function a(c){if(c.transitions)for(var d=0,e=c.transitions.length;d<e;d++)b[c.transitions[d].event]=!0;if(c.states)for(var f=0,g=c.states.length;f<g;f++)a(c.states[f])}var b={};return a(this._model),Object.keys(b)},getSnapshot:function(){if(this._isStepping)throw new Error("getSnapshot cannot be called while interpreter is executing a big-step");return[this.getConfiguration(),this._serializeHistory(),this._isInFinalState,this._model.$serializeDatamodel()]},_serializeHistory:function(){var a={};return Object.keys(this._historyValue).forEach(function(b){a[b]=this._historyValue[b].map(function(a){return a.id})},this),a}}),q.prototype=r(p.prototype),q.prototype.gen=function(a,b){var c;switch("undefined"==typeof a?"undefined":u(a)){case"string":c={name:a,data:b};break;case"object":if("string"!=typeof a.name)throw new Error('Event object must have "name" property of type string.');c=a;break;default:throw new Error("First argument to gen must be a string or object.")}if(this._isStepping)throw new Error("Cannot call gen during a big-step");return this._isStepping=!0,this._performBigStep(c),this._isStepping=!1,this.getConfiguration()},q.prototype.genAsync=function(a,b){if("object"!==("undefined"==typeof a?"undefined":u(a))||!a||"string"!=typeof a.name)throw new Error("expected currentEvent to be an Object with a name");if(this._isStepping)throw new Error("Cannot call gen during a big-step");"function"!=typeof b&&(b=s),this._isStepping=!0;var c=this;this._performBigStepAsync(a,function(a,d){c._isStepping=!1,b(a,d)})};var A=/\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/i;t.prototype={raise:function(a){this._interpreter._internalEventQueue.push(a)},send:function(a,b){function c(a,b,c){if(a.target){var d=A.test(a.target);if(!d)return this.raise({name:"error.execution",data:"Target is not valid URI",sendid:b.sendid})}var e=Object.keys(x).map(function(a){return x[a].location});return e.indexOf(a.type)===-1?this.raise({name:"error.execution",data:"Unsupported event processor type",sendid:b.sendid}):void c.call(this,a,b)}function d(a,b){if("undefined"==typeof setTimeout)throw new Error("Default implementation of Statechart.prototype.send will not work unless setTimeout is defined globally.");var c=setTimeout(this._interpreter.gen.bind(this._interpreter,a),b.delay||0);b.sendid&&(this._timeoutMap[b.sendid]=c)}function e(){this._interpreter.emit(a.name,a.data)}a.type=a.type||x.scxml.location;var f;f="https://github.com/jbeard4/SCION#publish"===a.type?e:this._interpreter.opts.customSend?this._interpreter.opts.customSend:d,b=b||{},this._interpreter._log("sending event",a.name,"with content",a.data,"after delay",b.delay),c.call(this,a,b,f)},cancel:function(a){if(this._interpreter.opts.customCancel)return this._interpreter.opts.customCancel.apply(this,[a]);if("undefined"==typeof clearTimeout)throw new Error("Default implementation of Statechart.prototype.cancel will not work unless setTimeout is defined globally.");a in this._timeoutMap&&(this._interpreter._log("cancelling ",a," with timeout id ",this._timeoutMap[a]),clearTimeout(this._timeoutMap[a]))}},a.exports={BaseInterpreter:p,Statechart:q,ArraySet:h,STATE_TYPES:w,initializeModel:f,InterpreterScriptingContext:t,ioProcessorTypes:x}}); |
@@ -13,4 +13,4 @@ var _ = require('underscore'); | ||
var defaultFF = [5, 10, 20, 30, 40, 46, 47 ], | ||
defaultChrome = [26, 36, 51, 'beta']; | ||
var defaultFF = [5, 47], | ||
defaultChrome = [26, 51, 'beta']; | ||
@@ -20,30 +20,21 @@ addBrowser('internet explorer', 'Windows 10', 11); | ||
defaultChrome.forEach(addBrowser.bind(this, 'chrome', 'Windows 10')); | ||
_.difference(defaultFF,[46, 47, 'dev']) //these fail on sauce labs due to sauce labs bugs | ||
_.difference(defaultFF,[47]) //these fail on sauce labs due to sauce labs bugs | ||
.forEach(addBrowser.bind(this, 'firefox', 'Windows 10')); | ||
[8, 9, 10, 11].forEach(addBrowser.bind(this, 'internet explorer', 'Windows 7')); | ||
_.difference(defaultChrome,[36]) //TODO: fix Chrome 36 on Windows 7, which consistently fails for unknown reason | ||
.forEach(addBrowser.bind(this, 'chrome', 'Windows 7')); | ||
[11, 12].forEach(addBrowser.bind(this, 'opera', 'Windows 7')); | ||
defaultFF.forEach(addBrowser.bind(this, 'firefox', 'Windows 7')); | ||
addBrowser('safari', 'Windows 7', 5); | ||
addBrowser('internet explorer', 'Windows XP', 8); | ||
[26, 36, 49].forEach(addBrowser.bind(this, 'chrome', 'Windows XP')); | ||
[ | ||
//11, //TODO: opera 11 on Windows XP always timing out. probable sauce labs platform bug | ||
12].forEach(addBrowser.bind(this, 'opera', 'Windows XP')); | ||
[ 4, 10, 20, 30, 40, 45 ].forEach(addBrowser.bind(this, 'firefox', 'Windows XP')); | ||
[26, 49].forEach(addBrowser.bind(this, 'chrome', 'Windows XP')); | ||
[12].forEach(addBrowser.bind(this, 'opera', 'Windows XP')); | ||
[ 4, 45 ].forEach(addBrowser.bind(this, 'firefox', 'Windows XP')); | ||
_.difference(defaultChrome,[26]) //chrome 26 not supported on OS X 10.11 | ||
.forEach(addBrowser.bind(this, 'chrome', 'OS X 10.11')); | ||
[51].forEach(addBrowser.bind(this, 'chrome', 'OS X 10.11')); | ||
defaultFF.forEach(addBrowser.bind(this, 'firefox', 'OS X 10.11')); | ||
addBrowser('safari', 'OS X 10.11', 9); | ||
_.difference(defaultChrome,[26,36]) //chrome 36 and 26 not supported on OS X 10.10 | ||
.forEach(addBrowser.bind(this, 'chrome', 'OS X 10.10')); | ||
[32, | ||
//42, //FIXME: Firefox 42 times out | ||
44].forEach(addBrowser.bind(this, 'firefox', 'OS X 10.10')); | ||
[51].forEach(addBrowser.bind(this, 'chrome', 'OS X 10.10')); | ||
[32, 44].forEach(addBrowser.bind(this, 'firefox', 'OS X 10.10')); | ||
addBrowser('safari', 'OS X 10.10', 8); | ||
@@ -56,13 +47,11 @@ | ||
[27, | ||
//30, //Chrome 30 not supported | ||
40, 49].forEach(addBrowser.bind(this, 'chrome', 'OS X 10.8')); | ||
[27, 49].forEach(addBrowser.bind(this, 'chrome', 'OS X 10.8')); | ||
defaultFF.forEach(addBrowser.bind(this, 'firefox', 'OS X 10.8')); | ||
addBrowser('safari', 'OS X 10.8', 6); | ||
[26, 30, 40, 48].forEach(addBrowser.bind(this, 'chrome', 'Linux')); | ||
[4, 10, 20, 30, 40, 45].forEach(addBrowser.bind(this, 'firefox', 'Linux')); | ||
[26, 48].forEach(addBrowser.bind(this, 'chrome', 'Linux')); | ||
[4, 45].forEach(addBrowser.bind(this, 'firefox', 'Linux')); | ||
addBrowser('opera', 'Linux', 12); | ||
['9.4','9.3','9.2','9.1','9.0','8.4','8.3','8.2','8.1','8.0'].forEach(function(version){ | ||
['9.4','8.4'].forEach(function(version){ | ||
browsers.push({ | ||
@@ -69,0 +58,0 @@ "appiumVersion": "1.5.3", |
@@ -74,3 +74,3 @@ module.exports = function(grunt) { | ||
throttled: 5, | ||
statusCheckAttempts : -1, | ||
statusCheckAttempts : 180, | ||
sauceConfig: { | ||
@@ -126,4 +126,4 @@ 'video-upload-on-pass': false | ||
grunt.registerTask('run-browser-tests-prod-require', ['express:prod-require', 'saucelabs-custom','express:prod-require:stop' ]); | ||
grunt.registerTask('build', ['babel', 'replace-reserved-words', 'uglify']); | ||
grunt.registerTask('build', ['browserify', 'babel', 'replace-reserved-words', 'uglify']); | ||
grunt.registerTask('default', ['build']); | ||
}; |
128
lib/scion.js
@@ -98,4 +98,14 @@ // Copyright 2011-2012 Jacob Beard, INFICON, and other SCION contributors | ||
function transitionToString(sourceState){ | ||
return `${sourceState} -- ${this.events ? '(' + this.events.join(',') + ')' : null}${this.cond ? '[' + this.cond.name + ']' : ''} --> ${this.targets ? this.targets.join(',') : null}`; | ||
} | ||
function stateToString(){ | ||
return this.id; | ||
} | ||
function traverse(ancestors,state){ | ||
if(printTrace) state.toString = stateToString; | ||
//add to global transition and state id caches | ||
@@ -126,2 +136,3 @@ if(state.transitions) transitions.push.apply(transitions,state.transitions); | ||
transition.source = state; | ||
if(printTrace) transition.toString = transitionToString.bind(transition, state); | ||
}; | ||
@@ -414,3 +425,3 @@ | ||
/** @constructor */ | ||
function ArraySet(l) { | ||
function ArraySet(l) { | ||
l = l || []; | ||
@@ -475,3 +486,3 @@ this.o = new Set(l); | ||
toString : function() { | ||
return "Set(" + Array.from(this.o).toString() + ")"; | ||
return this.o.size === 0 ? '<empty>' : Array.from(this.o).join(',\n'); | ||
} | ||
@@ -506,5 +517,9 @@ }; | ||
function scxmlPrefixTransitionSelector(state, event, evaluator) { | ||
function scxmlPrefixTransitionSelector(state, event, evaluator, selectEventlessTransitions) { | ||
return state.transitions.filter((t) => { | ||
return (!t.events || (event && event.name && isTransitionMatch(t, event.name))) | ||
return ( | ||
selectEventlessTransitions ? | ||
!t.events : | ||
(!t.events || (event && event.name && isTransitionMatch(t, event.name))) | ||
) | ||
&& (!t.cond || evaluator(t.cond)); | ||
@@ -514,2 +529,6 @@ }); | ||
function eventlessTransitionSelector(state){ | ||
return state.transitions.filter(function(transition){ return !transition.events || ( transition.events && transition.events.length === 0 ); }); | ||
} | ||
//model accessor functions | ||
@@ -668,3 +687,3 @@ var query = { | ||
//perform big step without events to take all default transitions and reach stable initial state | ||
if (printTrace) this.opts.console.log("performing initial big step"); | ||
this._log("performing initial big step"); | ||
@@ -690,3 +709,3 @@ //We effectively need to figure out states to enter here to populate initial config. assuming root is compound state makes this simple. | ||
if (printTrace) this.opts.console.log("performing initial big step"); | ||
this._log("performing initial big step"); | ||
@@ -730,4 +749,10 @@ this._configuration.add(this._model.initialRef); | ||
var currentEvent = this._internalEventQueue.shift() || null; | ||
var selectedTransitions = this._selectTransitions(currentEvent, true); | ||
if(selectedTransitions.isEmpty()){ | ||
selectedTransitions = this._selectTransitions(currentEvent, false); | ||
} | ||
this.emit('onSmallStepBegin', currentEvent); | ||
var selectedTransitions = this._performSmallStep(currentEvent); | ||
this._performSmallStep(currentEvent, selectedTransitions); | ||
this.emit('onSmallStepEnd', currentEvent); | ||
keepGoing = !selectedTransitions.isEmpty(); | ||
@@ -750,4 +775,9 @@ } | ||
var currentEvent = self._internalEventQueue.shift() || null; | ||
var selectedTransitions = self._selectTransitions(currentEvent, true); | ||
if(selectedTransitions.isEmpty()){ | ||
selectedTransitions = self._selectTransitions(currentEvent, false); | ||
} | ||
self.emit('onSmallStepBegin', currentEvent); | ||
selectedTransitions = self._performSmallStep(currentEvent); | ||
self._performSmallStep(currentEvent, selectedTransitions); | ||
self.emit('onSmallStepEnd', currentEvent); | ||
@@ -779,13 +809,11 @@ } catch(err) { | ||
/** @private */ | ||
_performSmallStep : function(currentEvent) { | ||
_performSmallStep : function(currentEvent, selectedTransitions) { | ||
if (printTrace) this.opts.console.log("selecting transitions with currentEvent: ", currentEvent); | ||
this._log("selecting transitions with currentEvent", JSON.stringify(currentEvent)); | ||
var selectedTransitions = this._selectTransitions(currentEvent); | ||
this._log("selected transitions", selectedTransitions); | ||
if (printTrace) this.opts.console.log("selected transitions: ", selectedTransitions); | ||
if (!selectedTransitions.isEmpty()) { | ||
if (printTrace) this.opts.console.log("sorted transitions: ", selectedTransitions); | ||
this._log("sorted transitions", selectedTransitions); | ||
@@ -804,6 +832,6 @@ //we only want to enter and exit states from transitions with targets | ||
if (printTrace) this.opts.console.log("basicStatesExited ", basicStatesExited); | ||
if (printTrace) this.opts.console.log("basicStatesEntered ", basicStatesEntered); | ||
if (printTrace) this.opts.console.log("statesExited ", statesExited); | ||
if (printTrace) this.opts.console.log("statesEntered ", statesEntered); | ||
this._log("basicStatesExited ", basicStatesExited); | ||
this._log("basicStatesEntered ", basicStatesEntered); | ||
this._log("statesExited ", statesExited); | ||
this._log("statesEntered ", statesEntered); | ||
@@ -813,3 +841,3 @@ var eventsToAddToInnerQueue = new this.opts.Set(); | ||
//update history states | ||
if (printTrace) this.opts.console.log("executing state exit actions"); | ||
this._log("executing state exit actions"); | ||
@@ -819,3 +847,3 @@ for (var j = 0, len = statesExited.length; j < len; j++) { | ||
if (printTrace || this.opts.logStatesEnteredAndExited) this.opts.console.log("exiting ", stateExited.id); | ||
this._log("exiting ", stateExited.id); | ||
@@ -851,3 +879,3 @@ //invoke listeners | ||
if (printTrace) this.opts.console.log("executing transitition actions"); | ||
this._log("executing transitition actions"); | ||
@@ -869,3 +897,3 @@ | ||
if (printTrace) this.opts.console.log("executing state enter actions"); | ||
this._log("executing state enter actions"); | ||
@@ -875,3 +903,3 @@ for (var enterIdx = 0, enterLen = statesEntered.length; enterIdx < enterLen; enterIdx++) { | ||
if (printTrace || this.opts.logStatesEnteredAndExited) this.opts.console.log("entering", stateEntered.id); | ||
this._log("entering", stateEntered.id); | ||
@@ -887,4 +915,4 @@ this.emit('onEntry',stateEntered.id); | ||
if (printTrace) this.opts.console.log("updating configuration "); | ||
if (printTrace) this.opts.console.log("old configuration ", this._configuration); | ||
this._log("updating configuration "); | ||
this._log("old configuration ", this._configuration); | ||
@@ -896,7 +924,7 @@ //update configuration by removing basic states exited, and adding basic states entered | ||
if (printTrace) this.opts.console.log("new configuration ", this._configuration); | ||
this._log("new configuration ", this._configuration); | ||
//add set of generated events to the innerEventQueue -> Event Lifelines: Next small-step | ||
if (!eventsToAddToInnerQueue.isEmpty()) { | ||
if (printTrace) this.opts.console.log("adding triggered events to inner queue ", eventsToAddToInnerQueue); | ||
this._log("adding triggered events to inner queue ", eventsToAddToInnerQueue); | ||
this._internalEventQueue.push(eventsToAddToInnerQueue); | ||
@@ -1050,3 +1078,3 @@ } | ||
/** @private */ | ||
_selectTransitions : function(currentEvent) { | ||
_selectTransitions : function(currentEvent, selectEventlessTransitions) { | ||
if (this.opts.onlySelectFromBasicStates) { | ||
@@ -1078,3 +1106,3 @@ var states = this._configuration.iter(); | ||
for (var stateIdx = 0, stateLen = states.length; stateIdx < stateLen; stateIdx++) { | ||
var transitions = transitionSelector(states[stateIdx],currentEvent,e); | ||
var transitions = transitionSelector(states[stateIdx], currentEvent, e, selectEventlessTransitions); | ||
for (var txIdx = 0, len = transitions.length; txIdx < len; txIdx++) { | ||
@@ -1087,3 +1115,3 @@ enabledTransitions.add(transitions[txIdx]); | ||
if (printTrace) this.opts.console.log("priorityEnabledTransitions", priorityEnabledTransitions); | ||
this._log("priorityEnabledTransitions", priorityEnabledTransitions); | ||
@@ -1103,6 +1131,6 @@ return priorityEnabledTransitions; | ||
if (printTrace) this.opts.console.log("enabledTransitions", enabledTransitions); | ||
if (printTrace) this.opts.console.log("consistentTransitions", consistentTransitions); | ||
if (printTrace) this.opts.console.log("inconsistentTransitionsPairs", inconsistentTransitionsPairs); | ||
if (printTrace) this.opts.console.log("priorityEnabledTransitions", priorityEnabledTransitions); | ||
this._log("enabledTransitions", enabledTransitions); | ||
this._log("consistentTransitions", consistentTransitions); | ||
this._log("inconsistentTransitionsPairs", inconsistentTransitionsPairs); | ||
this._log("priorityEnabledTransitions", priorityEnabledTransitions); | ||
@@ -1119,6 +1147,6 @@ while (!inconsistentTransitionsPairs.isEmpty()) { | ||
if (printTrace) this.opts.console.log("enabledTransitions", enabledTransitions); | ||
if (printTrace) this.opts.console.log("consistentTransitions", consistentTransitions); | ||
if (printTrace) this.opts.console.log("inconsistentTransitionsPairs", inconsistentTransitionsPairs); | ||
if (printTrace) this.opts.console.log("priorityEnabledTransitions", priorityEnabledTransitions); | ||
this._log("enabledTransitions", enabledTransitions); | ||
this._log("consistentTransitions", consistentTransitions); | ||
this._log("inconsistentTransitionsPairs", inconsistentTransitionsPairs); | ||
this._log("priorityEnabledTransitions", priorityEnabledTransitions); | ||
@@ -1135,3 +1163,3 @@ } | ||
if (printTrace) this.opts.console.log("transitions", transitionList); | ||
this._log("transitions", transitions); | ||
@@ -1154,2 +1182,16 @@ for(var i = 0; i < transitionList.length; i++){ | ||
_log : function(){ | ||
if(printTrace){ | ||
var args = Array.from(arguments); | ||
this.opts.console.log( | ||
`${args[0]}: ${ | ||
args.slice(1).map(function(arg){ | ||
return arg === null ? 'null' : | ||
( arg === undefined ? 'undefined' : arg.toString()); | ||
}).join(', ') | ||
}` | ||
); | ||
} | ||
}, | ||
/** @private */ | ||
@@ -1163,7 +1205,7 @@ _conflicts : function(t1, t2) { | ||
if (printTrace) this.opts.console.log("transition scopes", t1.scope, t2.scope); | ||
this._log("transition scopes", t1.scope, t2.scope); | ||
var isOrthogonal = query.isOrthogonalTo(t1.scope, t2.scope); | ||
if (printTrace) this.opts.console.log("transition scopes are orthogonal?", isOrthogonal); | ||
this._log("transition scopes are orthogonal?", isOrthogonal); | ||
@@ -1441,3 +1483,3 @@ return isOrthogonal; | ||
if (printTrace) this._interpreter.opts.console.log("sending event", event.name, "with content", event.data, "after delay", options.delay); | ||
this._interpreter._log("sending event", event.name, "with content", event.data, "after delay", options.delay); | ||
@@ -1454,3 +1496,3 @@ validateSend.call(this, event, options, sendFn); | ||
if (sendid in this._timeoutMap) { | ||
if (printTrace) this._interpreter.opts.console.log("cancelling ", sendid, " with timeout id ", this._timeoutMap[sendid]); | ||
this._interpreter._log("cancelling ", sendid, " with timeout id ", this._timeoutMap[sendid]); | ||
clearTimeout(this._timeoutMap[sendid]); | ||
@@ -1457,0 +1499,0 @@ } |
{ | ||
"name": "scion-core", | ||
"version": "1.3.2", | ||
"version": "1.4.0", | ||
"description": "StateCharts Interpretation and Optimization eNgine (SCION) CORE is an implementation of Statecharts in JavaScript.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -6,4 +6,18 @@ var async = require('async'); | ||
var tests = require('../../tests/tests.js'); | ||
var testSerializations = [true, false]; | ||
var testGenAsync = [true, false]; | ||
var testSerialization = false; | ||
var testOptions = []; | ||
testSerializations.forEach(function(testSerialization){ | ||
testGenAsync.forEach(function(testGenAsync){ | ||
tests.forEach(function(test){ | ||
testOptions.push({ | ||
testSerialization : testSerialization, | ||
testGenAsync : testGenAsync, | ||
test : test, | ||
name : test.name + (testSerialization ? '(serialization)' : '') + (testGenAsync ? '(async)' : '') | ||
}); | ||
}); | ||
}); | ||
}) | ||
@@ -14,3 +28,3 @@ module.exports = function(scion){ | ||
tests.forEach(function(test){ | ||
testOptions.forEach(function(test){ | ||
//console.log(test); | ||
@@ -22,3 +36,3 @@ fixtures[test.name] = function(t){ | ||
var sc = new scion.Statechart(test.sc); | ||
var sc = new scion.Statechart(test.test.sc); | ||
@@ -29,13 +43,13 @@ var actualInitialConf = sc.start(); | ||
t.deepEqual(actualInitialConf.sort(),test.test.initialConfiguration.sort(),'initial configuration'); | ||
t.deepEqual(actualInitialConf.sort(),test.test.test.initialConfiguration.sort(),'initial configuration'); | ||
var mostRecentSnapshot; | ||
async.eachSeries(test.test.events,function(nextEvent,cb){ | ||
async.eachSeries(test.test.test.events,function(nextEvent,cb){ | ||
function ns(){ | ||
if(testSerialization && mostRecentSnapshot){ | ||
if(test.testSerialization && mostRecentSnapshot){ | ||
//load up state machine state | ||
sc = new scion.Statechart(test.sc,{snapshot : JSON.parse(mostRecentSnapshot)}); | ||
sc = new scion.Statechart(test.test.sc,{snapshot : JSON.parse(mostRecentSnapshot)}); | ||
} | ||
@@ -45,16 +59,32 @@ | ||
var actualNextConf = sc.gen(nextEvent.event); | ||
var actualNextConf; | ||
if(test.testGenAsync){ | ||
sc.genAsync(nextEvent.event, ns2); | ||
} else { | ||
try { | ||
var conf = sc.gen(nextEvent.event); | ||
ns2(null, conf); | ||
} catch (e){ | ||
ns2(e); | ||
} | ||
} | ||
//console.log('next configuration',actualNextConf); | ||
function ns2(err, actualNextConf){ | ||
t.deepEqual(actualNextConf.sort(),nextEvent.nextConfiguration.sort(),'next configuration after sending event ' + nextEvent.event.name); | ||
//dump state machine state | ||
//TODO: handle err | ||
//console.log('next configuration',actualNextConf); | ||
if(testSerialization){ | ||
mostRecentSnapshot = JSON.stringify(sc.getSnapshot()); | ||
//console.log('mostRecentSnapshot',mostRecentSnapshot); | ||
sc = null; //clear the statechart in memory, just because | ||
t.deepEqual(actualNextConf.sort(),nextEvent.nextConfiguration.sort(),'next configuration after sending event ' + nextEvent.event.name); | ||
//dump state machine state | ||
if(test.testSerialization){ | ||
mostRecentSnapshot = JSON.stringify(sc.getSnapshot()); | ||
//console.log('mostRecentSnapshot',mostRecentSnapshot); | ||
sc = null; //clear the statechart in memory, just because | ||
} | ||
cb(); | ||
} | ||
cb(); | ||
} | ||
@@ -61,0 +91,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
879709
223
20045