- REDIRECT Training:UML – State machines
State machines
You can use a state machine to define the allowed lifecycle states and transitions for an object in your MDriven model.
What a state machine defines
A state machine describes the dynamics of an object: the states it may be in and the rules for moving between those states. The class model defines the static structure; a state machine defines the allowed behaviour over time.
For example, a Car can move from a state such as BrandNew to InOwnershipTransaction and then to OwnershipTable. Each state is shown as a box in the state diagram.
A state machine creates a state attribute on its class. The state diagram defines the only allowed string values for that attribute and the transitions between them.
| Term | Meaning | Example |
|---|---|---|
| State | An allowed condition of an object. | A Car is BrandNew.
|
| Transition | A defined path from one state to another. | A transition moves a Car from BrandNew to InOwnershipTransaction.
|
| Trigger | A method on the class that performs a transition and its associated actions. | InitiateSale moves a car into an ownership transaction.
|
| Guard | A rule that must be fulfilled before a transition is allowed. | A car cannot start production until its deposit is paid. |
Create the state machine
- In MDriven Designer, right-click the class and select Add State machine.
- Add the states that represent the allowed lifecycle values.
- Add transitions between the states.
- Name each transition with the trigger that moves the object between the states.
- Add a guard when a transition requires a business rule.
For a House, you can model Plan, Construction, Maintenance, and Demolition. A StartConstruction trigger can move the house from planning into construction. Construction can contain a region with substates such as GroundWork and Building.
Control transitions with guards
A guard uses OCL (Object Constraint Language) to evaluate model data before the transition occurs. OCL navigates the model using its domain names, so the rule remains close to the business requirement.
For example, a transition can require both a paid deposit and a selected brand:
self.DepositPaid and self.TheBrandOfTheCar->notEmpty
You can also define entry actions when an object enters a state and exit actions when it leaves a state.
Use more than one state perspective
A class can have multiple state attributes when the object has separate lifecycles from different perspectives. For example, a Car can have a LifeTimeState and a CleanlinessState.
Each state must have a unique name within its class. This lets you ask whether an object is in a named state regardless of which state attribute holds that state, for example self.oclIsInState(#SomeState).
Re-evaluate guard-driven transitions
Use stateMachineTrigger when you want an object to re-evaluate transitions without named triggers. The expression self.stateMachineTrigger checks the guards from the current state and moves the object if a transition is possible.
