If you want to have multiple state machines in a class, or 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!
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:
We want these classes to interact with each other using signals as trigger for state transitions:
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:
- Message queue for each state machine (contained by class executing state machines)
- Send Signal method (already mentioned here: Send Signal Action)
This method inserts sent signals into message queue(s) of receiving state machines
- Task or main loop executing the state machines forwarding singals from message queue to the state machines main function:
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.