Conditional Transitions

Based on the tutorials by @thure.

By this point you probably understand generally how statecharts work. In this section we’ll discuss less ubiquitous features of SCXML that nonetheless will come in handy in certain circumstances.

Occasionally a state machine needs to react to an event differently depending on the situation. Are all of the criteria met to transition to this state, or do we need to transition to that state instead?

Let’s consider a situation where we want the logic for a traffic light to be entirely handled by a statechart. The only event the interpreter will receive is "tick", given at an evenly but arbitrarily spaced interval of time. When the interpreter hears a tick, it may need to change the lights, but it may need to keep its present configuration. How do we decide?

Enter cond

The cond attribute lets us specify an expression that the statechart has to evaluate to determine which <transition> to follow. Using cond, we can set up the traffic lights’ statechart this way:

Interval:
I am a traffic light
Loading:
    I am a state machine

    Click to show source code

    While it isn’t time to change lights, we transition to the same state after adding 1 to interval. This is totally legal and a useful pattern in situations where it’s still useful to react to the event without transitioning to a new state. The earlier example in 1.3 used this same pattern to update the position of the element while dragging was still underway.

    Using cond, we give the statechart all of the logic pertaining to changing the lights, and it’s only up to the interpreter’s environment to keep track of time by firing "tick" on some interval.

    < Previous Page (Compound States)Next Page (The History State) >