Synchronous Active Objects Introduce CSP's Primitives in Java - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Synchronous Active Objects Introduce CSP's Primitives in Java

Description:

We want to develop a module (object) to get a username and then a ... Card reader. ATM. Network. Card validator. Session manager. 1. insert card. 2. read card ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 34
Provided by: ltie4
Category:

less

Transcript and Presenter's Notes

Title: Synchronous Active Objects Introduce CSP's Primitives in Java


1
Synchronous Active Objects Introduce CSP's
Primitives in Java
Claude Petitpierre, Switzerland
2
1. One reason commercial applications are so
badly designed lies in the use of the observer
design pattern !
3
2. The CSP alternation can be used instead
!3. Provided it can mix inputs and outputs !
4
Content
Assessment of event driven programming -
example Synchronous calls to the rescue - same
example Impact on software engineering -
design - modeling - analysis - validation
5
Example Handling a Dialog Box
6
The 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)

7
A 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
8
Use of the Dialog Box module (interface)
main ADialog aDialog new ADialog (
) names aDialog.getNames (
) System.out.println ( names 0 names 1
)
9
Usual Treatment with Listeners and Events
10
The 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 !!
11
Implementation 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 )
12
Implementation with events ( Timer listener )
timer new Timer (5000, new ActionListener ( )
public void actionPerformed (ActionEvent e)
dm.continuation (null) )
13
In 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) )
14
Rectifier
GUI thread
main's thread
Listener getEvent()
actionPerformed() while ( ! trigger )
trigger true wait () 
notify ( ) trigger false
event
lisnr.getEvent ( )
15
Waiting for Several Events
Common listener while
(!maskguards) mask X wait () 
notify ( )
reset(mask)
cl.getEvent(guards)
event
event
event
16
Between the specification and the
implementationthe program control has been
inverted !The alternation makes it possible
to avoid this inversion !
17
Active 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.

18
Object O1
Object O2
run () . . . select
case O2.send (msg) case
O3.m(x) . . .
void send (String msg) .......... run ()
accept send
19
Object O1
Object O2
void send (String msg) .......... run ()
accept send
void send (String msg) ......... run ()
accept send
20
Dialog Box Solution using rendezvous in an
alternation as implemented in our language
21
The 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
22
Impact upon Software Engineering
23
An example ! How to use this UML diagram ?
P. Kruchten, The Rational Unified Process An
Introduction, Addison-Wesley
24
Implementation 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 ?"
25
Reintroducing the main
GUI
Select
stub
skeleton
The select makes listeners unnecessary.
26
Guarded outputs are indispensable !
27
Debugging
28
An Example
Ex1 ma ? t mb ?
Ex2 ma ? mb ? ex1.ma !
Ex3 ex1.ma ! ma ? ex2.ma !
Ex4 ex1.ma ! ex2.mb ! ex3.ma ! t
29
Program Analysis
30
A Deadlock
a2 select m ? a2.m !
a2.m !
a1 select m ? a2.m !
a2.m !
31
State diagram obtained with our tool
32
Conclusion
  • 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

33
Thank you !
Write a Comment
User Comments (0)
About PowerShow.com