Giter Club home page Giter Club logo

hsm's Introduction

HSM

Issues NPM version

Hierarchical State Machine implementation, with support for guards and enter/exit events.

It extends FSM, read its documentation before reading this.

Install

$ npm install --save @fabiospampinato/hsm

Usage

import HSM from '@fabiospampinato/hsm';

// The `states` object is similar to FSM's.
// The only difference being that an optional `states` key can be used to nest states.

const states = {
  alive: {
    states: {
      standing: {
        transitions: {
          walk: 'walking'
        }
      },
      walking: {
        transitions: {
          stop: 'standing',
          speedUp: 'running'
        }
      },
      running: {
        transitions: {
          slowDown: 'walking'
        }
      }
    }
  },
  dead {}
};

// The `model` object doesn't differ from the one FSM uses.

const Model = new class {
  walk () {
    console.log ( 'Starting to walk...' );
  }
  walkingEnter () {
    console.log ( 'Now I\'m walking' );
  }
  walkingExit () {
    console.log ( 'Now I\'n not walking anymore' );
  }
};

const machine = new HSM ( Model, states, 'standing' );

machine.transition ( 'walk' );

machine.is ( 'walking' ); // true
machine.is ( 'alive' ); // true
machine.is ( 'alive', true ); // false

API

The API is the same that FSM provides, except for the following differences:

new HSM ( model, states, initial? )

Here the initial state can be implicit, in that case the root state will be setted as the initial one.

If there are multiple states at the root level an error will be thrown.

.is ( state: string, exactly?: boolean ): boolean

By default it will return true also for parent states, if you only want it to match the current state pass true as the exactly argument.

Related

  • FSM - Finite State Machine implementation, with support for guards and enter/exit events.

License

MIT © Fabio Spampinato

hsm's People

Contributors

fabiospampinato avatar

Watchers

James Cloos avatar abecedarian avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.