I'm using transitions library for Python and I'm finding it really useful. In my specific case I'm using FSMs that make decisions inside the states (in callbacks) and accordingly to an internal logic call new transitions. For instance:
states = ["Begin", "CheckCondition", "MakeA", "MakeB", "End"]
transitions = [
["proceed", "Begin", "CheckCondition"],
["path_a", "CheckCondition", "MakeA"],
["path_b", "CheckCondition", "MakeB"],
["proceed", "MakeA", "End"],
["proceed", "MakeB", "End"]
]
class MyModel:
def __str__(self):
return f">>>>> STATE: {self.state}"
def on_enter_CheckCondition(self):
if random.randint(1, 10) > 5:
self.path_a()
return
else:
self.path_b()
return
my_model = MyModel()
machine = HierarchicalGraphMachine(
model=my_model,
states=states,
transitions=transitions,
initial="Begin",
ignore_invalid_triggers=False
)
while my_model.state != "End":
my_model.proceed()
This approach keeps all logic inside the FSM, that is what I want, but has 2 drawbacks:
- I can have potentially an infinite chain of calls that will cause stack overflow (only
proceed()
transition, called bywhile
loop, interrupts it) - I have to remember to manually call
return
after invoking any transition from a callback
Is there any better way to use transitions library in order to achieve what I want (to call a transition from a callback)?
Thanks for help!