91.3913Nov 2002 R. McFadyen1 Statechart Diagrams - Ch 29 Example: Suppose we have a class Copy,...

24
91.3913 Nov 2002 R. McFa dyen 1 Statechart Diagrams - Ch 29 Example: Suppose we have a class Copy, representing copies of books. We can consider two states: available, borrowed; and model how a single copy can move from one state to the other. Borrowed return borrow Available event transiti on another state Initial state
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    215
  • download

    0

Transcript of 91.3913Nov 2002 R. McFadyen1 Statechart Diagrams - Ch 29 Example: Suppose we have a class Copy,...

91.3913 Nov 2002 R. McFadyen 1

Statechart Diagrams - Ch 29

Example: Suppose we have a class Copy, representing copies of books.

We can consider two states: available, borrowed; and model how a single copy can move from one state to the other.

Borrowed

return

borrow

Available

eventtransitionanother

state

Initial state

91.3913 Nov 2002 R. McFadyen 2

Statechart Diagram

A Statechart Diagram describes

•states for an object

•how/why an objects’ state changes

A Statechart Diagram is a State Machine

Possible states are related to an objects attributes, related classes, operations

A State Diagram models an object’s decision about what it does when it receives a message

91.3913 Nov 2002 R. McFadyen 3

States, Events, Transitions

State - a condition or situation during the life of an object during which it satisfies some condition, performs some activity, or waits for some event.

Event - a noteworthy occurrence

•An event can trigger a state transition

Transition – a relationship between two states indicating that on object in the first state will perform certain actions and enter the second state when a specified event occurs and specified conditions are satisfied

91.3913 Nov 2002 R. McFadyen 4

State Machine

State Machine - a specification of the sequences of states that an object goes through in response to events during its life

•A State Machine is a graph of states and transitions that describes the response of an instance of a classifier to the receipt of events

•State Machines may be attached to classes, use cases, ...

91.3913 Nov 2002 R. McFadyen 5

State Diagram - General Form

state1 state2event [guard] /action

initial state

We choose to represent events that suit us - enabling a diagram to represent the level of detail we need

final state

91.3913 Nov 2002 R. McFadyen 6

Employee Example

Working On Break

Start break

End break

Suppose we have a class Employee, representing employees of our company

Two states: working and on break

91.3913 Nov 2002 R. McFadyen 7

Library Book Example

Now consider a class Book. Suppose there may be several copies of the book. Here, we’ll model the states Borrowable, and Not borrowable, which are related to books (not copies).

Not borrowable Borrowablereturned

borrowed[last copy]

returned

borrowed[not last copy]

91.3913 Nov 2002 R. McFadyen 8

Library Book Example

Not borrowable Borrowablereturned

borrowed[last copy]

returned

borrowed[not last copy]Note:

•when a Book receives the returned event, the next state depends only on the current state

•when a Book receives the borrowed event, the next state is determined by the guard

91.3913 Nov 2002 R. McFadyen 9

Selling Seats Example

available full

Consider a situation where tickets are being sold for a voyage. Tickets can be sold as long as there are seats available. At some point, all seats could have been sold and the voyage is full. Eventually, sales are closed and no more seats can be sold. We’ll consider states full and available for a voyage.

Seat sold [not last one]

Seat sold [last one]

Sales closedSales closed

91.3913 Nov 2002 R. McFadyen 10

Orders Example

Suppose we have an Order processing system:

Suppose Orders can be in these states:

•Checking: the Order Lines are being examined to see if the products are available or not

•Waiting: quantity on hand is not sufficient for some of the products

•Dispatching: all products are available in sufficient quantities

•Delivered: the Order has been delivered.

91.3913 Nov 2002 R. McFadyen 11

Orders Example

Checking Dispatching

DeliveredWaiting

Line checked[all items checked & all items available]

Line checked[not all items checked]/ get next item

Line checked[all all items checked & some not in stock]

Item received

[some item not in stock]

delivered

Item received [all items available]

91.3913 Nov 2002 R. McFadyen 12

Process Sale Use CaseRecall

We can construct a State Diagram showing states for this use case

91.3913 Nov 2002 R. McFadyen 13

Waiting For Sale; Entering Items; Waiting For Payment are System states that arise in the Process Sale use case.

Figure 29.2

Transitions are system events … the diagram clearly shows when events, such as makeNewSale, are legal.

91.3913 Nov 2002 R. McFadyen 14

Figure 29.3

Figure 29.2 extended to include the Authorizing Payment state.

91.3913 Nov 2002 R. McFadyen 15

State Diagram - POS Sale Example

Consider a Sale object.

What states does it go through?

•Incomplete, Completed, Paid ?

Incomplete Completed

Paid

Add a line item

Completed

Make payment

91.3913 Nov 2002 R. McFadyen 16

Consider a banking environment where an Account is considered active when it is first created (i.e. opened).

When a Customer performs a transaction (withdraws or deposits money), the balance in the Account is affected. If the Account balance becomes negative the Account is considered overdrawn.

As well as withdraw or deposit money, a Customer might close his/her Account. Only active accounts can be closed. Closed accounts can be reopened.

At some time in the future, after the Customer closed the account, the Bank might delete the Account (and it is gone forever).

In the example, events, guards, transitions and actions are shown.

Bank Account Example

91.3913 Nov 2002 R. McFadyen 17

active

Four States:

active overdrawnclosed deleted

overdrawn

deletedclosed

Bank Account Example

We’ll develop the example by considering each state, and the transitions that can occur when an account is in that state.

91.3913 Nov 2002 R. McFadyen 18

active

trx [pos]

overdrawntrx [neg]/warn()

closed

clos

e

An account begins by being Active. Once in the Active state, the account can remain active, become overdrawn, or be closed. {trx stands for “transaction” which represents a deposit or withdrawal}

Bank Account Example

91.3913 Nov 2002 R. McFadyen 19

active overdrawn

trx[pos] trx [neg]/warn()

close /warn()

From the Overdrawn state, an account can remain overdrawn, or move to the active state

Bank Account Example

[pos] is a guard that stands for “the account balance is positive”

[neg] …

91.3913 Nov 2002 R. McFadyen 20

deleteddelete

closed

From the Closed state, an account can move to the Deleted state; it can also be made active again.

Deleted is considered the final state for an accountre

open

active

Bank Account Example

91.3913 Nov 2002 R. McFadyen 21

active

trx [pos]

overdrawntrx [neg]/warn()

deleteddelete

closed

clos

e

trx [pos] trx [neg]/warn()

close /warn()

reop

en

All together:

Bank Account Example

91.3913 Nov 2002 R. McFadyen 22

Example with substates

Suppose we are modeling machines in a factory. At one level of abstraction machine has three states: waitingForRepair, inRepair, and inService. When a machine is in service, there are four states that it could be in: standingBy, accelerating, running, or decelerating.

We can depict these concepts separately, and we can also combine them: first, the states when a machine is in service

standingBy accelerating running

decelerating

stop machine

start machine

91.3913 Nov 2002 R. McFadyen 23

Example with substates

Considering inService, inRepair, waitingForRepair:

inService

inRepair

waitingForRepairRemove from service

Begin maintenance

Return to service

91.3913 Nov 2002 R. McFadyen 24

Example with substates

Combining with substates

inRepair

waitingForRepair

inService

standingBy

accelerating

running

decelerating