Javascript version of squirrel state machine
Clone or download
Latest commit e666ebe Jun 10, 2014
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
exmaple implement execution service Apr 3, 2014
lib add minified js May 23, 2014
test context expression evaluate Mar 24, 2014
.gitignore fix bugs and add example for browser usage Mar 18, 2014
.travis.yml Initial commit Mar 7, 2014
LICENSE-MIT Initial commit Mar 7, 2014
Makefile Initial commit Mar 7, 2014
README.md update read me Jun 10, 2014
index.js Initial commit Mar 7, 2014
package.json Initial commit Mar 7, 2014

README.md

squirrel-statemachine.js

squirrel-statemachine.js is a javascript implementation of squirrel-foundation which is a java state machine library.

Sample Code:

var SimpleStateMachine = Squirrel.StateMachine.extend({
    // state machine definition
    machine : {
      states : {
        A : { onEntry: "enterA", onExit: "exitA", initial:true },
        B : { onEntry: "enterB", onExit: "exitB" },
        F : { onEntry: function() { this.callSequence += ".enterF"; },
          onExit:  function() { this.callSequence += ".exitF"; },
          final: true }
      },

      transitions : [
        { from : "A", to : "B", on : "A2B", perform : "fromAToB" },
        { from : "B", to : "A", on : "B2A", perform : function fromBToA() { this.callSequence += ".fromBToA"; } },
        { from : "B", to : "C", on : "B2C", perform : function fromCToD() { this.fire("C2D"); this.fire("D2E"); } },
        { from : "C", to : "D", on : "C2D" },
        { from : "D", to : "E", on : "D2E" },
        { from : "D", to : "F", on : "D2F", when : function moreThanTen(context) {return context>10;} },
        { from : "A", to : "F", on : "END" }
      ]
    },

    // state machine initialize function
    initialize : function() {
      this.callSequence = "";
    },

    // state machine actions
    enterA : function() {
      this.callSequence += ".enterA";
    },

    methodMissing : function(methodName) {
      this.callSequence += "."+methodName;
    }
  });

  var SimpleStateMachineEx = SimpleStateMachine.extend({
    machine : {
      initial : "B",
        states : {
          A : { onEntry: "enterAFromEx:0" }, // invoked after original entry method
          B : { onExit: "exitBFromEx:0"}     // invoked after original entry method
        }
    },

    enterAFromEx : function() {
      this.callSequence += ".enterAFromEx";
    },

    exitBFromEx : function() {
      this.callSequence += ".exitBFromEx";
    }
  });

  var simpleStateMachineExInstance = new SimpleStateMachineEx(null /*use default*/, {isDebugInfoEnabled: true});
  simpleStateMachineExInstance.start();
  console.log("Current State: "+simpleStateMachineExInstance.getCurrentState());
  simpleStateMachineExInstance.fire("B2A");
  console.log("Call sequences: "+simpleStateMachineExInstance.callSequence);