1.1.2 • Published 10 years ago

mix-machine v1.1.2

Weekly downloads
2
License
MIT
Repository
github
Last release
10 years ago

mixMachine.js

A simple state machine mixin implementation. To use, simple pass an object through stateMachine().

Example: Creaing a statemachine from an empty object:

var mix = require("mixMachine"); var myMachine = mix.stateMachine({});

Note: the {} are actually optional in this case because stateMachine will make it's own empty object if no object is provided. The object is accepted because you can pass anything in! stateMachine() will turn whatever object you you provide into a stateMachine by giving it three functions:

###addState(identifier, action, transition)

Adds a state to the machine. Action and transition must be functions and all three arguments are required. Whenever the state machine is run, action will be performed, followed by transition. Anything returned by action will be returned but if the return value of transition matches any state's identifier that state will be run next time runState is called.

###setState(identifier)

Manually sets the state which will be used next time runState is called. Note, this must be called once before the machine is run for the first time.

###runState()

Runs the function associated with the current state and then possibly transitions. runState() will return anything returned by the current state's action function, pass any arguments passed to it on to the action and transition functions, and binds the state machine to this for their execution.

##Examples

// Create a new State Machine
var waveMaker = require('mixMachine').stateMachine();
waveMaker.value = 0;

// Add increasing and decreasing states.
waveMaker.addState("increasing", 
                   function(){ ++this.value; },
                   function(){
                     if (this.value >= 100) {
                       return "decreasing";
                     }
                     return "no-transition";
                   });

waveMaker.addState("decreasing", 
                   function(){ --this.value; },
                   function(){
                     if (this.value <= -100) {
                       return "increasing";
                     }
                     return "no-transition";
                   });

// Start the machine in the increasing state.
waveMaker.setState("increasing");

// Run it a few times.
for (var i=0; i < 1000; ++i) {
    console.log(waveMaker.value);
    waveMaker.runState();
}

Generator

Generator inherits from stateMachine but changes the way transitions work. Instead of supplying a transition function you supply either an array or map of states to transition to. If you supply an array the state will randomly transition to one of the provided states after being run. If you supply a map the same thing happens but the keys are the states to transition to and the values are weights. For instance, if you pass a map {'foo':2, 'bar':1} for a state, the next state after it will be foo two thirds of the time and bar the other third.

Example:

var arrGen = mix.Generator();
var heads = 0, tails = 0;

arrGen.addState("heads", function(){
    ++heads;
}, ['heads','tails']);

arrGen.addState("tails", function(){
    ++tails;
}, ['heads', 'tails']);

arrGen.setState('heads');

for (var i=0; i<1000; ++i) {
    arrGen.runState();
}

console.log("Array Generator test:");
console.log("Heads: " +heads+ " Tails: " + tails);