Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User...

54
Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

Transcript of Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User...

Page 1: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

Dynamic Modeling

Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2nd edition, Addison Wesley, 2005.

Page 2: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

2

Outline

• Dynamic models• State machine diagrams (a.k.a. statechart)

– Modeling object or system states

• Interaction diagrams– Sequence diagrams (a.k.a. message sequence)

• The time order of interactions between objects

– Communication diagrams (a.k.a. collaboration)• Messages passed between objects

2

Page 3: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

3

Dynamic Models

• Used to model control aspects (e.g., when)• Several different models possible, e.g.,

– Focusing on state changes– Focusing on interactions

3

Page 4: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

4

Dynamic Models

• Used to model control aspects (e.g., when)• Several different models possible, e.g.,

– Focusing on state changes• Used to show software control• Sequence of operations and events• Transitions between states• UML state machine diagrams

– Focusing on interactions

4

Page 5: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

5

Dynamic Models

• Used to model control aspects (e.g., when)• Several different models possible, e.g.,

– Focusing on state changes– Focusing on interactions

• Used to show dynamic relationships between objects • Messages, interfaces, and timings• UML interaction diagrams

– Sequence diagrams, focusing on ordered interactions

– Communication diagrams, focusing on interaction links

– Timing diagrams, focusing on interaction timings

5

Page 6: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

6

Outline

Dynamic models• State machine diagrams• Interaction diagrams

6

Page 7: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

7

Modeling State Changes

• Modeling object/system states– UML state machine diagram– Depicts the flow of control using states and

transitions by describing• How an object or system changes over time• Event (e.g., operation calls) it may respond to• How it respond to them

– Generalization of finite state machines

7

Page 8: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

8

Modeling Object States

• Network of states and transitions• One diagram for each object with important

dynamic behavior• Independent timing of state machines for

different objects (asynchronous)

On Off

switch pushed

switch pushed

8

Page 9: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

9

Elements of State Machine Diagrams

Idle

Initial State

Running

Final State

State

Transition

9

Page 10: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

10

Initial and Final States

White’s turn

Black’s turn

blackmoved

whitemoved

start

Blackwin

Draw

Whitewin

checkmate

checkmate

stalemate

stalemate

10

Page 11: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

11

States

• Some phase during the lifetime of an object that is significant of its behavior (e.g., observable or different behavior)

• Condition or situation in the life of a system (or object) during which it:– Satisfies some condition (i.e., state invariant),

– Performs some activity, or

– Waits for some events.• Set of values of properties that affect the behavior of the system

(or object).– Determines response to an event

– Thus, different states may produce different responses to the same event

Page 12: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

12

States (Cont.)

• All objects have states.• A state is an abstraction of the values

maintained by the object that determine behavior.

• Examples:– The invoice is paid.– The car is parked.– The engine is running.– Kate is working.– Jim is playing.

12

Page 13: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

13

State Corresponds to Interval of Time between Events

• Events: points in time• State: interval of time; may correspond

to a continuous activity– E.g., waiting, ringing, and flying

• State may be associated with value of object satisfying some condition.– E.g., automobile transmission is in reverse.– E.g., balance of account is negative

13

Page 14: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

14

Identifying States

• Ignore attributes that don't affect behavior

• Combine set of attributes that form a parameter of the control– E.g., the control is not changed by

changing the digits in the phone call.

14

Page 15: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

15

Specifying States• Name (optional)• Internal activities (optional)

– Actions and activities performed while in the state– Predefined: entry, exit, do

• Internal transitions (optional)– Reaction within a state but without changing state

Brewing

do / brew coffee

pot removed/ suspendpot replaced/ resume

Name

Internal activities

Internal transitions

Page 16: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

16

Internal Activities and Transitions

• Actions and activities performed while in the state– Action: atomic– Activity: more complex behavior

• Action/activity label, specifying triggering conditions– Entry: performed on entry to state– Exit: performed on exit from state– Do: performed while in the state

• Internal transitions– Reactions to events that doesn’t

cause state changes (see transitions)

16

Attacking

entry/unsheathe sworddo/chargeexit/sheathe sword

enemy swings[distance<3 ft]/dodge

Page 17: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

17

Example

Starting

entry / start dial toneexit / end dial tone

Dialing

entry / number.append(n)

dialed (n)

dialed (n)

[number.isValid()]

Page 18: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

18

Events

• Something that happens at a point in time– No duration: instantaneous– At least we think of it this way when we model

(abstraction)

• Stimulus from one object to another• May result in a change of state• May result in event being sent to another

object

18

Page 19: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

19

Two Events:

• May be unrelated (concurrent)– E.g., flight 123 departs El Paso and flight

555 departs Las Vegas.

