Nested state diagrams and concurrency
Notes on nested (composite) state diagrams and concurrency (orthogonal regions, history, entry/exit actions, run-to-completion) for UML-style state machines.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
State machines are used to model reactive software (user interfaces, protocols, embedded controllers). Nested (composite) states and concurrency let an engineer capture hierarchy and independent behaviours cleanly: nested states reduce repetition, and orthogonal regions model components that run in parallel. Clear state models reduce bugs in design and make implementation and testing easier.
Key ideas
- Define event: an occurrence (external or internal) that may trigger transitions between states.
- Define transition: a directed relationship between two states that is taken when its trigger event occurs and its guard condition is true; a transition can perform actions.
- Define composite (nested) state: a state that contains other states (its substates). A composite state allows grouping related substates and sharing entry/exit actions and transitions.
- Exact statement: A composite state S is a state whose behavior is described by a contained state machine; entering S means entering its initial substate (unless a history pseudostate directs otherwise).
- Assumption: substates in a composite are mutually exclusive unless further partitioned into orthogonal regions.
- Define orthogonal region (concurrency): a partition inside a composite state into two or more regions that are active simultaneously; the overall active state is the Cartesian product of active substates, one from each region.
- Exact statement: If a composite state C contains n orthogonal regions R1..Rn, and region Ri has mi possible active substates (leaf states), then the number of possible combined configurations while C is active is m1·m2·...·mn (product), assuming no additional constraints.
- Define history pseudostates: special nodes that let a composite state remember previous active substate when re-entered.
- Shallow history (H): remembers the last active substate in the immediate regions of that composite (not recursively inside nested composites).
- Deep history (H)*: remembers the last active configuration recursively inside nested composites.
- Define entry / exit actions: actions that run on entering or exiting a state. Entry actions run when a state becomes active; exit actions run when a state is left.
- Define do / activity action: an action that runs while a state is active, often interruptible by transitions.
- Define guard: a boolean condition
[guard]that must be true for a transition to be taken when its trigger occurs. - Define internal, local and external transitions (UML terms):
- External transition: default, exits source state up to the least common ancestor (LCA) then enters target state, executing exit/entry actions accordingly.
- Local transition: does not exit the source composite if the target is within the same composite — it avoids exiting and re-entering the composite; implementation differs by tools; mention in answers when you assume local semantics.
- Internal transition: handled inside a state without exiting the state; entry/exit actions of the state are not executed.
- Define run-to-completion semantics: when an event is dispatched to a state machine, the resulting reactions (transitions and their actions) are processed to completion before the next event is handled (so the machine reaches a stable configuration between events). This affects ordering and atomicity assumptions in concurrent regions.
What to draw for exam diagrams (precise instructions):
- To draw a simple composite state: draw a rounded rectangle for the composite. Inside it draw its substates as smaller rounded rectangles. Mark the initial pseudostate as a filled black circle at top-left inside the composite, with an arrow to the initial substate. If you want history, draw a small circle with an H (for shallow) or H* (for deep) placed inside the appropriate region (near the boundary is fine); draw an incoming arrow from outside to that H when needed, and include a default transition from H (or H*) to the default substate to use when no history exists.
- To show orthogonal regions: draw the composite rounded rectangle and divide the interior into vertical (or horizontal) compartments by dashed lines; label each compartment as Region1, Region2, etc. Put states inside each compartment. Place one initial pseudostate inside each region; when the composite is entered, each region takes its own initial transition to its initial state.
- To show transitions: draw arrows between states, label them
event [guard] / action. For entry/exit actions writeentry / actionandexit / actioninside the state box. To draw a final state: draw a bull’s-eye (a filled black circle inside an unfilled circle). In a composite with orthogonal regions, the composite completes when all regions have reached a final state.
Semantics (exact behaviors often examined):
- Entry semantics: when an external transition enters a composite state, enter the composite, then follow its initial pseudostates for all regions (or history if used) until reaching leaf states; run entry actions on the way in, from outer to inner.
- Exit semantics: on leaving a composite (external transition), exit nested active states from inner to outer, running exit actions in that order. Transition effect actions execute after all required exits and before any entries.
- Concurrency semantics: each orthogonal region executes its own state machine; the global active configuration is the set of active leaf states. Transitions in different regions may be triggered by the same event and may execute as part of the same run-to-completion cycle; ordering is model/tool dependent — be explicit in answers.
Assumptions to state in exam answers: whether your model uses UML semantics exactly (e.g., run-to-completion), whether transitions are atomic, and whether local transitions are supported.
Formulas
stateName — a state label (leaf or composite). Use one word or CamelCase.
entry / action — action executed when state is entered.
exit / action — action executed when state is exited.
do / activity — activity run while state is active.
event [guard] / action — transition label: event triggers it, optional guard boolean in brackets, action after slash.
CompositeState { Region1 | Region2 } — draw composite with vertical or horizontal compartments for orthogonal regions.
initial pseudostate — filled black circle; place one inside each region and connect it to that region’s initial state.
history (H or H*) — shallow (H) or deep (H*) history pseudo-state inside the relevant region of a composite.
Worked examples
Example 1 — Nested composite state with history (media player):
Problem statement (what to draw): Draw a state machine for a MediaPlayer with top-level states Off and On. When On, it has a composite with substates Stopped, Playing, and Paused. Off<->On transitions toggle power. When powering On, if the player was previously in Playing or Paused, it should return to that substate; otherwise go to Stopped. Include entry and exit actions: On has entry / lightOn() and exit / lightOff(). Use a shallow history to remember the last substate of On.
Answer (diagram instructions): Draw two top-level rounded boxes labelled Off and On. Put a filled circle (initial) with arrow to Off. From Off to On draw transition powerOn / start(); from On to Off powerOff / stop(). Make On a composite: inside On draw rounded rectangle with three small states Stopped, Playing, Paused. Place an H (shallow history) in the composite edge labelled H. On entry from Off use transition to H: label incoming arrow to On as powerOn / start() targeting the H pseudo-state (arrow enters the H). If history empty, H defaults to initial: draw an initial circle inside composite with arrow to Stopped.
Step-by-step trace (numbered):
- Initial: Off is active. (State configuration = {Off})
- Event
powerOnoccurs. Transition Off->On is taken; execute Off exit actions (none), then transition actionstart(), then On entry actionlightOn(). Because the transition targets H, check history: history empty -> follow initial to Stopped. Active configuration = {On, Stopped}. - Event
playoccurs (transition inside composite: Stopped -> Playingplay / startPlayback()): exit Stopped, then transition actionstartPlayback(), then enter Playing. Active = {On, Playing}. - Event
powerOffoccurs. Transition On->OffpowerOff / stop()executes: exit Playing, then On (run exit actionslightOff()), then transition actionstop(), then enter Off. Save shallow history for On = Playing. - Later
powerOn: On entry targets H; H remembers Playing, so re-enter Playing directly (not Stopped). Final answer: show states, transitions and label H; explanation of history behavior.
Example 2 — Concurrency (orthogonal regions) with counting configurations (phone example): Problem: Model a Phone device with a composite state Active that has two orthogonal regions: ScreenRegion (ScreenOff, ScreenOn) and CallRegion (Idle, Dialing, InCall). Show how many possible combined active configurations exist when Active is entered. Also trace events: from initial (PhoneOff) turn on power, then an incoming call arrives while screen is off — show which region transitions. Diagram instructions: Draw composite Active as rounded rectangle divided vertically into two compartments. Left compartment label ScreenRegion and put ScreenOff and ScreenOn. Right label CallRegion with Idle, Dialing, InCall. Place an initial pseudostate inside each region, with a transition to that region’s initial state. Counting configurations: ScreenRegion has 2 leaf states, CallRegion has 3 leaf states, so total = 2·3 = 6 combined configurations (product). Show list: (ScreenOff,Idle), (ScreenOff,Dialing), (ScreenOff,InCall), (ScreenOn,Idle), (ScreenOn,Dialing), (ScreenOn,InCall). Trace steps:
- Start: PhoneOff active.
powerOnevent: transition to Active; entry actions: enter Active then initial of both regions. Active configuration becomes (ScreenOff, Idle).incomingCallevent: target transition in CallRegion Idle->InCall with actionring(). Because regions are orthogonal, only CallRegion changes; ScreenRegion remains ScreenOff. Now configuration (ScreenOff, InCall).pressScreenButtonevent: transition ScreenOff->ScreenOn; now (ScreenOn, InCall). Both transitions are independent and may be triggered by different events. Explain that processing of each external event happens under run-to-completion semantics — the machine completes reaction to one event (which can include triggers in several regions) before taking the next event.
Example 3 — Deep history (short): Problem: Composite A contains composite B which contains states b1 and b2. Use deep history so re-entering A restores b2 if that was the active leaf. Answer: Draw A containing B. Place H* in A. Show transitions that enter A target H*. Explain that H* restores the full nested active configuration recursively.
Common mistakes
- Confusing shallow and deep history: shallow remembers only immediate substates, not recursive nested substates.
- Counting active configurations incorrectly for orthogonal regions (adding instead of multiplying counts).
- Drawing orthogonal regions with solid lines or wrong initial pseudostate placement — always use dashed separators and place one initial pseudostate inside each region (each initial has exactly one outgoing transition).
- Assuming transitions in different regions are fully independent in implementation — concurrency semantics and ordering may be tool-specific; always state assumptions.
- Omitting guards or writing invalid syntax for transitions (forgetting slash before action or square brackets around guards).
For your exam
Typical question types:
- Definition: define composite state, orthogonal region, history, entry/exit actions.
- Short note: explain run-to-completion or compare shallow vs deep history.
- Diagram question: Draw a state diagram for a given problem, using nested states and concurrency, mark initial/final states, entry/exit actions and history. Include labels
event [guard] / action. - Derivation/problem: compute number of global configurations for a composite with orthogonal regions; or trace execution for a sequence of events and show state configuration after each event.
- Explanation: explain the order of execution for entry/exit actions and how transitions that cross composite boundaries are taken. What a full-marks answer includes:
- Clear definitions using bolded terms and exact semantics (e.g., how history works, how entry/exit order is applied).
- A correct, well-labelled diagram when required (initial/final pseudostates, history, dashed lines for orthogonal regions, transitions labeled correctly).
- If asked to trace, list configurations step-by-step, show which transitions fire, and show entry/exit/action execution order.
- Any assumptions clearly stated (UML run-to-completion semantics; whether local transitions used).
Quick check
- What symbol do you draw for the initial pseudostate inside a composite?
- If a composite has two orthogonal regions with 3 and 4 leaf states respectively, how many combined configurations exist while composite is active?
- What is the difference between shallow history (H) and deep history (H*)? Answers: 1) a filled black circle. 2) 3·4 = 12 combined configurations. 3) Shallow H remembers only the immediate substate(s) of that composite; deep H* remembers the full nested configuration recursively.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?