Transition Inheritance

A core objective of UML2 is to reduce duplication of code.

As such UML2 supports the concept of 'inherited' transitions.

When you define a transition in a parent state each of the child states 'inherit' that transition.

 var machine = StateMachine.create((g) => g
    ..initialState<S>()
    ..state<Alive>((b) => b
      ..on<OnDeath, Dead>()
      ..state<Young>((b) => b)
      ..state<Old>((b) => b))
    ..state<Dead>((b) => b)

In the above example we have four states:

  • Alive

  • Young

  • Old

  • Dead

It doesn't matter if you are Young or Old you can always die.

FMS2 allows us to place the '..on<OnDeath, Dead>' transition in the parent 'Alive' state. We now say that both the Young and Old states inherit the OnDeath transition. This correctly models the fact that you can die at any moment (so stop programming right now and go out and enjoy the day!).

Handling of inherited transitions

So what occurs when stateMachine.applyEvent(OnDeath()) is called?

When using nested states or concurrent regions you state machine can be in multiple states simultaneously.

In the above example if you are Young then you are also Alive, so we can say that you are in both the 'Alive' and the 'Young' state.

Our StateOfMind object would have a single StatePath 'Alive -> Young'. If you had active concurrent regions then the StateOfMind would have a StatePath for each active concurrent state.

When searching for a transition handler for the given event we start at leaf state (Young) and search up the tree until we find a matching handler. In this case we find one on the 'Alive' state.

A single transition event will be emitted to all transition listeners which will indicate that an event was handled by the 'Young' state even though the transition handler was in the 'Alive' state.

A single event can result in multiple transition events if you are in a concurrent region or an 'onFork' transition is triggered.

A single StateOfMind will be added to the stream (StateMachine.stream) regardless of the number of transitions that occur when processing the event.

Last updated