#JSFSA: A Finite State Automaton in #JavaScript

JSFSA

JSFSA is a small javascript lib I wrote that allows for the creation of asynchronous, hierarchical, non-deterministic finite-state automata (more popularly called finite-state machines, but doesn’t ‘automaton’ sound infinitely more cool?)

Features

  • Hierarchical states: states can be configured to have an unlimited number of substates, an unlimited number of levels deep.
  • Guards: an unlimited amount of callbacks can be set to guard both entry and exit of states.
  • Listeners: an unlimited amount of callbacks can be executed on both entry and exit of states.
  • Named transitions: switching of states happens through named transitions.
  • Asynchronous transitioning: transitioning from one state to the other can be paused/resumed.
  • Framework independent: doesn’t rely on any other 3rd party frameworks.

Quick Example

var offState = new jsfsa.State( 'off' )
    .addTransition( 'ignite', 'on' )
    .isInitial = true
;
var onState = new jsfsa.State( 'on' )
    .addTransition( 'shutdown', 'off' )
;

var fsm = new jsfsa.Automaton()
    .addState( offState )
    .addState( onState )
    .doTransition( 'ignite' )
;
console.log( fsm.getCurrentState().name );//outputs 'on'

Code and examples

For source code, a more extensive explanation and examples head on over to the github project

About these ads
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 548 other followers

%d bloggers like this: