基于状态图的python状态机框架(scxml)
py-scm的Python项目详细描述
这是一个python库,支持David Harel提出的statechart(scxml格式)。 分层状态、并行状态和历史记录等功能都可以供您命令。 您不会发现另一个LIB容易和灵活地用作SCM。
请查看并运行scm_tutorial.py。 代码如下:
fromscmimportStateMachineManagerclient_scxml=""" <scxml> <state id='appear'> <transition event='born' ontransit='say_hello' target='live'/> </state> <parallel id='live'> <transition event='hp_zero' target='dead'/> <state id='eat'> </state> <state id='move'> </state> </parallel> <final id='dead'/> </scxml> """classLife:def__init__(self):self.mach_=StateMachineManager.instance().getMach('the life')self.mach_.set_do_exit_state_on_destroy(True)self.mach_.register_state_slot("appear",self.onentry_appear,self.onexit_appear)self.mach_.register_state_slot("live",self.onentry_live,self.onexit_live)self.mach_.register_state_slot("eat",self.onentry_eat,self.onexit_eat)self.mach_.register_state_slot("move",self.onentry_move,self.onexit_move)self.mach_.register_state_slot("dead",self.onentry_dead,self.onexit_dead)self.mach_.register_action_slot('say_hello',self.say_hello)self.mach_.StartEngine()deftest(self):self.mach_.enqueEvent("born")#self.mach_.frame_move(0) # state change to 'live'StateMachineManager.instance().pumpMachEvents()self.mach_.enqueEvent("hp_zero")#self.mach_.frame_move(0) # state change to 'dead'StateMachineManager.instance().pumpMachEvents()defonentry_appear(self):print("come to exist")defonexit_appear(self):print("we are going to...")defonentry_live(self):print("start living")defonexit_live(self):print("no longer live")defonentry_eat(self):print("start eating")defonexit_eat(self):print("stop eating")defonentry_move(self):print("start moving")defonexit_move(self):print("stop moving")defonentry_dead(self):print("end")defonexit_dead(self):assert(0and"should not exit final state");print("no, this won't get called.")defsay_hello(self):print("\n*** Hello, World! ***\n")if__name__=='__main__':StateMachineManager.instance().set_scxml("the life",client_scxml)life=Life()life.test()StateMachineManager.instance().pumpMachEvents()
您应该看到的输出
come to exist we are going to... *** Hello, World! *** start living start eating start moving stop eating stop moving no longer live end
简单地
- 可以从外部文件或代码中定义的字符串加载SCXML。 您连接这些^ {STR 1 } OnCythy<<强> ^ {STR 1 } $ ONEXITU<<强>,等等槽
- 启动引擎,在主循环中调用framemove。
完成。
就这么简单
阅读教程:
- (英语)http://zen747.blogspot.tw/2017/07/a-scm-framework-tutorial-statechart.html
- (繁体中文)http://zen747.blogspot.tw/2017/07/scm-framework.html