28
States States

States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

Embed Size (px)

Citation preview

Page 1: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

StatesStates

Page 2: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

2

IntroductionIntroduction A state machine …A state machine …

– Models the behavior of an individual object Models the behavior of an individual object (statechart diagram) or a society of objects (statechart diagram) or a society of objects (activity diagram).(activity diagram).

– Is a dynamic model of the system.Is a dynamic model of the system.– Is event driven.Is event driven.

Events trigger activities which, in turn, trigger Events trigger activities which, in turn, trigger actions.actions.

Actions are atomic.Actions are atomic. Actions may cause the return of a value or the Actions may cause the return of a value or the

change of state of an object.change of state of an object.

– State machines come in two varieties:State machines come in two varieties: Activity diagrams.Activity diagrams. Statechart diagrams.Statechart diagrams.

Page 3: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

3

IntroductionIntroduction

Page 4: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

4

Terms and ConceptsTerms and Concepts States States

– A state is a condition or situation A state is a condition or situation during the life of an object in which it during the life of an object in which it satisfies some condition, performs satisfies some condition, performs some activity, or waits for some some activity, or waits for some event.event.

– A state may include …A state may include … NameName Entry/exit actionsEntry/exit actions Internal transitionsInternal transitions ActivitiesActivities Substates - may sequential or concurrentSubstates - may sequential or concurrent Deferred events (infrequently used)Deferred events (infrequently used)

Page 5: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

5

Terms and ConceptsTerms and Concepts

States States – Special categories of statesSpecial categories of states

Initial stateInitial state - indicates the initial starting - indicates the initial starting state for for the state machine or a state for for the state machine or a substate.substate.

Final stateFinal state - indicates the state - indicates the state machine’s execution has completed.machine’s execution has completed.

– Real-time state machines frequently do not Real-time state machines frequently do not include a final state.include a final state.

Neither initial or final states contain any Neither initial or final states contain any of the parts found in traditional states.of the parts found in traditional states.

Page 6: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

6

Terms and ConceptsTerms and Concepts

Page 7: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

7

Terms and ConceptsTerms and Concepts TransitionTransition

– A directed relationship between two states.A directed relationship between two states.– A flow of control through a statechart diagram.A flow of control through a statechart diagram.– Contains five partsContains five parts

Source state - current state before Source state - current state before transition fires.transition fires.

Event trigger - external stimulus that has Event trigger - external stimulus that has the potential to cause a transition to fire. the potential to cause a transition to fire.

Guard condition - a condition that must be Guard condition - a condition that must be satisfied before a transition can fire.satisfied before a transition can fire.

Action - an executable atomic computation.Action - an executable atomic computation. Target state - new state after transition Target state - new state after transition

fires.fires.

Page 8: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

8

Terms and ConceptsTerms and Concepts

Page 9: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

9

Terms and ConceptsTerms and Concepts

Advanced States and TransitionsAdvanced States and Transitions– Entry actionEntry action - Upon each entry to a state, a - Upon each entry to a state, a

specified action is automatically executed.specified action is automatically executed. Syntax (to be placed inside the state symbol): Syntax (to be placed inside the state symbol):

entry / actionentry / action

– Exit actionExit action - Just prior to leaving a state, a - Just prior to leaving a state, a specified action is automatically executed.specified action is automatically executed.

Syntax (to be placed inside the state symbol): Syntax (to be placed inside the state symbol): exit / actionexit / action

Page 10: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

10

Terms and ConceptsTerms and Concepts

Advanced States and TransitionsAdvanced States and Transitions– Internal TransitionsInternal Transitions - The handling of an - The handling of an

event without leaving the current state.event without leaving the current state. Used to avoid a states entry and exit actions.Used to avoid a states entry and exit actions. Syntax (to be placed inside the state symbol): Syntax (to be placed inside the state symbol):

event / actionevent / action

– ActivitiesActivities - Ongoing work that an object - Ongoing work that an object performs while in a particular state. The performs while in a particular state. The work automatically terminates when the work automatically terminates when the state is exited.state is exited.

Syntax (to be placed inside the state symbol): Syntax (to be placed inside the state symbol): do / activitydo / activity

Activities are ongoing operations; actions are Activities are ongoing operations; actions are instantaneous operations.instantaneous operations.

Page 11: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

11

Terms and ConceptsTerms and Concepts

Page 12: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

12

Terms and ConceptsTerms and Concepts

Advanced States and TransitionsAdvanced States and Transitions– Deferred EventDeferred Event - An event whose - An event whose

occurrence is responded to at a later occurrence is responded to at a later time.time.

Syntax (to be placed inside the state Syntax (to be placed inside the state symbol): symbol): event / deferevent / defer

Once an event has been deferred it remains Once an event has been deferred it remains deferred until a state is entered where that deferred until a state is entered where that particular type of event is not listed as particular type of event is not listed as deferred. The state diagram then responds deferred. The state diagram then responds to the event as if it had just occurred.to the event as if it had just occurred.

Page 13: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

13

Terms and ConceptsTerms and Concepts Advanced States and TransitionsAdvanced States and Transitions

