Skip to content

Example 4: A toggle switch #2

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

Extending the previous example, the two actions are merged into one.

TurnLightsOffAction + TurnLightsOnAction = TurnLightsAction

The new action decides whether to switch the lights on or off based on the current state.

public void execute(StateContext context) throws ActionException {
    Switch powerSwitch = (Switch) stateContext;
    if (powerSwitch.isOn()) {
        System.out.println("Action: Turned the lights off!");
    } else {
        System.out.println("Action: Turned the lights on!");
    }
}

This is possible because the action has access to the state context.

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

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

Execution of example 3

Clone this wiki locally