• May depend on each other (sequential)– E.g., flight 123 departs El Paso and flight

123 arrives Las Vegas.

19

Page 20: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

20

Transitions

• Relationship between two states indicating that a system (or object) in the first state will:– Perform certain actions and– Enter the second state when specified event

occurs and specified condition is satisfied.• Consists of:

– Source and target states– Optional event trigger, guard condition, and action

Sourcetrigger [guard] / action

Target

Page 21: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

21

Transitions (Cont.)

• Source state– State affected by transition

• Event trigger– Triggering the transition

• Guard condition– Boolean evaluated after the

event trigger. Transition only occurs if guard is true

• Action– Atomic operation that occurs as

the transition occurs• Target state

– State active after transition

21

Source

trigger [guard] / action

Target

Page 22: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

22

Example

keyPressed(key) [key == ENTER_KEY] / str := inputBuffer

Note that every transition should be labeled.

Sourcetrigger [guard] / action

Target

22

Page 23: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

23

Transitions (Cont.)

• Lead from one state to another• Instantaneous• Several types of events:

– A condition becomes true.– An explicit signal is received from an object.– An operation is called by an object.– A designated period of time passes.

23

Page 24: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

24

Phone Example

digit dialed (n)[incomplete]

Dialing Connecting

busy

connected

digit dialed (n)[valid] / connectdigit dialed (n)

[invalid]

Invalid

Ringing

Busy

24

Page 25: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

25

Protocol vs. Behavior Machines

• Protocol state machine– Specifies the intended pattern of calls on an object by specifying

• which operations of the object can be called in which state

• and under which condition, thus specifying the allowed call sequences on the object's operations.

– Describes an object's life cycle.

– Post-conditions are used instead of actions

• Behavior state machine– Expresses the behavior of part of a system, e.g., to define object

and operation behavior

– Actions are used instead of post-conditions

– States can have actions (entry, exit, do)

25

Page 26: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

26

Protocol State Machine

26

Not Overdrawn[balance >= 0]

Overdrawn[balance < 0]

credit(x)

debit(x)[balance - x >= 0]

credit(x)[balance + x >= 0]

credit(x)[balance + x >= 0]

debit(x)[balance - x < 0]

Page 27: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

27

Protocol State Machine

27

Not Overdrawn[balance >= 0]

Overdrawn[balance < 0]

credit(x)

debit(x)[balance - x >= 0]

credit(x)[balance + x >= 0]

credit(x)[balance + x >= 0]

debit(x)[balance - x < 0]

Account

-balance: Integer

+ balance(): Integer+ credit(x:Integer): void+ debit(x:Integer): void

Page 28: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

28

In Class: Digital Watch

• (Pairs) Draw a state machine diagram

The watch has a single mode button and a single advance button. Pressing the mode button once and then pressing the advance button increments the hours by 1. Each press of the advance button increments the hour. Pressing the mode button the second time allows advancing the minutes by one. Pressing the mode button a third time displays the current time. While displaying the current time, the advance button is ignored. Pressing the mode button allows the user to set the hour again.

28

Page 29: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

29

In Class: Toy Train (Pairs) Draw a state machine diagram

Initially, headlight is off, and train is not moving. If power on, headlight shines, and train moves forward. If power off, headlight goes out, but train still moves forward. If power on, headlight comes on, and train does not move. If power off, headlight goes off, and train stays stopped. If power on, headlight comes on, and train moves backward. If power off, headlight goes off, but train still moves backward. If power on, headlight comes on, and train doesn’t move. If power off, headlight off, and train remains stopped. Repeats from step 2 above.

Page 30: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

30

Structuring State Machine Diagrams

anomalyNormal Recovery

Identification

PressureRecovery

TemperatureRecovery

recovery success

recovery success

recovery success

temperatureproblem

pressureproblem

recovery failure

recoveryfailure

recoveryfailure

Can this diagram be presented better?

Unstructured!

Page 31: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

31

Better Presented:

anomaly

Normal RecoveryIdentification

PressureRecovery

TemperatureRecovery

recoverysuccess temperature

problempressureproblem

Recovery

recoveryfailure

31

Page 32: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

32

Composite States (Cont.)

• States containing one or more state diagrams• Used to simplify diagrams• Inside, looks like state machine diagrams• May have composite transitions• May have transitions from substates• May have "history states" (H symbol)

denoting the most recently occupied state• Sequential (OR) and parallel (AND) states

32

Page 33: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

33

Composite States and Transitions

Idle

Maintenance

Printing

Selecting Processing

Validating

Transition from substate

Transition to/from composite state

Active

33

H

Transition to history state

Page 34: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

34

Example of Composite State

34

Page 35: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

35

Including Composite States

