【发布时间】:2023-03-09 13:51:01
【问题描述】:
当转换从当前状态无效时,触发方法似乎仍在运行,然后引发 MachineError 异常。有没有办法阻止触发器的执行,以便在模型上调用触发器只会引发异常而不执行触发器?
抱歉,忘记提及使用常见问题解答中覆盖的 _checked_assignment 可能是导致此行为的原因。
from transitions import State, Machine
class StateMachine(Machine):
def _checked_assignment(self, model, name, func):
if hasattr(model, name):
predefined_func = getattr(model, name)
def nested_func(*args, **kwargs):
predefined_func()
func(*args, **kwargs)
setattr(model, name, nested_func)
else:
setattr(model, name, func)
class Rocket(StateMachine):
def __init__():
StateMachine.__init__(
self,
states=["on_pad", "fueling", "ready", "launched", "meco", "second_stage", "orbit"],
transitions=[
{'trigger': 'fuel', 'source': 'on_pad', 'dest': 'fueling'},
{'trigger': 'power_on', 'source': 'fueling', 'dest': 'ready'},
{'trigger': 'launch', 'source': 'ready', 'dest': 'launched'}
],
initial='on_pad'
)
def fuel():
print("cryos loading...")
def launch():
print("launching")
def main():
rocket = Rocket()
rocket.launch() # prints "launching" then throws Machine Error, need to block actual method execution
【问题讨论】:
-
您能否为您的问题提供一个代码示例? “触发器”包括从当前状态检查转换是否有效,但不会处理实际转换。
-
添加示例。抱歉,忘了提及使用常见问题解答中的覆盖
_checked_assignment,我认为这是导致该行为的原因
标签: python state-machine pytransitions