JavaFrame and State Machines Aspects of our reality ….

advertisement
JavaFrame and State Machines
Version 021108
INF-UIT 2001 / JavaFrame and StateMachines / Slide 1
Øystein Haugen
Ketil Stølen
Aspects of our reality ….
l Telecom systems are still complex, concurrent and real time
l Dependability is still in fashion
– even though telecom converges with computer science
l Java is the hottest programming language
– but Threads are very difficult to handle properly!
l UML is the modelling language everybody says they use
– but do they really produce code from it? At least not effective Java!
l Design ITU languages SDL and MSC have come with standards
updates for 2000
– but they are not in fashion despite their technical assets
l Rose-RT supports a (proprietary) UML real time profile
– but it should be improved and extended.
INF-UIT 2001 / JavaFrame and StateMachines / Slide 2
Øystein Haugen
Ketil Stølen
Our goals
l A good way of thinking for
– modellers
– programmers
l such that their programs will become:
–
–
–
–
–
rapidly made according to specification
have high quality
be efficient
maintainable by competent persons
be adaptive to a changing environment of requirements and third party
software
l This should apply to large and small programs
INF-UIT 2001 / JavaFrame and StateMachines / Slide 3
Øystein Haugen
Ketil Stølen
JavaFrame - the solution
UML
UMLJavaFrame
transformation
JavaFrameUML
transformation
mapping
modeling by
programming
JavaFrame
JavaFrame
JavaFrame
JavaFrame
Java
Java
Java
Java
INF-UIT 2001 / JavaFrame and StateMachines / Slide 4
Øystein Haugen
Ketil Stølen
JavaFrame - the architecture
PtnMain
Composites
PtnComposite
PtnMessage(int)
State Machines
PtnStateMachine
EdgeMediator
PtnStartState
Router
PtnMessage(int)
TimerMsg()/
PtnMessage(
mylocalvariable)
PtnMessage(x)/
mylocalvariable = x;
ptnTimerMsg.startTimer(
)
Mediators
EdgeMediator
PtnState
Router
TimerMsg()/
startTimer(400)
PtnNoneState
None/
PtnMessage(mylocalvariable); PtnMessage(int)/
ptnTimerMsg.startTimer()
Scheduler
INF-UIT 2001 / JavaFrame and StateMachines / Slide 5
Øystein Haugen
Ketil Stølen
JavaFrame
Observation
tool, input dialog
UML, UML-RT,
UML 2.0
Thread-safe, runtime
effective, reentrant
composite states
Asynch. Interacting
Active Objects
2002:
•Avantel: Amigos
•UML 2.0 laboratory
Programming
templates
INF-UIT 2001 / JavaFrame and StateMachines / Slide 6
Øystein Haugen
Ketil Stølen
Experiences - The Performance Model
Stimuli
MultiTube
Response
Tube
JavaFrame 6.92ms
INF-UIT 2001 / JavaFrame and StateMachines / Slide 7
Trad. method 10.13ms
Øystein Haugen
Ketil Stølen
Experiences - The Lego Mindstorm experiment
socket mediators
server trace
server
sockets
client
sockets
control
INF-UIT 2001 / JavaFrame and StateMachines / Slide 8
Øystein Haugen
Ketil Stølen
client trace
Concept Library (1/2)
ActiveObject +parts *
containment
+owner
1
attachment
Asynch. Interacting
Active Objects
Addressable
+to *
*
+whole 1
Mediator +from *
StateMachine
Composite
connection
Øystein Haugen
INF-UIT 2001 / JavaFrame and StateMachines / Slide 9
Ketil Stølen
Concept Library (2/2)
Mediator
RouterMediator
MulticastMediator
ProtocolMediator
EdgeMediator
Mediators of many
different kinds
INF-UIT 2001 / JavaFrame and StateMachines / Slide 10
Øystein Haugen
Ketil Stølen
Execution Environment
Message
Mediator
associated with
connected to
StateMachine
schedules
0..n
INF-UIT 2001 / JavaFrame and StateMachines / Slide 11
1
Schedule
r
Thread-safe,
runtime effective,
reentrant composite
states
Thread
1
Øystein Haugen
1
Ketil Stølen
Java templates / patterns
package ptnpackage;
import se.ericsson.eto.norarc.javaframe.*;
/** Purpose: define a class for a new State.
* Example class PtnState:*/
public class PtnState extends State {
/** Required: State constructor.*/
public PtnState(String sn) {
super(sn);
}
Programming
templates
/** Optional: redefine the actions do be done when entering this State.
* Example:*/
protected void entry(StateMachine curfsm) {
((PtnStateMachine)curfsm).ptnTimerMsg.startTimer();
}
/** Optional: redefine the actions do be done when exiting from this State.
* Example:*/
protected void exit(StateMachine curfsm) {
((PtnStateMachine)curfsm).ptnTimerMsg.stopTimer();
}
}
INF-UIT 2001 / JavaFrame and StateMachines / Slide 12
Øystein Haugen
Ketil Stølen
Modeling Guidelines
PlayerAdmState
noGame
Machine 1
Timeout2
Timeout2
PlayerAdm
GameAdm
Timout1 /
notify player,
start Timer2
SignOn /
start Timer1
Scissors |
Paper |
Stone
signing
Timeout1 /
create
notify player,
playing start Timer2
CancelPlay
Player
Environment
Play /
sign player on for
a new game
Player
UML, UML-RT,
UML 2.0
StartPlay /
Inform player
to move, start Timer1
signedOn
PlayerAdm
ff
Env
ToggleMode
ToggleMode
LocAdm
Player
toggleMode
Status("Mode changed")
create
Status("Mode changed")
INF-UIT 2001 / JavaFrame and StateMachines / Slide 13
Øystein Haugen
Ketil Stølen
Observation Support
Observation
tool, input dialog
INF-UIT 2001 / JavaFrame and StateMachines / Slide 14
Øystein Haugen
Ketil Stølen
JavaFrame Modeling - preparing for analysis also
s
p
e
c
i
f
i
c
a
t
i
o
n
d
e
s
i
g
n
Object Model
Property Model
describe
subscA
subscA
subscB
subscB
hook off
call
compare
reply
program
compare
class SubscA extends StateMachine
{ SubscB subscB;
int stateIdle, stateBusy, ...;
};
class Idle extends State
{ int run()
{ ..... };
};
class IdleHookOff extends Transition
{ ... };
INF-UIT 2001 / JavaFrame and StateMachines / Slide 15
Trace
Process
State
Message
Next
subscA
Idle
hookoff Wait
subscB...Idle...call....Conv
subscA...Wait...reply...Conv
...
execute
Øystein Haugen
Ketil Stølen
The Pattern System
l The Pattern System is a JavaFrame system available in source
code in the JavaFrame release
l The Pattern System is the system of all patterns (or templates if
you like)
l The Pattern System executes, but does not do anything very
sensible
INF-UIT 2001 / JavaFrame and StateMachines / Slide 16
Øystein Haugen
Ketil Stølen
JavaFrame system and environment
The whole Java
virtual machine
The total system
that we describe
PtnMain
PtnGUI env
PtnComposite oursystem
Asynchronous
message
<PtnMessage>
Input Mediator,
only entry from
the outside
PtnSimpleRouterMediator
inputMediator
Graphical User
Interface (GUI)
Output Mediator
only exit toward
the outside
void addLog(String stat)
OutputEdgeMediator
outputMediator
The
environment will
receive info
INF-UIT 2001 / JavaFrame and StateMachines / Slide 17
Øystein Haugen
Ketil Stølen
Active Objects
ActiveObject +parts *
All communication
goes through
Mediators
containment
+owner
1
Behavior is in the
end described by
State Machines
attachment
Addressable
*
A hierarchy of
composites makes
up a system
+to *
Mediator +from *
+whole 1
StateMachine
Composite
connection
INF-UIT 2001 / JavaFrame and StateMachines / Slide 18
Øystein Haugen
Ketil Stølen
Composite
Routing: one in
one of many out
PtnComposite
Mediator myoutputmediator
<PtnMessage>
<PtnMessage>
PtnStateMachine
inputMediator <
Ptn
M
ess
ag
e>
outputMediator
addLog(…)
Mediator
e>
ag
ess
tnM
<P
Mediator
PtnStateMachine
<PtnMes
sage>
Mediator myoutputmediator
Øystein Haugen
INF-UIT 2001 / JavaFrame and StateMachines / Slide 19
Notice two
instances of
PtnStateMachine
The state
machine must
name its output
mediators
Ketil Stølen
Asynchronous Messages
Messages are
objects
Messages are consumed
by State Machines in due
time
Message
(from javaframe)
+consumed
Message()
messageName()
messageContent()
duplicate()
+nextMessage
del()
StateMachine
(from javaframe)
#nextFSM
-timedStateMachine
Timers are messages that
come according to time; they
are not necessarily consumed
at that time!
INF-UIT 2001 / JavaFrame and StateMachines / Slide 20
TimerMsg
(from javaframe)
TimerMsg()
actionPerformed()
setDelay()
startTimer()
startTimer()
stopTimer()
Øystein Haugen
Ketil Stølen
Schedulers
There is exactly one
Thread for each
Scheduler
Thread
1
StateMachine
1
Scheduler
(from javaframe)
inSchedulerList : boolean = false
StateMachine()
moveStateMachine()
getScheduler()
getCurrentState()
forward()
execStartTransition()
restart()
exec()
#wantedScheduler
#myScheduler
-fsmListFirst
-fsmListLast
-curStateMachine
(from javaframe)
The Scheduler gives the
designer the opportunity
to tune the performance
$ MAXMES : int = 30
Scheduler()
Scheduler()
readyFSM()
run()
+trc
Trace
StateMachines can be moved
between Schedulers, or taken out
of all Schedulers.
State Machines only execute if
they are in the list of a Scheduler
INF-UIT 2001 / JavaFrame and StateMachines / Slide 21
There is one trace per
Scheduler
(from javaframe)
Øystein Haugen
Ketil Stølen
Finite State Machines
l Finite
– a finite number of states
– [here] a small number of named states
l State
– a stable situation where the process awaits stimuli
– a state in a state machine represents the history of the execution
l Machine
– that only a stimulus (signal, message) triggers behavior
– the behavior consists of executing transitions
– may also have local data
INF-UIT 2001 / JavaFrame and StateMachines / Slide 22
Øystein Haugen
Ketil Stølen
A simple state space (?) described by a State Chart
disconnected
Starting point
disconnect
^PS(disconnect)
Chat
ConnectDefault
ConnectPlayer
playing
disconnect
^PS(disconnect)
Connecting
disconnect
^PS(disconnect)
Scissor|Paper|Stone
startPlay /
startTimer1
disconnect
^PS(disconnect)
Chat
connected
noGame
cancelPlay / stopTimer1&2
Simple states
responseTimer2
^PlayerSignal
signOn /
startTimer1
cancelPlay /
stopTimer1
Chat
Chat
Signing
signedOn
Play ^PlayerSignal(Play)
Transitions:
trigger / action or
trigger ^send
responseTimer1 / startTimer2
INF-UIT 2001 / JavaFrame and StateMachines / Slide 23
Øystein Haugen
Ketil Stølen
A State Space is a Composite State
explicit entry point
1
PtnCompositeState iCS1
1
if x = “E1”
explicit exit point
exit: PtnTimerMsg.stop
State iS1
PtnMessage
PtnState iS2
Simple state with actions
on entry and exit
entry: PtnTimerMsg.start
PtnMessage
in enterState()
Transitions are described
local to the enclosing
composite state!
in execTrans()
2
INF-UIT 2001 / JavaFrame and StateMachines / Slide 24
2
Øystein Haugen
Ketil Stølen
State conceptual model
State
(from javaframe)
State()
State()
stateName() #currentState
entry()
exit()
enterState()
StateMachine
(from javaframe)
+enclosingState
CompositeState
+theStatespace
(from javaframe)
CompositeState()
CompositeState()
exitState()
performExit()
sameState()
save()
output()
enterState()
enterState()
execTrans()
outofInnerCompositeState()
INF-UIT 2001 / JavaFrame and StateMachines / Slide 25
Øystein Haugen
Ketil Stølen
State Machine refers to a CompositeState
start
PtnCompositeStateWICS states
PtnMessage
the state space
1
PtnCompositeState iCS1
State aS1
1
if x = “E1”
exit: PtnTimerMsg.stop
State iS1
PtnMessage
PtnState iS2
PtnMessage
entry: PtnTimerMsg.start
PtnTimerMsg
PtnStateMachine
State aS2
PtnMessage
in enterState()
in execTrans()
2
2
static CompositeState states = new PtnCompositeStateWICS("outermostState");
in enterState()
in execTrans()
in outofInnerCompositeState()
exit port
The action of each transition part (arrow) is output of a PtnMessage with
parameters describing the location of the arrow.
public int mylocalvariable;
public TimerMsg ptnTimerMsg = new TimerMsg(10000, this);
entry port
variable
<PtnMessage>
Mediator myoutputmediator
timer
<PtnMessage>
INF-UIT 2001 / JavaFrame and StateMachines / Slide 26
Mediator
Øystein Haugen
Ketil Stølen
Pattern Composite State with inner composite states
start
PtnCompositeStateWICS states
PtnMessage
1
PtnCompositeState iCS1
State aS1
1
if x = “E1”
exit: PtnTimerMsg.stop
State iS1
PtnMessage
PtnState iS2
PtnMessage
entry: PtnTimerMsg.start
PtnTimerMsg
State aS2
PtnMessage
in enterState()
in execTrans()
2
2
Øystein Haugen
in execTrans()
in outofInnerCompositeState()
exit port
INF-UIT 2001 / JavaFrame and StateMachines / Slide 27
in enterState()
entry port
Ketil Stølen
Motivation for Composite States
l Some transitions are more global than others
– When a television set is ON you may be in textual mode or in normal
television mode
– When in textual mode, the digits have a different interpretation than they
do in normal television mode
– when you turn OFF the set, it goes black regardless of whether it is in
textual or television mode
l One achieves
–
–
–
–
better overview
more compact code, transitions need in lesser degree be duplicated
potential reuse of composite states
composite states are potential units for on-line replacement
INF-UIT 2001 / JavaFrame and StateMachines / Slide 28
Øystein Haugen
Ketil Stølen
Composite State - details (1)
exit: PtnTimerMsg.stop
PtnState iS2
PtnMessage
entry: PtnTimerMsg.start
PtnTimerMsg
State aS2
global transitions when
trigger is not handled
inside
Øystein Haugen
INF-UIT 2001 / JavaFrame and StateMachines / Slide 29
Ketil Stølen
Composite States - details (2)
start
PtnMessage
1
PtnCompositeState iCS1
1
if x = “E1”
explicit entry and exit
points make the composite
state compositional
INF-UIT 2001 / JavaFrame and StateMachines / Slide 30
Øystein Haugen
Ketil Stølen
Remaking the Knoble player state space
disconnected
disconnect-transition is
equivalent from four states
disconnect
^PS(disconnect)
Chat
ConnectDefault
ConnectPlayer
playing
disconnect
^PS(disconnect)
Connecting
disconnect
^PS(disconnect)
Scissor|Paper|Stone
startPlay /
startTimer1
disconnect
^PS(disconnect)
Chat
connected
connected
noGame
cancelPlay / stopTimer1&2
responseTimer2
^PlayerSignal
signOn /
startTimer1
cancelPlay /
stopTimer1
Chat
Chat
Signing
signedOn
Play ^PlayerSignal(Play)
chat-transition is equivalent
for these four states
responseTimer1 / startTimer2
Øystein Haugen
INF-UIT 2001 / JavaFrame and StateMachines / Slide 31
Ketil Stølen
Composite State Connected
default starting point of
Connected
“Asterisk” input, i.e. holds
for all states inside
Connected (here shown
outside due to Rose)
Chat | cancelPlay
Connected
noGame
Scissor|Paper|Stone
playing
startPlay /
startTimer1
signOn /
startTimer1
responseTimer2
^PlayerSignal
Signing
signedOn
Play ^PlayerSignal(Play)
responseTimer1 / startTimer2
INF-UIT 2001 / JavaFrame and StateMachines / Slide 32
Øystein Haugen
Ketil Stølen
Main state space revisited
d isconnected
disconnect
^PS(disconnect)
ConnectDefault
ConnectPlayer
connected
Connecting
Connected
Øystein Haugen
INF-UIT 2001 / JavaFrame and StateMachines / Slide 33
Ketil Stølen
Mediators
Mediator
Often simple Mediators
are enough. They contain
only a forwarding ref.
(from javaframe)
Mediator()
addAddress()
getAddress()
mediatorName()
forward()
input()
All of the different types
of Mediators can be used
for input as well as output
RouterMediator
MultiCastMediator
SimpleRouterMediator
(from javaframe)
(from javaframe)
MultiCastMediator()
forward()
addAddress()
SimpleRouterMediator()
addAddress()
forward()
(from javaframe)
RouterMediator()
getRouterMachine()
addAddress()
forward()
mediatorRouter
MulticastMediator clones
the Messages to all its
successors
Router
(from javaframe)
StateMachine
(from javaframe)
SimpleRouterMediators
are for simple decisions
of address
INF-UIT 2001 / JavaFrame and StateMachines / Slide 34
Øystein Haugen
Router()
addAddress()
getAddress()
execRouterTrans()
output()
exec()
execStartTransition()
Ketil Stølen
RouterMediators are for
more complex situations
e.g. when the address
resolution requires extra
communication
Routers are State
Machines that are
associated with
RouterMediators
The 11 Commandments (1/3)
l A JavaFrame system is a Composite (which is an Active Object)
l Active Objects are either Composites or StateMachines
l Composites may contain a set of interacting Active Objects.
l Interaction is through connectors. The connector ends are called
Mediators.
l All communication goes through Mediators.
– The network of connectors between the Mediators is what implements
the composite structure.
– Conceptually the mediators are on the edges of the Active Objects and
their identity is considered visible inwards and outwards, but only to the
nearest boundary.
INF-UIT 2001 / JavaFrame and StateMachines / Slide 35
Øystein Haugen
Ketil Stølen
The 11 Commandments (2/3)
l No connectors must cross boundaries of Composites.
l All State Machines are considered concurrent even though in fact
the Scheduler runs only one State Machine at the time.
l All addressing from a StateMachine (output) must refer only the
output mediators of this State Machine.
INF-UIT 2001 / JavaFrame and StateMachines / Slide 36
Øystein Haugen
Ketil Stølen
The 11 Commandments (3/3)
l A transition in JavaFrame is considered atomic and this implies
that the primitives called in the transition must return within a
given finite (and preferably small) time.
l A JavaFrame system should not contain any user-specified
Threads other than those associated with Schedulers in the system
– Implicitly there are Threads generated for Timers.
l We recommend that there be no global variables
– This means that all variables should be encapsulated by a State
machine. In some cases global variables may be advisory, and then all
access to such variables must be explicitly synchronized
– Constants or "write once" variables may of course be global. The term
"synchronized" is here meant to refer to the mutual exclusion
mechanism of Java
INF-UIT 2001 / JavaFrame and StateMachines / Slide 37
Øystein Haugen
Ketil Stølen
How to program with templates PtnPattern
l The patterns are there for a reason!
l The patterns will ensure that the Commandments are kept
l Do use the patterns, at least look at what they prescribe, because:
–
–
–
–
there may be subtleties that you have not understood properly
sometimes the order in which things are done is significant
the patterns have been tried out
using the patterns saves your creative thinking for problems worth
spending your creativity on
l When you use existing program fragments as a base (as we will do
in this course):
– confer with the patterns to make sure the changes are appropriate
l Remember to think in terms of the model and make it explicit!
INF-UIT 2001 / JavaFrame and StateMachines / Slide 38
Øystein Haugen
Ketil Stølen
Download