This appendix provides generic information about state machines.
Assuming we have states STATE1 , STATE2 and events EVENT1 , EVENT2 , logic of state machine can be defined as shown in below quick example.
public enum States < STATE1, STATE2 >public enum Events@Configuration @EnableStateMachine public class Config1 extends EnumStateMachineConfigurerAdapter < @Override public void configure(StateMachineStateConfigurer states) throws Exception < states .withStates() .initial(States.STATE1) .states(EnumSet.allOf(States.class)); > @Override public void configure(StateMachineTransitionConfigurer transitions) throws Exception < transitions .withExternal() .source(States.STATE1).target(States.STATE2) .event(Events.EVENT1) .and() .withExternal() .source(States.STATE2).target(States.STATE1) .event(Events.EVENT2); >>
@WithStateMachine public class MyBean < @OnTransition(target = "STATE1") void toState1() < >@OnTransition(target = "STATE2") void toState2() < >>
public class MyApp < @Autowired StateMachine stateMachine; void doSignals() < stateMachine.sendEvent(Events.EVENT1); stateMachine.sendEvent(Events.EVENT2); >>
State Machine Main entity driving a collection of states together with regions, transitions and events. State A state models a situation during which some invariant condition holds. State is the main entity of a state machine where state changes are driven by an events. Extended State An extended state is a special set of variables kept in a state machine to reduce number of needed states. Transition A transition is a relationship between a source state and a target state. It may be part of a compound transition, which takes the state machine from one state configuration to another, representing the complete response of the state machine to an occurrence of an event of a particular type. Event An entity which is send to a state machine which then drives a various state changes. Initial State A special state in which the state machine starts. Initial state is always bound to a particular state machine or a region. A state machine with a multiple regions may have a multiple initial states. End State Also called as a final state is a special kind of state signifying that the enclosing region is completed. If the enclosing region is directly contained in a state machine and all other regions in the state machine also are completed, then it means that the entire state machine is completed. History State A pseudo state which allows a state machine to remember its last active state. Two types of history state exists, shallow which only remember top level state and deep which remembers active states in a sub-machines. Choice State A pseudo state which allows to make a transition choice based of i.e. event headers or extended state variables. Junction State A pseudo state which is relatively similar to choice state but allows multiple incoming transitions while choice only allows one incoming transition. Fork State A pseudo state which gives a controlled entry into a regions. Join State A pseudo state which gives a controlled exit from a regions. Entry Point A pseudo state which allows a controlled entry into a submachine. Exit Point A pseudo state which allows a controlled exit from a submachine. Region A region is an orthogonal part of either a composite state or a state machine. It contains states and transitions. Guard Is a boolean expression evaluated dynamically based on the value of extended state variables and event parameters. Guard conditions affect the behavior of a state machine by enabling actions or transitions only when they evaluate to TRUE and disabling them when they evaluate to FALSE. Action A action is a behaviour executed during the triggering of the transition.
This appendix provides generic crash course to a state machine concepts.