How to describe one condition func call but different state change in one transition
hi,I have a question when using your awesome FSM, I want the code works like:
state = STATE_A
if condition_test():
state = STATE_B
else:
state = STATE_C
I write code like this, but it does work as expected. Becase the trigger: `mycall' will be called two times.
def condition_test():
return random.randint(0,1) == 0
transitions = [
{
'trigger': 'mycall',
'source': 'STATE_A',
'dest': 'STATE_B' ,
'conditions': 'condition_test'
},
{
'trigger': 'mycall',
'source': 'STATE_A',
'dest': 'STATE_C' ,
'unless': 'condition_test'
}
]
fsm = Machine(states=states, initial='STATE_A')
#### other code
So please help me, how to achieve one condition func call but different state change in one transition. Thanks verymuch!
You shouldn't need to specify any conditions in the second transition---so if you just remove the 'unless' argument, it should work fine. By default, transitions will be attempted in order until one successfully executes. So if you have a conditional on the first transition that fails, the FSM will proceed to execute the second transition.
If that doesn't work, let me know.
I tested it as you had told, I does works! So appreciated for your help! Awesome project!!!
Glad to hear it.