Active C# R. Güntensperger, J. Gutknecht ETH Zürich
Agenda 1. 2. 3. 4. 5.
Overview / History Activity Waiting for conditions Formal dialogs 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: class C { activity { // activity behavior } }
• Self-active behavior • Communication by formal dialogs (optional, see later)
– Inheritance: • Activities of the base class are implicitly invoked
Activity variants Unnamed Instance-bound
activity { // activity behavior }
Named
static
Executed by instance constructor
activity A1 { // activity behavior } Explicitly executed in instance context
static activity { // activity behavior } Executed by class constructor
static activity A1 { // activity behavior } Explicitly executed in static/type context
• executed automatically (including base class activities) • private access rights
• executed explicitly: new A1; • multiple instances possible • can implement a dialog (see later) • protected access rights
3. Waiting for conditions • Execution may depend on fulfillment of conditions Waits until condition is true Resumes execution activity { … lock { await(condition); } … }
1)
Sets condition to true
3)
activity { … lock { condition = true; } … }
2)
Implementation (lock) • Overloading standard lock – Choosing context (static/instance) automatically by compiler – Signaling possible condition change to (all) waiting activities lock { … }
Mapping
Signals: „Change of conditions possible“
Determined by the compiler: • instance-bound: this • static: typeof(Class)
Monitor.Enter(context); try { // statements } finally { Monitor.PulseAll(context); Monitor.Exit(context); }
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; while(!condition) (see before) {
await(condition);
if(!waitingAlready) { Monitor.PulseAll(ref); waitingAlready = true; } Monitor.Wait(ref);
Mapping
* Remark: It is impossible that any condition change takes place 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
– 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: Vehicle for syntax-controlled communication between objects (local/remote) • Dialog declaration: Interface for implementation
Dialog declaration
Dialog definition
Object
dialog D { k1, k2, k3 }
• Dialog definition: Implementation of server side class C { activity D1 : D { // statements } }
• Use of dialog: Instantiation by clients C c = new C(); D myDialog = new c.D1;
Client A
Client B
Dialog lifecycle Client side
Server side
sequence
Create instance
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) Action
By client
In parser context
Send
d!obj;
!obj;
Receive (blocking)
d?obj;
?obj;
Receive (non-blocking)
d??obj;
??obj;
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