项目作者: steelbreeze

项目描述 :
Hierarchical finite state machine for TypeScript and JavaScript
高级语言: JavaScript
项目地址: git://github.com/steelbreeze/state.git
创建时间: 2017-06-18T10:55:50Z
项目社区:https://github.com/steelbreeze/state

开源协议:MIT License

下载


state

Executable finite state machine for TypeScript and JavaScript.

If you like @steelbreeze/state, please star it…

@steelbreeze/state"">NPM Version
@steelbreeze/state"">NPM Downloads
Build Status
Maintainability
Test Coverage

Note: v8 is now live and contains breaking changes but offers a further simplified code base performance improvements. See the release notes for more information.

Warning: v8 does not yet contain any support for serialization due to the challanges brought by the introduction of deferred events which are cached within the state machine instance alongside the active state configuration.

Install

  1. npm i @steelbreeze/state

Usage

The API is broken up into two distinct parts:

  1. A set of classes that represent a state machine model (State, PseudoState, Region, etc.);
  2. An class managing the active state configuration of a state machine instance at runtime (Instance).

Together, they enable multiple instances of the same state machine model.

The full API reference can be found here.

TypeScript

  1. import * as state from "@steelbreeze/state";
  2. // create event class that a transition will respond to
  3. class MyEvent {
  4. public constructor(public fieldA: string, public fieldB: number) { }
  5. public toString(): string {
  6. return JSON.stringify(this);
  7. }
  8. }
  9. // log state entry, exit and trigger event evaluation
  10. state.log.add(message => console.info(message), state.log.Entry | state.log.Exit | state.log.Evaluate);
  11. // create the state machine model elements
  12. const model = new state.State("model");
  13. const initial = new state.PseudoState("initial", model, state.PseudoStateKind.Initial);
  14. const stateA = new state.State("stateA", model);
  15. const stateB = new state.State("stateB", model);
  16. // create the transition from initial pseudo state to stateA
  17. initial.to(stateA);
  18. // create a transtion from stateA to stateB a for events of type MyEvent with a guard condition
  19. stateA.on(MyEvent).when(myEvent => myEvent.fieldB > 2).to(stateB);
  20. // create an instance of the state machine model
  21. let instance = new state.Instance("instance", model);
  22. // send the machine events for evaluation
  23. instance.evaluate(new MyEvent("test", 1));
  24. instance.evaluate(new MyEvent("test", 3));

JavaScript (ECMAScript 2015)

  1. var state = require("@steelbreeze/state");
  2. // create event class that a transition will respond to
  3. class MyEvent {
  4. constructor(fieldA, fieldB) { this.fieldA = fieldA; this.fieldB = fieldB; }
  5. toString() { return JSON.stringify(this); }
  6. }
  7. // log state entry, exit and trigger event evaluation
  8. state.log.add(message => console.info(message), state.log.Entry | state.log.Exit | state.log.Evaluate);
  9. // create the state machine model elements
  10. const model = new state.State("model");
  11. const initial = new state.PseudoState("initial", model, state.PseudoStateKind.Initial);
  12. const stateA = new state.State("stateA", model);
  13. const stateB = new state.State("stateB", model);
  14. // create the transition from initial pseudo state to stateA
  15. initial.to(stateA);
  16. // create a transtion from stateA to stateB a for events of type MyEvent with a guard condition
  17. stateA.on(MyEvent).when(myEvent => myEvent.fieldB > 2).to(stateB);
  18. // create an instance of the state machine model
  19. let instance = new state.Instance("instance", model);
  20. // send the machine events for evaluation
  21. instance.evaluate(new MyEvent("test", 1));
  22. instance.evaluate(new MyEvent("test", 3));

Output

The output of the above code will be:

  1. instance enter model
  2. instance enter model.default
  3. instance enter model.default.initial
  4. instance leave model.default.initial
  5. instance enter model.default.stateA
  6. instance evaluate {"fieldA":"test","fieldB":1}
  7. instance evaluate {"fieldA":"test","fieldB":3}
  8. instance leave model.default.stateA
  9. instance enter model.default.stateB

Note that in the example above, a default region is inserted as a child of model and parent of initial, stateA and stateB; the name of default regions copy their parent state hence seeing model.model in the output above.

License

MIT License

Copyright (c) 2022 David Morris