Fork and Join in spring state machine (Parallel execution)

Viewed 612

I have an UML diagram as below : UMl

How to configure states and transitions in state machine from SI to END based on events....

 public class Config14
            extends EnumStateMachineConfigurerAdapter<States, Events> {
    
        @Override
        public void configure(StateMachineStateConfigurer<States, Events> states)
                throws Exception {
                                  .....

}



    @Override
            public void configure(StateMachineTransitionConfigurer<States, Events> transitions)
                    throws Exception {
                                   .....
    }
}
1 Answers

Though it is late to answer, I will put my findings anyways. To achieve the show state transitions, you need to have a fork/join pseudo states. As it has been mentioned over here (https://www.uml-diagrams.org/state-machine-diagrams.html), Pseudostates are typically used to connect multiple transitions into more complex state transitions paths. And their respective definition is as follows:

Fork pseudostate vertices serve to split an incoming transition into two or more transitions terminating on orthogonal target vertices (i.e., vertices in different regions of a composite state).

Join pseudostate merges several transitions originating from source vertices in different orthogonal regions.

One should note that segments outgoing from a fork vertex must not have guards or triggers(same applies for a Join).

Your config should more or less be like the following:

@Override
public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
    states
        .withStates()
        .initial("SI")
        .fork("S1")
        .join("JOIN")
        .state("S11")
        .state("S12")
        .state("S2")
        .end("END")
        .and()
        .withStates()
            .parent("S1")
            .initial("S11")
            .end("S11") //not very sure about this
        .and()
        .withStates()
            .parent("S1")
            .initial("S12")
            .end("S12");
}

and

@Override
public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
    transitions.withExternal()
                .source("SI").target("S1").event("YOUR_EVENT")
             .and()
             .withExternal()
                .source("S2").target("END").event("YOUR_EVENT")
            .and()
            .withFork()
                .source("S1")
                .target("S11")
                .target("S12")
            .and()
            .withJoin()
                .source("S11")
                .source("S12")
                .target("JOIN");
}

For a sample, check https://github.com/amenski/statemachine

Related