Unexpected behaviour of ordered transitions
See original GitHub issueHey!
I’m having troubles with understanding behaviour of transitions generated using add_ordered_transitions
.
Use-case
I’m implementing a process which consists of several changes of state in predefined order, lets say A -> B -> C -> D
. Order in which entity can obtain those states never changes but the starting point can be different every time - namely we can have the entity which starts the process in state A
and then we need to do 3 transitions A -> B -> C -> D
but we as well can have entity which starts process with state C
and we should only make one transition C -> D
.
When starting from the “first state” - so here state A
- there are no problems, transitions go as they should, everything works as expected.
from transitions import Machine, State
states = ['A', 'B', 'C', 'D']
machine = Machine(states=states, initial='A')
machine.add_ordered_transitions(loop=False)
assert machine.state == 'A'
machine.next_state()
assert machine.state == 'B'
machine.next_state()
assert machine.state == 'C'
machine.next_state()
assert machine.state == 'D'
But as soon as I set initial state of the machine to something different than “first state” things are getting weird.
Expected behaviour
Since we are generating ordered
transitions base on ordered
list of states, when generating transitions we should end up with possible transitions:
A -> B
B -> C
C -> D
which would mean that there is only 1 possible transition for state C
which leads to state D
.
machine = Machine(states=states, initial='C')
machine.add_ordered_transitions(loop=False)
assert machine.state == 'C'
machine.next_state()
assert machine.state == 'D'
Actual behaviour
Apparently if the machine is in other state than “the first one” we end up with something completely different, for this specific case (so initial state = C
) we have transitions:
C -> A
A -> B
B -> D
from transitions import Machine, State
states = ['A', 'B', 'C', 'D']
machine = Machine(states=states, initial='C')
machine.add_ordered_transitions(loop=False)
print(machine.state) # outputs: C
machine.next_state()
print(machine.state) # outputs: A
machine.next_state()
print(machine.state) # outputs: B
machine.next_state()
print(machine.state) # outputs: D
Can you explain, why it is implemented in such way? You’re explicitly adding this extra transition from initial state to the first state from the list at the very beginning of add_ordered_transitions
, before iterating through states and setting up all the other transitions. For me it’s really misleading and has nothing to do with being ordered
. Is this a bug or desired behaviour? Maybe addition of extra transition could be wrapped in some if
statement checking some flag so we can have more control?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top GitHub Comments
Will do when #266 is resolved.
@aleneum awesome, thanks for letting me know! 😄