How to use ordered transitions with conditions #726
Replies: 2 comments
-
|
Hello @schaefer01,
So, transitions will create transitions A -next_state[check_A2B]-> B -next_state[check_C2A]-> C -next_state[check_B2C]-> A but none of them will actually be evaluated since your first set of transitions will always be succesfull. What you probably want to do is pass conditions and state order at the same call. from transitions import Machine
class Model:
def check_A2C(self):
return True
def check_C2B(self):
return True
def check_B2A(self):
return False
model = Model()
machine = Machine(model, states=["A", "B", "C"], initial="A")
machine.add_ordered_transitions(
["A", "C", "B"], conditions=["check_A2C", "check_C2B", "check_B2A"]
)
assert model.is_A()
assert model.next_state()
assert model.is_C()
assert model.next_state()
assert model.is_B()
assert not model.next_state() # transition unsucessful
assert model.is_B() # we are still in B since check_B2A failed |
Beta Was this translation helpful? Give feedback.
-
|
Thank you. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Again, perhaps I am coding this wrong without understanding the true behavior.
The section on the main page is titled "ordered transitions"
My assumption is, I am checking for transitions that cannot occur, but they do occur.
Perhaps I could use an example showing how the check operation behaves when bad behavior does occur?
the code:
states = ['A', 'B', 'C']
machine = Machine(states=states, initial='A')
machine.add_ordered_transitions(['A', 'C', 'B'])
machine.add_ordered_transitions(conditions=['check_A2B','check_C2A','check_B2C'])
print(machine.state)
A
machine.next_state()
True
print(machine.state)
C
machine.next_state()
True
print(machine.state)
B
machine.next_state()
True
print(machine.state)
A
Beta Was this translation helpful? Give feedback.
All reactions