question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected behaviour of ordered transitions

See original GitHub issue

Hey!
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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
aleneumcommented, Nov 27, 2017

Will do when #266 is resolved.

0reactions
janekbaraniewskicommented, Nov 30, 2017

@aleneum awesome, thanks for letting me know! 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unexpected behaviour of ordered transitions issue - PythonTechWorld
Unexpected behaviour of ordered transitions. Hey! I'm having troubles with understanding behaviour of transitions generated using add_ordered_transitions .
Read more >
Why Do Kids Have Trouble With Transitions?
Being asked to change activities or locations is often a trigger for behavior like whining, complaining, or throwing tantrums. Kids may not be ......
Read more >
Unexpected Behaviour with Vue.js transition - Stack Overflow
This is odd behavior, but it may be that accessing the offsetHeight property directly forces Vue to perform a calculation that is ordinarily ......
Read more >
Mining sequences with exceptional transition ... - Springer Link
Mining sequences with exceptional transition behaviour of varying order using quality measures based on information-theoretic scoring ...
Read more >
pytransitions/transitions - GitHub
Queued transitions​​ The default behaviour in Transitions is to process events instantly. This means events within an on_enter method will be processed before ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found