Skip to content

Multiple State Machines

If you want to have multiple state machines in a class, if you want more than one instance of a state machine in a class, or if you simply want to define the name of the state machine instance yourself, you can model these relationships by using a composition between the state machine and the class.

In the source role of the composition, you can define the name of the instance the state machine will get.

State machines modeled by composition will automatically be initialized when the class initialization method is called.


Please make sure that the name is unique in the class, and that it doesn't clash with attributes. If you use this method, no implicit state machine instances will be created!

Example Model

Multiple State Machine

Interaction between multiple State Machines

If you want 2 or more state machines to send and receive signals to / from each other, we have prepared one possible solution described below.

Let's assume we have 2 classes CarTrafficLight and PedestrianTrafficLight called by main loop:

Multiple State Machine

We want these classes to interact with each other using signals as trigger for state transitions:

Multiple State Machine Multiple State Machine

CarTrafficSM is time triggered, while PedestrianSM is signal triggered and reacts on state transitions of CarTrafficSM based on signals carGo and carStop.

To realize this interaction, you have to take care of the following components:

  1. Message queue for each state machine (contained by class executing state machines)
  2. Send Signal method
    This method inserts sent signals into message queue(s) of receiving state machines
  3. Task or main loop executing the state machines forwarding signals from message queue to the state machines main function:

Multiple State Machine

You can find a more detailed description and the example model here: Examples

This example is only one simple way of implementing interaction between state machines!

It needs to be modified (additional routing method) for each additional state machine interacting with the ones included in the example though.