programming coordinated behavior in java david harel
play

Programming Coordinated Behavior in Java David Harel, Assaf Marron, - PowerPoint PPT Presentation

Programming Coordinated Behavior in Java David Harel, Assaf Marron, Gera Weiss Ben-Gurion University of the Negev 1 David Harel Gera Weiss Assaf Marron 2 Overview Scenario-based


  1. Programming Coordinated Behavior in Java David Harel, Assaf Marron, Gera Weiss �ב תטיסרבינוא� בגנב �וירוג Ben-Gurion University of the Negev 1

  2. David Harel Gera Weiss Assaf Marron 2

  3. Overview • Scenario-based programming is a natural and useful approach: – Behaviors are programmed separately – oblivious of each other – Interlaced and coordinated centrally at run time • Introduced via the visual language LSC - Live Sequence Charts – [Damm and H., 2001, H. and Marelly, 2003] • We extend it to OO / procedural programming – Behaviors are coded as Java threads (b-threads) – Coordination/ interlacing thru a new Java lib & API • A general paradigm emerges: Behavioral Programming 3

  4. Outline • Formal semantics • The Java library: BPJ • Examples • Future research directions 4

  5. Decomposing Complex Behaviors Human behavior can often be decomposed into interlaced threads Traveling = Directions + Daily Schedule ���������� �������������� Game Playing = Rules + Basic Strategies + Tips & Tricks 5

  6. Behavior thread (“b-thread”) A software component that coordinates and controls a system behavior. The b-thread: • • Requests that events be considered for triggering Requests that events be considered for triggering • Watches for events that were triggered • Blocks events (requested by others) from being triggered …and all b-threads are interlaced and synchronized 6

  7. Interlacing and Synchronization Cycle 1. “Bidding” : Each b-thread announces requested , blocked , watched events 2. “Auction and Announcement” : Select a requested, not blocked event 3. Arbitrary computing Requested events b-thread b-thread b-thread Blocking b-thread Selected event 7

  8. The “Block” Idiom Example: B-Threads for adjusting water temperature & flow (Thanks to Amir Nissim) AdjustHighTemp: – watch for tempTooHigh – request addCold or reduceHot , and block addHot and reduceCold AdjustLowTemp AdjustLowTemp – watch for tempTooLow – request reduceCold or addHot , and block reduceHot and addCold AdjustHighFlow – watch for flowTooHigh – request reduceHot or reduceCold , and block addHot and addCold AdjustLowFlow – watch for flowTooLow C – request addHot or addCold , H and block reduceCold and reduceHot 8

  9. b-Thread: Formal Definition A b-thread is : – a transition system � � �� � � → � ���� � ; and – for each state � : • a set � ( � ) of requested events • a set � ( � ) of blocked events � � , � � � � , � � , � � � ( � � ) ={ � � , � � } � ( � � ) ={ � � , � � } . . . � ( � � ) ={ � � , � � } � ( � � ) ={ � � } The events on arrows are called watched events 9

  10. Execution of a set of b-threads: Formal Def. A run of a set of b-threads � {� � � �� � � �� → � �� ���� � �� � � �� � � � �� � ������� � } : The event � causes the transition iff: 10

  11. Controlling Event Selection • Priorities/order are added for programming control • Current implementation: – Each b-thread has a distinct priority (a real number) – Within each b-thread, the requested-events set is ordered • Select the first event that is requested and not blocked 11

  12. The BPJ Library and API • B-threads are Java threads • Events and event sets are Java objects and collections Development and execution do not require special environments • • Direct integration with other Java code BThread requestedEvents : Event set blockedEvents : Event set watchedEvents : Event set priority : Real runBThread() /* overridden for every b-thread */ bSync(requested, watched, blocked) Event Online: www.cs.bgu.ac.il/~geraw 12

  13. Example 1: “Hello, World!” Part 1. Events 13

  14. Part 2. b-Threads 14

  15. Part 3. Final Output 15

  16. Demo “Hello, World!” in Eclipse 16

  17. Example 2: A Behavioral Program for Tic-Tac-Toe • Events: X <0,0> X <0,1> X <0,2> O <0,0> O <0,1> O <0,2> – Move events X <1,0> X <1,1> X <1,2> O <1,0> O <1,1> O <1,2> X <2,0> X <2,1> X <2,2> O <2,0> O <2,1> O <2,2> – XWin, Owin, Draw • B-Threads • B-Threads – Rules: • Alternate player turns • Prevent multiple marking in same square • Detect win and draw conditions – Tactics, e.g. : • Attack: Wait for two O moves in a line – request marking of third square • Defense: Wait for two X moves in a line – request marking of third square • Prevent “fork” conditions • Default moves 17

  18. Incrementality and Behavior Autonomy Note that when b-threads were added: Little or no change to existing code New code has little or no dependency on old code Meaningful/Interesting behavior from early development stages 18

  19. Development with BP: Advantages Natural : Alignment with how people think about behavior / specify requirements • • Suitability to multi-core architectures • Incremental: Add/remove behavior with little or no change to existing b-threads – – Suitability for collaborative / open source projects – – Possibility for dynamic updates. Possibility for dynamic updates. • Modularity – Behavior Autonomy: – Code of b-threads has little or no dependency on code in other b-threads – “Redundant” requested events are unified, events that are not watched are ignored • Early/partial execution Integration with powerful languages (e.g. Java) • • Explicit behavior prioritization • Ability to “explain” program actions 19

  20. Evolution leading to this work • Natural language descriptions of behavior (e.g. r equirements / specification docs ) • Formal languages: MSCs (1996), UML Sequence Diagrams (1997) • Executable visual languages – StateCharts (1987, 1990) • LSC and the Play Engine : Scenario based programming (1999,2001,2003) – – Programming and running scenarios: play-in and play-out Programming and running scenarios: play-in and play-out – Multi-modality: • Must vs. May, Allow vs. Forbid, Universal vs. Existential, Execute vs. monitor Focused on executing or monitoring the specification – • BPJ : Imperative idioms for programming behaviors (2010) – Basic idioms: b-threads, request/watch/block BPJ: Integrate b-thread programming with OO – – Focused on enabling scenario-based programming in traditional environments. 20

  21. LSC Example Phone - Speed Dialing Scenario 21

  22. Future Research Directions Deadlock/conflict detection : verification/model-checking, ‘’smart play-out’’ • (Java pathfinder) • Visualization of synchronized b-threads • Enriched semantics: Concurrent events, sets/hierarchy of behavioral programs, dynamic priorities, prioritized blocking • • Higher level interfaces and DSLs; end-user programming; tools; Higher level interfaces and DSLs; end-user programming; tools; • Applications: robotics, hybrid systems (w/ideas from fuzzy logic), games, biological simulation, multi-agent/swarms. • Expand programming environments: e.g. functional, in Erlang. • Automatic patching, genetic programming. • Performance: analysis and optimization • Development methodologies • Quantitative comparison to other approaches 22

  23. BPJ and Behavioral Programming: Summary • Behavioral programming – an emerging general paradigm • Bringing advantages of scenario-based programming from the realm of LSCs to classical programming: – Behavior modularity, incrementality, suitability for multi-core, natural development,… • A working Java implementation + several small-scale examples • Basic idioms: request / watch / block – implementable in other languages – extendable • Many interesting research questions • The library is available at www.cs.bgu.ac.il/~geraw 23

  24. Thank You Questions / Discussion 24

Recommend


More recommend