Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

35
Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    3

Transcript of Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

Page 1: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

Modeling Behavior[A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

Page 2: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

2

Web-voting system

System

user

Vote

Show result

UC: VoteMain flow:1. starts when the user sends a vote.2. if deadline is passed stop.3. System validates the vote. 4. if valid, system adds the vote, else not.

UC: Show resultMain flow:1. if deadline is not passed show nothing.2. Else show counting.

Page 3: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

3

: Counter

Working it out

Voteval : boolhash : int

Counterdeadlineadd(vote)isValid(vote)getCount() : int

*

votes

System

user

Vote

Show result

: System

opt [ok] add(v)

vote(v)

ok = isValid(v)opt [deadline not passed]

user

Page 4: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

4

Modeling “behavior”

• Static aspect of your system:– The signature of each class– Inheritance and association between classes

• Well modeled by class diagrams• So far we only have “sequence diagram” to model

behavior/dynamic

SD does not shows us well that objects can be in different states; and that may behave differently on different states. This can be an important aspect that you want to express in your model, e.g:

The system should keep counting, until the deadline.

This is implied by the use case, but is not very explicit in the subsequent SDs.

Page 5: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

5

State machine

• In the simplest form, it consists of a set of “states”, and (guarded) transitions between states:

• UML’s state machine allows richer concepts of state and transition. E.g. a state can be hierarchical...

accepting closed

add(v)

close()

getCount()Counter

Page 6: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

6

UML’s state machine

• Context

• Protocol SM to model which events are possible on different states, and declaratively specify the effect of those events.

• Behavioral SM to imperatively model how the context behave

• Different intent, also slightly different syntax • Which one is more abstract? it depends...• In practice people don’t typically make the distinction.

s t

Counter An SM defines something about its context. E.g. the behavior of every Counter object.

Page 7: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

Transition (behavioral SM)

7

S Tvote(v) [ v is valid ] / add(v)

The transition fires if all these conditions are met:• The state S is “active”• The triggering event occurs• The condition is true(That’s the case if the machine is deterministic; if it is not then ... see A&N)

Upon firing we do these, in this sequence: • “S is exited”• the action above is executed• the state “T is entered”

triggering event

condition

action

If no event is specified, then it is an auto transition (in the sense that it needs no event to occur.

Page 8: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

Transition (Protocol SM)

8

S T[ v is valid ] vote(v) / [ v is added ]

triggering eventpre-condition

post-condition

In protocol SM, a transition has no action. Rather, if the transition has some work to do, you declaratively specify it as a post condition.

Page 9: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

State (behv. SM)

9

Openentry/ actionexit/ actiondo/ activity

state’s name atomic

not atomic

• When a state is entered it becomes active and executes its entry action and do-activity.• “action” is atomic (cannot be interrupted), “activity” is not atomic.• You cannot leave the state before the entry-action is done.

• When a state is exited, it becomes passive.• The exit action is executed, and the do-activity is stopped.• Outgoing transition must wait until the exit action is done.

• States of Protocol SM do not have action nor activity.

You may have multiple active states in a machine that contains concurrent components.

Page 10: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

10

Example

Open

CounterisDeadlinePassed()add(vote)isValid(vote)getCount() : int

[isValid(v) ] add(v)

Closedwhen(deadline)

getCount()

Counter

Voteval : boolhash : int

*votes

The above (with post-condition) is a protocol SM. In a behavior SM, we can’t have a post-condition. So we have to “refine” the post-condition “votes.contains(v)” above to actions that realize it. E.g. replacing the transition with:

[not isDeadlinePassed() && isValid(v) ] add(v) / votes.add(v)

would turn the above to behavioral SM.

[ votes.contains(v) ]

time event

Page 11: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

11

Junction and choice pseudo-state

Open Closedwhen(deadline) getCount()

add(usr,v)

[isValid(v) ] / votes.add(v) ; result = ok

[! isValid(v) ] / result = reject

notify(usr,result)

• Guards should be disjoint• Compound transition

Page 12: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

12

Adding a reminder functionalitySystem

user

Vote

Show result

UC: RemindMain flow:1. starts when the system starts.2. sends reminder to all users.3. one day before deadline, send

reminders again.

Remind

Open Closedwhen(deadline)

getCount()

Waitingwhen(deadline – 1d)

/ remindUsers()

/ remindUsers()

vote(v)

On-line

This is called composite state, consisting of parallel regions, each has its own sub-machine.

Page 13: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

13

To watch out: exit protocol of composite state is a bit complicated...

Initializing On-lineno guarding condition not event auto-transition

X

Handling Error

initFail()

restart()

This transition is inherited to the regions, and through to the states in each region. Effective, this is as if this transition exists from each state within the composite state.

Page 14: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

14

Example: modeling ReversiGamerestart()w(square) // white moveb(square) // black movewok() : bool // white can movebok() : bool // black can movewok(square) // white can move on the square announceWinner() : String

white’s turn

black’s turn

[bok(s)] b(s)[wok(s)] w(s)

[!wok() && bok(s)] b(s)

[!bok() && wok(s)] w(s)

[!bok() && !wok()] / announceWinner()

[!bok() && !wok()]

/ announceWinner()

game over

rest

art()

Page 15: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

15

Designing UI: Blah vs Bol

See e.g. course “Usability Engineering”

Page 16: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

16

User Interface design

determineneed

build prototype

evaluate prototype

(Ambler, Agile Model-driven Development with UML 2.0)

Visual Design in Eclipse“essential design” with flip charts, pens, and stickies. (Ambler)

major UI element (screen or report)

elements of a major UI element is called minor UI elements (buttons, forms, etc)

Page 17: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

17

UI Flow Diagramto model navigation between major UI elements.

Registration page

PurchaseBrowse items pageMain page

search btn

login btn,logout btn

regi

ster

btn

subm

it bt

n

next btn,prev btn

buy btn

ok btn

(modeled with a state diagram)

Page 18: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

18

Modeling “behavior” in UML

• State machine: model behavior in an event-driven way, good from perspective of a class

• Sequence diagram: model behavior as sequence of collaboration; good from perspective of actor

• Activity diagram: model behavior as sequence of activities; good from perspective of actor; in theory, natural for streaming way of processing, but no direct mapping of this aspect to e.g. Java.

: System b : Basket

getTotalPrice()buy()

totprice

c : Customer

charge(totprice)

user

Page 19: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

19

Activity diagram

• Activity : consists of actions and flows• Action : do some work

– unit level : calling an operation, treated as “atomic” (it means here that it is undecomposable into more “actions”).

– representing another activity; non-atomic

• Flow : control execution sequence, and transport data

eat sleep

this is an action (!), not a state

One day of my life

Page 20: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

20

Token semantic

calculate price

charge customer ship

Buy

• Used to be a special case of SM (UML 1)• Since UML 2 it has a “token-game” semantic just for defining execution sequence, does not oblige you to implement that way.• Fork and join • Inherently concurrent; e.g. multiple actions can be active even at the beginning• Notice the stream-way of processing

An activity diagram consists of “nodes” and “edges/flows”.

action node

<<localPrecondition>>destination in Europe

<<localPostcondition>>item shipped

Page 21: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

21

Control flow and object/data flow

eat sleep

calculate pricecalculate

taxint int

control flow transports control tokens, which carries no data

object/data flow carries object tokens, which have data

calculate pricecalculate

tax

int int

Alternate syntax, with “pins”:

Page 22: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

22

When do they fire ?

• Atomic action node: only when all its incoming flows have tokens, and the node’s pre-cond is satisfied. Produce tokens on all out-flows.

• Activity : depends on what its actions do (e.g. fig 14.23 A&N)

• A “call” action node, is not atomic, so it doesn’t have to, in principle, wait for all tokens to be available on its incoming pins.

A

A B

C

p

q

rThis activity can start executing when there are tokens: (1) at q will fire C, or (2) at p and q will fire A. If B is an atomic action, token at r cannot fire B, because it would need tokens from the other incoming edges as well.

Page 23: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

23

Calling another activity

this is an action Activity B |

|||

Activity A

Charge Ship | |||

Buy

Item [not wrapped]

Address

ShipmentManifest

| |||

pin

Page 24: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

24

Few more constructs

calculate price

charge customership

Buy

validatecredit card

validatedelivery address

[ok][ok]

[else]

[else]

Purchase ValidationShipment

implicit forking here

• Decision node, merge node• Invoking other activity diagram • Semantic of multiple out-flows and multiple in-flows • Swim lanes• Activity termination vs flow termination

||||

XX

Page 25: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

25

Example

http://www.youtube.com/watch?v=hr7leZlxmJo

• Each player (human or AI) has a set of attack and defense moves.• Use case PLAY, prim. actor: human player• At each round each player assess its situation then

chooses a move.• The moves are resolved.• This is cycled over until one is down to 0 hp.

P1 asses situation

P1 chooses move

P2 asses situation

P2 chooses move

resolve

Round-based approach:

PlayerGame MoveDefenseMove

AttackMoveKick

SwordSlash2 *

Human Player

Page 26: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

26

Expansion node

get items and calculate price

charge customer

wrap item

ChargeShip

hand overto courier

Set of Item[not wrapped]

ShipmentManifest

Address

pack all

Here, “get items and calculate price” also produces the set of items to be shipped, and a single destination address.

This activity will wrap the items one by one, then pack them all in a single package, then hand it over to a courier.

Expressing a constraint on the objects’ state: only objects of that state will be taken into this object node.

iterative

Page 27: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

27

Modeling error handling

Charge

Ship | |||

Buy

Panic Log

Interruptible region. All activities there are terminated when a token is placed on the interrupt edge.

This takes the role of exception handler.

| |||

Page 28: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

28

BPMN

“Business Process Modeling Notation” ; popular modeling language for business process.

Some similarities with UML’s activity diagram:• maintained by OMG• token-based semantic; but not the same!• you can check its language spec. at OMG

Beyond our scope; but to give you a summary, main modeling elements:

activitysequence flow

event

gatewaymessage flow

Page 29: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

29

BPMN: example

(!?)

(!?)

cancelationerror

(taken from BPMN spec doc.)

Page 30: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

30

Not used, future backup

Page 31: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

31

Example: ReversiLet’s make this more interesting :• Connectivity can be different• Squares can be distributed over Internet• Square may become disconnected

NE

NW

jumpy

Game

SquarebestFlip(clr) : Flip flip(flip)

LinkflipGain(clr) : intflip(clr)

*

*

W-link

N-link

Jump-link

1

1 from

to Flipclr : Colorgain : int

1

1

Playermove() : bool*

Page 32: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

32

Top level activity

white player moves

black player moves

check2x pass

Play reversi (turn-based version)

decide winner

check2x pass

| |||

| |||

Page 33: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

33

Player X moves

requestflips

set of requestpropose flip

at flip.sq

Requestsq : Square // request for flip at this squareflip : Flip // respond by setting this flip

find best flip

some timeout, in case the sq is disconnected

parallel

move

Page 34: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

34

Which one do you like ?: Counter: System

opt [ok] add(v)

vote(v)ok = isValid(v)

vote(v) / ok = counter.isValid(v) [ok]/counter.add(v)

[!ok]

:System

validate vote

add vote

[false]

[true]Vote

Vote

Page 35: Modeling Behavior [A&N 14, 15.1-15.5, 15.8.2, 15.11, 21,22.1-22.3]

35

Certain things are more natural in approach X

Open

[isValid(v) ] vote(v) / votes.add(v)

Closedwhen(deadline)

Counter

: Counter

opt [valid(v)]

Open

vote(v)

votes.add(v)loop [open]

par

ClosedA >= deadlineA