You are on page 1of 16

Active C#

R. Güntensperger, J. Gutknecht
ETH Zürich
Agenda
1. Overview / History
2. Activity
3. Waiting for conditions
4. Formal dialogs
5. Summary
1. Overview / History
• ROTOR project (2002/2003)
– Main ideas:
• Object-to-object communication by
formal dialogs
• Objects with autonomous, active
behavior  from Active Oberon
– SSCLI C# compiler & language
enhancements
• Active C# (2004)
– Consolidation of previous work
2. Activity
• Idea:
– Hide threading mechanism
• Compare:
– Pointer -> Reference
– Thread -> Activity

• Properties:
– New kind of Class-Member
– Provides support for:
• Self-active behavior
class C {
• Communication by formal dialogs
activity {
// activity behavior (optional, see later)
} – Inheritance:
}
• Activities of the base class are
implicitly invoked
Activity variants
Unnamed Named
activity { activity A1 {
Instance-bound

// activity behavior // activity behavior


} }
 Executed by instance  Explicitly executed in
constructor instance context

static activity { static activity A1 {


// activity behavior // activity behavior
static

} }
 Executed by class  Explicitly executed in
constructor static/type context

• executed automatically • executed explicitly: new A1;


(including base class activities) • multiple instances possible
• private access rights • can implement a dialog (see later)
• protected access rights
3. Waiting for conditions
• Execution may depend on fulfillment of
conditions
Waits until 1)
condition is true Sets
condition 2)
to true
Resumes
3)
execution

activity { activity {
… …
lock { lock {
await(condition); condition = true;
} }
… …
} }
Implementation (lock)
• Overloading standard lock
– Choosing context (static/instance)
automatically by compiler
– Signaling possible condition change to (all)
waiting activities
Determined by the
Monitor.Enter(context); compiler:
lock { … } Mapping try { • instance-bound: this
// statements • static: typeof(Class)
}
Signals: finally {
„Change of Monitor.PulseAll(context);
conditions Monitor.Exit(context);
possible“ }
Implementation (await)
• Stop activity until await-condition is true
• Signaling (all) other waiting activities
– Where conditions might have been changed *
• Right before the first Wait
• At end of lock bool waitingAlready = false;
(see before) while(!condition)
{
if(!waitingAlready)
await(condition); Mapping {
Monitor.PulseAll(ref);
waitingAlready = true;
}
* Remark: It is impossible that any
condition change takes place
Monitor.Wait(ref);
inside the await statement }
4. Formal dialogs
• Both dialog partners
– agree to a common syntax
formal grammar
• Described in EBNF-like representation
• Enhancements to EBNF:
– Typed tokens (of a universal set of types)
– Direction keyword Opposite direction
Example (Robot-Protocol):
position <coordX <coordY | battery <batteryLoad | move x y <doneX <doneY

– implement a parser which follows the


common syntax
Formal dialogs (cont.)
• Use activity to drive dialog parser
– Named
– public access1
– Started by dialog partner1
Each dialog invoked by the dialog
partner runs in its own thread of control

1
in difference to standard activities
Formal dialogs (cont.)
• Formal dialog: Dialog declaration
Vehicle for syntax-controlled
communication between
objects (local/remote) Dialog definition
• Dialog declaration:
Interface for implementation Object
dialog D { k1, k2, k3 }
• Dialog definition:
Implementation of server side
class C {
activity D1 : D { // statements } }
• Use of dialog:
Instantiation by clients Client A Client B
C c = new C();
D myDialog = new c.D1;
Dialog lifecycle
Client side Server side
Create instance
sequence

Exchange tokens

Discard instance

• Fully integrated into the Active C# language


• Local and accross adress space boundaries
• Needs universal coding
Dialog accessors
• send/receive tokens to/from dialog
• Handle set of universal tokens
– System.Object as common base type
• Implicit casting to target type (by compiler)
In parser
Action By client
context
Send d!obj; !obj;
Receive
d?obj; ?obj;
(blocking)
Receive
d??obj; ??obj;
(non-blocking)
Example (1)
• Dialog definition (Server side)
static activity SimpleRobot : RobotControl {

keyword command;
float batteryLoad; int posX, posY;

?command;
switch(command) {
case RobotControl.position:
!posX; !posY; break;
case RobotControl.battery:
!batteryLoad; break;
case RobotControl.move:
?movex; ?movey;

!movedx; !movedy;

Example (2)
• Use of dialog (Client side)
DialogManager.Start(new TCPTransportManager("129.132.134.11"));
myRobot = DialogManager.Open("RobotServer", "SimpleRobot");

// get position
myRobot!RobotControl.position;
int posx, posy;
myRobot?posx; myRobot?posy;

// move the robot and verify the move
myRobot!RobotControl.move;
myRobot!movex;
myRobot!movey;
myRobot?donex;
myRobot?doney;

myRobot??specialMsg; // non-blocking
if(specialMsg != null) MessageBox.Show(specialMsg);
Summary
• Modelling autonomous, communicative
objects
• Scaling (local/remote)
• Suitable for stateful dialogs (i.e. for agents)
• Fully implemented on ROTOR/.NET
• Try it out: http://www.avocado.ethz.ch/ActiveCSharp

You might also like