【发布时间】:2017-08-07 11:50:40
【问题描述】:
我正在使用 pytransitions/transitions 模块并尝试构建一些分层状态机。
在下面的 sn-p 中,我触发了从一个嵌套状态到另一个嵌套状态的转换。
问题是,如果我将 on_enter 回调附加到目标嵌套状态,则库正在父计算机中搜索此回调。
from transitions.extensions import HierarchicalMachine as Machine
from transitions.extensions.nesting import NestedState as State
class Nested(Machine):
def print_msg(self):
print("Nested")
def __init__(self):
self.states = ['n1', {'name':'n2', 'on_enter':'print_msg'}]
Machine.__init__(self, states=self.states, initial='n1')
self.add_transition(trigger='goto_n2',
source='*',
dest='n2')
class Top(Machine):
def print_msg(self):
print("Top")
def __init__(self):
self.nested = Nested()
self.states = [ 't1',
{'name': 't2',
'children': self.nested}]
Machine.__init__(self, states=self.states, initial='t1')
self.add_transition(trigger='goto_t2',
source='*',
dest='t2_n1')
top_machine = Top()
top_machine.goto_t2()
top_machine.goto_n2()
脚本的输出是“Top”
如果我从 Top 类中删除 print_msg(),那么我会得到 AttributeError。
虽然理论上我可以在顶级机器中拥有回调,但我肯定更愿意将我的状态和回调一起保存在嵌套机器的明确定义的边界中。
知道如何实现吗?
【问题讨论】:
标签: python transitions fsm