Dynamic Modeling: Object-Oriented Modeling and Design
Dynamic Modeling: Object-Oriented Modeling and Design
Analysis and
Design
5 DYNAMIC
MODELING
Source: OBJECT-ORIENTED
MODELING A N D DESIGN
James Rumbaugh, Michael
Blaha,
William Premerlani, Frederick
Eddy,
William Lorensen
✘Temporal relationship are difficult to understand. A system can best be
understood 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. Then we examine
changes to the objects and their relationships over time. Those aspects of a system
that are concerned with time and changes are the dynamic model.
✘Control is that aspect of a system that describes the sequences of operations that
occur in response to external stimuli.
✘The major dynamic modeling concepts are events, which represent external
stimuli, and states, which represent values of objects.
The pattern of events, states, and state transitions for a given class can be
abstracted and represented as a state diagram. A state diagram is a network of
states and events, just as an object diagram is a network of classes and
relationships. The dynamic model consists of multiple state diagrams.
Events
Two events that are causally unrelated are said to be concurrent; they have no effect
on each other.
An event conveys information from one object to another. The data values conveyed
by an event are its attributes.
event
States
✘ A state is an abstraction of the attribute values and links of an object.
✘The response of an object to an event may include an action or a
change of state by the object.
✘A state corresponds to the interval between two events reveived by an
object. Events represent points in time; states represent intervals of time.
✘ A state has duration; it occupies an interval of time.
✘A state is often associated with the value of an object satisfying some
condition.
✘ In the simplest case, each enumerated value of an attribute defines a
separate state.
State: Alarm ringing
Description: alarm on watch is ringing to indicate target time
Event sequence that produces the state:
set alarm (target time)
any sequence not including clear alarm
current time = target time
Various Condition that characterizes the state:
characterizations
alarm = on, and target time ≤ current time ≤ target time + 20 seconds,
of a state
and no button has not been pushed since target time
Events accepted in the state:
event action next state
current time = target time + 20 reset alarm normal
button pushed (any button) reset normal
alarm
Can links have state? In as much as they can be considered objects, links can have
state. A s a practical matter, it is generally sufficient to associate state only with
objects. The state of an object can include the values of its links.
State Diagrams
The state diagram specifies the state sequence caused by an event sequence. If an
object is in a state and an event labeling one of its transitions occurs, the object
enters the state on the target end of the transition. The transition is said to fire.
time-out
Dial tone Time-out
-out
digit(n) it me
digit(n) Recorded
Dialing
invalid number message
Connecting
message
Fast usy done
busy tone trunk b routed
event
Ringing
name
called phone answers State
diagram
Connected for phone
line
Continuous
loop called phone hangs up
Disconnected
It is not necessary to distinguish between different numbers as separate states since
they all have the same behavior, but the actual number dialed must of course be
saved as an attribute.
A state diagram describes the behavior of a single class of objects. Since all
instances of a class have the same behavior (by definition), they all share the same
state diagram, as they all share the same class features.
One-shot diagrams represent objects with finite lives. A one-shot diagram has
initial and final states. The initial state is entered on creation of an object; entering
the final state implies destruction of the object.
initial state final state
(solid circle) (bull's eye)
White's checkmate
Start Black wins
turn
stale
mate
black white
Draw
e
mat
moves stale
moves
Black's
White wins
turn checkmate
One-shot
state diagram
for chess game
One shot
The dynamic model is a collection of state diagrams that interact with each other
via shared events. An object model represents the static structure of a system, while
dynamic model represents the control structure of a system.
Conditions can be used as guards on transitions. A guarded transition fires when its
events occurs, but only if the guard condition is true.
guarded
condition
state
transition
time-out [cars in N/S left lanes]
North/south North/south
may go straight [no may turn left
c ars time
in N -ou
/S lt transition
e ft la
[no nes time-out
time-out ti ]
s in me-o
car
E /W ut
left State diagram
lan with
East/west es] East/west
may turn left may go straight guarded
transitions
time-out [cars in E/W left lanes]
2. OPERATIONS
Controlling Operations
Actions can also represent internal control operations, such as setting attributes or
generating other events.
State1 State2
do: activity1 event1 (attribs) [condition1] / action1 ...
Summary of
notation for
unstructured
state diagrams
on-hook on-hook
Idle
off-hook
on-hook Dial tone time-out Time-out
do: sound dial tone do: sound loop beep
-out
activity digit(n) time
Recorded
digit(n)
Dialing message
on-hook invalid number do: play message
Representing such an object with a single flat state diagram would require 2 n states.
By partitioning the state into n indenpendent state machines, however, only 2n
states are required. n 2 transitions are needed to connect every state to every other
state. If this model can be reformulated using structure, the number of transitions
could be reduced as low as n.
Combinatorial
explosion of
transitions in flat
state diagram
Nesting State Diagrams
The lack of event label indicates that the transition fires automatically when the
activity in the state is complete.
internal
coins in(amount) / set balance action
Collecting money
Idle
coins in(amount) / add to balance
cancel / refund coins
[item select(item) [change < 0]
empty]
Vending do: test item and compute change
machine
model [change < 0] [change > 0]
lamda
transition
do: dispense item do: make change
This activity corresponds to a sequence of lower-level states
and events that are invisible in the original high-level state Dispense
diagram. item activity
of vending
Subdiagram for the machine
dispense item activity
do: move arm arm ready do: move arm to arm ready do: push item off pushed
to correct correct colomn shelf
row
Events can also be expanded into subordinate state diagrams. The label on the
bull's-eye indicates the event generated on the higher-level state diagram
✘Generalization is the or-relationship. The states in the nested diagram are all
refinements of the state in the high-level diagram.
✘States may have substates that inherit the transitions of their superstates. Any
transition or action that applies to a state applies to all its substates, unless
overridden by an equivalent transition on the substate.
✘A superstate is drawn as a large rounded box enclosing all of
its substates. Substates in turn can enclose further substates.
✘ Within state Forward, substate First is the default initial State diagram of
state. Forward is just an abstract class state; control car transmission
with
must be in a real state, such as First.
generalization
Transmission push R
Neutral push N Reverse
contour
inherited
by each
gear push N push F
superstate
default
Forward abstract state
substate
A dynamic model describes a set of concurrent objects, each with its own state and
state diagram. The objects in a system are inhrently concurrent and can change
state independently. In many systems, the number of objects can change
dynamically as well.
A state diagram for an assembly is a collection of state diagrams, one for each
component. Aggregation implies concurrency. The aggregate state corresponds to
the combined states of all the component diagrams. Aggregation is the a n d -
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 the more
interesting cases, the component states interact. Guarded transitions for one object
can depend on another object being in a given state. This allows interaction between
the state diagrams, while preserving modularity.
An aggregation
and its
concurrent state
diagrams
Car
Ignition
turn key to start
[Transmission in Neutral] release key
Off Starting On
push N push F
Forward
Accelerator Brake
Playing rubber
default
state N-S vulnerability
Not N-S game N-S game N-S wins
Vulnerable
vulnerable rubber
concurrent
subdiagrams
E-W vulnerability
Not E-W game E-W game E-W wins
Vulnerable
vulnerable rubber
default
state
5. ADVANCED DYNAMIC MODELING CONCEPTS
Entry and Exit Actions
Actions
on
transitions
Whenever the state is entered, by any incoming transition, the entry action is
performed. An entry action is equivalent to attaching the action to every incoming
transition. If an incoming transition already has an action, its action is performed
first.
Whenever the state is exited, by any outgoing transition, the exit action is
performed first.
If multiple operations are specified on a state, they are performed in the following
order: actions on the incoming transition, entry actions, do activities, exit actions,
actions on the outgoing transition. D o activities can be interrupted by events that
cause transitions out of the state, but entry actions and exit actions are completed
regardless, since thay are considered to be instantaneous actions. If a do activity is
interrupted, the exit action is nevertheless performed.
Closed Open
depress
Actions entry / motor off entry / motor off
on entry
to door closed Closing depress
states entry / motor down
It is possible to use actions attached to transitions as well as entry and exit actions
in a diagram.
Internal Actions
State1
do: activity1 event1 (attribs1) [condition1] / action1 State2
entry / action2
...
exit / action3
event / action4 event2 (attribs2)
Object class
Summary of
extended
notation for
state diagrams
Automatic Transition
An arrow without an event name indicates an automatic transition that fires when
the activity associated with the source state is completed. If there is no activity, the
unlabeled transition fires as soon as the state is entered (but the entry and exit
actions are always performed). Such unlabeled transitions are sometimes called
lambda transitions.
If a state has one or more automatic transitions, but none of the guard conditions
are satisfied, then the state remains active until one of the conditions is satisfied or
until an event causes another transition to fire.
Sending Events
The dotted line from a transition to an object indicates that an event is sent to the
object when the transition fires.
If a state can accept events from more than one object, the order in which
concurrent events are received may affect the final state; this is called a race
condition.
Synchronization of Concurrent Activities
Sometimes one object must perform two (or more) activities concurrently. The
internal steps of the activities are not synchronized, but both activities must be
completed before the object can progress to its next state.
This is an example of splitting control into concurrent activities and later merging
control.
activitiy composite
(assumes one state from each subdiagram)
Emetting
concurrent
activities cash taken
do: dispense cach
Merging of concurrent control is shown by an arrow with a forked tail. The target
state becomes active when both events occur in any order. The events need not be
simultaneous. Each subdiagram terminates as soon as its part of the transition fires,
but all parts of the transition must fire before the entire transition fires and the
composite state is terminated.
The exit actions (if any) of all subdiagram are performed when the merge transition
fires. In the example, the transitions cash taken and card taken are part of a single
merge transition. When both parts of the merge transition fire, state Ready to
reset becomes active.