A tutorial on SCXML History Element - Remembering State Information
An SCXML with a complex state (A state with substates/parallel elements) can be made to preserve its state information on exit. When a complex state with a history is entered again, it enters the state it was last in rather than the initial state which it would have otherwise entered.
Use Case
To define an error-handler. In many cases, it might be desirable to have the error-handling logic at one place which can be re-used in all the error conditions. Normally, the flow would be, action1-state [act] do action1 on error do error-handling back to action1-state else action2-state [act] do action1 on error do error-handling back to action2-state else action3-state [act] do action1 on error do error-handling back to action3-state else
Example based on the use case above (without history)
<scxml
xmlns="http://www.w3.org/2005/07/SCXML"
version="1.0"
xmlns:somespace="http://stack.mera.com/CCXML"
initialstate="action1-state">
<state id="action1-state">
<transition event="act" target="action2-state"/>
</state>
<state id="action2-state">
<onentry>
<somespace:some-action/>
</onentry>
<transition event="act" target="action3-state"/>
<transition event="application.error" target="action2-error-handler"/>
</state>
<state id="action3-state">
<onentry>
<somespace:some-action/>
</onentry>
<transition event="act" target="action4-state"/>
<transition event="application.error" target="action3-error-handler"/>
</state>
<state id="action4-state" final="true"/>
<state id="action2-error-handler">
<onentry>
<somespace:handle-error/>
</onentry>
<transition target="action2-state"/>
</state>
<state id="action3-error-handler">
<onentry>
<somespace:handle-error/>
</onentry>
<transition target="action3-state"/>
</state>
<state id="action4-state" final="true"/>
</scxml>
SCXML Using history element
<?xml version="1.0"?>
<scxml xmlns="http://www.w3.org/2005/07/SCXML"
xmlns:my="http://my.custom-actions.domain/CUSTOM"
version="1.0"
initialstate="universe">
<state id="universe">
<initial>
<transition target="history-actions"/>
</initial>
<transition event="application.error" target="error-handler"/>
<history type="deep" id="history-actions">
<transition target="action-1"/>
</history>
<state id="action-1">
<transition event="action.done" target="action-2"/>
</state>
<state id="action-2">
<transition event="action.done" target="action-3"/>
</state>
<state id="action-3" final="true"/>
</state>
<state id="error-handler">
<transition event="error.handled" target="history-actions"/>
</state>
</scxml>
Explanation
TODO
Source Code
TODO
Contributed By: Fasihullah Askiri