E NABLING THE A UTOMATION OF H ANDLER B INDINGS IN E VENT -D RIVEN P ROGRAMMING YungYu Zhuang and Shigeru Chiba The University of Tokyo
FRP and Signals ! FRP (Functional-reactive programming) was developed for reactive programs ◦ Signals (behaviors) To describe continuous data streams, e.g. mouse position ◦ Event streams A series of events on the timeline, e.g. mouse click Used to get a snapshot of signals at a specific time Enabling the Automation of Handler Bindings in Event-Driven Programming, YungYu Zhuang and Shigeru Chiba, The University of Tokyo
A simple example: spreadsheet ! Can be regarded as an A1 ! ! =B1+C1 ! ! interactive programming A ! B ! C ! environment 1 3 ! 2 ! 1 ! ◦ Cells are fields 2 ◦ Sheets are some sort of objects Use spreadsheet program to A cell can contain a constant define a sheet ! value or an expression ◦ B1 = 2 ◦ C1 = 1 ◦ A1 = B1 + C1 ß updated automatically when the value of B1 or C1 is changed Enabling the Automation of Handler Bindings in Event-Driven Programming, YungYu Zhuang and Shigeru Chiba, The University of Tokyo
It’s easy to implement the sheet with signals ! 1. < body onload ="loader()"> 2. < table width =200> Flapjax: JavaScript-based FRP 3. < tr >< th >A1</ th > 4. < th >B1</ th > [L. A. Meyerovich et al, OOPSLA’09] 5. < th >C1</ th > 6. </ tr > 7. < tr >< th >< input id ="A1" size =2 value ="0" /></ th > 8. < th >< input id ="B1" size =2 value ="2" /></ th > 9. < th >< input id ="C1" size =2 value ="1" /></ th > 10. </ tr > Extract a signal (behavior) 11. </ table > from UI component ! 12. </ body > 13. Simply describe 14. < script type ="text/flapjax"> 15. function loader() { them as in 16. var b = extractValueB("B1"); spreadsheet 17. var c = extractValueB("C1"); 18. var a = b + c; programs! ! 19. insertValueB(a, "A1", "value"); 20. } Insert a signal (behavior) into UI component ! 21. </ script > Enabling the Automation of Handler Bindings in Event-Driven Programming, YungYu Zhuang and Shigeru Chiba, The University of Tokyo
Wait a minute! We already have events ! Event-driven programming has been developed for a long time ◦ Widely used in OO libraries Can’t we use events to implement this sheet example? à Yes, of course we can. ! Enabling the Automation of Handler Bindings in Event-Driven Programming, YungYu Zhuang and Shigeru Chiba, The University of Tokyo
We can also implement it with events ! 1. class Sheet { EScala: Scala-based event system 2. var a: Int = _ [V. Gasiunas et al, AOSD’11] 3. var b: Int = _ 4. var c: Int = _ 5. def setB(nb: Int) { b = nb; } 6. def setC(nc: Int) { c = nc; } An event occurring after the 7. specified method is executed ! 8. evt eb[Unit] = afterExec (setB) 9. evt ec[Unit] = afterExec (setC) 10. def ha() { a = b + c; } 11. eb += ha; The relation is 12. ec += ha; 13. } described in a handler ! Add a handler (right-hand side) to the event (left-hand side) ! Enabling the Automation of Handler Bindings in Event-Driven Programming, YungYu Zhuang and Shigeru Chiba, The University of Tokyo
Both of them state “a = b + c”, but… ! In the signal version ◦ The assignment looks like an equation In the event version ◦ Only effective just after it is executed ◦ Might not be “true” until it is executed again signal version: event version: : evt eb[Unit] = afterExec (setB) : evt ec[Unit] = afterExec (setC) var a = b + c; def ha() { a = b + c; } : eb += ha; ec += ha; : Enabling the Automation of Handler Bindings in Event-Driven Programming, YungYu Zhuang and Shigeru Chiba, The University of Tokyo
Our proposal: expanding event systems ! To provide the automation in handlers à allow binding a handler to all events inside itself automatically 1. Automatic inference Can find out e b and e c according to h a 2. Implicit binding Can bind h a to e b and e c A prototype implementation: ReactiveDominoJ (RDJ) Enabling the Automation of Handler Bindings in Event-Driven Programming, YungYu Zhuang and Shigeru Chiba, The University of Tokyo
RDJ is a Java-based language Such a method-like declaration can be both event and handler. public void setX(int nx) { this.x = nx; } ◦ Add a handler to the event s.setX s.setX += o.update; ◦ Add the handler s.setX to an event t.tick += s.setX; Syntax: ⟨ event ⟩ ⟨ assignment operator ⟩ ⟨ handler ⟩ ; Enabling the Automation of Handler Bindings in Event-Driven Programming, YungYu Zhuang and Shigeru Chiba, The University of Tokyo
The braces operator in RDJ ! ⟨ event ⟩ can be also a handler within {} ◦ To select all the involved events inside a handler E.g. {h a } refers to all the involved events in h a ◦ // enables the automation {this.updateA} += this.updateA(); ◦ {_} += this.updateA; // “_” refers to the right one à RDJ compiler will do the rest ◦ Infers the involved events and binds the handler to them Enabling the Automation of Handler Bindings in Event-Driven Programming, YungYu Zhuang and Shigeru Chiba, The University of Tokyo
Compare RDJ version with Flapjax-like version ! RDJ version with {} public class PlusSheet 1. No need to manually extends Sheet { 2. enumerate all events ! public PlusSheet() { 3. : 4. {_} += this.updateA(); 5. } 6. RDJ version without {} public void updateA() { 7. a.setValue(b.getValue() + c.getValue()); public class PlusSheet 8. 1. } extends Sheet { 9. 2. } public PlusSheet() { 10. 3. : 4. Flapjax-like version b.setValue += this.changed; 5. public class PlusSheet c.setValue += this.changed; 1. 6. extends Sheet { this.changed += this.updateA; 2. 7. public PlusSheet() { } 3. 8. : public void changed(int v); 4. 9. Behavior b = b1.extractValueB(); public void updateA(int v) { 5. 10. Behavior c = c1.extractValueB(); a.setValue(b.getValue() + c.getValue()); 6. 11. Behavior a = b + c; } 7. 12. a1.insertValueB(a); } 8. 13. } 9. } 10. In order to make it easier to compare we assume that there were a Flapjax-like Java-based language ! Enabling the Automation of Handler Bindings in Event-Driven Programming, YungYu Zhuang and Shigeru Chiba, The University of Tokyo
What RDJ compiler does ! A binding with {} is boiled down ◦ {_} += this.updateA(); à b.setValue += this.updateA(); c.setValue += this.updateA(); No need to manually ensure the consistency between bindings and handler body ◦ Reduce the risk of bugs ◦ Make code shorter Enabling the Automation of Handler Bindings in Event-Driven Programming, YungYu Zhuang and Shigeru Chiba, The University of Tokyo
Implicit vs. Explicit! ! That’s the difference between FRP and existing event systems The braces operator makes it implicit ◦ As what signals do What is the insufficiency in existing event systems? ◦ i.e. how the braces operator does? Enabling the Automation of Handler Bindings in Event-Driven Programming, YungYu Zhuang and Shigeru Chiba, The University of Tokyo
Let’s check the meaning of signals again ! Signal ! Expression ! a = b + c ! Signal assignment ! A signal ( a ) ◦ Is reevaluated when any of the signals ( b , c ) in its expression is reevaluated ◦ Then all signals whose expressions contain this signal ( a ) will be reevaluated as well For detailed description of signals and signal assignments, please refer to our paper ! Enabling the Automation of Handler Bindings in Event-Driven Programming, YungYu Zhuang and Shigeru Chiba, The University of Tokyo
How signals are translated in event systems ! Field ! Expression ! a = b + c ! Not automatic bound to at all ! ! the events in the expression ! Handler for updating the field ! A signal is a field ( a ) with an event ( e a ) Its assignment is a handler ( h a ) ◦ The handler is executed when any of events ( e b , e c ) in its expression occurs ◦ Then its event ( e a ) occurs and other fields using the field ( a ) in their expressions will be set Enabling the Automation of Handler Bindings in Event-Driven Programming, YungYu Zhuang and Shigeru Chiba, The University of Tokyo
Actually RDJ is built on top of DJ ! RDJ expands DJ (DominoJ * ) ◦ By enabling the automation in method slots * ◦ But the idea can be applied to any event system A method slot is an object’s property ◦ A “field” holds an array of function closures object s : class Shape (void (int nx)) { target.update(nx); } int x | target = o methodslot setX … method slots can be used as both events (void (int nx)) { this.x = nx; } | this = s and handlers! ! *Our previous work presented at AOSD’13: Method slots: supporting methods, events, and advices by a single language construct ! Enabling the Automation of Handler Bindings in Event-Driven Programming, YungYu Zhuang and Shigeru Chiba, The University of Tokyo
Recommend
More recommend