python 3的简单状态机

simplesm的Python项目详细描述


简化

python 3的简单状态机,使用字典创建状态。

安装

pip3 install simplesm

文档

SimpleSM子类应该有:

PropertyDescription
^{}machine will start from this state.
^{}machine current state
^{}machine previous state
^{}test action shows current value to be tested, it will be set before testing it against machine states, so it will always have the latest value tested even if machine does not accept that action.
^{}machine current action, this value will be set after action being accepted by state, so it will always have the latest acceptable value tested.
^{}just the last acceptable action before ^{}.
^{}return ^{} if machine is in a final state, otherwise ^{}.
^{}machine states! is a dict with keys as machine states, each state value is a dict too.

计算机statesdict值可以有:

PropertyDescription
^{}a dict that keys are acceptable actions (that could be string or a function name) and values are dicts with ^{} that defines next state and ^{} that defines event name to call.
^{}this event will be called before testing action against machine states.
^{}if none of ^{} actions be accepted ^{} will be called.
^{}if exist and equals ^{} shows that this state is a final state (means acceptable) otherwise state is not acceptable.

计算机首先尝试将操作字符串与当前状态转换匹配,如果没有找到,则尝试调用每个当前状态转换,如果返回True,则接受该转换。

MethodDescription
^{}get a string or list of strings as ^{} and try to perform that actions on machine, if ^{} be ^{}, ^{}s will not be called.
^{}set ^{}, ^{}, ^{}, ^{} and ^{} to ^{}.
^{}get a string or list of strings as ^{} and try to perform that actions on machine and return ^{} if machine stops in a final state, otherwise ^{}. this method does not change current values of machine and also by default does not call ^{}s.

Example

的用法和功能

simplesm导入SimpleSM,并创建一个类:

fromsimplesmimportSimpleSMclassTestMachine(SimpleSM):start_state="S0"states={"S0":{"transitions":{"0":{"state":"S1","event":"print"},"1":{"state":"S2","event":"print"}},"event":"welcome"},"S1":{"final":False,"transitions":{"1":{"state":"S0","event":"print"}},"event":"welcome"},"S2":{"final":True,"transitions":{"0":{"state":"S1","event":"print"},"2":{"state":"S3","event":"print"},"is_a_to_d":{"state":"S0","event":"print"}},"else":"s2_wrong","event":"welcome"},"S3":{"event":"goto_s2"}}defgoto_s2(self):print("we are just going to state S2")self.current_state="S2"defwelcome(self):print("we are in state {0} and get action {1}".format(self.current_state,self.test_action))defprint(self):print("going from {0} to {1} with {2}".format(self.previous_state,self.current_state,self.current_action))defs2_wrong(self):print("unacceptable input {0}".format(self.test_action))self.current_state="Error"defis_a_to_d(self):ifself.test_actionin"abcd":returnTruereturnFalsedefmain():m=TestMachine()m.perform(list("0110112a1d011"))print("current state is",m.current_state,"before calling accept")print("does machine accept 0110112a1d011 ?",m.accept(list("0110112a1d011")))print("current state is",m.current_state,"after calling accept")print("current state is acceptable ?",m.is_acceptable)if__name__=='__main__':main()

输出:

we are in state S0 and get action 0
going from S0 to S1 with 0
we are in state S1 and get action 1
going from S1 to S0 with 1
we are in state S0 and get action 1
going from S0 to S2 with 1
we are in state S2 and get action 0
going from S2 to S1 with 0
we are in state S1 and get action 1
going from S1 to S0 with 1
we are in state S0 and get action 1
going from S0 to S2 with 1
we are in state S2 and get action 2
going from S2 to S3 with 2
we are just going to state S2
going from S2 to S0 with a
we are in state S0 and get action 1
going from S0 to S2 with 1
we are in state S2 and get action d
going from S2 to S0 with d
we are in state S0 and get action 0
going from S0 to S1 with 0
we are in state S1 and get action 1
going from S1 to S0 with 1
we are in state S0 and get action 1
going from S0 to S2 with 1
current state is S2 before calling accept
does machine accept 0110112a1d011 ? False
current state is S2 after calling accept
current state is acceptable ? True

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java GridBagLayout不填充区域   java Memozied Fibonacci未运行与常规Fibonacci解决方案   Java Web启动未启动问题   Java中异常和if-then的区别   java从命令提示符运行批处理文件获取错误   socket在Java中验证SSL证书的公共名称   如何在JAVA中检查字符串数组中的相等字   用java语言将音频文件转换成文本文件的语音识别   java为什么foo(1,2,3)没有传递给varargs方法foo(对象…)作为整数[]   java通过蓝牙将奇怪的数据从Arduino传输到Android   java ContainerRequestFilter获取空entitystream   java如何从安卓 studio中删除不兼容类型错误   基本Java错误   在Spring引导中使用REST API时发生java错误   javascript通过从SQL查询派生的URL打开页面