gpt4 book ai didi

python - 如何为 `transitions` 状态机定义触发器枚举?

转载 作者:行者123 更新时间:2023-12-04 13:08:27 26 4
gpt4 key购买 nike

作为 this answer 的不相关跟进,它使用以下工作代码:

from transitions import Machine
from transitions import EventData
from typing import Callable
from enum import Enum, auto


class Observer:

def state_changed(self, event_data: EventData):
print(f"state is now '{event_data.state.name}'")


class State(Enum):
SOLID = auto()
LIQUID = auto()
GAS = auto()


class SubscribableMachine(Machine):

transitions = [
{'trigger': 'heat', 'source': State.SOLID, 'dest': State.LIQUID},
{'trigger': 'heat', 'source': State.LIQUID, 'dest': State.GAS},
{'trigger': 'cool', 'source': State.GAS, 'dest': State.LIQUID},
{'trigger': 'cool', 'source': State.LIQUID, 'dest': State.SOLID}
]

def __init__(self):
super().__init__(states=State, transitions=self.transitions,
initial=State.SOLID, send_event=True)

def subscribe(self, func: Callable, state: State):
self.get_state(state).on_enter.append(func)

def unsubscribe(self, func: Callable, state: State):
self.get_state(state).on_enter.remove(func)


machine = SubscribableMachine()
observer = Observer()
machine.subscribe(observer.state_changed, State.LIQUID)
machine.heat() # >>> state is now 'LIQUID'
machine.heat()
assert machine.state == State.GAS
machine.unsubscribe(observer.state_changed, State.LIQUID)
machine.cool() # no output
assert machine.state == State.LIQUID

我也想为 Trigger 设置一个枚举,就像我为 State 设置一个枚举一样。

唉,当我尝试的时候

class Trigger(Enum):
heat = auto()
cool = auto()

transitions = [
{'trigger': Trigger.heat, 'source': State.SOLID, 'dest': State.LIQUID},
{'trigger': Trigger.heat, 'source': State.LIQUID, 'dest': State.GAS},
{'trigger': Trigger.cool, 'source': State.GAS, 'dest': State.LIQUID},
{'trigger': Trigger.cool, 'source': State.LIQUID, 'dest': State.SOLID}
]

def __init__(self):
super().__init__(states=State, transitions=self.transitions,
initial=State.SOLID, send_event=True)

我明白了

Traceback (most recent call last):
File "C:/code/EPMD/Kodex/Algorithms/src/python/epmd/ablation_points/queries/dfgfdsg.py", line 42, in <module>
machine = SubscribableMachine()
File "C:/code/EPMD/Kodex/Algorithms/src/python/epmd/ablation_points/queries/dfgfdsg.py", line 33, in __init__
initial=State.SOLID, send_event=True)
File "C:\Code\EPMD\Kodex\EPD_Prerequisite\python_3.7.6\Lib\site-packages\transitions\core.py", line 589, in __init__
self.add_model(model)
File "C:\Code\EPMD\Kodex\EPD_Prerequisite\python_3.7.6\Lib\site-packages\transitions\core.py", line 607, in add_model
self._add_trigger_to_model(trigger, mod)
File "C:\Code\EPMD\Kodex\EPD_Prerequisite\python_3.7.6\Lib\site-packages\transitions\core.py", line 813, in _add_trigger_to_model
self._checked_assignment(model, trigger, partial(self.events[trigger].trigger, model))
File "C:\Code\EPMD\Kodex\EPD_Prerequisite\python_3.7.6\Lib\site-packages\transitions\core.py", line 807, in _checked_assignment
if hasattr(model, name):
TypeError: hasattr(): attribute name must be string

我可以使用 Enum.name 解决它:

    transitions = [
{'trigger': Trigger.heat.name, 'source': State.SOLID, 'dest': State.LIQUID},
{'trigger': Trigger.heat.name, 'source': State.LIQUID, 'dest': State.GAS},
{'trigger': Trigger.cool.name, 'source': State.GAS, 'dest': State.LIQUID},
{'trigger': Trigger.cool.name, 'source': State.LIQUID, 'dest': State.SOLID}
]

但是 StateTrigger 之间的不对称让我很困扰。
难道我做错了什么?为什么枚举适用于 State 而不是 Trigger

最佳答案

作为vish已经指出,没有办法直接使用枚举作为触发器。 Transitions 将触发器名称作为方法绑定(bind)到模型,因此要求触发器是字符串。但是,Machine 和所有子类在编写时都考虑到了继承。如果你想使用枚举而不是类和类属性,你可以派生一个合适的EnumTransitionMachine并得到一个统一的接口(interface):

from transitions import Machine
from enum import Enum, auto


class State(Enum):
A = auto()
B = auto()
C = auto()


class Transitions(Enum):
GO = auto()
PROCEED = auto()


class EnumTransitionMachine(Machine):

def add_transition(self, trigger, *args, **kwargs):
super().add_transition(trigger.name.lower() if hasattr(trigger, 'name') else trigger, *args, **kwargs)


transitions = [[Transitions.GO, State.A, State.B], [Transitions.PROCEED, State.B, State.C]]
m = EnumTransitionMachine(states=State, transitions=transitions, initial=State.A)
m.go()
assert m.state == State.B
m.proceed()
assert m.is_C()

仅供引用:还有可能将 Enums 与字符串值一起使用:

class State(Enum):
A = "A"
B = "B"
C = "C"


class Transitions(Enum):
GO = "GO"
PROCEED = "PROCEED"

# you could use the enum's value then instead:
def add_transition(self, trigger, *args, **kwargs):
super().add_transition(trigger.value.lower() if hasattr(trigger, 'value') else trigger, *args, **kwargs)

关于python - 如何为 `transitions` 状态机定义触发器枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68290388/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com