What is SCXML?State Chart XML (SCXML) is a general-purpose event-based state machine language that can be used in many ways. The definitive guide to authoring SCXML documents is the W3C Working Draft of the SCXML specification. Hello WorldHere is the canonical hello world example for SCXML. The interesting bits are: <scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" initial="hello"> <final id="hello"> <onentry> <log expr="'hello world'" /> </onentry> </final> </scxml>
TransitionsTransitions allow the state machine to change state. A transition is "followed" if its "trigger event" is received, and the "guard condition", if one is available is valid. Here are some transition variants: <!-- ... begin scxml, some states ... --> <state id="foo1"> <!-- ... some content ... --> <transition target="bar" /> </state> <state id="foo2"> <!-- ... some content ... --> <transition event="foo.bar" target="bar" /> </state> <state id="foo3"> <!-- ... some content ... --> <transition event="foo.bar" cond="some-boolean-expression" target next="bar" /> </state> <state id="bar"> <!-- ... some content ... --> </state> <!-- ... remaining states, end scxml ... -->
Composite statesStates can contain states, so we can think of an SCXML document as a recursive transition network. Here is a snippet (here is the entire version of this microwave example ): <!-- ... begin snippet ... --> <state id="on"> <initial> <transition target="idle"/> </initial> <state id="idle"> <!-- content for state "idle" --> </state> <state id="cooking"> <!-- content for state "cooking" --> </state> <!-- other content for state "on" --> <!-- ... end snippet ... -->
ParallelThis is a wrapper element that encapsulates multiple <state>s -- or state machines, since in the section on composite states we took a look at the "recursion" or "nesting" for the <state> element, whereby each state can become a state machine in its own right -- that are "active" at the same time. Here is a relevant snippet (the entire version of this parallel microwave example ): <!-- ... begin snippet ... --> <state id="microwave"> <parallel id="parts"> <state id="oven"> <!-- state machine for "oven" (idle/cooking) --> </state> <state id="door"> <!-- state machine for "door" (open/closed) --> </state> </parallel> </state> <!-- ... end snippet ... -->
Hello World with a custom actionThe Commons SCXML implementation allows you to register custom actions. Here is the Commons SCXML hello world example using a custom action. The interesting bits are: <scxml xmlns="http://www.w3.org/2005/07/scxml" xmlns:my="http://my.custom-actions.domain/CUSTOM" version="1.0" initial="custom"> <final id="custom"> <onentry> <my:hello name="world" /> </onentry> </final> </scxml>
|