State Chart
State Chart
2023
Lecture 4 - State-Chart (State-Machine)
Diagram
SessionOutcomes
• Introduction to State Diagrams
• State Diagram symbols
• States
• Simple states
• Composite states
• Transitions
• Call Event
• Change Event
• Time Event
• Composite states in detail
• Composite states with direct sub states
• Composite states with regions
SLIIT 2
What is a State in General?
• State is a particular condition that someone or
something is in at a specific time.
• A Car
What are the States of an Object?
Objects has states …
Active Idle
Waiting
UML State Machine Diagram
• A state machine diagram models the behavior of a single
object, specifying the sequence of events that an object
goes through during its lifetime.
• There is only one state machine diagram for a class.
• A state diagram is typically drawn for only for the classes
which contains significant dynamic behavior.
State-Machine Diagram Notations
•States
•Transitions
State Machine Diagram Example
Source: https://www.visual-paradigm.com/tutorials/how-to-draw-state-machine-diagram-in-uml/
What is a State?
• A state represents a state that an object is in at a
particular time.
• Shown as a rectangle with rounded corners, with the state
name shown within the state.
• While in the state, the object satisfies some condition,
performs some action, or waits for some event.
ON OFF
Special States
• Initial state (the object being constructed)
✓denoted by a filled black circle
Idel Run
State with compartments
• A state may be subdivided into multiple compartments
separated from each other by a horizontal line.
• Basic Compartments in a state are:
• Name compartment Enrollment
Enrollment
Internal Behaviors/Activities compartment
• This compartment holds a list of internal behaviors
associated with a state.
• Each activity has the following format:
<<behavior-type-label>> / <<action>>
• The <behavior-type-label> identifies the situations under
which will be invoked and can be one of the following:
• entry
• exit
• do
Entry and Exit Activities/Behaviors
• Entry label identifies a Behavior, specified by the
corresponding expression, which is performed upon
entry to the State (entry Behavior).
• Exit label identifies a Behavior, specified by the
corresponding expression, that is performed upon exit
from the State (exit Behavior). Enrollment
create When
Invoice ( ) (Paid)
Self-Transitions
• A self transition is a transition that starts and ends in the same
state.
• When a self transition is taken, the state in question is exited and
entered again.
• Self transitions are commonly used to “restart” the current state,
causing the exit actions to happen, followed by the entry actions.
Types of Triggers
• Mainly there are three types of triggers.
28
Triggers with Guard Conditions -Example
Activity 02
Draw a state machine diagram for the Seminar class during registration.
• TheSeminarisfirstproposedandthenitisscheduledforanagreeddate.
• Then the Seminar will be opened for enrollment for the students. When
theenrollingstarts, seatcapacityiszero.
• As the number of seats are limited, as long as the Seminar is opened for
enrollment, the remaining seats/size will be updated when each time a
studentisenrolledtotheseminar.
Activity 03
DrawtheStateDiagramforthefollowingScenario.
• Burglaralarmisinitiallyatthestateofresting.
• Then by setting the alarm, burglar alarm state may be changed to the state
set.
• When the alarm is set, it may be turned off. This will allow the alarm to be
resting. While the alarm is set it can be triggered, which will make it ring.
When the alarm is ringing, it can be turned off. Then the alarm will be
restingagain.DrawastatediagramfortheBurglarAlarmclass.
Note:Noneedtomodelthefinalstate.
Activity 04
Draw the State Diagram for the following diagram
• You can logoff once you have completed using the Courseweb. There
is also a timeout of 5 minutes if you are inactive and you are
automatically logged off. Draw a state diagram for the Courseweb
user class.
Composite State
• Astatethathassubstates(nestedstates).
• Substatescouldbesequential(disjoint)orconcurrent.
• Sub states are in a separate compartment called “Decomposition”
compartment.
Composite State cont…
• Composite state can be simple composite state or Orthogonal
composite state.
•.
Composite State cont…
Orthogonal Composite State
• Orthogonal composite state has more than one region within
the decomposition compartment.
Composite State – Hidden Decomposition Indicator
• In some cases, it is convenient to hide the decomposition of a composite
state.
• For example, there may be a large number of states nested inside a
composite state and they may simply not fit in the graphical space
availableforthediagram.
• In that case, the composite state may be represented by a simple state
graphic with a special “composite” icon, usually in the lower right-hand
corner.
Composite State with a
hidden decomposition indicator icon
Activity 05
Draw a State Chart for the given ATM
• ATM is initially turned off, it is in the Off state. After the power is
turned on, ATM performs startup action and enters Self Test state. If
the test unsuccessful, ATM goes into Out of Service state, otherwise
goes to the Idle state. In this state ATM waits for customer interaction.
• The ATM state changes from Idle to Serving Customer when the
customer inserts debit or credit card in the ATM's card reader. On
entering the Serving Customer state, the entry action readCard is
performed. The state also has exit action ejectCard which releases
customer's card on leaving the state, no matter what caused the
transition out of the state.
Activity 05 cont…
• Serving Customer state is a composite state with sequential sub
states Customer Authentication, Selecting Transaction and Transaction.
Customer Authentication and Transaction are composite states by
themselves which is shown with hidden decomposition indicator icon. These
states follow in order within the composite state.
• Serving Customer state has a transition back to the Idle state after
transaction is completed . While serving customer if failure occurs it will move
into the Out of Service state. ATM machine can be turned off when it is in the
Idle state.
• While in the Out of Service state when a maintenance is performed it will
move back to Self Test state
• A transition from Serving Customer state back to the Idle state could be
triggered by cancel event as the customer could cancel transaction at any
time.
State Charts VS Activity Diagrams
• A UML activity diagram can sometimes be used as an alternative to a
statechart.
• AUMLstatechart:
• shows all possible states and transitions (i.e. events) that can occur
duringthelifecycleofasingleobject;
• allpossiblesequencesofeventscanbetracedviathetransitions.
• AUMLactivitydiagram:
• caninvolvemanydifferentobjects;
• showsonepossiblesequenceoftransitions.
State Machine Diagram Guidelines
• Keep the diagram simple
• If it is too complex, perhaps it should be broken down into separate
diagrams.
• Question “Black-Hole” States
• A black-hole state is one that has transitions into it but none out of it,
something that should be true only of final states.
• Question “Miracle” States
• A miracle state is one that has transitions out of it but none into it,
something that should be true only of start points.
References
• UML 2 Bible
• Learning UML 2.0 by Kim Hamilton,
Russ Miles
• Chapter 15UML 2 Bible
45
End of the Lecture