27
START

START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Embed Size (px)

Citation preview

Page 1: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

START

Page 2: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Translation ofprocess algebras to Java

Paul BilokonSamuel LauAndrew Roberts

Page 3: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Introduction

Modelling Finite State Processes (FSP) Threads and monitors Mission statement

Page 4: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Modelling

Simplicity: abstraction from irrelevant details

Determination of relevant factors

Predict long term behaviour “What if” scenarios

Page 5: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Finite State Processes (FSP) “A process calculus is a small language that allows us to give precise

descriptions of the essential properties of concurrent and communicating programs.”

FSP is a process calculus. FSP is finite, so we can do proofs, safety checks, etc. Example:

SUPREMA_GROUP = (enter -> present -> leave -> SUPREMA_GROUP).AUDIENCE = (yawn -> sleep -> wakeUp -> AUDIENCE).||FUN = (SUPREMA_GROUP || AUDIENCE).

Wishful thinking:yawn sleep enter wakeUp present leave…

Page 6: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Mission statement

Investigate the relationship between FSP and Java Restrict the subset of FSP under consideration Consider examples, develop the theory Discover interesting patterns and methods Formalize it, if possible, using mathematical notation Evaluate the results. Refine the model, if necessary Could we use these results to build an automated

FSP to Java translator? Would this be a useful tool?

Page 7: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Translation

The action prefix and simple processes Guarded actions Choice Variables Simple parallel composition Parallel composition of dependent processes Other FSP constructs

Page 8: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Quick FSP Recap

P1 =(a1 -> a2 -> … -> an -> STOP).

P = (when(B) a -> STOP).

DRINKS_MACHINE =

(red -> coffee -> DRINKS_MACHINE

|blue -> tea -> DRINKS_MACHINE).

Actions

Guarded Actions

Choice

FSP Processes Java classes FSP Actions Java Methods

Guards While-wait loop

Choice External factor allows choice

Page 9: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Variables

