title: Why a state is exiting while transitioning to one of its sub-states?
body: <p>I am trying to implement a state machine with nested states (as linked below) using Spring Statemachine (version 2.1.3).</p>
  
  <p><a href="https://i.stack.imgur.com/c7zbl.png" rel="nofollow noreferrer">Aimed state machine</a></p>
  
  <p>In the code below, I assume <code>stateMachineDepth3</code> corresponds to that machine</p>
  
  <pre class="lang-java prettyprint-override"><code>package de.iotiq.test.statemachine;
  
  import org.springframework.statemachine.StateContext;
  import org.springframework.statemachine.StateMachine;
  import org.springframework.statemachine.config.StateMachineBuilder;
  import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
  import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
  import org.springframework.statemachine.guard.Guard;
  import org.springframework.statemachine.listener.StateMachineListenerAdapter;
  import org.springframework.statemachine.state.State;
  
  public class Main {
      public static void main(String[] args) throws Exception {
          StateMachine&lt;States, String&gt; stateMachineDepth2;
          StateMachine&lt;States, String&gt; stateMachineDepth3;
  
          StateMachineBuilder.Builder&lt;States, String&gt; builder2 = StateMachineBuilder.builder();
          StateMachineBuilder.Builder&lt;States, String&gt; builder3 = StateMachineBuilder.builder();
  
          configureL2(builder2.configureStates(), null);
          configureL2(builder2.configureTransitions());
  
          configureL1(builder3.configureStates());
          configureL1(builder3.configureTransitions());
  
          stateMachineDepth2 = builder2.build();
          stateMachineDepth3 = builder3.build();
  
          stateMachineDepth2.addStateListener(new StateMachineListenerAdapter&lt;States, String&gt;() {
  
              @Override
              public void stateEntered(State&lt;States, String&gt; state) {
                  System.out.println("State (D2) -&gt; " + state.getId());
              }
  
              @Override
              public void stateExited(State&lt;States, String&gt; state) {
                  System.out.println("State (D2) &lt;- " + state.getId());
              }
          });
  
          stateMachineDepth3.addStateListener(new StateMachineListenerAdapter&lt;States, String&gt;() {
  
              @Override
              public void stateEntered(State&lt;States, String&gt; state) {
                  System.out.println("State (D3) -&gt; " + state.getId());
              }
  
              @Override
              public void stateExited(State&lt;States, String&gt; state) {
                  System.out.println("State (D3) &lt;- " + state.getId());
              }
          });
  
          stateMachineDepth2.start();
          stateMachineDepth3.start();
      }
  
      private static void configureL1(StateMachineStateConfigurer&lt;States, String&gt; states) throws Exception {
          states
              .withStates()
                  .initial(States.S1)
                  .state(States.S1)
                  .state(States.S2)
          ;
  
          configureL2(states, States.S2);
      }
  
      private static void configureL2(StateMachineStateConfigurer&lt;States, String&gt; states, States parent) throws Exception {
          states
              .withStates()
                  .parent(parent)
                  .initial(States.S2a)
                  .state(States.S2a)
                  .state(States.S2b)
          ;
  
          configureL3(states);
      }
  
      private static void configureL3(StateMachineStateConfigurer&lt;States, String&gt; states) throws Exception {
          states
              .withStates()
                  .parent(States.S2b)
                  .initial(States.S2b1)
                  .state(States.S2b1)
                  .choice(States.S2b2)
                  .state(States.S2b2)
                  .state(States.S2b3)
                  .state(States.S2b4)
                  .state(States.S2b5)
          ;
      }
  
      private static void configureL1(StateMachineTransitionConfigurer&lt;States, String&gt; transitions) throws Exception {
          transitions
              .withExternal()
                  .source(States.S1)
                  .target(States.S2)
          ;
  
          configureL2(transitions);
      }
  
      private static void configureL2(StateMachineTransitionConfigurer&lt;States, String&gt; transitions) throws Exception {
          transitions
              .withExternal()
                  .source(States.S2a)
                  .target(States.S2b)
          ;
  
          configureL3(transitions);
      }
  
      private static void configureL3(StateMachineTransitionConfigurer&lt;States, String&gt; transitions) throws Exception {
          transitions
              .withExternal()
                  .source(States.S2b1)
                  .target(States.S2b2)
                  .and()
              .withChoice()
                  .source(States.S2b2)
                  .first(States.S2b3, decide())
                  .last(States.S2b4)
                  .and()
              .withExternal()
                  .source(States.S2b3)
                  .target(States.S2b5)
                  .and()
              .withExternal()
                  .source(States.S2b4)
                  .target(States.S2b5)
          ;
      }
  
      private static Guard&lt;States, String&gt; decide() {
          return new Guard&lt;States, String&gt;() {
  
              public boolean evaluate(StateContext&lt;States, String&gt; context) {
  
                  System.out.println("eveluating...");
  
                  return false;
              }
          };
      }
  }
  </code></pre>
  
  <p>when I start it with <code>stateMachineDepth3.start();</code> I get the following output</p>
  
  <pre><code>State (D3) -&gt; S1
  State (D3) &lt;- S1
  State (D3) -&gt; S2
  State (D3) -&gt; S2a
  State (D3) &lt;- S2a
  State (D3) -&gt; S2b
  State (D3) -&gt; S2b1
  eveluating...
  State (D3) &lt;- S2b1
  State (D3) &lt;- S2b
  State (D3) -&gt; S2b4
  State (D3) &lt;- S2b4
  State (D3) -&gt; S2b5
  </code></pre>
  
  <p>As it can be seen from the output, while transitioning from <code>S2b1</code> to <code>S2b4</code>, it is exiting <code>S2b</code> which should't be the case.</p>
  
  <p>For a state machine with a depth of 2 as in <code>stateMachineDepth2</code>, which consists of <code>S2</code> internals, it looks like transitions are as expected. The output is like below:</p>
  
  <pre><code>State (D2) -&gt; S2a
  State (D2) &lt;- S2a
  State (D2) -&gt; S2b
  State (D2) -&gt; S2b1
  eveluating...
  State (D2) &lt;- S2b1
  State (D2) -&gt; S2b4
  State (D2) &lt;- S2b4
  State (D2) -&gt; S2b5
  </code></pre>
  
  <p>Can you see something wrong about my statemachine implementation?</p>