transitions icon indicating copy to clipboard operation
transitions copied to clipboard

How to describe one condition func call but different state change in one transition

Open firefoxbug opened this issue 11 years ago • 3 comments

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!

firefoxbug avatar Nov 28 '15 08:11 firefoxbug

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.

tyarkoni avatar Nov 28 '15 09:11 tyarkoni

I tested it as you had told, I does works! So appreciated for your help! Awesome project!!!

firefoxbug avatar Nov 30 '15 12:11 firefoxbug

Glad to hear it.

tyarkoni avatar Nov 30 '15 14:11 tyarkoni