statemachine (1)

29
STATE MACHINE MODEL

Upload: ghaziashahid

Post on 09-Jul-2016

213 views

Category:

Documents


0 download

DESCRIPTION

its about the state diagram how to design machines and input ouputs

TRANSCRIPT

Page 1: StateMachine (1)

STATE MACHINE MODEL

Page 2: StateMachine (1)

State Machine View describes the dynamic behavior of

objects over time each object is treated in isolation the view describes the events and operations that manipulate the object

the state view of the entire system is described by a collection of state diagrams, one corresponding to each class in the system

Page 3: StateMachine (1)

About State Diagram one state diagram per class in the

class diagram one logical state diagram may be

spread out into one or more physical diagrams for space considerations

enrichment of the finite state machine model

based on David Harel’s State charts, tailored towards object-orientation

Page 4: StateMachine (1)

Main components of a state diagram

a collection of states of the object under consideration

a collection of actions while entering a state, existing a state or within a state

a collection of transitions between the states of the object

a collection of events that trigger transitions

a collection of conditions (optional) that might constrain the triggering of events, or implementations of transitions

Page 5: StateMachine (1)

State diagram – basic syntax

initial

final

State name

Unnamed states

entry/ …

exit/ …

do/

event (params) [guard] /

action (params)

actions

actions

event (params)

event (params)

Page 6: StateMachine (1)

State diagram - semantics there must be exactly one initial state and

one or more final states a transition between states is represented

by the event that triggers the transition (a more concrete definition given later)

transitions may have guards or conditions under which the transitions fire

the actions associated with a transition will be executed as soon as the transition fires

Page 7: StateMachine (1)

State diagram – semantics (continued)

a state may optionally have a label every state may have

an entry action – executed as soon as the state is entered

an exit action – executed just before leaving the state

a “do” action – executed while the object is in this state; may be ongoing until the object leaves the state

Page 8: StateMachine (1)

State – a formal definition is a condition during the life of an object

during which the object performs an action or waits for some event

is represented by the collection of attributes and their corresponding values

an object after being created must be at one particular state at any instant unless otherwise mentioned, an object

remains in a state for a finite time UML allows modeling of transient states

(states that exist only for a very short and insignificant duration)

Page 9: StateMachine (1)

State – a formal definition (continued)

(directly or indirectly) includes links (instances of associations) connected with the object at that instant

may be decomposed into concurrent substates (AND relationship)

may be composed using mutually exclusive disjoint substates (OR relationship)

Page 10: StateMachine (1)

Event – a formal definition a noteworthy occurrence something that happens within the system or

interacting with the system at an instant something that has a significant impact on the

system examples

sending a signal or data receiving a signal or data making a request for execution a Boolean condition becoming true a timeout condition becoming true …

Page 11: StateMachine (1)

Four types of events in UML signal event

occurs when an object sends a signal to another object

call event occurs when a method or operation in

an object is invoked change event

occurs when a Boolean condition is changed

time event occurs when a time limit has reached

Page 12: StateMachine (1)

Representation of eventsevents are represented by unique labels in the diagram

sometimes the transitions that are fired by the occurrence of the event is also represented by the same label

change event labels are preceded by the keyword “when”

time event labels are preceded by the keyword “after”

Page 13: StateMachine (1)

Generating an Event an event is generated by the

runtime system asking for inputs producing outputs execution of a method transfer control of execution from one object to another (sending messages or receiving messages)

abnormal termination error handling, exceptions

Page 14: StateMachine (1)

Transitionrepresents the change of states of an object switch from “Joined University” to “Registered for Fall”

is an abstraction of an operation registering for a course

has finite and significant duration time taken to complete registration of one course

Page 15: StateMachine (1)

Transition (continued) may have parameters

a transition corresponding to the registration process may have “course name” and “prerequisites” as parameters.

is triggered/invoked/fired by the occurrence of an event change of semester from “Summer” to “Fall” may initiate the registration process

request by administration or department may initiate the registration process

Page 16: StateMachine (1)

Transition (continued) may have a guard/condition

transition for registration may require that the student must have his/her ID validated

transition for graduation may require previous library dues to be paid off

transition to admit new students may require that the students can register only after officially admitted into the program

an event may cause several transitions to fire completion of registration process may cause

the course object to update its enrolment and at the same time, the account object to update the tuition fees to be paid

Page 17: StateMachine (1)

Transition (continued) is normally identified by the same label

as the event that fires the transition “register”, “withdraw”, …

may be associated with an action different from actions associated with the states

“register” may have an action to check the validity of parameters, to check that no previous registration has been done for the same set of parameters etc.

Page 18: StateMachine (1)

Example 2 – State diagram for Account class in ATM

initial Normal

Overdraft

deposit / deposit()

withdraw [amt = balance] / withdraw()

deposit/ deposit()

withdrawNormal [amt < balance] / withdraw()

withdrawInitial / withdraw()

open/ open()

close

/ clo

se()

closeAC [amt=-balance] / deposit()

depositOP [amt=-balance]/ deposit()

depositOP [amt < -balance] / deposit()

withdrawOP / withdraw()

Page 19: StateMachine (1)

Communications between objects

Objects communicate by sending messages to each other.

A message is realized as an event/transition in a state diagram.

The object that sends the message is said to generate an event. Modeled by the action associated with the transition

The object that receives the message is said to realize / accept that event.

Page 20: StateMachine (1)

Simple and composite states

A state is composite, in contrast to a simple state, if it has a graphical decomposition

A diagram for a composite state has two or more sub-diagrams connected by simple and/or concurrent transitions Example: concurrent states

The word “superstate” is used sometimes to refer to a composite state

Page 21: StateMachine (1)

Composite State – basic syntax

Simple State Simple State

Page 22: StateMachine (1)

Composite State – Transparent Transitions

Simple State Simple State

Page 23: StateMachine (1)

Composite State - Example

CD drawer closed

CD drawer open

No CD Loaded

CD loaded

CD playing

CD paused

CD stopped

Power ON [no CD]

Power ON [CD in]

Power OFF

Power OFF

Eject

Eject [no CD]

EjectEject [CD in]

Pause

Pause or Play

Stop

Play

Stop

Page 24: StateMachine (1)

Observations The state diagram for the CD player

example indicates a composite state that includes two MUTUALLY EXCLUSIVE states The player will be in only one of these two

substates at any time, but not in both at the same time

The guard condition on the initial state chooses the appropriate substate upon Power ON

There is only one terminal state that is common to both the substates

Page 25: StateMachine (1)

Abstract view of the CD player

No CD loaded

CD loadedPower OFF

Power ON [no CD]

Power ON [CD in]

Eject [CD in]

Eject

Page 26: StateMachine (1)

Composite State with Concurrent Transitions

Composite State

Concurrent transition

Concurrent transition

Page 27: StateMachine (1)

Concurrent States - example

Incomplete

passed

failed

Lab 1 Lab 2

Project

Final exam

Lab done

Lab done

Project done

pass

fail

Student attending a course

Page 28: StateMachine (1)

Observations If a composite state has concurrent

substates, an entry point to the composite state represents a concurrent transition, even if it is not represented

an exit point from the composite state represents a concurrent transition, even if it is not represented

all concurrent substates start at the same time

the composite state terminates only when all the concurrent substates terminate

Page 29: StateMachine (1)

Observations (continued) if there is a transition from any one of the substates that lead to a state outside the composite state, then all the other concurrent substates terminate prematurelythe transition “fail” in the third substate illustrates this situation