Title: Synchronous Active Objects Introduce CSP's Primitives in Java
1Synchronous Active Objects Introduce CSP's
Primitives in Java
Claude Petitpierre, Switzerland
21. One reason commercial applications are so
badly designed lies in the use of the observer
design pattern !
32. The CSP alternation can be used instead
!3. Provided it can mix inputs and outputs !
4Content
Assessment of event driven programming -
example Synchronous calls to the rescue - same
example Impact on software engineering -
design - modeling - analysis - validation
5Example Handling a Dialog Box
6The problem
- We want to develop a module (object) to get a
username and then a password from the same field. - The entry of the username may take any amount of
time. - Once the username has been typed in,
- either a password is typed in or
- the cancel button is pressed (and the demand is
cancelled) or - nothing happens for a predefined amount of time
(and the demand is cancelled)
7A specification of the internal functioning of
the Dialog Box module
instantiateDialogWindow ( ) names 0
textField.getText ( ) label.setText ("password
") CHOICE names 1 textField.getText (
) OR delay ( 5000 ) OR cancel.pressed (
) return names // null if no names
8Use of the Dialog Box module (interface)
main ADialog aDialog new ADialog (
) names aDialog.getNames (
) System.out.println ( names 0 names 1
)
9Usual Treatment with Listeners and Events
10The observer design pattern
the user types enter
text field
subject
listener
modification required by another part of the
program
event
modify
observer
update data representation
subject
The main program has disappeared !!
11Implementation with events ( main )
public class DialogMain public static void main
(String args ) dm new DialogMain (
) aDialog new ADialog ( dm ) public void
continuation ( String names ) // continue
here after the events of the dialog System.out.pri
ntln ( names 0 names 1 )
12Implementation with events ( Timer listener )
timer new Timer (5000, new ActionListener ( )
public void actionPerformed (ActionEvent e)
dm.continuation (null) )
13In the text field listener
name.addActionListener (new ActionListener ()
boolean readUsername true public void
actionPerformed (ActionEvent e) if
(readUsername) names 0 name.getText
() label.setText ("password
") readUsername false cancelButton.addAc
tionListener (new ActionListener () public
void actionPerformed (ActionEvent e)
timer.stop () dm.continuation
(names) ) timer.start () else
names 1 name.getText ( ) timer.stop
() dm.continuation (names) )
name.addActionListener (new ActionListener ()
boolean readUsername true public void
actionPerformed (ActionEvent e) if
(readUsername) names 0 name.getText
() label.setText ("password
") readUsername false cancelButton.addAc
tionListener (new ActionListener () public
void actionPerformed (ActionEvent e)
timer.stop () dm.continuation
(names) ) timer.start () // code
executed somewhere else else names 1
name.getText ( ) timer.stop
() dm.continuation (names) )
14Rectifier
GUI thread
main's thread
Listener getEvent()
actionPerformed() while ( ! trigger )
trigger true wait ()Â
notify ( ) trigger false
event
lisnr.getEvent ( )
15Waiting for Several Events
Common listener while
(!maskguards) mask X wait ()Â
notify ( )
reset(mask)
cl.getEvent(guards)
event
event
event
16Between the specification and the
implementationthe program control has been
inverted !The alternation makes it possible
to avoid this inversion !
17Active Objects with Synchronous Calls
- The instantiation, references, calls, structures
of active objects are identical to those of the
usual (passive) objects. - An active object has a thread executing its run
method.It can be considered as a process. - A synchronous active object can decide when an
incoming call can take place. Such calls realize
the inter-process communications.
18Object O1
Object O2
run () . . . select
case O2.send (msg) case
O3.m(x) . . .
void send (String msg) .......... run ()
accept send
19Object O1
Object O2
void send (String msg) .......... run ()
accept send
void send (String msg) ......... run ()
accept send
20Dialog Box Solution using rendezvous in an
alternation as implemented in our language
21The event triggers a pending synchronous call in
a select
active class ADialog String getNames( )
instantiateDialogWindow ( ) names 0
textField.getText ( ) label.setText
("password ") select case names 1
textField.getText ( ) case delay ( 5000
) case cancel.pressed ( ) return
names
22Impact upon Software Engineering
23An example ! How to use this UML diagram ?
P. Kruchten, The Rational Unified Process An
Introduction, Addison-Wesley
24Implementation with a main program
4. new session 8. PIN entered
Session manager
Card validator
2. read card
3. validate card
Card reader
1. insert card
5. "Enter PIN"
Display
9. send (validate_PIN)
Authenticator
6. get PIN
7. PIN_entered
11a. receive (PIN validity) ?
10. PIN valid ?
11b. cancel ?
Keypad
ATM Network
12. "Amount ?"
25Reintroducing the main
GUI
Select
stub
skeleton
The select makes listeners unnecessary.
26Guarded outputs are indispensable !
27Debugging
28An Example
Ex1 ma ? t mb ?
Ex2 ma ? mb ? ex1.ma !
Ex3 ex1.ma ! ma ? ex2.ma !
Ex4 ex1.ma ! ex2.mb ! ex3.ma ! t
29Program Analysis
30A Deadlock
a2 select m ? a2.m !
a2.m !
a1 select m ? a2.m !
a2.m !
31State diagram obtained with our tool
32Conclusion
- Synchronous objects avoid the program control
inversion introduced by event driven programming - They make non deterministic programs as simple as
sequential ones - They are very close to well known formal
languages (CSP, CCS ) and are thus prone to
analysis and validation - They can use the current libraries of Java
- They are particularly well adapted to distributed
applications - We have a compiler and a framework with
synchronous libraries on our Web site
http//ltiwww.epfl.ch/sJava
33Thank you !