Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

State modelling: events, states and transitions

State modelling (events, states, transitions) explains how systems change behaviour over time using states, events (triggers), transitions, guards and actions; useful for designing controllers, protocols and UI flows.

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 modelling is how we describe the behaviour of objects and systems that change over time. Engineers use state models when designing control logic (embedded controllers, protocol handlers, UI screens) to ensure correct responses to events, and to find missing cases or unwanted behaviours before coding.

Key ideas

(We define each term before using it and use plain English.)

  • State — a condition or situation in which an object or system may exist, described by the values of its attributes and relevant environment at a point in time. Example: a turnstile can be in state Locked or Unlocked. Draw as a rounded rectangle labelled with the state name.

  • Event (also called trigger) — an instantaneous occurrence that may cause the system to change state. Events come from outside (user push) or inside (timer expiry). Do not confuse with conditions that are continuously true; those are evaluated as guards. Write events as the first part of a transition label. UML also defines time events (e.g., after(5s), at(t0+10s)) and change events (when(condition)), and allows events to be deferred in a state by listing them under defer to be postponed until the state is exited.

  • Transition — a directed relationship from a source state to a target state that describes what happens when an event occurs and optional conditions are satisfied. Draw as an arrow from source state to target state. A transition label commonly has the form: event [guard] / action.

  • Guard — a Boolean condition that must be true for the transition to be taken when the event occurs. Write guards in square brackets after an event: [money >= price].

  • Action — an operation executed when a transition fires (often side-effects like setting variables, producing output). Actions appear after a slash: / return_ticket().

  • Internal transition — a transition that does not leave the state but responds to an event by performing an action. In UML 2.x, show it inside the state's internal transitions compartment (as a line labeled event [guard] / action inside the state); do not draw it as a self-loop arrow because self-transitions exit and re-enter the state.

  • Entry action and Exit action — operations executed on entering or leaving a state respectively. Notated as entry / ... and exit / ... inside the state box in UML. Also, a do activity is long-running behavior executed while the state is active, notated as do / ....

  • Initial pseudostate — a black filled circle that marks where execution starts inside a state diagram or composite state.

  • Final state — a concentric circle (bullseye) that marks completion for that region. A terminate pseudostate (black circle with an X) ends the entire state machine. History pseudostates remember the last active substate of a composite state: shallow history H (single encircled H) and deep history H* (encircled H*), used to resume on re-entry.

  • Composite (nested) state — a state that contains a sub-state machine (substates) to model internal behaviour. Use nested rounded rectangles.

  • Orthogonal (concurrent) regions — independent submachines inside a composite state separated by dashed lines; they run concurrently.

  • Run-to-completion (RTC) semantics — (common assumption for UML state machines) when an event is handled, the triggered transition and all resulting entry/exit actions complete before the next external event is taken. This prevents interleaving of two events while handling one. Exact RTC rules vary by tool/standard; check your tool's version.

Exact definitions and exam-style statements:

  • Deterministic transition rule (formal): "A state machine is deterministic iff, for every active state and for every event occurrence e (including completion events), at most one transition from that state with a trigger matching e is enabled when evaluated with its guards." (Assumption: guards are side-effect-free boolean expressions evaluated instantaneously.)

  • Enabled transition: "A transition t with trigger e is enabled in state S if the trigger e occurs and the guard of t evaluates to true in S." (Assumption: guard evaluation uses current attribute values.)

  • Reachability: "A state T is reachable from S if there exists a finite sequence of events and transitions that leads from S to T." This is used to check dead states.

How to derive a state diagram (step-by-step method universities ask for):

  1. List the possible observable modes of the object as candidate states.
  2. List events (external inputs, timers, internal completions) that can cause change.
  3. For each event in each state, decide whether the system stays (maybe with internal action) or moves to another state — create a transition.
  4. For transitions that depend on data, add guards (Boolean conditions). Reduce duplicated transitions by adding actions or parameters.
  5. Add entry/exit actions for actions that must always run when entering or leaving a state.
  6. Add initial and final pseudostates and check that every event in every state is considered (or explicitly ignored).
  7. Verify determinism: no two outgoing transitions from the same state have the same trigger with guards that can be true at the same time.

What to draw (exact diagram instructions):

  • Draw an initial black circle. Draw a solid arrow from it to the initial state.
  • Draw states as rounded rectangles. Label clearly.
  • Draw transitions as solid arrows. Label each arrow with event [guard] / action (omit parts that do not apply).
  • For final state draw a circled bullseye. If composite states are used, draw a rounded rectangle containing nested states; show a small black circle for that region's initial.
  • For concurrency, divide the composite state with dashed vertical lines into regions.

Note: UML toolkits and professors expect labels in the exact order event → guard → action and expect the initial pseudostate and the final state to be present where indicated. Run-to-completion (UML 2.x): one event is taken from the queue and processed to completion (including any resulting internal microsteps and completion events) before the next external event is dequeued; effects from two events do not interleave.

Formulas (syntax to remember)

event [guard] / action — transition label. event is the trigger name, guard is a Boolean in [...], action is executed when transition fires. Examples: time event after(5s), change event when(x>y).

