TypeState, 一种强类型有限状态机

分享于 

5分钟阅读

GitHub

  繁體 雙語
A strongly typed finite state machine for TypeScript
  • 源代码名称:TypeState
  • 源代码网址:http://www.github.com/eonarheim/TypeState
  • TypeState源代码文档
  • TypeState源代码下载
  • Git URL:
    git://www.github.com/eonarheim/TypeState.git
    Git Clone代码到本地:
    git clone http://www.github.com/eonarheim/TypeState
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/eonarheim/TypeState
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    

    Build Statusnpm versionNuGet versionBower version

    TypeState

    TypeState是一个强大的用于打字或者JavaScript的有限状态机。 有限状态机对于建模复杂流和跟踪状态非常有用。

    安装

    Install-Package TypeState

    npm install typestate

    bower install typestate

    基本示例

    // Let's model the states of an elevator// Define an Enum with all possible valid statesenum Elevator {
     DoorsOpened,
     DoorsClosed,
     Moving
    }// Construct the FSM with the inital state, in this case the elevator starts with its doors openedvar fsm =newTypeState.FiniteStateMachine<Elevator>(Elevator.DoorsOpened);// Declare the valid state transitions to model your system// Doors can go from opened to closed, and vice versafsm.from(Elevator.DoorsOpened).to(Elevator.DoorsClosed);fsm.from(Elevator.DoorsClosed).to(Elevator.DoorsOpened);// Once the doors are closed the elevator may movefsm.from(Elevator.DoorsClosed).to(Elevator.Moving);// When the elevator reaches its destination, it may stop movingfsm.from(Elevator.Moving).to(Elevator.DoorsClosed);// Check that the current state is the initial stateif(fsm.is(Elevator.DoorsOpened)){
     console.log("The doors are open Dave"); 
    }// Test validity of transitions from the current state, in this case 'Elevator.DoorsOpened'fsm.canGo(Elevator.DoorsClosed); // returns truefsm.canGo(Elevator.Moving); //returns false// Go to a new state, closing the elevator doors. fsm.go(Elevator.DoorsClosed); // The fsm.currentState is now set to 'Elevator.DoorsClosed'// The elevator can now move or open the doors againfsm.canGo(Elevator.Moving); // returns truefsm.canGo(Elevator.DoorsOpened); //returns true

    使用 JavaScript

    JavaScript很容易使用 TypeState。 有限状态机依赖于可以用 .toString() 方法转换为字符串的状态。 所以,使用JavaScript简单替换前面示例的前几行,如下所示:

    var Elevator = {
     DoorsOpened :"DoorsOpened",
     DoorsClosed :"DoorsClosed",
     Moving :"Moving"}var fsm =newFiniteStateMachine(Elevator.DoorsOpened)

    侦听状态更改

    TypeState允许你侦听状态更改。 如电梯开始移动,我们想播放一些电梯音乐。 另外我们想在电梯停止时停止音乐。

    fsm.on(Elevator.Moving, (from:Elevator)=>{
     playGroovyElevatorMusic();
    });fsm.on(Elevator.DoorsClosed, (from:Elevator)=>{
     stopGroovyElevatorMusic();
    });

    中断转换

    有时候你需要中断过渡。 可以中断到带有 onEnter(STATE, CALLBACK)的状态,并中断从具有 onExit(STATE, CALLBACK)的状态转换到状态。 如果 CALLBACK 返回 false,则转换被取消,状态不会改变。

    console.log("DoorsOpened", fsm.currentState===Elevator.DoorsOpened); // truevar handsInDoor =true;// Listen for transitions to DoorsClosed, if the callback returns false the transition is canceled.fsm.onEnter(Elevator.DoorsClosed, (from:Elevator)=>{
     if(handsInDoor){
     returnfalse;
     }
     returntrue;
    });// Attempt to transitionfsm.go(Elevator.DoorsClosed);// State does not change to DoorsClosedconsole.log("DoorsOpened", fsm.currentState===Elevator.DoorsOpened); //true

    通配符转换

    如果所有转换都有效,则有一个convience通配符方法 fromAny(STATE_ENUM)toAny(STATE_ENUM) 这种情况下的和。

    enum ValidStates {
     A,
     B,
     C,
     D}var newFsm =newTypeState.FiniteStateMachine<ValidStates>(ValidStates.A);newFsm.from(ValidStates.A).toAny(ValidStates);newFsm.fromAny(ValidStates).to(ValidStates.B);

    STA  str  mac  type  MACH  类型  
    相关文章