State Modeling
State Modeling
1
Introduction
• You can best understand a system by first examining its static
structure-that is, the structure of its objects and their relationships to
each other at a single moment in time (the class model).
• Examples:
– User depresses left button
– Flight 123 departs from Amman
– Power turned on
– Alarm set
– Paper tray becomes empty
3
Events (2)
4
Signal Events (1)
5
Signal Events (2)
6
Change Event
7
Time event
8
States
9
States
• Notation:
– Figure 5.4 shows the UML notation for a state-a rounded box containing
an optional state name. Our convention is to list the state name in
boldface, center the name near the top of the box, and capitalize the
first letter
11
State
12
Transitions and Conditions
13
Transitions and conditions
• guarded transition fires when its event occurs, but only if the guard
condition is true.
• For example, "when you go out in the morning (event), if the
temperature is below freezing (condition), then put on your gloves
(next state)."
• A guard condition is checked only once; at the time the event
occurs, and the transition fires if the condition is true.
• If the condition becomes true later, the transition does not then fire.
• Note that a guard condition is different from a change event-a guard
condition is checked only once while a change event is, in effect,
checked continuously.
• The UML notation for a transition is a line from the origin state to the
target state. An arrowhead points to the target state. The line may
consist of several line segments. An event may label the transition
and be followed by an optional guard condition in square brackets.
14
Transitions and conditions
• Figure 5.7 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
15
State Diagram
• State names must be unique within the scope of the state diagram.
• All objects in a class execute the state diagram for that class.
• The class state diagram models the common behavior of the class
objects.
16
State diagram example
•Figure 5.8 shows a state diagram
for a telephone line.
17
• At the start of a call, the
telephone line is idle.
• When the phone is removed
from the hook, it emits a dial tone
and can accept the dialing of
digits.
• Upon entry of a valid number,
the phone system tries to
connect the call and route it to
the proper destination.
• The connection can fail if the
number or trunk are busy.
• If the connection is successful,
the called phone begins ringing.
• If the called party answers the
phone, a conversation can occur.
• When the called party hangs up,
the phone disconnects and
reverts to idle when put on hook
again.
18
Activity
19
Examples
• Figure 5.12 shows the state diagram for a pop-up menu on 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.
20
Do-Activities
• A do-activity is an activity that continues for an extended
time.
• By definition, a do-activity can only occur within a state
and cannot be attached to a transition.
• For example, the warning light may flash during the
Paper jam state for a copy machine (Figure 5.13).
• Do-activities include continuous operations, such as
displaying a picture on a television screen, as well as
sequential operations.
21
Entry and Exit Activities
• As an alternative to showing activities on transitions, you can bind
activities to entry or to exit from a state.
• Figure 5.14 shows the control of a garage door opener. The user
generates depress events with a push button to open and close the
door. Each event reverses the direction of the door, but for safety
the door must open fully before it can be closed. The control
generates motor up and motor down activities for the motor. The
motor generates door open and door closed events when the
motion has been completed. Both transitions entering state Opening
cause the door to open.
22
Entry and Exit Activities
• Figure 5.15 shows the same model using activities on
entry to states. An entry activity is shown inside the state
box following the keyword entry and. a “/” character.
Whenever the state is entered, by any incoming
transition, the entry activity is performed, An entry
activity is equivalent to attaching the activity to every
incoming transition. If an incoming transition already has
an activity, its activity is performed first.
23
• if a state has multiple activities, they are
performed in the following order:
1. activities on the incoming transition,
2. entry activities,
3. do-activities,
4. exit activities,
5. activities on the outgoing transition.
• Events that cause transitions out of the state
can interrupt do-activities. If a do-activity is
interrupted, the exit activity is still performed.
24
Examples
• In general, any event can occur within a state and cause an activity to be
performed.
• Entry and exit are only two examples of events that can occur.
• As Figure 5.16 shows, there is a difference between an event within a state
and a self-transition; Only the self-transition causes the entry and exit
activities to be executed.
25
Complete Transition
• Often the sole purpose of a state is to perform a sequential activity. When
the activity is completed, a transition to another state fires.
• A guard condition is tested only once, when the event occurs. If a state has
one or more completion transitions, but none of the guard conditions are
satisfied, then the state remains active and may become "stuck"-the
completion event does not occur a second time, therefore no completion
transition will fire later to change the state. If a state has completion
transitions leaving it, normally the guard conditions should cover every
possible outcome.
26
Sending signals
27
Case study
28
29