entry / action — action executed when entering a state.

exit / action — action executed when leaving a state.

state1 --> state2 : event [guard] / action — textual description showing source, target, and label.

initial (black circle) — shows start point in the diagram.

final (bullseye) — shows completion.

state { region1 || region2 } — indicates orthogonal regions (concurrency) inside a composite state.

(inside state) event [guard] / action — denotes an internal transition (no exit/entry).

Worked examples

Example 1 — Turnstile (simple exam-style model) Problem: Model a turnstile that starts Locked. Events: coin (inserts a coin), push (user tries to push). When Locked + coin → Unlocked. When Unlocked + push → Locked. coin in Unlocked returns coin as action. push in Locked does nothing (ignored). Answer (step-by-step):

  1. States: Locked, Unlocked.
  2. Initial state: Locked (draw initial filled circle → Locked).
  3. Transitions:
    • Locked --> Unlocked : coin / unlock()
    • Unlocked --> Locked : push / lock()
    • Unlocked --> Unlocked : coin / return_coin() (or model as internal transition)
  4. Final not required. Diagram instructions: draw two rounded rectangles labelled Locked and Unlocked. Draw arrows as above. Label actions after slash. Result: This diagram shows deterministic behaviour: from each state, events lead to at most one transition.

Example 2 — Vending machine (guards and actions, numerical values) Problem: A simple vending machine sells an item for Rs 15. It tracks money inserted. Events: insert(amount), select, cancel. Model states and transitions. Answer (step-by-step):

  1. States: Idle (money=0), Accepting (money>0), Dispensing, ChangeReturn.
  2. Initial: Idle.
  3. Transitions (sample set):
    • Idle --> Accepting : insert(x) / money += x
    • Accepting --> Accepting : insert(x) / money += x
    • Accepting --> Dispensing : select [money >= 15] / dispense_item(); money -= 15
    • Dispensing --> ChangeReturn : [money > 0] / return_change()
    • Dispensing --> Idle : [money == 0]
    • ChangeReturn --> Idle : / money = 0 (finalise)
    • Accepting --> Idle : cancel / return_money(); money = 0
  4. If money < 15 and select occurs, stay in Accepting and show message: Accepting --> Accepting : select [money < 15] / show "insert more". Units: money in Indian Rupees (INR) — used as model data, not a physical SI unit. Notes: Guards [money >= 15] prevent dispensing until sufficient money is in. This is a textbook exam example.

Example 3 — Washing machine with nested states (short) Problem: Washing machine has a high-level state Running with substates: Filling, Washing, Spinning. When running completes, goes to Done. Answer (steps):

  1. High-level states: Idle, Running (composite), Done.
  2. Running contains initial pseudostate → Filling -> Washing -> Spinning -> final state.
  3. Transitions: from Idle on start -> Running (entry action start_timer()), then internal sequence Filling --(fill_complete)--> Washing --(wash_complete)--> Spinning --(spin_complete)--> [final state of Running].
  4. On stop in any substate, transition to Idle with exit actions to stop motors. Diagram instructions: draw a composite state Running as a big rounded rectangle, draw the three substates inside with their transitions and an initial black circle entering Filling. Draw an outgoing transition from the composite state to Done when submachine completion occurs.

Common mistakes

  • Forgetting initial pseudostate or final state in composite regions — leads to incomplete diagrams.
  • Creating nondeterminism: two outgoing transitions with same event and overlapping guards. Always check guard overlap.
  • Treating internal transitions as normal transitions that cause exit and re-entry; internal transitions do not perform exit/entry actions.
  • Drawing orthogonal regions without stating how they synchronise (if needed). Assume concurrency unless specified otherwise.
  • Using the wrong label order (professors expect event [guard] / action).

For your exam

Kinds of questions:

  • Definitions: define state, event, transition, guard, action, composite and orthogonal states. Include diagrams.
  • Short notes: explain run-to-completion semantics, internal transitions, entry/exit actions.
  • Diagram questions: draw state diagrams from a problem statement (turnstile, vending machine, protocol). Diagrams must show initial state, states, transitions with labels in event [guard] / action form, and final state if relevant.
  • Derivations/algorithms: convert a state table to a state diagram or prove determinism/not-deterministic.
  • Design questions: extend a given state machine with nested states or concurrency and explain reasoning.

What a full-marks answer includes:

  1. Clear list of states and events (define each term used).
  2. Initial and final markings where applicable.
  3. All transitions drawn with correct labels event [guard] / action.
  4. Entry/exit actions for states if required.
  5. Note on determinism and reachability: state which states are unreachable or where nondeterminism exists and how to fix it.
  6. Short justification for composite or concurrent design choices.

Quick check

  1. What is a guard?
  2. How do you show an initial state on a UML state diagram?
  3. Give the transition label format and explain each part.

Answers: A guard is a Boolean condition required for a transition; initial state is shown as a filled black circle; label format is event [guard] / action where event is the trigger, guard is Boolean condition in brackets, and action is the operation executed when transition fires.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?