title: Correct way to use substatemachine in spring-statemachine
body: <p>I'm not sure I clearly understand how to use substates.
  I didn't find any good explained example of correct spring state machine configuration, so I would be glad if somebody can share it we me.</p>
  <p>On the picture below there is dummy representation of spring machine i'm currently working on (actual number of states is much bigger).
  Logically inner states may be grouped into 3 parent states: registration, user configuration and admin configuration.</p>
  <p>Question #1. Is this correct usage of substates? I can write it without substates, but I feel like it may and should be grouped. Second picture is a diagram of SM from the spring docs and it looks pretty the same what i have in my own SM: <a href="https://docs.spring.io/spring-statemachine/docs/current/reference/index.html#configuring-hierarchical-states" rel="nofollow noreferrer">https://docs.spring.io/spring-statemachine/docs/current/reference/index.html#configuring-hierarchical-states</a></p>
  <p>Question #2. There is some output in logs I can't understand.</p>
  <p>This is part of my transitions config:</p>
  <pre><code>.external(TestState.SELECT_TYPE, TestState.CHOICE_SELECT_TYPE, TestEvent.TYPE_SELECTED)
  .choice(
      TestState.CHOICE_SELECT_TYPE,
      TestState.EMPLOYEE,
      TestState.SEARCHER,
      registrationStateService::isEmployeeUserTypeSelected
  )
  .external(
      TestState.ADMIN_ROLE,
      TestState.ADMIN_SMTH,
      TestEvent.ADMIN_ROLE_SELECTED
  )
  .external(
      TestState.ADMIN_SMTH,
      TestState.ADMIN_SMTH_ELSE,
      TestEvent.ADMIN_SMTH_SELECTED
  )
  </code></pre>
  <p>And states config:</p>
  <pre><code>.and()
  .withStates()
  .parent(TestState.ADMIN)
  .initial(TestState.ADMIN_ROLE)
  </code></pre>
  <p>So ADMIN is a parent state, and initial state of this submachine is ADMIN_ROLE.</p>
  <p>This is the logs of my SM execution:</p>
  <pre><code>...
  State changed from: FILL_SURNAME to SELECT_TYPE by NAME_FILLED
  State entered: SELECT_TYPE
  State changed from: SELECT_TYPE to CHOICE_SELECT_TYPE by TYPE_SELECTED
  State entered: ADMIN
  State changed from: null to ADMIN_ROLE by null
  State entered: ADMIN_ROLE
  State changed from: ADMIN_ROLE to ADMIN_SMTH by ADMIN_ROLE_SELECTED    &lt;-BREAK POINT 1
  State entered: ADMIN_SMTH
  State changed from: ADMIN_SMTH to ADMIN_SMTH_ELSE by ADMIN_SMTH_SELECTED    &lt;-BREAK POINT 2
  State entered: ADMIN_SMTH_ELSE
  ...
  </code></pre>
  <p>I put breakpoints before sending event to state machine. And for the first BP, the result of statemachine.state.id was ADMIN - state that marked as parent. However, before that I see in logs we already entered into ADMIN_ROLE state. Ok, I understood this as state.id is a currentState of main SM.
  For breakpoint #2, result of statemachine.state.id was ADMIN_SMTH.
  So the question is, why in case 1 I see state of main state machine, and for case 2 the result is state of submachine? Maybe I did something wrong?</p>
  <p><a href="https://i.stack.imgur.com/KbB1V.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KbB1V.png" alt="enter image description here" /></a>
  <a href="https://i.stack.imgur.com/jHL4y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jHL4y.png" alt="enter image description here" /></a></p>