1 / 155

A Short Introduction to Adaptive Programming (AP) with collaborations and adapters

A Short Introduction to Adaptive Programming (AP) with collaborations and adapters. Northeastern Team. Problem addressed. “To what extent is it possible to construct useful programs without knowing exactly what data types are involved?”

heidit
Download Presentation

A Short Introduction to Adaptive Programming (AP) with collaborations and adapters

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Short Introduction to Adaptive Programming (AP)with collaborations and adapters Northeastern Team DJ

  2. Problem addressed • “To what extent is it possible to construct useful programs without knowing exactly what data types are involved?” • First sentence of paper: “Generic functional programming with types and relations” by Richard Bird, Oege De Moor, Paul Hoogendijk, J. Functional Programming, 6(11): 1-28, January 1996. DJ

  3. Approaches • In paper mentioned: • Generalize specific algorithms to make them adaptive for different data types: pattern matching, maximum segment sum. • In AP: • Provide programming tools for writing adaptive algorithms in general. Current focus on Java: DJ. DJ

  4. Example: The Publisher-Subscriber Protocol • Have two collaborating participants: Publisher and Subscriber • All subscribers are reachable from publisher. We call this a “complete” version of the pattern (no attach or detach). • Describe a specific version of the pattern. DJ

  5. Publisher-Subscriber-complete collaboration PublisherSubscriberProtocol { participantPublisher { inTraversalGraph subscribers(); in-out void changeOp(*) { expected(); List subscrs = subscribers().asList(this); for (int i = 0; i < subscrs.size(); i++) {((Subscriber)subscrs.elementAt(i)). newUpdate(this);}} participantSubscriber { in void subUpdate(Publisher publ); protected Publisher publ; out void newUpdate(Publisher aPubl) { publ = aPubl; subUpdate(publ);} } } } Meta variable: bold Keywords: underscored DJ

  6. Classes for deployment Class Point { void set(…) } class InterestedInPointChange { void public notifyChange() { System.out.println("CHANGE ..."); } } DJ

  7. Deployment 1 adapter PubSubConn1 { Point isPublisherwith { changeOp = set*;} { TraversalGraph subscribers() { ClassGraph cg = new ClassGraph(true, false); String s = “from Point via B bypassing C to InterestedInPointChange”; return new TraversalGraph(s,cg); } InterestedInPointChange isSubscriberwith { void subUpdate(Publisher publ) { notifyChange(); System.out.println(”on Point object " + ((Point) publ).toString()); } } } DJ

  8. Adaptive DeploymentQosketeer adapter PubSubConn1 { Point isPublisherwith { changeOp = set*;} { TraversalGraph subscribers() { ClassGraph cg = new ClassGraph(true, false); String s = // computed from a System property return new TraversalGraph(s,cg); } InterestedInPointChange isSubscriberwith { void subUpdate(Publisher publ) { notifyChange(); System.out.println(”on Point object " + ((Point) publ).toString()); } } } DJ

  9. Red: expected replaced Blue: from adapter Deployment 1 Point Publisher changeOp calls newUpdate subscribers set InterestedInPointChange Subscriber subUpdate(Publisher) newUpdate(Publisher) subUpdate(Publisher) notifyChange() DJ

  10. Deployment 2 adapter PubSubConn2 { TicTacToe is Publisher with { changeOp = {startGame, newPlayer, putMark, endGame} TraversalGraph subscribers() { ClassGraph cg = new ClassGraph(true, false); String s = “from TicTacToe via B bypassing C to {BoardDisplay, StatusDisplay}”; return new TraversalGraph(s,cg); } }; {BoardDisplay, StatusDisplay} is Subscriber with { void subUpdate(Publisher publ) { setGame((Game) publ); repaint(); } }; } DJ

  11. Overview • DJ introduction • AspectJ and DJ • Aspect-oriented Programming in pure Java using the DJ library DJ

  12. AP • Late binding of data structures • Programming without accidental data structure details yet handling all those details on demand without program change DJ

  13. Concepts needed(DJ classes) • ClassGraph • Strategy • TraversalGraph • ObjectGraph • ObjectGraphSlice • Visitor DJ

  14. Adaptive Programming Strategy Bold names refer to DJ classes. is use-case based abstraction of ClassGraph defines family of ObjectGraph DJ

  15. Adaptive Programming Strategy defines traversals of ObjectGraph plus Strategy defines ObjectGraphSlice DJ

  16. Adaptive Programming Strategy guides and informs Visitor DJ

  17. Software Design and Development with DJ (very brief) • Functional decomposition into generic behavior • Decomposition into methods • Decomposition into formal traversal graphs • Decomposition into visitors • Adaptation of generic behavior • Identify class graph • Identify traversal strategies DJ

  18. Abstract pointcut set of execution points where to watch advice what to do Concrete pointcut set notation using regular expressions Abstract object slice set of entry/exit points where to go visitor what to do Actual object slice path set notation using traversal strategies AspectJ DJ DJ

  19. AspectJ: Observer Pattern: Abstract Pointcut public abstract aspect Subject { ... abstract pointcut stateChanges(); after(): stateChanges() { for (int i = 0; i < observers.size(); i++) { ((Observer)observers.elementAt(i)).update(); } } …} DJ

  20. AspectJ: Observer Pattern:Concrete Pointcut aspect ColoredNumberAsSubject extends Subject of eachobject(instanceof(ColoredNumber)) { pointcut stateChanges(): (receptions(void setValue(..)) || receptions(void setColor(..))); …} DJ

  21. DJ: Counting Pattern:Abstract Pointcut classBusRoute{ int countPersons(TraversalGraph WP) { Integer result = (Integer) WP.traverse(this, new Visitor(){ int r ; public void before(Person host){ r++; } public void start() { r = 0;} public Object getReturnValue() {return new Integer ( r);} }); return result.intValue();} } DJ

  22. DJ: Counting Pattern:Concrete Pointcut // Prepare the traversal for the current class graph ClassGraph classGraph = new ClassGraph(); TraversalGraph WPTraversal = new TraversalGraph (“from BusRoute via BusStop to Person”, classGraph); int r = aBusRoute.countPersons(WPTraversal); DJ

  23. Example : Count Aspect Pattern • collaboration Counting { • participantSource { • import TraversalGraph getT(); • public int count (){ // traversal/visitor weaving • getT().traverse(this, new Visitor(){ int r; • public void before(Target host){ r++; } • public void start() { r = 0;} …) } } • participantTarget {} • } Base: Meta variable: bold Keywords: underscore DJ

  24. DJ Example: TBR • Terminal Buffer Rule: • Terminal classes (i.e., classes not defined in the present class graph), if used as a part class, must be the only part part class. • Address = String String Number. • Address = Streetname CityName ZipCode. StreetName = String. CityName = String. ZipCode = Number. DJ

  25. Terminal Buffer Rule (TBR) //class Cd_graph public void TBRchecker( TraversalGraph definedClassNamesT, TraversalGraph allPartsT, TraversalGraph fetchIdentT) { // definedClassNamesT defines // the part of the object graph // that is relevant for finding // all defined classes. // allPartsT defines // the part of the object graph // that is relevant for checking // the TerminalBufferRule DJ

  26. TBR: generic behavior // find defined classes DefinedClassVisitor v1 = new DefinedClassVisitor (fetchIdentT); Vector definedClasses = (Vector) definedClassNamesT. traverse( this, v1); // check for violations TBRVisitor v2 = new TBRVisitor(definedClasses); allPartsT.traverse(this, v2); } DJ

  27. TBR: DefinedClassVisitor public class DefinedClassVisitor extends Visitor { ... private Vector vNonTerminals = new Vector(); public void before(Adj o) { Ident idCurrentAdj = fetchIdentT.fetch(o); vNonTerminals.addElement(idCurrentAdj);} public Object getReturnValue() { return vNonTerminals; } } DJ

  28. TBR: Adaptation to a concrete Structure 1 ClassGraph cg = new ClassGraph();//reflection TraversalGraph tg1 = new TraversalGraph( "from Cd_graph to Adj", cg); // The purpose of traversal tg2 // is to visit all parts of all classes TraversalGraph tg2 = new TraversalGraph( "from Cd_graph via Construct to Vertex", cg); TraversalGraph tg3 = … “from Adj through Vertex to Ident” …; CdGraph cdGraph = new CdGraph(...); cdGraph.TBRchecker(tg1,tg2,tg3); DJ

  29. Example 2: Cd_graph 0..* Entry EParse entries Cd_graph ClassDef BParse Body parts Part className 0..* super Ident ClassName Concrete Abstract DJ

  30. Example 2: Adaptation definedClassNamesT "from Cd_graph to ClassDef" 0..* Entry EParse entries Cd_graph ClassDef BParse Body parts Part className 0..* super Ident ClassName Concrete Abstract DJ

  31. Example 2: Adaptation allPartsT "from Cd_graph through Part to ClassName" 0..* Entry EParse entries Cd_graph BParse ClassDef Body parts Part className 0..* super Ident ClassName Concrete Abstract DJ

  32. Example 2: Adaptation fetchIdentT “from ClassDef through ->className to Ident” 0..* Entry EParse entries Cd_graph ClassDef BParse Body parts Part className 0..* super Ident ClassName Concrete Abstract DJ

  33. TBR: Adaptation to a concrete Structure 2 ClassGraph cg = new ClassGraph();//reflection TraversalGraph tg1 = new TraversalGraph( "from Cd_graph to ClassDef", cg); // The purpose of traversal tg2 // is to visit all parts of all classes TraversalGraph tg2 = new TraversalGraph( "from Cd_graph through Part to ClassName", cg); TraversalGraph tg3 = … “from ClassDef through ->className to Ident” …; CdGraph cdGraph = new CdGraph(...); cdGraph.TBRchecker(tg1,tg2,tg3); DJ

  34. Participant Graph:connections are elastic entries Cd_graph 0..* ClassDef Body parts Part className 0..* Ident ClassName DJ

  35. AP history • Programming with partial data structures = propagation patterns • Programming with participant graphs • Programming with object slices • partial data structures = all the constraints imposed by visitors DJ

  36. Collaborating Classes find all persons waiting at any bus stop on a bus route busStops BusRoute BusStopList OO solution: one method for each red class buses 0..* BusStop BusList waiting 0..* passengers Bus PersonList Person 0..* DJ

  37. find all persons waiting at any bus stop on a bus route Traversal Strategy from BusRoute through BusStop to Person busStops BusRoute BusStopList buses 0..* BusStop BusList waiting 0..* passengers Bus PersonList Person 0..* DJ

  38. find all persons waiting at any bus stop on a bus route Traversal Strategy from BusRoute through BusStop to Person busStops BusRoute BusStopList buses 0..* BusStop BusList waiting 0..* passengers Bus PersonList Person 0..* DJ

  39. find all persons waiting at any bus stop on a bus route Robustness of Strategy from BusRoute through BusStop to Person villages BusRoute BusStopList buses VillageList busStops 0..* 0..* BusStop BusList Village waiting 0..* passengers Bus PersonList Person 0..* DJ

  40. Writing Adaptive Programs with Strategies (DJ=pure Java) String WPStrategy=“from BusRoute through BusStop to Person” classBusRoute{ int countPersons(TraversalGraph WP) { Integer result = (Integer) WP.traverse(this, new Visitor(){ int r ; public void before(Person host){ r++; } public void start() { r = 0;} public Object getReturnValue() {return new Integer ( r);} }); return result.intValue();} } DJ

  41. Writing Adaptive Programs with Strategies (DJ=pure Java) String WPStrategy=“from BusRoute through BusStop to Person” // Prepare the traversal for the current class graph ClassGraph classGraph = new ClassGraph(); TraversalGraph WPTraversal = new TraversalGraph (WPStrategy, classGraph); int r = aBusRoute.countPersons(WPTraversal); DJ

  42. Writing Adaptive Programs with Strategies (DJ=pure Java) String WPStrategy=“from BusRoute through BusStop to Person” classBusRoute{ int countPersons(TraversalGraph WP) { Integer result = (Integer) WP.traverse(this, new Visitor(){...}); return result.intValue();} } ObjectGraph objectGraph = new ObjectGraph(this, classGraph); ObjectGraphSlice objectGraphSlice = new ObjectGraphSlice(objectGraph, WP); objectGraphSlice.traverse(visitor); WP.traverse(this,visitor) <===> DJ

  43. ObjectGraph BusRoute ( <busStops> BusStopList { BusStop( <waiting> PersonList { Person() Person()})} <buses> BusList{ Bus( <passengers> PersonList { Person() Person()})}) DJ

  44. ObjectGraph: in UML notation :BusList Route1:BusRoute buses busStops :BusStopList Bus15:Bus passengers CentralSquare:BusStop waiting :PersonList :PersonList Joan:Person Paul:Person Seema:Person Eric:Person DJ

  45. find all persons waiting at any bus stop on a bus route TraversalGraph from BusRoute through BusStop to Person busStops BusRoute BusStopList buses 0..* BusStop BusList waiting 0..* passengers Bus PersonList Person 0..* DJ

  46. ObjectGraphSlice BusRoute ( <busStops> BusStopList { BusStop( <waiting> PersonList { Person() Person()})} <buses> BusList{ Bus( <passengers> PersonList { Person() Person()})}) DJ

  47. ObjectGraphSlice BusList Route1:BusRoute buses busStops :BusStopList Bus15:Bus passengers CentralSquare:BusStop waiting :PersonList :PersonList Joan:Person Paul:Person Seema:Person Eric:Person DJ

  48. Example Short-cut strategy: {A -> B B -> C} class graph strategy A B C A A x traversal graph ??? c 0..1 b x c B X 0..1 x b c B X x c C C DJ

  49. Short-cut strategy: {A -> B B -> C} Object graph Traversal graph :A A start set x1:X :B x 0..1 b b x2:X X X B x c1:C x c b B c2:C finish set C c3:C DJ

  50. Short-cut • ObjectGraphSlice • static view • ObjectGraph, TraversalGraph, start nodes, finish nodes • dynamic view • when traverse method gets called • traversal history: node sequence of nodes in object graph • subgraph of object graph (but only part of the story) • visualize it by a movie of traverse DJ

More Related