Finite State Machines and
Statecharts
Steps in Using COMET/UML
Develop statecharts for state dependent objects
Finite State Machines and Statecharts
• Many information and real-time systems are state dependent
– Action depends not only on input event
– Also depends on state of system
• Finite State Machine
– Finite number of states
– Only in one state at a time
• Statechart
– Graphical representation of finite state machine – States are rounded boxes
– Transitions are arcs
• Statechart relates events and states
UML notation for statechart: composite state with sequential
substates
States and Events
• State
– A recognizable situation
– Exists over an interval of time
– Represents an interval between successive events
• Event
– A discrete signal that happens at a point in time – Also known as a stimulus
– Has no duration
– Causes change of state
• Referred to as state transition
Example of Events and States
Events and Conditions
• State transition label – Event [Condition]
• Condition is a Boolean function
– Conditions are optional on statecharts
– Condition is true for finite period of time
• When event occurs, condition must be true for state transition to occur.
• If condition is false, state transition does not occur
Example of Events and Conditions
Actions
• State transition label
– Event / action(s)
– Event [condition] / action(s)
• Action
– Executed as a result of state transition
– Executes instantaneously at state transition – Terminates itself
– Is optional
Example of actions
Entry and Exit Actions
• Entry action
– Action executed on entry into state
• Entry / action
– E.g., Start Cooking
• Exit action
– Action executed on exit from state
• Exit / action
– E.g, Stop Cooking
Example of entry action
Example of exit action
Hierarchical Statecharts
• Disadvantages of State Transition Diagrams and Flat Statecharts
– Complex State Transition Diagrams get very cluttered
– Limited capability for managing complexity
• Hierarchical Statecharts
– Based on Harel Statecharts
– Notation for hierarchical decomposition of state transition diagrams
• Composite state decomposed into substates
• Default entry states
• Transition out of composite state corresponds to transition out of every substate
Example of same event and action on different state
transitions
Example of hierarchical statechart showing composite state
and substates
Hierarchical Statecharts
•Sequential decomposition
– When object is in composite state
• It is in one and only one of substates
– Transition into composite state
• Valid PIN / Display Menu
• Waiting for Customer Choice
• Must be to one and only one of substates Aggregation of state transitions
– If same event causes transition out of every substate
– Then aggregate into transition out of composite state
Example of hierarchical statechart showing composite state
without substates
Guidelines on Statecharts
• State name must be passive not active
– Represents time period when something • ishappenING,e.g.,WaitingforPIN
• Identifiablesituation,e.g.,Idle,Initial
• State names must be unique
• Must be able to exit from every state
• Flat statechart
– Statechart is only in one state at a time
• Hierarchical statechart
– sequential decomposition
• Statechart is only in one substate at a time
Guidelines on Statecharts
• Event is the cause of the state transition
– Event happens at a moment in time
– Event name indicates something has just happened
• e.g, Card Inserted, Door Closed
• Action is the result of the state transition
– Action is a command, e.g., Dispense Cash, Start Cooking
– Action executes instantaneously
– Activity executes throughout a given state
Guidelines on Statecharts
• More than one action possible with a state transition
– No sequential dependency between actions
• Condition is a Boolean value
– Event [Condition]
– State transition only occurs if
• Eventhappens&ConditionisTrue
– Condition is True over some interval of time
• Actions, Activities and Conditions are optional
Developing Statechart from Use Case
• Develop state dependent use case
• Start with scenario (one path through use case)
– Consider sequence of interactions between actor and system
• Consider sequence of external events
– Input event from external environment – Causes state transition to new state
– Action may result from state transition
• Initially develop flat statechart
Banking System use case model
Statechart for ATM Control - Validate PIN use case
Developing Statechart from Use Case (continued)
• Consider alternative external events
– Could result in additional states
– Could result in additional state transitions
• Develop hierarchical statechart
– States that can be aggregated to form composite state – Event causing transition
from several states
• Create composite state with one transition out of composite state
• Instead of many transitions out of substates
Example of integrated statechart
ﺷﻜﺮا ﻟﻜﻢ