SUB STATE MACHINES
Submachine states and sub state machines are an efficient way to capture common design patterns or fragments for reuse both within a design and across different designs. This page briefly describes the submachine state and the sub state machine features.
FEATURES
The idea with sub state machines is that you can create a state machine once and then reuse it in every place where a similar state and transition structure is needed, even if it's a different project. When you need to instantiate such a state machine you draw a special submachine state and associate the state with the sub state machine. This can be all that's needed to reuse an arbitrarily complex state machine; but you can just as well design a reusable state machine where things like event names, action function names, variable names etc can be associated with entities that match the point of use.
HOW TO CREATE A SUB STATE MACHINE
There are three ways to create a reusable state machine:- Design a normal hierarchic state with the behavior you need as part of another state hierarchy. When done, right-click on the top of the state and select Convert to submachine state. The state will then be converted to
the special submachine state and the content of the original state will be used to create a new free-standing state machine as part of the project, visible in the project browser window.
The submachine state and the state machine are associated.
Any transitions crossing the state boundary of the topmost state in the converted state machine will be converted to transitions going to and from entry and exit connection point references inside the state machine. Corresponding connection points will be created on the submachine state boundary for the part of the transitions that are visible on the enclosing state chart. - Right-click in the project browser window and select Add New State Machine. This will add a new sub state machine that is not yet associated with any submachine state in the design.
- Add an existing sub state machine file to the project.
The created sub state machine can be edited in the sam way as any other part of the design. Note that transitions that need to go out of or in to the sub state machine should be drawn to connection points. Corresponding connection point references will be automatically added on the submachine states associated with the sub state machine.
Look at the picture above. It depicts a simple state machine that utilizes the same sub state machine in two submachine states. The sub state machine is a simple counter that
counts an event and on hitting a limit it emits a signal that can trigger a transition in the state machine instantiating the sub state machine. The sub state machine is designed so that the event,
the counter variable and the counter limit constant are all declared so that they can be bound to elements of the instantiating state machine.
One of the submachine states will count button events and the other will count timer events. Each state machine has a separare count limit and it's own counter variable, as specified by the bindings.
You can look at the picture below to examine the bindings for SubmachineState1.
If you want to be able to remap names of action functions, event names, variables names, signals or constants you must make sure the corresponding element is declared in the sub state machine and not defined. If it is defined in the sub state machine it is treated as a local name that is not supposed to be remapped and it will not show in the Bindings tab. All elements are automatically declared when converting an existing state to a sub state machine.
USING A SUB STATE MACHINE
It's straightforward to reuse an existing state machine:- Selct the Submachine state tool from the Diagram toolbar and draw the state as desired.
- Double-click the state to bring up the usual compose dialog.
- Select a state machine from the drop-down menu to associate with the submachine state.
- If you want to remap event names, action functions or similar, select the Bindings tab and do the appropriate changes.
- Draw transitions to and from any connection point references on the submachine state frame. Entry and exit connection points are a convenient way to specify transitions that go directly in or out of a state inside the sub state machine.