Terminal/sink pytransitions 中的状态

Terminal/sink state in pytransitions

例如,我正在使用 pytransitions 和状态机

from transitions import Machine
from transitions import EventData


class Matter(object):
    def __init__(self):
        transitions = [
            {'trigger': 'heat', 'source': 'solid', 'dest': 'liquid'},
            {'trigger': 'heat', 'source': 'liquid', 'dest': 'gas'},
            {'trigger': 'cool', 'source': 'gas', 'dest': 'liquid'},
            {'trigger': 'cool', 'source': 'liquid', 'dest': 'solid'}
        ]
        self.machine = Machine(
                model=self,
                states=['solid', 'liquid', 'gas'],
                transitions=transitions,
                initial='solid',
                send_event=True
        )

    def on_enter_gas(self, event: EventData):
        print(f"entering gas from {event.transition.source}")

    def on_enter_liquid(self, event: EventData):
        print(f"entering liquid from {event.transition.source}")

    def on_enter_solid(self, event: EventData):
        print(f"entering solid from {event.transition.source}")

我想添加一个状态,对于该状态,任何触发器都保持相同状态,不调用转换,不明确指定每个可能的触发器,也不忽略所有无效触发器(因为这非常适合调试)。

例如,我想要一个状态 crystal,可以通过从 liquid 触发 crystalize 来达到,任何事件对此都不会做任何事情。

这可以通过库来实现吗?

表达这个问题的另一种方式是 ignore_invalid_triggers=True 仅针对特定州,而不是所有州。

我最终做的是在机器已经用它的转换实例化之后:

self.machine.get_state(states.crystal.name).ignore_invalid_triggers = True

我真的不喜欢那个解决方案,因为它破坏了作为列表的转换初始化的非常干净的设计,但这是我能找到的所有东西并且它确实有效。

与转换类似,状态也可以用字典定义:

from transitions import Machine, MachineError


class Matter(object):
    def __init__(self):
        transitions = [
            {'trigger': 'heat', 'source': 'solid', 'dest': 'liquid'},
            {'trigger': 'heat', 'source': 'liquid', 'dest': 'gas'},
            {'trigger': 'cool', 'source': 'gas', 'dest': 'liquid'},
            {'trigger': 'cool', 'source': 'liquid', 'dest': 'solid'},
            # add a transition to 'crystal' which is valid from anywhere
            {'trigger': 'crystallize', 'source': '*', 'dest': 'crystal'},
        ]
        self.machine = Machine(
                model=self,
                states=['solid', 'liquid', 'gas',
                        # initialized 'crystal' with dictionary
                        {'name': 'crystal', 'ignore_invalid_triggers': True}],
                transitions=transitions,
                initial='solid',
                send_event=True
        )


m = Matter()
assert m.is_solid()
try:
    m.cool()  # raises a machine error since cool cannot be called from 'solid'
    assert False
except MachineError:
    pass
assert m.crystallize()  # transitions to 'crystal'
assert m.is_crystal()
assert not m.heat()  # note that the transition will return 'False' since it did not happen but no exception was thrown
assert m.is_crystal()  # state machine is still in state 'crystal'

您也可以传递 State(name='crystal', ignore_invalid_triggers=True) 而不是 {'name': 'crystal', 'ignore_invalid_triggers': True}documentation's state section:

中提到了这种形式

But in some cases, you might want to silently ignore invalid triggers. You can do this by setting ignore_invalid_triggers=True (either on a state-by-state basis, or globally for all states):