It appears the trigger methods still run then raise the MachineError exception afterwards when transition is not valid from current state. Is there a way to block the execution of a trigger so that a call to the trigger on the model will simply raise the exception and not execute the trigger?
Sorry, forgot to mention using the overridden _checked_assignment
from the FAQ which may be reason for this behavior.
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