ch13 State Modeling PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 77

CH 5

STATE MODELLING
Class Diagram Pros
– discovering related data and attributes
– getting a quick picture of the important
entities in a system
– seeing whether you have too few/many
classes
– seeing whether the relationships between
objects are too
complex, too many in number, simple
enough, etc.
– spotting dependencies between one
class/object and another
Class Diagram Cons
Not so great for:
– discovering algorithmic (not data-driven)
behavior
– finding the flow of steps for objects to solve
a given problem
– understanding the app's overall control flow
(event-driven?
web-based? sequential? etc.)
STATE MODELLING
Concerned with the time and sequencing of
operations-events that mark changes, states
that define the context for events and
organization of events and states.
Aspects of a system that are concerned with
time and changes are called the State
Model.
STATE MODELLING
Most imp. Concepts :

Events and State


Event : represents external stimuli
State : represent values of objects
STATE MODELLING
A state model consists of : State Diagrams –
One for each class with behaviour that is
important to an application.
A state diagram specifies : the possible
states, which transition are allowed
between states, what stimuli cause the
transition to occur and what operation are
executed in response to stimuli.
A state diagram describes the collective
behaviour for the objects in a class.
Event
Event is a significant occurrence at a certain
point in time that changes a value or several
values of an object, and thereby changes the
object’s state.
An event occurs only when some designated
condition evaluates to true.
Event
In software implementation, an event
represents the invocation of a method of the
class.
Events may include signals, calls, the passing
of time or a change in state.
Types of Events
External or Internal
External events are those that pass between
the system and its actors.
Eg. Pushing of a button and an interrupt from
a collision sensor are external events.
Internal events are those that pass among
the objects that live inside the system.
Eg. Overflow exception.
What does an event represent ?
Commands or requests from other objects
Time
Circumstances or happenings in other
objects
Creation, deletion, updation
Types of Events in UML
Change events : occur when a condition
becomes true
Signal events : designate the receipt of an
explicit (real time) signal from one object or
actor to another
Call events : indicate the receipt of a call for
an operation by an object or actor
Time events : mark the passage of a
designated period of time e.g. after (10
seconds)
STATE
Abstractions of values & links of object.
set of values & links are grouped together
into state according to behavior of object.

Object finite no. of possible state


but one time object can be in one state
Object respond to certain event when it is in certain
state.

Event represents points in time

state represents intervals of time.


•STATE :- a condition or situation during the life of
•an object at which time it satisfies some condition,
• performs some activity, or waits for some event.
Example
Observe the following process and draw the state
diagram for the same.

ON ON

OFF
State Diagram

on

Lamp On

on

off

off
Lamp Off
TRANSITIONS & CONDITIONS
Transition is change from one state to another.
Ex:- called phone is answered phone line
transitions from RINGING state to CONNECTED
state.
The transition is said to fire upon the change
from the source state to the target state.
The origin and target of a transition usually are
different states, but may be the same.
TRANSITIONS & CONDITIONS
GUARD CONDITION :-It’s a Boolean expression
that must be true in order for transition to occur
Ex:- “When you go out in morning (event) ”
if temp. is below freezing (condition)
then put on your gloves (next state)”
Guard condition checked only once, at time the
event occurs & transition fires if conditon is true
TRANSITIONS & CONDITIONS
GUARD CONDITION :-It’s a Boolean expression
that must be true in order for transition to occur.
E.g. 1 : a traffic light at an intersection may
change only if a road has cars waiting.
A guarded transition fires when its event occurs,
but only if the guard condition is true.
Fig shows guarded transitions for traffic lights at
an intersection. One pair of electric eyes checks
the north-south left turn lanes; another pair
checks the east-west turn lanes. If no car is in the
north-south and/or east-west turn lanes, then the
traffic light control logic is smart enough to skip
the left turn portion of the cycle.
TRANSITIONS & CONDITIONS
STATE DIAGRAM
graph whose nodes = state & Arcs = transitions
between states
specifying the sequence of states it goes
through during its lifetime in response to
events, together with the responses to those
events
describe changes in state in a procedure
Sample State Diagram
ONE SHOT DIAGRAM
State diagrams can represent continuous loops or one-
shot life cycles.
Diagram for phone line is a continuous loop
In normal usage, we do not care how the loop is started.

