Notes

Powered by The LE Company

Tuesday, January 9, 2018

Spring Finite State Machine

This is a basic example about designing a Finite State Machine (FSM) using Spring Statemachine in combination with Spring MVC.

Some basic FSM definitions you can reference at wiki or Spring statemachine reference.
Here we go directly to design a simple FSM as above figure. There are some points to explain:

(1): The default state is STATE 1, meaning that when FSM is initialized the FSM will auto enter to STATE 1. Also from above figure, whenever FSM enters to STATE1 then function state1Entry will be executed and whenever FSM exits the STATE1 then function state1Exit will be executed.
(2): When the FSM is in STATE1 and event EVENT3 is triggered then function state1Action will be executed. There is no state change, FSM is still in STATE1.
(3): When the FSM is in STATE1 and event EVENT1 is triggered then function toState2 will be executed and FSM will enters to STATE2.
(4): When the FSM is in STATE2 and event EVENT2 is triggered then function toState1 will be executed and FSM will enters to STATE1.
(5): whenever FSM enters to STATE2 then function state2Entry will be executed and whenever FSM exits the STATE2 then function state2Exit will be executed.
(6): When the FSM is in STATE2 and event EVENT4 is triggered then function state1Action will be executed. There is no state change, FSM is still in STATE2.

The difference between toState2 and state2Entry that is the function toState2 is only executed when FSM changes from STATE1 to STATE2 due to EVENT1, in contrary the function state2Entry always executes whenever FSM enters to STATE2. Similarly with toState1 and state1Entry.

In above design, the initial state is STATE1 meaning that once FSM is started, the FSM will automatically enters to the STATE1 and function state1Entry will be executed.


Note:
This are only basic features of  Spring FSM, there are some more features as Guard evaluation, Fork state, Join state...
FSM in a real project usually has more states than this example and also it is usually designed with parent states, child states...
Also there are different FSM implementations, so behavior of that different FSMs are a bit different depending on implementors. However the main concepts are similar.

Now, we create a simple Java Spring MVC project to implement above FSM.
Basically, in order to design and implement a FSM with Spring statemachine, there are some steps need to do:

1. Declare states and events (Events.java, State.java)
2. Configure states and transitions as initial state, when a state transition will happen... (FSMConfig.java).
3. Implement actions which will execute when it matches certain conditions (FSMExternalActions.java, FSMInternalActions.java).
4. Start FSM (InitFSM.java).
5. Trigger events to the FSM (MainController.java).

Basically, the events are sent from a web client to the MVC controller at: http://localhost:8080/SpringFiniteStateMachine.
In this example, these events are selected from a combo box and will be sent to the server when user presses button "Submit Selected Event".

Configuration for Spring MVC has been done in /resources/applicationContext.xml and /WEB-INF/web.xml.

The project is build with Maven and source code is in GitHub here. If everything is setup correctly, then you could see the simple web page as below.



No comments: