-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathstate.min.js
1 lines (1 loc) · 16.1 KB
/
state.min.js
1
!function t(e,n,i){function r(a,s){if(!n[a]){if(!e[a]){var u="function"==typeof require&&require;if(!s&&u)return u(a,!0);if(o)return o(a,!0);var c=new Error("Cannot find module '"+a+"'");throw c.code="MODULE_NOT_FOUND",c}var h=n[a]={exports:{}};e[a][0].call(h.exports,function(t){var n=e[a][1][t];return r(n?n:t)},h,h.exports,t,e,n,i)}return n[a].exports}for(var o="function"==typeof require&&require,a=0;a<i.length;a++)r(i[a]);return r}({1:[function(t,e,n){"use strict";function i(t,e){return!t.parent||i(t.parent.parent,e)&&e.getLastKnownState(t.parent)===t}function r(t,e){if(t instanceof E){var n=e.getLastKnownState(t);return void 0!==n&&n.isFinal()}return t.regions.every(function(t){return r(t,e)})}function o(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];for(var i=0,r=e;i<r.length;i++)for(var o=r[i],a=0,s=o;a<s.length;a++){var u=s[a];t.push(u)}}function a(t,e,n,i){void 0===i&&(i=!1);for(var r=0,o=t;r<o.length;r++){var a=o[r];a(e,n,i)}}function s(t){n.random=t}function u(t,e,i){void 0===i&&(i=!0),e?(i&&t.clean===!1&&u(t),n.console.log("initialise "+e),a(t.onInitialise,void 0,e)):(n.console.log("initialise "+t.name),t.accept(new K,!1),t.clean=!0)}function c(t,e,i,r){return void 0===r&&(r=!0),r&&t.clean===!1&&u(t),n.console.log(e+" evaluate "+i),!e.isTerminated&&h(t,e,i)}function h(t,e,o){var a=!1;if(o!==t&&t.regions.every(function(n){var r=e.getLastKnownState(n);return!r||!h(r,e,o)||(a=!0,i(t,e))}),a)o!==t&&r(t,e)&&h(t,e,t);else{var s=t.outgoing.filter(function(t){return t.guard(o,e)});1===s.length?a=l(s[0],e,o):s.length>1&&n.console.error(t+": multiple outbound transitions evaluated true for message "+o)}return a}function l(t,e,n){for(var i=t.onTraverse.slice(0);t.target&&t.target instanceof x&&t.target.kind===m.Junction;)t=p(t.target,e,n),o(i,t.onTraverse);return a(i,n,e),t.target&&(t.target instanceof x&&t.target.kind===m.Choice?l(p(t.target,e,n),e,n):t.target instanceof C&&r(t.target,e)&&h(t.target,e,t.target)),!0}function p(t,e,i){var r=t.outgoing.filter(function(t){return t.guard(i,e)});return t.kind===m.Choice?0!==r.length?r[n.random(r.length)]:f(t):(r.length>1&&n.console.error("Multiple outbound transition guards returned true at "+t+" for "+i),r[0]||f(t))}function f(t){return t.outgoing.filter(function(t){return t.guard===I})[0]}function v(t){n.console=t}function g(t){n.internalTransitionsTriggerCompletion=t}function d(t){t.accept(new M)}var y=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])};return function(e,n){function i(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(i.prototype=n.prototype,new i)}}();n.__esModule=!0;var m;!function(t){t[t.Choice=0]="Choice",t[t.DeepHistory=1]="DeepHistory",t[t.Initial=2]="Initial",t[t.Junction=3]="Junction",t[t.ShallowHistory=4]="ShallowHistory",t[t.Terminate=5]="Terminate"}(m=n.PseudoStateKind||(n.PseudoStateKind={}));var S;!function(t){t[t.External=0]="External",t[t.Internal=1]="Internal",t[t.Local=2]="Local"}(S=n.TransitionKind||(n.TransitionKind={}));var b=function(){function t(t){this.parent=t}return t}();n.Element=b;var w=function(t){function e(n,i){var r=t.call(this,i)||this;return r.name=n,r.qualifiedName=i?i.toString()+e.namespaceSeparator+n:n,r}return y(e,t),e.prototype.toString=function(){return this.qualifiedName},e.namespaceSeparator=".",e}(b);n.NamedElement=w;var E=function(t){function e(e,n){var i=t.call(this,e,n)||this;return i.vertices=new Array,i.parent.regions.push(i),i.getRoot().clean=!1,i}return y(e,t),e.prototype.remove=function(){for(var t=0,e=this.vertices;t<e.length;t++){var n=e[t];n.remove()}this.parent.regions.splice(this.parent.regions.indexOf(this),1),this.getRoot().clean=!1},e.prototype.getRoot=function(){return this.parent.getRoot()},e.prototype.accept=function(t,e){return t.visitRegion(this,e)},e.defaultName="default",e}(w);n.Region=E;var T=function(t){function e(e,n){var i=t.call(this,e,n instanceof C?n.getDefaultRegion():n)||this;return i.outgoing=new Array,i.incoming=new Array,i.parent&&i.parent.vertices.push(i),i.getRoot().clean=!1,i}return y(e,t),e.prototype.getRoot=function(){return this.parent.getRoot()},e.prototype.ancestry=function(){return(this.parent?this.parent.parent.ancestry():new Array).concat(this)},e.prototype.remove=function(){for(var t=0,e=[this.outgoing,this.incoming];t<e.length;t++)for(var n=e[t],i=0,r=n;i<r.length;i++){var o=r[i];o.remove()}this.parent&&this.parent.vertices.splice(this.parent.vertices.indexOf(this),1),this.getRoot().clean=!1},e.prototype.to=function(t,e){return void 0===e&&(e=S.External),new O(this,t,e)},e}(w);n.Vertex=T;var x=function(t){function e(e,n,i){void 0===i&&(i=m.Initial);var r=t.call(this,e,n)||this;return r.kind=i,r}return y(e,t),e.prototype.isHistory=function(){return this.kind===m.DeepHistory||this.kind===m.ShallowHistory},e.prototype.isInitial=function(){return this.kind===m.Initial||this.isHistory()},e.prototype.accept=function(t,e){return t.visitPseudoState(this,e)},e}(T);n.PseudoState=x;var C=function(t){function e(e,n){var i=t.call(this,e,n)||this;return i.exitBehavior=new Array,i.entryBehavior=new Array,i.regions=new Array,i}return y(e,t),e.prototype.getDefaultRegion=function(){return this.defaultRegion||(this.defaultRegion=new E(E.defaultName,this))},e.prototype.isFinal=function(){return 0===this.outgoing.length},e.prototype.isSimple=function(){return 0===this.regions.length},e.prototype.isComposite=function(){return this.regions.length>0},e.prototype.isOrthogonal=function(){return this.regions.length>1},e.prototype.remove=function(){for(var e=0,n=this.regions;e<n.length;e++){var i=n[e];i.remove()}t.prototype.remove.call(this)},e.prototype.exit=function(t){return this.exitBehavior.push(t),this.getRoot().clean=!1,this},e.prototype.entry=function(t){return this.entryBehavior.push(t),this.getRoot().clean=!1,this},e.prototype.accept=function(t,e){return t.visitState(this,e)},e}(T);n.State=C;var N=function(t){function e(e,n){return t.call(this,e,n)||this}return y(e,t),e.prototype.accept=function(t,e){return t.visitFinalState(this,e)},e}(C);n.FinalState=N;var R=function(t){function e(e){var n=t.call(this,e,void 0)||this;return n.clean=!1,n}return y(e,t),e.prototype.getRoot=function(){return this.parent?this.parent.getRoot():this},e.prototype.accept=function(t,e){return t.visitStateMachine(this,e)},e}(C);n.StateMachine=R;var k=function(){return!0},I=function(){return!1},O=function(){function t(t,e,n){void 0===n&&(n=S.External);var i=this;this.source=t,this.target=e,this.kind=n,this.transitionBehavior=new Array,this.target||(this.kind=S.Internal),this.guard=t instanceof x?k:function(t){return t===i.source},this.source.outgoing.push(this),this.target&&this.target.incoming.push(this),this.source.getRoot().clean=!1}return t.prototype.else=function(){return this.guard=I,this},t.prototype.when=function(t){return this.guard=t,this},t.prototype.effect=function(t){return this.transitionBehavior.push(t),this.source.getRoot().clean=!1,this},t.prototype.remove=function(){this.source.outgoing.splice(this.source.outgoing.indexOf(this),1),this.target&&this.target.incoming.splice(this.target.incoming.indexOf(this),1),this.source.getRoot().clean=!1},t.prototype.accept=function(t,e){return t.visitTransition(this,e)},t.prototype.toString=function(){return"["+(this.target?this.source+" -> "+this.target:this.source)+"]"},t}();n.Transition=O;var H=function(){function t(t){void 0===t&&(t="unnamed"),this.name=t,this.current={},this.last={},this.isTerminated=!1,this.name=t}return t.prototype.setCurrent=function(t,e){this.current[t.toString()]=e,e instanceof C&&(this.last[t.toString()]=e)},t.prototype.getCurrent=function(t){return this.current[t.toString()]},t.prototype.getLastKnownState=function(t){return this.last[t.toString()]},t.prototype.toString=function(){return this.name},t}();n.StateMachineInstance=H;var A=function(){function t(t){void 0===t&&(t="unnamed"),this.name=t,this.current={},this.isTerminated=!1}return t.prototype.setCurrent=function(t,e){this.current[t.toString()]=e,e instanceof C&&(this.getNode(t).lastKnown=e.name)},t.prototype.getCurrent=function(t){return this.current[t.toString()]},t.prototype.getLastKnownState=function(t){var e=this.getNode(t).lastKnown;return t.vertices.reduce(function(t,n){return n instanceof C&&n.name===e?n:t},void 0)},t.prototype.getNode=function(t){if(t.parent){var e=this.getNode(t.parent),n=e.children.reduce(function(e,n){return n.name===t.name?n:e},void 0);return n||(n={name:t.name,children:[]},e.children.push(n)),n}return this.activeStateConfiguration||(this.activeStateConfiguration={name:t.name,children:[]})},t.prototype.toJSON=function(){return JSON.stringify(this.activeStateConfiguration)},t.prototype.fromJSON=function(t){return this.activeStateConfiguration=JSON.parse(t)},t.prototype.toString=function(){return this.name},t}();n.JSONInstance=A;var B=function(){function t(){}return t.prototype.visitNamedElement=function(t,e){},t.prototype.visitRegion=function(t,e){this.visitNamedElement(t,e);for(var n=0,i=t.vertices;n<i.length;n++){var r=i[n];r.accept(this,e)}},t.prototype.visitVertex=function(t,e){this.visitNamedElement(t,e);for(var n=0,i=t.outgoing;n<i.length;n++){var r=i[n];r.accept(this,e)}},t.prototype.visitPseudoState=function(t,e){return this.visitVertex(t,e)},t.prototype.visitState=function(t,e){for(var n=this.visitVertex(t,e),i=0,r=t.regions;i<r.length;i++){var o=r[i];o.accept(this,e)}return n},t.prototype.visitFinalState=function(t,e){return this.visitState(t,e)},t.prototype.visitStateMachine=function(t,e){return this.visitState(t,e)},t.prototype.visitTransition=function(t,e){},t}();n.Visitor=B,n.isComplete=r,n.random=function(t){return Math.floor(Math.random()*t)},n.setRandom=s,n.initialise=u,n.evaluate=c;var L=function(){function t(){this.leave=new Array,this.beginEnter=new Array,this.endEnter=new Array}return t.prototype.enter=function(){var t=new Array;return o(t,this.beginEnter,this.endEnter),t},t}(),_=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return y(e,t),e.prototype.visitTransition=function(t,e){switch(t.onTraverse=new Array,t.kind){case S.Internal:this.visitInternalTransition(t,e);break;case S.Local:this.visitLocalTransition(t,e);break;case S.External:this.visitExternalTransition(t,e)}},e.prototype.visitInternalTransition=function(t,e){o(t.onTraverse,t.transitionBehavior),n.internalTransitionsTriggerCompletion&&t.onTraverse.push(function(e,n){t.source instanceof C&&r(t.source,n)&&h(t.source,n,t.source)})},e.prototype.visitLocalTransition=function(t,e){var n=this;t.onTraverse.push(function(r,o){for(var s=t.target.ancestry(),u=0;i(s[u],o);)++u;var c=o.getCurrent(s[u].parent);for(c&&a(e(c).leave,r,o),a(t.transitionBehavior,r,o);u<s.length;)n.cascadeElementEntry(t,e,s[u++],s[u],function(t){return a(t,r,o)});a(e(t.target).endEnter,r,o)})},e.prototype.visitExternalTransition=function(t,e){for(var n=t.source.ancestry(),i=t.target.ancestry(),r=Math.min(n.length,i.length)-1;n[r-1]!==i[r-1];)--r;for(o(t.onTraverse,e(n[r]).leave,t.transitionBehavior);r<i.length;)this.cascadeElementEntry(t,e,i[r++],i[r],function(e){return o(t.onTraverse,e)});o(t.onTraverse,e(t.target).endEnter)},e.prototype.cascadeElementEntry=function(t,e,n,i,r){if(r(e(n).beginEnter),i&&n instanceof C)for(var o=0,a=n.regions;o<a.length;o++){var s=a[o];r(e(s).beginEnter),s!==i.parent&&r(e(s).endEnter)}},e}(B),K=function(t){function e(){var e=null!==t&&t.apply(this,arguments)||this;return e.behaviors={},e}return y(e,t),e.prototype.behavior=function(t){return this.behaviors[t.toString()]||(this.behaviors[t.toString()]=new L)},e.prototype.visitNamedElement=function(t,e){this.behavior(t).leave.push(function(e,i){return n.console.log(i+" leave "+t.qualifiedName)}),this.behavior(t).beginEnter.push(function(e,i){return n.console.log(i+" enter "+t.qualifiedName)})},e.prototype.visitRegion=function(t,e){for(var n=this,i=t.vertices.reduce(function(t,e){return e instanceof x&&e.isInitial()?e:t},void 0),r=0,s=t.vertices;r<s.length;r++){var u=s[r];u.accept(this,e||i&&i.kind===m.DeepHistory)}this.behavior(t).leave.push(function(e,i){var r=i.getCurrent(t);r&&a(n.behavior(r).leave,e,i)}),e||!i||i.isHistory()?this.behavior(t).endEnter.push(function(e,r,o){return a(n.behavior(o||i.isHistory()?r.getLastKnownState(t)||i:i).enter(),e,r,o||i.kind===m.DeepHistory)}):o(this.behavior(t).endEnter,this.behavior(i).enter()),this.visitNamedElement(t,e)},e.prototype.visitVertex=function(e,n){t.prototype.visitVertex.call(this,e,n),this.behavior(e).beginEnter.push(function(t,n){e.parent&&n.setCurrent(e.parent,e)})},e.prototype.visitPseudoState=function(e,n){var i=this;t.prototype.visitPseudoState.call(this,e,n),e.isInitial()?this.behavior(e).endEnter.push(function(t,n,r){var o;(r||e.isHistory())&&(o=n.getLastKnownState(e.parent))?(a(i.behavior(e).leave,t,n),a(i.behavior(o).enter(),t,n,r||e.kind===m.DeepHistory)):l(e.outgoing[0],n)}):e.kind===m.Terminate&&this.behavior(e).beginEnter.push(function(t,e){return e.isTerminated=!0})},e.prototype.visitState=function(t,e){for(var n=0,i=t.regions;n<i.length;n++){var r=i[n];r.accept(this,e),o(this.behavior(t).leave,this.behavior(r).leave),o(this.behavior(t).endEnter,this.behavior(r).enter())}this.visitVertex(t,e),o(this.behavior(t).leave,t.exitBehavior),o(this.behavior(t).beginEnter,t.entryBehavior)},e.prototype.visitStateMachine=function(e,n){var i=this;t.prototype.visitStateMachine.call(this,e,n),e.accept(new _,function(t){return i.behavior(t)}),e.onInitialise=this.behavior(e).enter()},e}(B);n.console={log:function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n]},warn:function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n]},error:function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];throw t}},n.setConsole=v,n.internalTransitionsTriggerCompletion=!1,n.setInternalTransitionsTriggerCompletion=g,n.validate=d;var M=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return y(e,t),e.prototype.visitPseudoState=function(e){t.prototype.visitPseudoState.call(this,e),e.kind===m.Choice||e.kind===m.Junction?(0===e.outgoing.length&&n.console.error(e+": "+e.kind+" pseudo states must have at least one outgoing transition."),e.outgoing.filter(function(t){return t.guard===I}).length>1&&n.console.error(e+": "+e.kind+" pseudo states cannot have more than one Else transitions.")):(0!==e.outgoing.filter(function(t){return t.guard===I}).length&&n.console.error(e+": "+e.kind+" pseudo states cannot have Else transitions."),e.isInitial()&&(e.outgoing.length>1?n.console.error(e+": initial pseudo states must have no more than one outgoing transition."):1===e.outgoing.length&&e.outgoing[0].guard!==k&&n.console.error(e+": initial pseudo states cannot have a guard condition.")))},e.prototype.visitRegion=function(e){t.prototype.visitRegion.call(this,e);for(var i=0,r=0,o=0,a=0,s=e.vertices;a<s.length;a++){var u=s[a];u instanceof x&&(u.kind===m.Initial?i++:u.kind===m.DeepHistory?r++:u.kind===m.ShallowHistory&&o++)}i>1&&n.console.error(e+": regions may have at most one initial pseudo state."),r>1&&n.console.error(e+": regions may have at most one deep history pseudo state."),o>1&&n.console.error(e+": regions may have at most one shallow history pseudo state.")},e.prototype.visitState=function(e){t.prototype.visitState.call(this,e),e.regions.filter(function(t){return t.name===E.defaultName}).length>1&&n.console.error(e+": a state cannot have more than one region named "+E.defaultName)},e.prototype.visitFinalState=function(e){t.prototype.visitFinalState.call(this,e),0!==e.outgoing.length&&n.console.error(e+": final states must not have outgoing transitions."),0!==e.regions.length&&n.console.error(e+": final states must not have child regions."),e.entryBehavior.length>0&&n.console.warn(e+": final states may not have entry behavior."),e.exitBehavior.length>0&&n.console.warn(e+": final states may not have exit behavior.")},e.prototype.visitTransition=function(e){t.prototype.visitTransition.call(this,e),e.kind===S.Local&&e.target&&e.target.ancestry().indexOf(e.source)===-1&&n.console.error(e+": local transition target vertices must be a child of the source composite sate.")},e}(B)},{}],2:[function(t,e,n){window[((document.currentScript||document.getElementsByTagName("script")[scripts.length-1]).attributes.target||{textContent:"fsm"}).textContent]=t("../lib/node/state.js")},{"../lib/node/state.js":1}]},{},[2]);