8.16. fsm 模块 API 文档

8.16.1. API

def debug(*info):...
def error(*info):...
def info(*info):...

8.16.1.1. class State:

def __init__(self,name:str,function:callable):...
def getName(self):...
def getFunction(self):...
def __str__(self):...

8.16.1.2. class StateMachine:

def __init__(self):...
def addState(self,state:State):...
def getStateByName(self,stateName:str):...
def getStateByFunction(self,stateFunction:callable):...
def getState(self,stateNameOrFn:str):...
def setCurrentState(self,stateNameOrFn:str)->int:...
def runCurrentState(self)->str:...
def mainLoop(self,initStateName:str):...
def start(self,initStateName:str):...
def stop(self):...
def wait(self):...
def _initDefaultStateMachine():...
def addState(stateFunction:callable,stateName:str='default'):...
def start(initStateName:str):...
def stop():...
def wait():...

8.16.2. Examples

8.16.2.1. test1.py

import fsm


def state1():
    print("state1")
    return "state2"


def state2():
    print("state2")
    return state3


def state3():
    print("state3")
    fsm.stop()
    return "state1"


def test_fsm():
    fsm.addState(state1, "state1")
    fsm.addState(state2, "state2")
    fsm.addState(state3, "state3")
    fsm.start("state1")
    fsm.wait()

test_fsm()