מידול התנהגותי 1. today’s session sequence diagrams state machines 2
TRANSCRIPT
ניתוח ועיצוב מערכות תוכנה
2012אביב התנהגותי מידול
1
Today’s Session
Sequence Diagrams
State Machines
2
Sequence Diagrams
Object Destruction
obj2 : Class2
do)…(
:Class3create)…(
obj1 : Class1
useroperate)(
Object Creation
Return Message
foo)(
Messages to self
Message
Life line
Sequence Diagrams
4
X-Axis (objects)
Y-Ax
is (ti
me)
Sequence Diagram Message
Invokes an operation on an object Call
Returns a value to the caller Return
Sends a signal to an object Send
Create an object Create
Destroys an object Destroy
Is the specification of a communication among objects that conveys information with the expectation that activity will ensue.
:SomeClass
5
Syntax, Synchronous & Asynchronous What is the difference?(besides the Source / Source)
a) b) c)
6
Conditions (Guards) and parameters
7
Structured controls (combined fragments)
Structured control Meaning
alt Alternative fragment for mutual exclusion conditional logic expressed in the guards (if... then... else).
opt Optional fragment that executes if guard is true.
par Parallel fragments that execute in parallel.
ref Refers to an interaction defined in another diagram.
loop The fragment may execute multiple times, and the guard indicates the basis of iteration
summarization of some common frame operators
8
Structured control - alt
9
Structured control - opt
10
Structured control - loop
11
Nested diagrams
12
4: return value
State Modeling
What’s Missing in Behavior Modeling?Using an interaction, you can model the behavior of a set of objects that
work together.
Using a state machine, you can model the behavior of an individual object whether its an instance of a class, use case, or even an entire system (a set of classes).
The behavior of an object that must respond to asynchronous messages or whose current behavior depends on its past is best specified by a state machine.
We need some language for modeling the behavior of an individual object, in order to: Model how an object responds to events Enumerate the possible states of an object Model how the history of an object can affect its behavior
14
Elements of a State Diagram
15
Elements Description UML Notation
StatesThe set of properties and values describing the object in a well defined instant.
Initial StateThe initial state shows the starting point or first activity of the flow.
Final StateThe final state corresponding completion transition on the enclosing state.
History StateA history state is used to remember the previous state of a state machine when it was interrupted
Composite State A composite state is a state that contains other states (sub-states).
TransitionA transition is a directed relation between a source state and a destination state.
H
Transition
16
event –name [guard-condition] / actionState 1 State 2
Open Closedclose
Open Closedclose[balance=0]
Open Closedclose[balance=0]/moveToArchive)(
The event that triggers the transition
Conditions that must be met for the transition to take place.
The action that takes place when the transition is taken
Transition
17
event –name [guard-condition] / actionState 1 State 2
Open Closedclose
Open Closedclose[balance=0]
Open Closedclose[balance=0]/moveToArchive)(
The event that triggers the transition
Conditions that must be met for the transition to take place.
The action that takes place when the transition is taken
States: Initial, Transitions and Final
18
Initialize variables on the action clause of the initial pseudo-state transition.
after )2 seconds( / send ping signal
/target = null
targetAt)p( [isThreat)p(] / addTarget)p(
Sometimes tm is used instead of ‘after’, indicating ‘timer’. )i.e. tm)2 seconds(.The timer is individual per state, and is restarted upon each entry.
Taking Class Example: Nested, Parallel States
19
Self Transition and Internal Action
20
If occurrence event1
then calls: Activity3,
Activity1, Activity2
If occurrence event1
then calls:Activity1
?
A state can also define a “ do/Activity” clause – an activity performed during the state; the activity is interrupted when a transition fires.
ATM (History, Aggregated Transitions) Model an ATM that can:
1. Wait for customers.2. Be in maintenance mode (for instance: when replenishing the cash store).3. Upon insertion of a credit card it will validate the card and process the
client requests.4. The former action can be cancelled before completion.
21
ATM - Solution
22
Idle entry / readCardexit / ejectCard
Active
Selecting
[more]
exit / displayFinishedPrinting
[finished]
Processing
[more]
[finished]
Validating
Maintenance
Selecting
[more]
exit / displayFinishedPrinting
[finished]
Processing
[more]
[finished]
Validating
exit / displayFinishedPrinting
Validating Selecting
Processing
mainten
cardInserted
cancel
[more]
[finished]
Visual Paradigm for UML Standard Edition)Ben Gurion University(
Idle entry / readCardexit / ejectCard
Active
Selecting
[more]
exit / displayFinishedPrinting
[finished]
Processing
[more]
[finished]
Validating
Maintenance
Selecting
[more]
exit / displayFinishedPrinting
[finished]
Processing
[more]
[finished]
Validating
exit / displayFinishedPrinting
Validating Selecting
Processing
mainten
cardInserted
cancel
[more]
[finished]
Visual Paradigm for UML Standard Edition)Ben Gurion University(
History
1. Notice that real-life ATM machines do not have history! This is just an example.2. In this case there is no difference between shallow and deep history.
23
H
ATM (cont.)What if we wanted to state that from every state in the “active” composite
state you can transition to the “selecting” state upon a certain event?
24
H
ATM (cont.) - Reducing Transitions
Reducing transitions is important for: avoiding error prone situations and increasing readability.
25
Idle entry / readCardexit / ejectCard
Active
Selecting
[more]
small cancel
exit / displayFinishedPrinting
[finished]
Processing
[more]
[finished]
Validating
Maintenance
Selecting
[more]
small cancel
exit / displayFinishedPrinting
[finished]
Processing
[more]
[finished]
Validating
exit / displayFinishedPrinting
Validating Selecting
Processing
mainten
cardInserted
cancel
[more]
[finished]
small cancel
Visual Paradigm for UML Standard Edition)Ben Gurion University(