UML State Diagrams
UML State Diagrams Describe how an object’s state changes in response to external events Similar to State Transition Diagrams / FSMs Nodes are states Arrows are events/actions/stimulus Ideal for describing behavior of a single object Sequences of states that an object goes through Events that cause a transition from one state to another Actions that result from a state change
State Diagram Key Terms Transition Activity entry, exit, do, entry Action Event State – The current status of an object based upon the total set of events it has responded to so far. Transition – Associations between states. Transitions are fired (taken) based upon events that the object responds to. Activity – Nonatomic sequences of actions that have some duration. (assumed to take time) Action – Atomic executable statements that invoke an operation. (assumed to happen almost instantly) Entry Action – Always performed on entering a state. Exit Action – Always performed when the state is exited. Do Action – Performed continuously as long as the object is in the state Include Action - Invoke the states internal state machine (Do not reference operations). Event – Occurrences that may trigger an object to change states.
State Machine Syntax Name Normal State Activities Start/Initial State Stop/Accepting/Final State Transition EventName [Guard] / Action
Actions vs Activities Actions are associated with transitions, are considered to be processes that occur quickly and are not interruptible Activities are associated with states, can take longer, and can be interrupted by events “do” events can iterate “entry” events happen only on entry to state
Basic State Diagram: Order Entry [Not all items checked] / getNextItem [All items checked && All items available] dispatching checking do/initiate delivery do/check item Item Received [All items available] [All items checked && Some items not in stock] shipped waiting delivered Item Received [Some items not In stock] cancelled cancelled cancelled cancelled
State Diagram with Superstates Active [Not all items checked] / getNextItem [All items checked && All items available] dispatching checking do/initiate delivery do/check item [All items checked && Some items not in stock] waiting Item Received [All items available] Item Received [Some items not In stock] shipped cancelled cancelled delivered
Order Entry With Concurrency Ordering waiting checking shipping authorizing authorized
State Diagram Exercise Draw a statechart diagram modeling a vacuum cleaner robot’s behavior. If activated, it starts vacuuming offices, afterwards the canteen, and finally the laboratory. If it finishes cleaning the laboratory, it starts again in the offices. If deactivated, it returns to its locker and stops there. Every time on changing its operational area, it empties its dust bin into some container. If it runs low on energy, it goes to a recharging station and recharges its batteries. Afterwards it continues the cleaning where it has stopped.
State Diagram Exercise (1) These arrows indicate that when the superstate is entered, the first state reached is “Clean Offices” Problem: when you go to “Charge Robot”, how do you know where to return to?
Encoding “History” Information In Statecharts, sometimes, you’d like to remember “history”, such as when you want to return to a specific state within a substate/superstate. One way to do this is to set/check a variable, and UML lets you do this within activities. But you’d have to do this in each state and add proper transitions. An alternative is to use a “history state”. In such a state, your first entry goes to the state denoted, inside the substate/superstate, with an arrow. On any subsequent entry it is assumed that you return to the state from which you last left.
State Diagram Exercise (2) The “H” indicates that this is a “history” superstate
Basic State Diagram: Order Entry Dial(n) [valid && incomplete] DialTone Dialing Caller lifts handset do/play dial tone do/dial Idle Dial(n) [invalid] Dial(n) [complete] / connect Invalid Connected do/play message busy connected Busy Ringing do/play busy tone do/play ring tone Caller replaces handset
Questions What does the positioning of the “Idle” state and the edges into and out of it indicate? Provide an interpretation of the meaning of and actions associated with the “Dialing” state and the edges out of it. Modify the diagram to indicate that, when the system is in the Connecting state and a busy signal is detected, if the callee has an answering service, the call is directed to the service instead of to a busy tone.