– Simple stateSimple state - A state that contains no - A state that contains no substatessubstates..

– Composite stateComposite state - A state that contains - A state that contains substatessubstates..

– SubstateSubstate - A state that is nested inside - A state that is nested inside another state.another state.

Substates allow state diagrams to show Substates allow state diagrams to show different levels of abstraction.different levels of abstraction.

Substates may be sequential or concurrent.Substates may be sequential or concurrent. Substates may be nested to any level.Substates may be nested to any level.

Page 14: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

14

Terms and ConceptsTerms and Concepts

Advanced States and TransitionsAdvanced States and Transitions– Sequential SubstatesSequential Substates - The most - The most

common type of substate. Essentially, common type of substate. Essentially, a state diagram within a single state. a state diagram within a single state.

The “containing” state becomes an The “containing” state becomes an abstract state.abstract state.

The use of substates simplifies state The use of substates simplifies state diagrams by reducing the number of diagrams by reducing the number of transition lines.transition lines.

A nested sequential state diagram may A nested sequential state diagram may have at most one initial state and one final have at most one initial state and one final state.state.

Page 15: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

15

Terms and ConceptsTerms and Concepts

Page 16: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

16

Terms and ConceptsTerms and Concepts Advanced States and TransitionsAdvanced States and Transitions

– History StatesHistory States - Allows an object to - Allows an object to remember which substate was last active remember which substate was last active when the containing state was exited.when the containing state was exited.

Upon re-entry to the containing state, the Upon re-entry to the containing state, the substate that was last active will be re-entered substate that was last active will be re-entered directly.directly.

Page 17: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

17

Terms and ConceptsTerms and Concepts Advanced States and TransitionsAdvanced States and Transitions

– Concurrent SubstatesConcurrent Substates - Used when two - Used when two or more state diagrams are executing or more state diagrams are executing concurrently within a single object.concurrently within a single object.

Allows an object to be in multiple states Allows an object to be in multiple states simultaneously.simultaneously.

The concurrent state diagrams within a The concurrent state diagrams within a “containing” state must begin and end “containing” state must begin and end execution simultaneously.execution simultaneously.

If one concurrent state diagram finishes If one concurrent state diagram finishes first, it must wait for the others to first, it must wait for the others to complete before exiting the containing complete before exiting the containing state.state.

Page 18: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

18

Terms and ConceptsTerms and Concepts

Page 19: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

19

ConcurreConcurrent State nt State

Diagrams Diagrams Using Using OMT OMT

NotationNotation

Page 20: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

20

State Diagram for a Phone State Diagram for a Phone LineLine

Page 21: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

21

Event Event Trace for Trace for an ATM an ATM

TransactioTransactionn

Page 22: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

22

State Diagrams ATM, Bank, and State Diagrams ATM, Bank, and ConsortiumConsortium

Page 23: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

Statechart DiagramsStatechart Diagrams

Page 24: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

24

IntroductionIntroduction A statechart diagram, also known as A statechart diagram, also known as

state diagram, models the dynamic state diagram, models the dynamic aspects of the system by showing the aspects of the system by showing the flow of control from state to state for a flow of control from state to state for a particular class.particular class.

Every class will have its own statechart Every class will have its own statechart diagram.diagram.– In practice, only classes with interesting or In practice, only classes with interesting or

complex internal behavior will be modeled complex internal behavior will be modeled with statechart diagrams.with statechart diagrams.

– Statechart diagrams are not shared among Statechart diagrams are not shared among classes.classes.

Page 25: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

25

IntroductionIntroduction Whereas an activity diagram models Whereas an activity diagram models

the flow of control from the flow of control from activity to activity to activityactivity, a statechart diagram models , a statechart diagram models the flow of control from the flow of control from event to eventevent to event..

Statechart diagrams show how an Statechart diagrams show how an object reacts to external events or object reacts to external events or conditions during the course of its conditions during the course of its lifetime.lifetime.

Page 26: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

26

Terms and ConceptsTerms and Concepts State State

– A state is a condition or situation during the A state is a condition or situation during the life of an object in which it satisfies some life of an object in which it satisfies some condition, performs some activity, or waits condition, performs some activity, or waits for some event.for some event.

TransitionTransition– A transition is a relationship between two A transition is a relationship between two

states indicating that an object in the first states indicating that an object in the first state will perform certain actions and enter state will perform certain actions and enter the second state when certain events and the second state when certain events and conditions have occurred. conditions have occurred.

Page 27: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

27

Terms and ConceptsTerms and Concepts EventEvent

– An event is the specification of a significant An event is the specification of a significant occurrence that has a location in time and occurrence that has a location in time and space.space.

ActivityActivity– An activity is an ongoing non-atomic An activity is an ongoing non-atomic

execution within a state machine.execution within a state machine. ActionAction

– An action is an atomic execution within a An action is an atomic execution within a state machine that results in a change of state machine that results in a change of state or the return of a value.state or the return of a value.

Page 28: States. 2 Introduction n A state machine … –Models the behavior of an individual object (statechart diagram) or a society of objects (activity diagram)

28

Terms and ConceptsTerms and Concepts