Statecharts - School of Computing - National University of Singapore

advertisement
Warm up – the big picture
System
Model
(Rough
Idea)
System
to be
built
(Dream)
Statecharts
Checking
Method
(Automat
ed)
Properties
to Satisfy
(caution)
Abhik Roychoudhury
School of Computing
National University of Singapore
Refine
the
model
Violations
CS4271, 20112011-12
1
Background
¾
Readings
Finite state machines
z
z
¾
Other variants
Model Reactive and transformational systems
¾
¾
Statecharts is one of the simplest
p
and
most popular modeling formalism
z
z
Very intuitive, visual.
An illustration of how to model systems with
statecharts will be shown via Rhapsody tool.
¾
Statecharts: A visual formalism for
complex systems, by David Harel, Science
of Computer Programming, 1987
Executable object modeling with
statecharts, by David Harel and Eran
Gery, IEEE Computer, 1997
Basic understanding of states/transitions
is introduced first.
• Also, tested in the first lab assignment.
CS4271, 2011-12
3
CS4271, 2011-12
Introducing FSMs --- a puzzle
¾
¾
¾
¾
¾
State change
A man with a goat, a wolf and a cabbage
wants to cross a river.
A boat can carry only 2 of the 4 entities.
Wolf wants to eat the goat.
Goat wants to eat the cabbage.
cabbage
z
4
MWGC---
Transport
Goat
WC---MG
Cabbage
Wolf
How to transport all the 4 entities ?
Think of modeling the local state of each
entity – on which side of the river?
z
A global state is a composition of these local
states --- transitions of global states form FSM
CS4271, 2011-12
5
CS4271, 2011-12
6
1
State change
Modeling using FSMs
Cabbage
MWGC---
¾
Transport
Goat
WC---MG
Cabbage
Wolf
z
W---MCG
z
C---MWG
Goat
Goat
---MWGC
A solution to our problem is a path from
the initial state to a state where all 4
entities are on other side of river.
¾
MGW-C
Minor note:
z
MGC---W
Notion of “termination” of the problem.
Shown as accepting states of FSMs
Not all cycles in the FSM for this problem have
been shown.
wolf
MG--WC
cabbage
G---MCW
CS4271, 2011-12
7
CS4271, 2011-12
FSM --- Definition
¾
FSM --- Example
M = (S, S0, ∑, →, F)
z
z
z
z
¾
S is a set of states
S0 ⊆ S is the set of initial states
→ ⊆ S × ∑ × S is the transition relation
F ⊆ S is the set of final or accepting states
z
0
An infinite collection of finite strings
9
CS4271, 2011-12
Transition Systems
Transition systems go one step further where all
states are accepting.
z TS = (S, S0, ∑, →)
0
1
An infinite string is accepted iff it visits at least one final
state infinitely often.
0
1
Traces captured by this transition system are
• No notion of terminating or accepting states
• The alphabet ∑ labeling the transitions is also
optional.
• The traces captured by a transition system are
obtained by unrolling the graph from the initial
state(s).
CS4271, 2011-12
10
TS - Example
FSMs can accept infinite strings too, change
accepting condition
z
¾
1
Accepts all binary strings with odd number of 1s
L(M) = all strings which have a path from an
initial state to an accepting state.
Using finite state machines for recognizing or
distinguishing (infinite) set of (finite) strings.
CS4271, 2011-12
0
1
The set of strings accepted by M or the
language of M
z
¾
8
( 0* 1)* 0ω
(0*1)ω
11
CS4271, 2011-12
12
2
Transformational Systems
¾
Conventional notion of a terminating
program.
z
z
z
¾
Takes in input.
Performs computation step.
Terminates after producing output.
¾
Continuously interacts with its
environment.
¾
Interaction with environment is typically
asynchronous.
Often consists of a concurrent composition
of processes.
¾
System behavior
z
¾
Reactive Systems
¾
Can be described as a transformation function
over the input.
¾
What about controllers ?
z
In continuous interaction with the
environment.
CS4271, 2011-12
¾
¾
CS4271, 2011-12
z
z
z
¾
¾
¾
¾
Important/imperative at initial design
stages.
Vital for communication.
Formal visual languages can help in:
z
Too much complexity
Many processes --- concurrency
Each process has many states --- hierarchy
What kind of inter-process communication?
z
z
15
CS4271, 2011-12
Statecharts
z
z
z
z
¾
16
General Idea
Statecharts =
z
¾
Documentation
Initial analysis.
Developing correct-by-construction translation
to more detailed (non-visual) descriptions.
The language of Statecharts addresses
these practical issues !!
CS4271, 2011-12
¾
14
Visual Formalisms
(Infinite) collection of infinite traces.
Traces denote ongoing interaction with
environment.
Use state transition systems to describe
behavior
b
a o of
o a reactive
a
system
y
z
Often, its response to environment needs to
obey time constraints.
13
Reactive system behavior
¾
No notion of system termination.
FSMs +
Depth +
Orthogonality +
Structured transitions +
Broadcast communication
¾
Statecharts
¾
What does the FSM denote?
z
z
z
Used in the Rhapsody tool.
Included in UML 2.0 as state diagrams.
z
= FSM + many features to contain complexity.
System response to external triggers.
States of the FSM = internal states of the
system
Transitions of the FSM are labeled by such
triggers.
• An external trigger may in turn generate internal
triggers which can also form the labels.
z
Traces of the FSM are sequence of transitions.
• System response should stabilize eventually, waits
for the next external trigger from environment.
CS4271, 2011-12
17
CS4271, 2011-12
18
3
Statecharts
¾
z
z
¾
Depth : OR States
Depth:
States can have internal structure.
OR type states
Orthogonality
z
z
z
Independent states
Concurrency
AND type states
¾
Structured transitions
¾
Broadcast communication
z
z
Succinct descriptions of transition families.
Succinct descriptions of synchronizations
(b) is the statechart representation of the FSM (a).
CS4271, 2011-12
19
Depth : OR States
CS4271, 2011-12
Depth : OR States
A and C are clustered into a superstate D
e, f : are trigger (external) events.
A and C are the internal exclusive-or components of the D state.
g [ c ]: g, a trigger event and c a condition
CS4271, 2011-12
21
Depth : OR States
CS4271, 2011-12
22
Depth : OR States
f is a transition from D to B.
h is transition from B to D (A or C).
From any D-state (A or C) there is an f-move to B
The actual state entered is the default entry state; the state C
CS4271, 2011-12
20
23
CS4271, 2011-12
24
4
Depth : OR States
Depth: OR States
D is the initial state.
Which state will transition e yield in (b) and (c)?
The actual initial state within D is not the default state C.
Which state will transition h yield in (b) and (c)?
Instead, it is A.
What’s the default state for the superstate E in (c)? Hierarchically!
CS4271, 2011-12
25
CS4271, 2011-12
A Concrete Example: OR-chart
OR-State: in a nutshell
¾
¾
¾
¾
¾
CS4271, 2011-12
27
Orthogonality: AND States
An OR-state can contain other states as its internal
substates (hierarchical internal structure);
A super OR-state is active, if and only if one of its
immediate substates is active (exclusive or);
When the control enters a (super) OR-state,
OR state its
default substate is entered and becomes active;
When the control leaves a (super) OR-state, all its
substates become inactive!
More issues: history, priority, …
CS4271, 2011-12
28
Orthogonality: AND States
Y is an AND state.
It has two orthogonal components A and D.
A is an OR state with components B and C.
D is an OR state with components E, F and G.
(b) is the statechart representation of the FSM (a).
CS4271, 2011-12
26
29
CS4271, 2011-12
30
5
Orthogonality: AND States
Orthogonality: AND States
Y is an AND state.
It has two orthogonal components A and D.
a state of Y is composed of a state of A and a state of D
What is the default initial state of Y?
CS4271, 2011-12
Y is an AND state.
It has two orthogonal components A and D.
a state of Y is composed of a state of A and a state of D
What is the default initial state of Y? (B,F)
31
f belongs to only A.
e belongs to both A and D.
From (B,F) there is a simultaneous e-move to (C,G)
f belongs to only A.
e belongs to both A and D.
From (B,F) there is a simultaneous e-move to (C,G)
33
Orthogonality: AND States
CS4271, 2011-12
34
Orthogonality: AND States
f belongs to only A.
e belongs to both A and D.
From (B,F) there is a simultaneous e-move to (C,G)
CS4271, 2011-12
32
Orthogonality: AND States
Orthogonality: AND States
CS4271, 2011-12
CS4271, 2011-12
f belongs to only A.
e belongs to both A and D.
From (B,F) there is a simultaneous e-move to (C,G)
35
CS4271, 2011-12
36
6
Orthogonality: AND States
Orthogonality: AND States
From every Y state (how many? ) there is a p-move to I
CS4271, 2011-12
From every Y state (6! ) there is a p-move to I
From I there is an e-move to the Y-state (?, ?)
37
38
Orthogonality: AND States
Orthogonality: AND States
From I there is an e-move to the Y-state (C, G)
For each (?, F) state there is an m-move to I
Note the [in G] condition attached to the f-move from C
(state reference!).
What if there is an e-arrow from I to just the surface of Y?
CS4271, 2011-12
CS4271, 2011-12
39
AND-state: in a nutshell
CS4271, 2011-12
40
Broadcast Communication
An AND-state is composed of several
independent (OR-)states that run in parallel
(concurrency);
¾ An active state of an AND-state comprises a state
of each concurrent component, i.e., (s1,s2,…,sn);
¾ When the control enters (leaves) an AND-state, it
simultaneously enters (leaves) all its components;
¾ An AND-state can even occur inside an OR-state
(different from conventional programming
languages)
¾
A transition has a trigger and an action (output!)
But the output of a transition can be inputs for other orthogonal
components!
CS4271, 2011-12
41
CS4271, 2011-12
42
7
Broadcast Communication
Broadcast Communication
Start configuration (B, F, J)
Start configuration (B, F, J)
m/e: m is the trigger event, while e is the action (output!)
Suppose m (external event) occurs.
Suppose m (external event) occurs.
H goes to I from J ; e-moves are enabled in A and D
CS4271, 2011-12
43
Broadcast Communication
CS4271, 2011-12
44
Broadcast Communication
Start configuration (B, F, J)
Suppose event n comes,
m occurs
What happen now?
Final configuration (C, G, I)
CS4271, 2011-12
45
Broadcast Communication
46
Broadcast Communication
Now suppose event n comes,
What happen? Transition
CS4271, 2011-12
Now suppose event n comes,
is fired, f is generated,
What happen? Transition
which fires transition
which fires transition
CS4271, 2011-12
47
is fired, f is generated,
, which again fires
CS4271, 2011-12
48
8
Broadcast Communication
What are the triggers/actions
¾
Method call
¾
Or, Event
¾
Is there a difference?
z
z
z
z
is fired, f is generated,
which fires transition
Event_name(parameters)
Lots, in terms of semantics
A method call involves a transfer of control
• If there are nested method calls, they can cause
further transfer of control
Now suppose event n comes,
Transition
Method_name(parameters)
z
An event will be lodged in a system queue
• It will be removed by the recipient later.
, which again fires
Finally yielding (B,E,J)
CS4271, 2011-12
49
CS4271, 2011-12
Events and Method calls
¾
Most General form of …
Event based communication
z
z
… annotation for a transition
¾
Trigger is event expression or method
invocation
Condition is like a branch condition on
data variables
Action is a program
z
Method call based communication
z
¾
Inherently asynchronous
• Designer does not worry about controlling all
interaction sequences (this is taken care of by the
system queue)
¾
¾
Synchronous, involving transfer of control
Involves close control by the designer over
interaction sequences ---
¾
z
• getting closer to code level
CS4271, 2011-12
50
51
Trigger[condition]/Action
Sequence of event generation or method
invocation or even code in a programming
language.
CS4271, 2011-12
52
Summary
¾
Practical Use of Statecharts in Modeling
Object-based systems
z
z
z
¾
Use statecharts to describe behavior of classes
(of active objects)
Class Associations given by class diagrams.
Contains code in the actions for realistic
designs
A realistic approach for modeling
(distributed) embedded controllers.
CS4271, 2011-12
53
9
Download