我一直在github,SO上寻找封闭的问题,并通过谷歌搜索来解决此问题。但是我无法解决我的问题,这似乎是正确的地方。我已经在github上打开了一个问题,但是我不确定这是否正确。 我正在制作一个状态机,它可以包含几个子状态,它们也都是状态机。因此,这基本上可以归结为根据readme重用HSM。
我最高级别的SM看起来像这样:
from transitions.extensions import LockedHierarchicalMachine as Machine
from coordination.running import RunningStateMachine
logging.basicConfig(level=logging.ERROR)
logging.getLogger("transitions").setLevel(logging.INFO)
class RPPStateMachine(Machine):
def __init__(self, name):
self._running = RunningStateMachine()
self.name = name
states = [
"init",
{"name": "running", "children": self._running},
"stop",
]
Machine.__init__(self, states=states, initial="init")
self.add_transition("e_run", "init", "run", after=self.run_machine)
self.add_transition("e_stop", "*", "stop")
def run_machine(self):
self._running.initialize()
如您所见,状态机具有三个状态init
,running
和stop
。一旦事件e_run()
通过类似的方式发送
machine = RPPStateMachine("my_machine")
machine.e_run()
计算机过渡到running
状态。
我以间接方式进行操作,因为我希望事情能够自动发生。 e_run()
导致过渡到running
,然后run_machine
调用运行类的initialize
方法,该方法将触发一个事件以启动事件链。下面我显示running
,这使事情变得很清楚。
因此,将运行状态定义为
from transitions.extensions import LockedHierarchicalMachine as Machine
from coordination.test_mode import TestingStateMachine
from coordination.release_mode import ReleaseStateMachine
class RunningStateMachine(Machine):
def __init__(self):
self._test_mode = TestingStateMachine()
self._release_demo = ReleaseStateMachine()
states = [
"init",
"configuration",
"idle",
{"name": "test_mode", "children": self._test_mode},
{"name": "release_mode", "children": self._release_mode},
]
Machine.__init__(self, states=states, initial="init")
self.add_transition("e_start_running", "init", "configuration", after=self.configuration)
self.add_transition("e_success_config", "configuration", "idle")
self.add_transition("e_test_mode", "idle", "test_mode")
self.add_transition("e_release_mode", "idle", "release_mode")
self.add_transition("e_start_running", "idle", "init")
def initialize(self):
print("Initialization step for running, emitting e_start.")
self.e_start_running()
def configuration(self):
print("Configuring...")
print( "Current state: " + self.state)
self.e_success_config()
类似于其父级,由几个状态和几个子状态组成。 我还启用了日志记录功能,以查看进入和退出的状态。以我的经验,嵌套状态机非常有用,因为您可以重用之前编写的状态。除了随着状态机的增长,它还有助于保持事物的模块化。因此,没有一种状态会变得庞大且难以阅读/理解。
所以异常的行为是,当调用e_run()
时,我得到了打印图像
INFO:transitions.core:Entered state running
INFO:transitions.core:Entered state running_init
Initialization step for running, emitting e_start.
INFO:transitions.core:Exited state init
INFO:transitions.core:Entered state configuration
Configuring...
current state: configuration
INFO:transitions.core:Exited state configuration
INFO:transitions.core:Entered state idle
如您所见
machine.state
>>> 'running_init'
同时
machine._running.state
>>> 'idle'
我当然可以将过渡定义移到父状态,但这并不方便。我不能对所有子州都这样做。显然,我希望每个子状态对其自身的行为负责。这里的惯例是什么?这是错误还是预期的行为?
如何将状态机整齐地嵌套在彼此之下?
答案 0 :(得分:0)
自transitions
0.7.1起,将状态机作为另一个状态机的子级传递,将会复制所有传递给状态机的状态到父级。通过的状态机保持不变(如我们所讨论的here)。
from transitions.extensions import MachineFactory
HSM = MachineFactory.get_predefined(nested=True)
fsm = HSM(states=['A', 'B'], initial='A')
hsm = HSM(states=['1', {'name': '2', 'children': fsm}])
# states object have been copied instead of referenced, they are not identical
assert fsm.states['A'] is not hsm.states['2_A']
hsm.to_2_A()
# both machines work with different models
assert fsm.models[0] is not hsm.models[0]
assert fsm.state is not hsm.state
当前建议的工作流程是拆分模型和计算机,并将计算机仅视为其父级的某种“蓝图”:
from transitions.extensions import MachineFactory
class Model:
pass
HSM = MachineFactory.get_predefined(nested=True)
# creating fsm as a blueprint, it does not need a model
fsm = HSM(model=None, states=['A', 'B'], initial='A')
# use a model AND also
model = Model()
hsm = HSM(model=['self', model], states=['1', {'name': '2', 'children': fsm}])
# will only update the machine's state
hsm.to_1()
assert model.state != hsm.state
# will update ALL model states
hsm.dispatch("to_2_B")
assert model.state == hsm.state
但是,这不能代替在父计算机中正确隔离(和/或限定范围)的计算机嵌套。已创建功能draft,并有望在可预见的将来实现。