public class public class Store { private intprivate int i;

public public Store() {() { thisthis.i = 0; } synchronized public intsynchronized public int put(int i) { thisthis.i = i; }} //END class

range T = 0..5

STORE = STORE[0],STORE[i:T]=(put[i:T]->STORE[i]).

FSP Constructor Java constructor

STORE has a state variable, represented in Java as a field variable

‘put’ has an action variable, represented as a parameter for the put method

Page 10: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Simple parallel composition

public classpublic class Composition { protectedprotected A1 _a1; protectedprotected A2 _a2; … protectedprotected An _an;

publicpublic Composition() { newnew Thread(_a1 = newnew A1()); newnew Thread(_a2 = newnew A2()); … newnew Thread(_an = newnew An()); }}

||COMPOSITION = (a1 || a2 || … || an),

No shared actions!

Instantiate and start threads

Non-simple composition uses similar composite class

Page 11: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Composition: caller/callee pattern Two shared actions: designate one the caller and the other

callee Caller is part of a thread Calls callee method, which is part of a monitor Only works with 2 shared actions!public classpublic class Caller implementsimplements Runnable {

publicpublic run() { … Callee.a(); … }}

public classpublic class Callee {

synchronized public voidsynchronized public void a() {

//do something

}}

Page 12: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Composition: semaphores

public classpublic class Bill implementsimplements Runnable {

public voidpublic void run() { play(); release(A); acquire(B); eat(); }} //END class

BILL = (play -> meet -> eat -> STOP).BEN = (work -> meet -> sleep -> STOP).

public classpublic class Ben implementsimplements Runnable {

public voidpublic void run() { work(); acquire(A); release(B); sleep(); }} //END class

Uses semaphores – not intuitive Java Works for many shared actions – can become complex!

Page 13: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

General composition of processes

P1 = (… -> a -> … -> STOP).P2 = (… -> a -> … -> STOP).…P(n – 1) = (… -> a -> … -> STOP).Pn = (… -> a -> … -> STOP).

Method 1. Extend the semaphore method. For n shared methods, (2n-2) semaphores required.Initialize to an ‘acquired’ state.

Method 2. Synchronization object. Uses Java’s inbuilt synchronization.This object is a monitor and counts in the shared actions. Once they have all ‘reported in’ it will let all the threads continue.

Page 14: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Case study: Roller Coaster

Apology Monitors and threads revisited Caller/callee pattern Problem: parameters or return values? Problem: action order Well-formed FSP

Page 15: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Caller/callee pattern

Only one type of process interaction – monitor/thread.

In general, this is the most common form of process communication.

‘Directionality’ is an important criterion for preferring this design pattern. E.g. the thread Passengers tells the monitor Controller that a newPassenger has arrived.

Page 16: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Problem: parameters vs return COASTERCAR has action getPassenger[i:1..MCar] with a ‘free’ variable (cf. Prolog).

CONTROLLER has action getPassenger[carSize]. The variable carSize is bound.

A method of Coastercar (thread) to be called from Controller (monitor)?

Use return values instead: synchronized public int getPassenger()in Controller.

Page 17: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Problem: action order PLATFORMACCESS =

(arrive -> depart -> PLATFORMACCESS). PLATFORMACCESS =

({arrive -> depart} -> PLATFORMACCESS).

publicpublic classclass PlatformAccess {

public booleanpublic boolean arrive_done = falsefalse;

synchronized public voidsynchronized public void arrive() {

whilewhile (arrive_done) wait();

arrive_done = truetrue;

}

synchronized public voidsynchronized public void depart() {

whilewhile (! arrive_done) wait();

arrive_done = falsefalse;

}

}

Page 18: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Well-formed FSP

Is this translation ‘natural’? Does Java ‘match’ FSP? Is it easy for an automated FSP2Java

program to spot this? Could ask the user to re-write the FSP to

comply with a well-formed FSP standard. For example…

Page 19: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Well-formed FSP II PLATFORMACCESS = (arrive -> depart -> PLATFORMACCESS).

PLATFORMACCESS = PLATFORMACCESS[0],PLATFORMACCESS[i:0..1]= (when(i=0) arrive->PLATFORMACCESS[1] |when(i=1) arrive->PLATFORMACCESS[0] ).

Page 20: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Conclusions

What is a good translation? A subset of FSP Limitation Automatic Translation Further work

FSP modificationValidation of translationRest of FSP

Page 21: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

What is a good translation?

The factors are: Complexity Readability

We have found out that the Caller/Callee pattern is the most intuitive. However, this pattern cannot be used for more than 2 shared actions.

We would like to read and understand the Java code easily.

Page 22: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Limitation

Caller/Callee pattern: Monitor? Thread?

Data flow between processes can be difficult to translate. One solution is to rewrite the FSP and merge the actions.

A FSP can send more than 1 item of data but Java cannot.

Page 23: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Automatic translation

Given more time, we may write a program that automatically translates FSP to Java.

Closer relation between FSP and Java code

Humans have benefit of context, which will effect implementation

Is this feasible?

Page 24: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

FSP modification

We may rewrite the FSP in some circumstances.

Aim: Make the translation more effective and concise.

Page 25: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

Validation of translation

We are able to translate a large subset of FSP.

But how can we prove that the Java code actually corresponds to the FSP given?

Very difficult: Logic reasoning?

Page 26: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

I Want More!

Prof. Magee’s home page:http://www.doc.ic.ac.uk/~jnm/

Our project home page:http://www.doc.ic.ac.uk/~pb401/Suprema/

Web articles and project report.

Test yourself – try Q&A!

You are dazzled, fascinated, intrigued… where do you want to go today?

Page 27: START Translation of process algebras to Java Paul Bilokon Samuel Lau Andrew Roberts

That’s all, folks!

http://www.doc.ic.ac.uk/~pb401/Suprema