State Machine Diagrams: Reading Object Behaviour
State Machine Diagrams describe how one object's permitted behaviour depends on its current condition and the events it receives. They are useful when the same request should produce different responses at different points in an object's life: unlocking an already unlocked door, for example, need not behave like unlocking a locked door.
Identify the active state
A state represents a condition or situation during an object's life in which it satisfies some condition, performs behaviour, or waits for events. Choose names that describe meaningful situations, such as Locked and Unlocked. Here, the diagrams have one region and no nested states, so one ordinary state is active at a time while the object awaits events.
In UML, rounded rectangles represent states. A filled circle marks the initial starting point; its outgoing arrow selects the first state. That initial arrow has no event trigger or guard. A final state, drawn as a filled circle inside another circle, marks completion of its region. It does not mean that the object has been deleted.
Read a transition label
An arrow represents a possible transition. Its label can contain three optional parts:
The trigger identifies the relevant event. A guard is a Boolean condition that must be true for that transition to be taken. The effect describes what happens when the transition executes.
Read unlock [valid] / release() as follows: while Locked is active, an unlock event can take the transition if valid is true; taking it executes release() and enters Unlocked. A true guard does not replace the required triggering event. Becoming valid without receiving unlock does not take this transition.
Evaluate the guard using the values available when the event is processed, before executing the transition's effect. An effect that changes a variable cannot retroactively make its own guard true. A label without an explicit trigger is also not shorthand for any event; UML includes completion transitions associated with completion of state behaviour.
Distinguish leaving from responding internally
For an external transition between simple states, any source exit behaviour executes first, followed by the transition effect and then the target entry behaviour. This order matters when these behaviours update counters, start timers, or release resources.
Response | Does the state exit? | Does entry behaviour execute? |
|---|---|---|
External transition to a different state | Yes | Yes, for the target |
External self-transition back to the same state | Yes | Yes, again for that state |
Internal transition written inside a state | No | No |
An external self-transition is drawn as an arrow leaving and returning to the same state. An internal response is written inside the state's behaviour compartment using the event and effect. Both can leave the same state name active afterward, but the exit and entry behaviours distinguish their consequences.
Trace events and check the model
To trace behaviour, record the active state and relevant variable values before each event. Inspect only applicable outgoing transitions and internal responses. Test their guards, execute the selected response, and record the new state and values before processing the next event.
For the simple machines used here, events are processed one at a time, with no deferred events. If an event enables no response, it is discarded without changing the state or variables. A failed guard does not create an automatic retry later.
Check boundary values as carefully as typical values. Also decide whether missing responses intentionally discard events or reveal incomplete requirements. A diagram's readability depends on those decisions, not just on drawing every possible arrow.
What to remember
- Start from the active state and the dispatched event.
- Check guards before executing effects.
- External self-transitions repeat exit and entry behaviour.
- Trace state and variable values together.
Example: Tracing an Account Lockout Controller
Example: An authentication service uses a lockout controller for one account. An event trace is an ordered sequence of event occurrences whose effects you follow through the machine. This controller starts in Monitoring with the nonnegative integer counter f=0.
In Monitoring, reject with f≤1 is an internal response that increments f. A reject event with f=2 instead increments f and enters Blocked. An accept event in Monitoring resets f to zero internally. In Blocked, reset returns to Monitoring and sets f=0.
The diagram gives the complete behaviour. Events are processed individually, there are no deferred events, and an event that enables no response is discarded without changing anything. No entry or exit behaviours modify f.
Process this sequence:
- reject
- reject
- accept
- reject
- reject
- reject
- reject
- reset
- reject
a) Determine the active state and counter after the first three events.
The first reject is processed in Monitoring with f=0. The internal guard is true, so the counter becomes ________. Monitoring remains active because the response is internal.
The second reject also satisfies the internal guard, giving a counter of ________. It does not enter Blocked: evaluate the guard before incrementing the counter. Before this event, f was only 1.
The third event is accept. Its internal effect resets the counter:
After the first three events, the controller is Monitoring with f = 0.
b) Determine the active state and counter after event 7, explaining whether that event increments the counter.
Start from Monitoring with f=0. Event 4 increments the counter to 1. Event 5 increments it to ________, still using the internal response because its pre-event value was 1.
At event 6, the pre-event value is now 2. The external transition's guard is true, so its effect gives:
The controller then enters Blocked. Event 7 is another reject, but Blocked has no response to reject. This is a discarded event: an occurrence removed without executing a response. It does not increment f and does not leave Blocked.
After event 7, the controller is Blocked with f = 3.
c) Determine the final active state and counter, and count how many increment effects execute over the full trace.
Event 8 is reset in Blocked. Its external transition sets the counter to ________ and returns the controller to Monitoring. Event 9 is reject, so the internal guard is true and the effect changes f from 0 to 1.
Count increments by executed responses, rather than by the final counter. Events 1 and 2 contribute two increments. Events 4, 5, and 6 contribute three more, and event 9 contributes one:
Event 7 contributes none because it was discarded. The accept and reset events clear the counter, so the final value does not equal the total number of increments.
The final result is Monitoring with f = 1, and 6 increment effects executed.
A pseudostate is a temporary control vertex that helps route transitions rather than represent a lasting condition of an object. You are reviewing elements in a printer controller's state diagram. Match each element to its role. Assume that a previous configuration has been recorded wherever history is used.
Restores the last active direct substate, with normal default entry inside any restored composite substate.
Provides the starting point of the default entry path into its enclosing region.
Marks completion of its enclosing region without necessarily ending the entire machine.
Selects an outgoing branch by evaluating guards when the incoming path reaches this vertex.
Choice pseudostate
Initial pseudostate
Shallow history pseudostate
Final state
A file-upload controller starts in Ready with b=0 MB. Its complete behaviour is shown below. In Uploading, chunk(k) is an internal response that adds the payload k, measured in MB, to b. The finish transition has no effect on b.
Events are processed one at a time. An event that enables no response is discarded; there are no deferred events or other automatic transitions. Complete the table after processing each event in order. Enter the state name and the exact integer upload amount in MB.
| Event processed | Active state afterward | Upload amount (MB) |
| start | ||
| chunk(5) | ||
| finish | ||
| chunk(7) | ||
| finish |