The State Machine
We add a new state DoorLocked and the new messages
Lock and Unlock:
Notice the diamond node in the state diagram — this represents a
conditional transition where the outcome depends on runtime data: The
unlock operation can succeed (transitioning to DoorClosed)
if the condition PinCorrect is met - or fail (staying in
DoorLocked) when the condition PinIncorrect is
met.
In the transition table the conditional transitions are expressed by the new “Guard” column. For most transitions however, this column is empty — these are unconditional transitions that always succeed.
| State | Message | Guard | State | |||
|---|---|---|---|---|---|---|
| DoorOpen | ⟶ | Close | ⟶ | DoorClosed | ||
| DoorClosed | ⟶ | Open | ⟶ | DoorOpen | ||
| DoorClosed | ⟶ | Lock | ⟶ | DoorLocked | ||
| DoorLocked | ⟶ | Unlock | ? | PinIncorrect | ⟶ | DoorLocked |
| DoorLocked | ⟶ | Unlock | ? | PinCorrect | ⟶ | DoorClosed |
Guard labels are not strictly required. Transitions can have multiple target states without explicit labels - like in the Countdown example. But the labels can be very useful to make the code and the state diagram more readable.