Starting

entry / start dial toneexit / end dial tone

Partial Dialing

entry / number.append(n)

dialed(n)

dialedn)

[number.isValid()]

Dialing

Dialing Number

Include / Dialing

35

Page 36: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

36

Sequential (OR) and Parallel (AND) States

• States can be divided into concurrent parts, called “regions”.

• OR state: composite state with single region• AND state: composite state with multiple

regions– System is in all regions of the state, i.e., executes

concurrently.

36

Page 37: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

37

Example of AND State

37

Page 38: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

38

Synchronization

• Concurrency (split of control)

• Synchronization

substate1 substate2

Superstate

substate3 substate4

38

Page 39: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

39

Example

Idle

Command

Testing

Waiting

Diagnose

Join

ForkComposite state

Maintaining

Testing

Commanding

Concurrent regions

39

Page 40: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

40

Another Example

HW1 HW2

Incomplete

Project

Midterm Final

Passed

Failedfail

40

Page 41: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

41

Example of Synchronization

Remote Control

Off OnOn

Off

Play

Stop

On/Stop On/PlayOff

On

Off

Off/Stop

Play

Stop

MP3 Player

41

Page 42: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

42

In Class: Avoiding Combinatorial Explosion of States

(Pairs) A car can be in the state of moving or stop. It can also be clean or dirty. Model the states of a car:• without using parallel states• using parallel states

What will happen if we add a third property (say, Red vs. Blue) and a fourth property (say, Enclosed vs. Convertible)?

Page 43: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

43

In Class: Toy Train (Pairs) Draw a state machine diagram

Initially, headlight is off, and train is not moving. If power on, headlight shines, and train moves forward. If power off, headlight goes out, but train still moves forward. If power on, headlight comes on, and train does not move. If power off, headlight goes off, and train stays stopped. If power on, headlight comes on, and train moves backward. If power off, headlight goes off, but train still moves backward. If power on, headlight comes on, and train doesn’t move. If power off, headlight off, and train remains stopped. Repeats from step 2 above.

Page 44: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

44

In Class: Office Phone

• (Pairs) Draw a state machine diagram describing the operation of an office phone.

Assume that the phone has keys for the digits 0-9, #, and *. It can detect when the receiver is on-hook or off-hook. The phone is idle when the receiver is on-hook. Model phone calls.– making calls (e.g., dialing, connecting,

talking), – receiving calls (e.g., ringing, talking)

44

Page 45: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

45

Outline

Dynamic modelsState machine diagrams• Interaction diagrams

45

Page 46: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

46

Modeling Interactions

• Focus on communications among elements– Ordering of interactions– Messages and interfaces– Communication links– Timings of messages (between when

message sent and received)

46

Page 47: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

47

UML Interaction Diagrams

• Whole class of diagrams including– Sequence Diagrams

• The time order of interactions between objects

– Communication Diagrams• Messages passed between objects

– Interaction Overview Diagram• Overview diagram that hides much of the detail

– Timing Diagram • Interactions with precise time axis (for real-time or time-

sensitive systems)

47

Page 48: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

48

Sequence Diagrams

• Depicts object interaction by highlighting the time ordering of method invocations

• Describes a sequence of method calls among objects

• (This is the only interaction diagram we’ll look at.)

48

Page 49: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

49

Example

: Customer : Order : Payment : Product : Supplier

place an order

process

validate

deliver

if ( payment ok )

back order

if ( not in stock )

get address

mail to address

message

lifetimecontrol

object

sdPlaceOrder

49

Page 50: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

50

Example (Cont.)

: Customer : Order : Payment : Product : Supplier

place an order

process

validate

deliver

if ( payment ok )

back order

if ( not in stock )

get address

mail to address

Sequence of message sending

50

Page 51: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

51

Different Types of Messages

51

o: Class

Synchronous message

Asynchronous message

Return message

Creation message

Destruction message

<<create>>

<<destroy>>

Page 52: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

52

Return MessagessdPlaceOrder

:CustomerWIndow :Customer

ChangeUpdate

Return(dashed line,open arrow)

52

Page 53: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

53

Creation and Destruction

sdPlaceOrder

:CustomerWIndow

:CustomerChange<<create>>

getRating()

<<destroy>>

53

Page 54: Dynamic Modeling Grady Booch, James Rumbaugh, and Ivar Jacobson, The Unified Modeling Language User Guide, 2 nd edition, Addison Wesley, 2005.

54

In Class: Weblog Content Management System (CMS)

• (Pairs) Draw a sequence diagram for creating a new blog account.

The content management system allows an administrator to create a new blog account, provided the personal details of the new blogger are verified using the author credential database. A summary of the new blog account’s details should be mailed to the author.

54