LIFE CYCLE OF CHESS GAME WITH INITIAL & FINAL STATE


ONE SHOT DIAGRAM
Represents objects with finite lives & have initial & final
states.
Initial State creation of object, shown by solid circle.
final state destruction of object, shown by bull’s eye.

LIFE CYCLE OF CHESS GAME WITH INITIAL & FINAL STATE


STATE DIAGRAM BEHAVIOUR
ACTIVITY EFFECTS:-
Specifies an optional behavior to be performed
when the transition fires.
Effect is reference to behavior that is executed
in response to event.
Ex:- disconnect phone line activity is executed
in response to onHook event
A program increment an internal counter every
time particular event occurs.
STATE DIAGRAM NOTATIONS
STATE : Drawn as a rounded box with the
name. Can be initial state or final state. Solid
circle or bull’s eye or encircled.
Transition : drawn as a line from origin to target
state. Arrow points to the target state.
Event : signal event is shown as a label on a
transition and may be followed by parenthesized
attributes. Change event is shown with the
keyboard when followed by a parenthesized
boolean expression
State diagram : enclosed in a rectangular frame
with diagram name in small pentagonal tag in
upper left corner
STATE DIAGRAM NOTATIONS
Guard Condition : optionally listed in square
brackets after an event.
Effects : can be attached to a transition or state
and listed after a slash.
Operations :
The state diagrams presented so far describe
the patterns of events and states for a single
object class.
State diagrams would be of little use if they just
described patterns of events. A behavioral
description of an object must specify what the
object does in response to events.
An activity is an operation that takes time to
complete. An activity is associated with a state.
Operations :
Activities include continuous operations, such as
displaying a picture on a TV screen, as well as
sequential operations that terminate by
themselves after an interval of time, such as
closing a valve or performing a computation.
A state may control a continuous activity, such
as ringing a telephone bell, that persists until an
event terminates it by causing a transition from
the state.
The notation “do:A” within a state box indicates
that activity A starts on entry to the state and
stops on exit.
Operations :
An action is an instantaneous operation. An action
is associated with an event.
An action represents an operation whose duration
is insignificant compared to the resolution of the
state diagram.
For e.g., disconnect phone line might be an action
in response to an on-hook event for the phone
line.
The notation for an action on a transition is a
slash(‘/’) and the name(or description) of the
action, following the name of the event that causes
it.
Operations :
Fig shows the state diagram for a pop-menu on
a workstation. When the right button is
depressed, the menu is displayed; when the
right button is released, the menu is erased.
While the menu is visible, the highlighted menu
item is updated whenever the cursor moves.
Notation :- / & name of activity
following event that causes it
DO ACTIVITIES
Activity that continues for an extended time.
Occur within state only & can not be attached to
transition
Ex:- warning light may flash during paper jam state for
copy machine

Notation :- do / name of activity


