Skip to content

Example 3: A toggle switch

Nikolaos Pougounias edited this page Jul 29, 2014 · 1 revision

Extending the previous example, when the event press arrives at STATE_ON, the state is moved back to STATE_OFF.

The state diagram of a toggle switch

So we have a second transition from STATE_ON to STATE_OFF. For this transition we define a new action to be performed.

public class TurnLightsOffAction extends AbstractAction {
    @Override
    public void execute(StateContext context) throws ActionException {
        System.out.println("Action: Turned the lights off!");
    }
}
    <bean id="actionTurnLightsOff" 
          class=" gr.ekt.fsmengine.example3.TurnLightsOffAction" />

    <bean id="transitionB" class="gr.ekt.fsmengine.api.DefaultTransition">
        <property name="fromState" ref="stateOn" />
        <property name="toState" ref="stateOff" />
        <property name="actions">
            <list>
                <ref bean="actionTurnLightsOff"/>
            </list>
        </property>
    </bean>

The complete declaration is under conf/context-example3.xml.

Run src/test/java/gr.ekt.fsmengine.example3.Run to see the execution flow.

Execution of example 3

Clone this wiki locally