What is a State Diagram?
3 ุฃุณุงู ู ูููุณ ุงูู diagram: State Diagram = State Machine Diagram = Statechart.
ุงูู State diagram ุจููุตู ุงูู behavior ุจุชุงุน:
- A whole systemุ ุฃู
- Some part of a systemุ ุฃู
- An individual object (ุงูุฃูุชุฑ ุดููุนุงู).
๐ ุงูููุฑุฉ ุงูุฃุณุงุณูุฉ
- ูู ุฃู point in timeุ ุงููุธุงู /ุงูู object ุจูููู ูู state ู ุนูู.
- ูููู ูู state ูุนูู ุฃูู ููุชุตุฑู ุจุดูู ู ุญุฏุฏ ุฑุฏูุง ุนูู ุงูู events.
- ุจุนุถ ุงูู events ุจุชุณุจุจ change of state (transition).
- ูู ุงูู state ุงูุฌุฏูุฏุ ุงูู behavior ุจูููู ู ุฎุชูู.
State diagram = directed graph:
- Nodes = states (ุงูุญุงูุงุช)
- Arcs (edges) = transitions (ุงูุงูุชูุงูุงุช)
Basic Elements โ ุงูุฑู ูุฒ ุงูุฃุณุงุณูุฉ
5 ุฑู ูุฒ ูุงุฒู ุชุนุฑููู .
- State = a set of values for the attributes of the given object.
- ุงูู object ุจููุถู ูู ุงูู state ุฏู ูุญุฏ ู ุง event ูุญุตู ููุณุจุจ transition.
ู
ุซุงู: Traffic Light โ ุงูู attributes ู
ู
ูู ุชููู: color = "red", activeForSeconds = 0. ูู ุงูู color attribute ุงุชุบูุฑ ูู "green"ุ ูุจูู ุงูู state ุงุชุบูุฑ.
Transitions & Events
Transition = change of state. ูุงูู Event = ุงููู ุจูุณุจุจ ุงูู transition.
ุงูู Transition ุจูู ุซู ุชุบููุฑ ูู ุงูู state ุฑุฏูุง ุนูู event. ุงูู label ุนูู ุงูู transition ุจููุตู:
- ุงูู Event (ุงูุฃูู ).
- [Guard] (ุดุฑุท ุงุฎุชูุงุฑู).
- / Action (action ุงุฎุชูุงุฑู).
ู
ุซุงู: requestToRegister(student) [classSize < max] / createRegistration
3 Event Types โญ
External ยท Internal ยท Temporal. ุงูุฏูุชูุฑ ุจูุญุจ ูุณุฃู ุนู ุงููุฑู.
๐ External Event
= System Event
ุณุจุจู ุญุงุฌุฉ ุจุฑูู ุงูู system boundary.
ู ุซุงู: ุงูู Cashier ุจูุถุบุท ุฒุฑ enter item ุนูู POS terminal.
๐ง Internal Event
ุณุจุจู ุญุงุฌุฉ ุฌููู ุงูู system boundary.
Operation invoked via message ู ู object ุชุงูู ุฏุงุฎูู.
ู ุซุงู: Object ุจูุณุชุฏุนู method ุนูู object ุชุงูู โ ูููุชุฌ ุนูู event.
โฐ Temporal Event
ุณุจุจู occurrence of a specific date/time ุฃู passage of time.
ุงูู syntax: after(30s) ุฃู at(midnight).
ู ุซุงู: Red light โ Green light ุจุนุฏ 30s.
after(...) ููู duration ุฃู at(...) ูู specific time. ุดุงุฆุนุฉ ูู ุงูู state diagrams.Activities vs Actions โญ
ุงููุฑู ุงูู ูู : Action = instantaneous. ู Activity = takes time.
โก Action
ุงูู Action ุจูุญุตู ูู 3 ุญุงูุงุช:
- On transition โ ู ุน ุงูุงูุชูุงู ููุณู
- On entry โ ูู ุง ูุฏุฎู state ู ุนูู
- On exit โ ูู ุง ูุฎุฑุฌ ู ู state ู ุนูู
ุฃู
ุซูุฉ: readCard, ejectCard, startMotor, stopMotor
Syntax: entry / actionName
Or: exit / actionName
๐ Activity
ุงูู Activity ุจูุญุตู while in state:
- ุจูุงุฎุฏ ูุชุฑุฉ ุฒู ููุฉ ู ูุญูุธุฉ
- ู ู ูู ุงูู system ูุฎุฑุฌ ู ู ุงูู state ูู ุง ูุฎูุต ุงูู activity (completion)
- ุฃู ูุฎุฑุฌ ูู event ุขุฎุฑ ููุทุนูุง (interruption โ early exit)
ุฃู
ุซูุฉ: play music, play dial tone, download file
Syntax: do / activityName
- ุงูู
entry / readCard= action ุจุชุญุตู ู ุฑุฉ ูุงุญุฏุฉ ูู ุง ูุฏุฎู ุงูู state. - ุงูู
do / process transaction= activity ุจุชุดุชุบู ุทูู ู ุง ุฅุญูุง ูู ุงูู state. - ุงูู
exit / ejectCard= action ุจุชุญุตู ูู ุง ูุฎุฑุฌ ู ู ุงูู state.
Guards & Conditional Transitions
Guard = ุดุฑุท ุจูููุงู ูุงุฒู ูููู true ุนุดุงู ุงูู transition ุชุญุตู.
ุงูู guard ุจูุชูุชุจ ุจูู [ ]. ูู ุงูู event ุญุตู ุจุณ ุงูู guard ู
ุด trueุ ูุงูู transition ู
ุง ุจุชุญุตูุด.
ู ุซุงู: Course Registration
classSize โฅ minimum = trueุ ูุงูู transition ุจุชุญุตู. ูู falseุ ูุงูู state ู
ุง ูุชุบูุฑุด.
ุงูู State diagram ู ู ูู ูููู ููู transition ู ู state ูููุณูุง. ุฏู ู ููุฏ ูู ุง ูุญุชุงุฌ ูุนู ู action ุจุฏูู ุชุบููุฑ state.
ู
ุซุงู: requestToRegister(student) / createRegistration โ self-transition ุจู action. ุงูู state ููุถู OpenNotEnoughStudents ุจุณ createRegistration ุจูุชุนู
ู.
Nested / Composite States
State ุฌููู state. ู ููุฏ ูุชูุธูู ุงูู complexity ูู ุงูู diagram.
ุงูู Composite state ุจูุญุชูู ุนูู substates ุฏุงุฎูู. ุดููู ู ุณุชุทูู ุฃูุจุฑ ููู states ุฃุตุบุฑ ุฌููุงู.
ุงูููุงุฆุฏ:
- ุชูุธูู โ ุจุชููู ุนุฏุฏ ุงูู transitions ุงูุธุงูุฑุฉ ูู ุงูู diagram.
- ุงูู outer state ุจุชูุทุจู ุนูู ูู ุงูู substates (ู ุซูุงู: exit ู ู ุฃู substate = exit from composite).
- Easier reading and maintenance.
ู ุซุงู: ATM โ Serving Customer composite
ุงูู outer transitions (ู
ุซูุงู cancel) ุจุชุดุชุบู ู
ู ุฃู substate ุฏุงุฎู Serving Customer. ู
ููุด ุฏุงุนู ูุถูู cancel arrow ู
ู ูู substate ููุญุฏู.
cancel) ุฃู ุจุชูุชู
ู ูููุณ ุงูู logical phase (ู
ุซู phases of serving customer).5 Worked Examples
ุฃู ุซูุฉ ูุงู ูุฉ ู ู ุงูุณูุงูุฏุฒ: Tic-Tac-Toe ยท Traffic Light ยท Garage Door ยท Online Chess ยท ATM.
๐ฎ Tic-Tac-Toe Game
Transitions with guards:
XTurn โ XWin [XPlays & 3 X's in a row]XTurn โ Tie [XPlays & Board Full & Not 3 X's]OTurn โ OWin [OPlays & 3 O's]
๐ฆ Traffic Light (Temporal Events)
ูู ุงูู transitions ุจุชุณุชุฎุฏู
temporal events (after(...)). ู
ููุด external triggers.
๐ช Automatic Garage Door (Actions)
ูู state ุนูุฏู entry action ุจู stop motor ุฃู run motor. ุงูู pressButton ู ู ูู ููุงุทุน (interrupt) ุงูู opening ูููููู ูู closing.
โ๏ธ Online Chess (UI Screens)
Transitions: Welcome โ New Account [Create New Account], Welcome โ Standings [Login], Standings โ Play [Choose Opponent], Play โ Results [Finish Game], Results โ Standings [Continue].
ุฏู state diagram ูู UI flow โ ุจูุดูููุง ูู ุงูู apps.
๐ง ATM (Composite States)
ุฃูุซุฑ example ู ุนูุฏ. ููู composite state ููู Serving Customer.
Off โ Self Test [turn on / startup]Self Test โ Idle(success)Idle โ Serving Customer [cardInserted]- Inside Serving Customer (composite): Authenticate โ Selecting โ Transaction โ end
Serving Customer โ Idle [cancel]Idle โ Maintenance [service]Maintenance โ Out of Service [failure]Out of Service โ Off [turn off / shutDown]
Serving Customer composite ุจุชุญุชูู substates ููู authentication flow. ูู ุญุตู cancel ู
ู ุฃู substateุ ูุงูู ATM ูุฑุฌุน ูู Idle. ุฏู ุงุณุชุฎุฏุงู
ูุธูู ููู composite.Practice Quiz โ Lecture 8
8 ุฃุณุฆูุฉ ุนูู State Diagrams. Question 2 ู ู ูู ุงู ุชุญุงู ููู 2-4 marks ุนูู state diagram.
after(30s) = passage of time = Temporal event.
do / play music. This is:do / ูุฏู ุนูู activity (ongoing). ุงูู play music ุจูุงุฎุฏ ูุชุฑุฉ.
entry / actionName. ยท
C โ Exit action syntax: exit / actionName. ยท
D โ ุงูู Guards ุจุชููู ุจูู [ ].
buy(item) [funds >= price] / debitAccount, what does [funds >= price] represent?[ ] = guard (boolean expression). ูู true โ ุงูู transition ุชุญุตู.
buy(item) ูุจู ุงูู guard. ยท
B (action) โ debitAccount ุจุนุฏ ุงูู /. ยท
C โ ู
ุด state name.
ServingCustomer with substates inside, and a cancel transition from ServingCustomer to Idle. What does this mean?Cheat Sheet
ูู ุงูุฑู ูุฒ ูุงูู syntax ูุงูู ุตุทูุญุงุช.
๐จ Symbols
๐ Transition Label Syntax
โก Action vs Activity
๐ข 3 Event Types
๐ Diagram Differences
โ Question 2 Checklist (State part)
Rapid Revision
Flashcards ยท Common Mistakes ยท What the doctor loves.
๐จ Common Mistakes
do /). ุงูู do / ุจูุฏู ุฏุงูู
ุงู ุนูู activity.cancel ู
ู ุฃู state)ุ ุงุณุชุฎุฏู
composite. ุบูุฑ ูุฏู ุงูู diagram ููุจูู cluttered ุจุฃุณูู
ูุชูุฑุฉ.โญ What Dr. El-Ramly Loves
- Question 2 (2-4 marks) โ ุจูุฏู object ู ุซู Message, Order, Booking ููุทูุจ state diagram. ุฑูุฒ ุนูู Initial โ states โ Final + transitions ู ุน events.
- Message states (Winter 2025): Received โ Read โ Deleted โ Purged.
- Order states (Winter 2023): Placed โ Pdf Valid โ Confirmed โ Processing โ Shippedุ ู ุน Cancelled ูู alternate path.
- Composite states โ ููู flows ุงููู ูููุง grouped behaviors.
- Guards โ ูู
ุง ุงูู transition ู
ุดุฑูุทุฉ (e.g.,
classSize โฅ minimum).