Omiting calling on_enter and on_exit callbacks, if origin and target state is the same (reentrant state) #698
-
My understanding of having before and after callbacks in additions to on_enter and on_exit was also to differentiate the entranc of the state and the transition itself. But now I tested my state machine and turns out, in case of re-entrant states the callbacks are triggered. How can I omit this behavior? For example: if I am in State "A" and call the 'to_A' method, then the callback "on_enter_A" will be called (but I don't want to call this because I am already in State "A"). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hello @nadlechSICKAG, the convenience "to_" methods behave like standard transitions with source equals target. What you are looking for are probably internal transitions. As mentioned here in the Readme you can define internal transitions by stating a source and from transitions import Machine
machine = Machine(states=["A"], initial="A", before_state_change=[lambda: print("before state change")])
machine.on_enter_A(lambda: print("enter State A"))
machine.on_exit_A(lambda: print("exit State A"))
machine.add_transition(trigger="internal", source="A", dest=None, after=[lambda: print("action after internal")])
machine.to_A()
# >> before state change
# exit State A
# enter State A
assert machine.state == "A"
machine.internal()
# >> before state change
# action after internal (but no exit/enter state change) This will not trigger I hope this helps. If you have further questions let me know, |
Beta Was this translation helpful? Give feedback.
Could you specify what you are looking for? If you are looking for transitions that can execute callbacks before and after a trigger has been called but do not exit or enter a state then I'd say internal transitions are what you are looking for. An 'external' transition is the default transitions type (we tried to follow state chart SCXML behaviour here; Section 3.5) should exit a state and enter a state, regardless of their source and destination.
So if you want a trigger to act as an internal transition when in the target state but different when not you could do something like the following: