Modeling behaviour via UML state machines [Software Modeling] [Computer Science] [Vrije...

40
Software and Services research group (S2) Department of Computer Science, Faculty of Sciences Vrije Universiteit Amsterdam VRIJE UNIVERSITEIT AMSTERDAM Modeling behaviour via UML state machines Software modeling (401016) – 2016/2017 Ivano Malavolta [email protected]

Transcript of Modeling behaviour via UML state machines [Software Modeling] [Computer Science] [Vrije...

Page 1: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

Software and Services research group (S2)

Department of Computer Science, Faculty of SciencesVrije Universiteit Amsterdam

VRIJEUNIVERSITEITAMSTERDAM

Modeling behaviour via UML state machines

Software modeling (401016) – 2016/2017

Ivano [email protected]

Page 2: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Roadmap

• Class diagram exercise• State machine diagrams

• Introduction• States• Transitions• Types of events• Types of states• Entry and exit points

2

Page 3: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Recall

3

VRIJEUNIVERSITEITAMSTERDAM

Exercise

1. Analyze the following system2. Select 5 functionalities that you know from using the

system3. Define a class diagram describing data and operations

behind the 5 functionalities

67

Exercise inspired by prof. Lago’s lecture at the VU

Page 4: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Introduction

§ Every object takes a finite set of different states during its life

§ State machine diagram is used as follows:§ to model the possible states of a system or object§ to show how state transitions occur as a consequence of events§ to show what behavior the system or object exhibits in each state

§ Example§ high-level description of the behavior of a lecture hall

Transition State

4

Page 5: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Example: Lecture Hall with Details

class LectureHall {private boolean free;

public void occupy() {free=false;

}public void release() {

free=true;}

}

5

Page 6: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Example: Digital Clock

6

Page 7: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

State

§States = nodes of the state machine

§When a state is active§ The object is in that state§ All internal activities specified in this state can

be executed

§entry / Activity(...)§ Executed when the object enters the state

§exit / Activity(...)§ Executed when the object exits the state

§do / Activity(...)§ Executed while the object remains in this

state7

Page 8: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Transition

Change from one state to another

Source state Target stateTransition

Event Guard Sequence of actions (effect)

8

Page 9: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Transition – Syntax

§ Event (trigger)§ Can trigger a state transition

§ Guard (condition)§ Boolean expression§ If the event occurs, the guard is checked§ If the guard is true

1. All activities in the current state are terminated2. Any relevant exit activity is executed3. The transition takes place

§ If the guard is false§ No state transition takes place, the event is discarded

§ Activity (effect)§ Sequence of actions executed during the state transition

9

Page 10: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Transition – Types (1/2)

Internal transition External transition

§ If event1 occurs§ Object remains in state1§ Activity3 is executed

§ If event1 occurs§ Object leaves state1 and Activity2 is executed

§ Activity3 is executed§ Object enters state1 andActivity1 is executed

10

Page 11: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Transition – Types (2/2)

§ When do the following transitions take place?

If e1 occurs, A1 is aborted and the object changes to S2

If e1 occurs and g1 evaluates to true, A1 is aborted and the object changes to S2

As soon as the execution of A1 is finished, a completion event is generated that initiates the transition to S2

As soon as the execution of A1 is finished, a completion event is generated; if g1 evaluates to true, the transition takes place; If not, this transition can never happen

11

Page 12: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Transition – Sequence of Activity Executions

Assume S1 is active … what is the value of x after e occurred?

S1 becomes active, x is set to the value 4

S1 is left, x is set to 5

e occurs, the guard is checked and evaluates to true

The transition takes place, x is set to 10

S2 is entered, x is set to 11

12

Page 13: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Event – Types (1/2)

§ Signal eventReceipt of a signal

§ E.g., rightmousedown, sendSMS(message)

§ Call eventOperation call

§ E.g., occupy(user,lectureHall), register(exam)

§ Time eventTime-based state transition

§ Relative: based on the time of the occurrence of the event§ E.g., after(5 seconds)

§ Absolute§ E.g., when(time==16:00), when(date==20150101)

13

Page 14: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Event – Types (2/2)

§ Any receive event§ Occurs when any event occurs that does not trigger another

transition from the active state§ Keyword all

§ Completion event§ Generated automatically when everything to be done in the

current state is completed

§ Change event§ Permanently checking whether a condition becomes true§ E.g., when(x > y), after(90min)

Page 15: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Change Event vs. Guard

Checked permanently

Only checked when event occurs

Question: What if the lecture is shorter than 90min? 15

Page 16: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Initial State

§ “Start” of a state machine diagram§ Pseudostate

§ Transient, i.e., system cannot remain in that state§ Rather a control structure than a real state

§ No incoming edges§ If >1 outgoing edges

§ Guards must be mutually exclusive and cover all possible cases to ensure that exactly one target state is reached

§ If initial state becomes active, the object immediately switches to the next state

§ No events allowed on the outgoing edges (exception: new())

16

Page 17: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Final State and Terminate Node

Final State

§ Real state§ Marks the end of the sequence of states§ Object can remain in a final state forever

Terminate Node§ Pseudostate§ Terminates the state machine§ The modeled object ceases to exist (= is deleted)

17

Page 18: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Decision Node

§ Pseudostate§ Used to model alternative transitions

equivalent?

=

≠equivalent?

18

Page 19: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Example: Decision Node

19

Page 20: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Parallelization and Synchronization Node

Parallelization node

§ Pseudostate§ Splits the control flow into multiple concurrent flows§ 1 incoming edge§ >1 outgoing edges

Synchronization node§ Pseudostate§ Merges multiple concurrent flows§ >1 incoming edges§ 1 outgoing edge

20

Page 21: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Composite State

§Contains other states – “substates“§ Only one of its substates is active at any point in time

§ Arbitrary nesting depth of substates

Composite state

Substates21

Page 22: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Entering a Composite State (1/2)

§ Transition to the boundary§ Initial node of composite

state is activated

Event State ExecutedActivities

„Beginning“ S3

e2 S1/S1.1 a0-a2-a3-a4

22

Page 23: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Entering a Composite State (2/2)

§Transition to a substate§ Substate is activated

Event State ExecutedActivities

„Beginning“ S3

e1 S1/S1.2 a0-a1-a3-a7

2323

Page 24: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Exiting from a Composite State (1/3)

§Transition from a substate Event State ExecutedActivities

„Beginning“ S1/S1.1 a3-a4

e3 S2 a6-a5-a2-a1

24

Page 25: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Event State ExecutedActivities

„Beginning“ S1/S1.1 a3-a4

e5 S2 a6-a5-a3-a1

Exiting from a Composite State (2/3)

§Transition from the composite stateNo matter which substate of S1 is active, as soon as e5 occurs, the system changes to S2

25

Page 26: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Event State ExecutedActivities

„Beginning“ S1/S1.1 a3-a4

e4 S1/S1.2 a6-a7

e4 S2 a8-a5-a1

Exiting from a Composite State (3/3)

§Completion transition fromthe composite state

26

Page 27: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Orthogonal State

§ Composite state is divided into two or more regions separated by a dashed line§ One state of each region is always active at any point in time, i.e., concurrent substates§ Entry: transition to the boundary of the orthogonal state activates the initial states of all regions§ Exit: final state must be reached in all regions to trigger completion event

You can use parallelization and synchronization node to enter different substates

27

Page 28: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Submachine State (SMS)

§ To reuse parts of state machine diagrams in other state machine diagrams§ Notation: state:submachineState§ As soon as the submachine state is activated, the behavior of the submachine is executed

§ Corresponds to calling a subroutine in programming languages

28

Refinement symbol(optional)

Page 29: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

History State

§ Remembers which substate of a composite state was the last active one§ Activates the “old” substate and all entry activities are conducted sequentially from the outside to the inside of the composite state§ Exactly one outgoing edge of the history state points to a substate. It is used if:

§ the composite state was never active before§ the composite state was exited via the final state

§ Shallow history state restores the state that is on the same level of the composite state

§ Deep history state restores the last active substate over the entire nesting depth

29

Page 30: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Example: History State (1/4)

Event State

„Beginning“ S5

e1 S4/S1/S1.1

e2 S1.2

e10 S5

e9 (H→) S1/S1.1

30

Page 31: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Example: History State (2/4)

Event State

„Beginning“ S5

e1 S4/S1/S1.1

e2 S1.2

e10 S5

e8 (H*→) S1.2

31

Page 32: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Example: History State (3/4)

Event State

„Beginning“ S5

e9 (H→) S1/S1.1

32

Page 33: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Example: History State (4/4)

Event State

„Beginning“ S5

e8 (H*→) S3/S3.1

33

Page 34: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Entry and Exit Points

§ Encapsulation mechanism§ A composite state shall be entered or exited via a state other

than the initial and final states§ The external transition must/need not know the structure of the

composite state

External view

34

Internal view

Page 35: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Notation Elements (1/2)

Name Notation Description

State

Description of a specific “time span” in which an object finds itself during its “life cycle”. Within a state, activities can be executed by the object.

Transition State transition e from a source state S to a target state T

Initial state Start of a state machine diagram

Final state End of a state machine diagram

Terminate node Termination of an object’s state machine diagram

35

Page 36: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Notation Elements (2/2)

Name Syntax Beschreibung

Decision node Node from which multiple alternativetransitions can origin

Parallelization node Splitting of a transition into multipleparallel transitions

Synchronization node

Merging of multiple parallel transitionsinto one transition

Shallow / deep history state

“Return address” to a substate or anested substate of a composite state

36

Page 37: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Exercise

Event sequence: e1, e2, e4, e4, e3, e1

Value of x?

37

Page 38: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Solution

38

Page 39: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

What this lecture means to you?

• Your models are starting to move now!

• State machine used for modelling the internal states of each object in your system

• In principles, you will have a state machine for each class in your class diagrams

• do not take it literally• you may need less

• the operator is human à you may not need to program it!• you model only the key states of the objects, not everything

39

Page 40: Modeling behaviour via  UML state machines [Software Modeling] [Computer Science] [Vrije Universiteit Amsterdam] [2016/2017]

VRIJEUNIVERSITEITAMSTERDAM

Readings

• UML@Classroom: An Introduction to Object-Oriented Modeling” – chapter 5

• Learning UML 2.0 – chapter 14

• TOOL guide• https://www.itemis.com/en/yakindu/statechart-

tools/documentation/user-guide/

40