State diagrams (2)

UML : Unified Modeling Language. • Use cases. • Class Diagram. • Interaction Diagrams. • State Diagrams. • Activity Diagrams. • based on Frank Maurer lecture, ...
56KB taille 9 téléchargements 297 vues
UML : Unified Modeling Language • • • • •

Use cases Class Diagram Interaction Diagrams State Diagrams Activity Diagrams

• based on Frank Maurer lecture, Univ. of Calgary (http://sern.ucalgary.ca/Courses/CPSC/333/W99/CS333.html)

• Recommended: UML distilled… (Addison Wesley) UML- State Diagrams 1

State diagrams • State diagram: Shows the behavior of one object (of one class): – how does it change its state based on the messages it receives – narrowly focused, fine-grained

• Other names – State transition diagram – Harel diagram (statecharts)

UML- State Diagrams 2

State diagrams (2) • State: condition/situation during lifetime of an object • State transition: relationship indicating a state change – atomic & non-interruptible

• Action: – atomic & non-interruptible

UML- State Diagrams 3

State notation (1) • Substates: disjoint/concurrent • Entry/exit actions – entry: an action that is performed on entry to the state – exit: an action performed on exiting the state

State name state variable(s) entry: entry action do: activity-A on: event-A: action-A exit: exit-action

• do: an ongoing activity performed while in the state (example: display window) – interruptible

• on: an action performed as a result of a specific event

UML- State Diagrams 4

Transition notation (2) Event(arguments)[condition]/action

State-A

State-B

• Event: significant occurrence that has a location in time and space – triggers the transition – signals, calls, passing of time, change in state

• Guard condition: • Transition only eligible to fire when guard evaluates to true • Guards of transition exiting one state are mutually exclusive

• Action: executable atomic computation UML- State Diagrams 5

State diagram notation (3) Initial state

Event(attribute)

State-B

• Start state – No event triggers allowed – branch conditions allowed – may not remain in start states

• End state – Top level end state terminates a state machine UML- State Diagrams 6

State Diagram and Use Cases • When we receive an order, we check each line item of the order to see if we have the goods in stock. If we do, we assign the goods to the order. If this assignment sends the quantity of goods in stock below the reorder level, we reorder the goods. While we are doing this, we check to see if the payment is OK. If the payment is OK and we have the goods in stock, we dispatch the order. If the payment is Ok but we do not have the goods, we leave the order waiting, if the payment is not OK, we cancel the order.

UML- State Diagrams 7

• When a supply delivery comes in, we look at the outstanding orders and decide which ones we can fill from this incoming supply. We then assign each of these to its appropriate orders. Doing this may release those orders for dispatching. We put the remaining goods in stock.

UML- State Diagrams 8

State transitions for an order get next item[ not all items checked ]

/ get first item

Item received[ some items not in stock ]

[ All items checked && some items not in stock ]

Checking

Waiting

do: check item

[ All items checked && all items available ]

Item received[ all items available ]

Dispatching

Delivered

do: initiate delivery

UML- State Diagrams 9

Delivered

Problem: Cancel the order • Want to be able to cancel an order at any time • Solutions – Transitions from every state to state “cancelled” – Superstate and single transition

UML- State Diagrams 10

Transitions to “cancelled” get next item[ not all items checked ]

/ get first item

Item received[ some items not in stock ]

[ All items checked && some items not in stock ]

Checking

Waiting

do: check item

[ All items checked && all items available ]

Item received[ all items available ] cancelled cancelled

Dispatching do: initiate delivery

cancelled Cancelled

Delivered

Delivered

UML- State Diagrams 11

State diagram notation (4) Superstate

Event A

State-A

Event B

UML- State Diagrams 12

State-B

Event C

Superstate Active

get next item[ not all items checked ]

/ get first item

Checking do: check item

Item received[ some items not in stock ]

[ All items checked && some items not in stock ]

Waiting cancelled

Cancelled

Item received[ all items available ]

[ All items checked && all items available ]

Delivered Dispatching do: initiate delivery

Delivered

UML- State Diagrams 13

Some remarks • Only one initial state may occur (directly) within a composite state • End state represents completion of a composite • End state triggers transition with composite as source

UML- State Diagrams 14

Concurrent state diagram • Unrelated components of objects combinatorial number of states • Example: Car states 4 car states: started_open started_closed stopped_open stopped_closed

– engine (started, stopped) – doors (open, closed)

• What happens when we add one component? – seat belt (fastened, open) 8 car states: started_open_open started_closed_open stopped_open_open stopped_closed_open

started_open_fastened started_closed_fastened stopped_open_fastened stopped_closed_fastened

UML- State Diagrams 15

Example: Payment authorization in class Order

Authorizing do: check payment

2 parallel processes: - authorization - order handling

[ payment not ok ]

[ payment ok ] Rejected

Authorized

Delivered

UML- State Diagrams 16

Concurrent state diagram for the class Order Cancelled

cel can

Waiting

Checking

Dispatching

Delivered

Authorizing

Authorized

[

[Payment not OK]

Rejected

UML- State Diagrams 17

Rules of thumb • Not every class needs a state diagram • Often: State diagram not very complex • State diagrams are often used for UI and control objects • Not to many concurrent sets of behavior occurring in a single object (in that case: split into separate objects)

UML- State Diagrams 18

TBu i l d e r 3 0 /1 1 /2 0 0 1 Si tu a ti o n d 'u n ter ra i n

- nom des états -gestion du nombre des maisons -… Acheter /Achat_propriété( ) libre a ch e té Vendre /Vente_propriété( ) Rés ilier hypothèque /Résilier_propriété( )

hypothèquer /Hypothéquer_propriété( )

Construire /Achat_batim ent( )

h yp o th é q u é Destruire /Vente_batim ent( )

b a ti m e n t

Destruire_hotel /Vente_batiment( )

struire_mais on(Nbre2]

Construire hôtel [si il y a 4 maisons] Te rra i n a ve c m a i s o n (s )

UML- State Diagrams 20

achat_terrain_joueur /changer_proprio(j:joueur)

DISPON IBL E e n try:p ro p ri é ta i re =0

acheter_terrain /propriétaire=num_joueur

AC H ETE e n try:n b _ m a i s o n s =0

vente_construction [nb_maisons=1] /nb_maisons=nb_maisons-1 achat_construction [nb_maisons=0] /nb_maisons=nb_maisons+1

CON STR U ITS

achat_construction [1