I am trying to implement a control flow using Apache scxml. My state transitions are like
<scxml initial="State1">
<state id = "State1"><transition event="event1" target="State2"></state>
<state id = "State2">
<transition event="event2" target="State3">
<transition event="event3" target="State4">
</state>
<state id = "State3">
<transition event="event4" target="State2">
</state>
<state id = "State4">
<transition event="event5" target="State2">
</state>
<state id = "State5"><transition target="State1">
</scxml>
as a state machine it works fine and working without any issue. But I have couple of requirement for which I am facing issues in
I have to check an external event in each state and if that event is true I have to go to the last state of the cycle. what would be the best way to achieve this?
It is possible that when I am in
state2
I can go tostate4
orstate5
viastate3
. Is it possible that I can fire an event or change the state fromstate3
after checking the conditions to go tostate4
orstate5
?
Both the issues above seems to have the same solution but I am not able to figure out the best way to implement them.
You can do this most elegantly by wrapping all states in a parent wrapper and transitioning from there. For example:
It seems like a bad pattern you are describing, but yes, this is possible. You need to set a flag on the way into the state, and use that flag to transition out immediately. For example, using a simple Lua datamodel (not supported by Apache SCXML, but used here for clarity):
Setting, testing, and clearing the data value in Apache SCXML is beyond my ken.
However, I must ask WHY you want to do this. It seems that it would be far better, in my opinion, to replicate the enter or exit actions you presumably want from
state3
on the transitions themselves, and let those transitions directly target state 4 or 5.