Skip to content
This repository has been archived by the owner on Jun 1, 2022. It is now read-only.
/ HSM Public archive

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

License

Notifications You must be signed in to change notification settings

fabiospampinato/HSM

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HSM

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published