Nested State Diagrams :
The problem with state diagrams is that they
lack expressive power and are impractical for
large problems. These problems are true of
flat, unstructured state diagrams.
Consider an object with n independent
Boolean attributes that affect control.
Representing such an object with a single flat
state diagram would require 2^n states.
Nested State Diagrams :
Nesting State Diagrams :
An activity in a state can be expanded as a
lower-level state diagram, each state
representing one step of the activity.
Nested activities are one-shot state diagrams
with input & output transitions, similar to
subroutines.
The set of nested state diagrams forms a
lattice.
Fig shows a top-level model for a vending
machine.
Nesting State Diagrams :
Nesting State Diagrams :
The diagram contains an activity dispense
item and an event select(item) that are
expanded in more detail in nested state
diagrams.
The diagram also shows a bit of alternate
notation.
The event coins in(amount) is written within
the Collecting money state.
This indicates a transition that remains within
a single state.
Nesting State Diagrams :
Also, the transition from the unnamed state
containing “do:dispense item” to state Idle
has no event label.
The lack of event label indicates that the
transition fires automatically when the activity
in the state is complete.
Nesting State Diagrams :
Fig below shows a subdiagram for the
dispense item activity. This activity
corresponds to a sequence of lower-level
states and events that are invisible in the
original high-level state diagram.
Nesting State Diagrams :
Events can also be expanded into subordinate
state diagrams. Fig shows the select item event,
which actually involves several low-level events.
The customer keys in an item no. and can start
over by hitting clear; the selection is confirmed by
hitting enter. The label on bull’s eye indicates the
event generated on the higher-level state
diagram.
Draw a nested State Diagram for the Phone
Line.
A nested state diagram receives the outgoing
transitions of its enclosing state.
State Generalization :
A nested state diagram is actually a form of
generalization on states.
Generalization is the “or-relationship”.
An object in a state in the high-level diagram
must be in exactly one state in the nested
diagram.
It must be in the first state, or the second
state or in one of the other states.
Draw Nested State Diagram (Generalization)
for Car Transmission.
Initially the car is in neutral. From neutral, one
can go either in reverse or in Forward
Direction (1st, 2nd and 3rd gear in sequence.)
State Generalization :
Event Generalization :
Events can be organized into a generalization
hierarchy with inheritance of event attributes.
Fig shows part of a tree of input events for a
workstation.
Providing an event hierarchy permits different
levels of abstraction to be used at different
places in a model.
Event Generalization :
Concurrency : Aggregation
Concurrency
A dynamic model describes a set of
concurrent objects, each with its own state
and state diagram. The objects in a system
are inherently concurrent and can change
state independently.
The state of the entire system cannot be
represented by a single state in a single
object; it is the product of the states of all the
objects in it.
Concurrency : Aggregation
Concurrency
Aggregation is the “and-relationship”.
The aggregate state is one state from the first
diagram, and a state from the second
diagram and a state from each other diagram.
In some cases they interact with each other.
Fig. shows the state of a car as an
aggregation of component states: the Ignition,
Transmission, Accelerator and Brake.
Each component also has substates.
Concurrency : Aggregation
Concurrency
ENTRY & EXIT ACTIVITIES
Bind activities to entry or to exit from a state
An entry activity is a behavior that
occurs while the object is transitioning into the state
An exit activity occurs as part of the transition out of the
state.
Ex:- control of garage door opener
user generates depress event with pushbutton to open &
close door
the control generates motor up & motor down activities

Opening
depress / motor up door open / motor off

depress / motor up
Closed Open

door closed / motor off depress / motor down

Closing
whenever state is entered by any incoming transition the
entry activity is performed
whenever state is exited by any outgoing transition the
exit activity is performed
NOTATION :- entry activity - keyword ‘entry’ & ‘/’ char
Exit activity - keyword ‘exit ’ & ‘/ ’ char
COMPLETION TRANSITION:
An arrow without event name indicates automatic
transition that fires when activity associated with state is
completed such a unlabeled transition is called
completion transition
An extension ladder has a rope, pulley & latch for raising ,
lowering & locking the extension. When latch is locked the
extension is mechanically supported & you may safely climb
ladder. to release latch you raise the extension slightly with
rope. You may then freely raise or lower the extension. The
latch produces a clacking sound as it passes rung of ladder.
The latch may be reengaged while raising the extension by
reversing direction just as the latch is passing a rung.
Prepare a state diagram of an extension ladder
Extension Ladder

Latch is locked release extension changed


do/ climb up the latch do/ freely
the ladder raise / lower
the extension

Passing a
rung of ladder

Clacking Sound
Few Examples for Practice.
Draw a State Diagram for Library
Management System with Aggregation where
the Library Management System consists of
Book, Librarian and Server.
State Diagram for ATM
Draw diagram of the ATM system as well as
the server.
Identify states and events for a Photocopier
Machine (Zerox) from the given description.
Initially the machine is off. When the operator
switches on the machine, it first warms up during
which it performs some Internal Tests. Once the
tests are over, machine is ready for making
copies. When operator loads a page to be
photocopied and press ‘start’ button, machine
starts making copies according to the number of
copies selected. While machine is making
copies, it may go out of paper. Once operator
loads sufficient pages, it can start making copies
again. During the photocopy process, if paper
jam occurs in the machine, operator may need to
clean the path by removing the jammed paper to
make machine ready.
Draw diagram of Online Shopping
System
It should have product, customer and server.
State Diagram for Online
Restaurant Management
It consists of the Online Server, the Customer
and the Manager. The Server can initialize
the session for the customer and display the
the menu. The user can input the item of food
and then the server calculates the bill
accordingly and finally terminates the
session.
The manager can perform the analysis of the
user, the order of food given as well as the
staff.
The customer can order the food.
Server
Customer
Manager

You might also like