1 / 114

A Short Introduction to Adaptive Programming (AP) for Java Programmers

This paper provides a short introduction to Adaptive Programming (AP) for Java programmers, addressing the problem of constructing useful programs without knowing the exact data types involved. It explores generic functional programming with types and relations as well as the DJ framework for adaptive programming in Java. The paper discusses the DJ concepts, strategies, and techniques for programming without accidental data structure details and reducing representational coupling. It also includes examples and strategies for writing adaptive programs in Java using the DJ library.

kilburnj
Download Presentation

A Short Introduction to Adaptive Programming (AP) for Java Programmers

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)for Java Programmers 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. Overview • DJ introduction • AspectJ and DJ • Aspect-oriented Programming in pure Java using the DJ library DJ

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

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

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

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

  9. Adaptive Programming Strategy guides Visitor DJ

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

  11. 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

  12. 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

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

  14. 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

  15. 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

  16. Example : Count Aspect Pattern • collaboration Counting { • roleSource { • in 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;} …) } } • roleTarget {} • } Base: Meta variable: bold Keywords: underscore DJ

  17. AP history • Programming with partial data structures = propagation patterns • Programming with participant graphs = high-level class graphs • Programming with object slices • partial data structures = all the constraints imposed by visitors DJ

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

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

  28. Applications of Traversal Strategies • Program Kinds in DJ • AdaptiveProgramTraditional(ClassGraph) • strategies are part of program: DemeterJ, Demeter/C++ • AdaptiveProgramDynamic(Strategies, ClassGraph) • strategies are a parameter. Even more adaptive. • AdaptiveProgram DJ_Typical (TraversalGraphs) • strategies are a parameter. Reuse traversal graphs. • AdaptiveProgramDJ(ObjectGraphSlices) • strategies are a parameter. Reuse traversal graph slices. DJ

  29. Example • For data member access: • C c = (C) Main.cg.fetch(this, “from A via B to C”); DJ

  30. Understanding the meaning of a strategy • Classes involved: Strategy, ObjectGraph, ObjectGraphSlice, ClassGraph • We want to define the meaning of a Strategy-object for an ObjectGraph-object as an ObjectGraphSlice-object (a subgraph of the ObjectGraph-object). Minimal attention necessary will be given to ClassGraph-object. DJ

  31. Simple case: from A to B • See lecture: Navigation in object graphs navig-object-graphs-1205.ppt DJ

  32. Graphs and paths • Directed graph: (V,E), V is a set of nodes, E Í V´ V is a set of edges. • Directed labeled graph: (V,E,L), V is a set of nodes, L is a set of labels, E Í V´ L´ V is a set of edges. • If e = (u,l,v), u is source of e, l is the label of e and v is the target of e. DJ

  33. Graphs and paths • Given a directed labeled graph: (V,E,L), a node-path is a sequence p = <v0v1…vn> where viÎV and (vi-1,li,vi)ÎE for some liÎL. • A path is a sequence <v0 l1 v1 l2 … ln vn>, where <v0 …vn> is a node-path and (v i-1, li, vi )ÎE. DJ

  34. Graphs and paths • In addition, we allow node-paths and paths of the form <v0> (called trivial). • First node of a path or node-path p is called the source of p, and the last node is called the target of p, denoted Source(p) and Target(p), respectively. Other nodes: interior. DJ

  35. Strategy definition:embedded, positive strategies • Given a graph G, a strategy graph S of G is any subgraph of the transitive closure of G. Source s, Target t. • The transitive closure of G=(V,E) is the graph G*=(V,E*), where E*={(v,w): there is a path from vertex v to vertex w in G}. DJ

  36. S is a strategy for G F=t F D D E E B B C C S pink edge must imply black path G A = s A

  37. Transitive Closure busStops BusRoute BusStopList buses 0..* BusStop BusList waiting 0..* passengers Bus PersonList Person 0..* DJ

  38. Strategy graph and base graph are directed graphs Key concepts • Strategy graph S with source s and target t of a base graph G. Nodes(S) subset Nodes(G) (Embedded strategy graph). • A path p is an expansion of path p’ if p’ can be obtained by deleting some elements from p. DJ

  39. A simple view of traversals • When a traversal reaches a target node in the object graph, the path traversed from the source, with suitable substitution of subclasses by superclasses, must be an expansion of an s-t path in the strategy graph. s is the source and t is the target of the strategy. Each edge in the strategy graph corresponds to at least one edge in the object graph. DJ

  40. A simple view of traversals • When a traversal reaches a final node in the object graph without being at a target, the path traversed from the source, with suitable substitution of subclasses by superclasses, must be a prefix of an expansion of an s-t path in the strategy graph. The prefix is the longest prefix such that there is still a possibility of success as determined by the class graph. DJ

  41. Only node paths shown for space reasons Example 1 strategy: {A -> B B -> C} Object graph Strategy s t :A A B C x1:X class graph S e1:Empty :R R A x2:X Empty B x c x c1:C X b OG : A X R X C OG’: A X B X C SG : A B C (CG: A X Bopt B X C) c2:C BOpt c c3:C C DJ

  42. Only node paths shown for space reasons Example 1A strategy: {A -> S S -> C} Object graph early termination Strategy s t :A A S C x1:X class graph S e1:Empty :R R A x2:X Empty B x c x c1:C X b OG : A X R X OG’: A X B X SG : A B (CG: A X Bopt B X) c2:C BOpt c c3:C C DJ

  43. S = from BusRoute through Bus to Person Example 2 busStops BusRoute BusStopList buses 0..* NGasPowered BusStop BusList waiting 0..* passengers Bus PersonList Person 0..* DieselPowered DJ

  44. OG : BR BL DP PL P OG’: BR BL B PL P SG : BR B P Example 2 Only node paths shown for space reasons BusList Route1:BusRoute buses busStops :BusStopList Bus15:DieselPowered passengers CentralSquare:BusStop waiting :PersonList :PersonList Joan:Person Paul:Person Seema:Person Eric:Person S = from BusRoute through Bus to Person DJ

  45. OG : BR BL OG’: BR BL SG : BR Example 3 Only node paths shown for space reasons early termination BusList Route1:BusRoute buses busStops :BusStopList Bus15:DieselPowered passengers CentralSquare:BusStop waiting :PersonList :PersonList Joan:Person Paul:Person Seema:Person Eric:Person S = from BusRoute via NGasPoweredto Person DJ

  46. Disallow • strategy: {A -> B B -> C} • class graph: A : B. B : C. C = . • object graph: C • Follow rule: each edge in the strategy graph corresponds to at least one edge in the object graph. DJ

  47. ObjectGraphSlice • The object graph slice starting with o1 is the slice built by following the edges POSS(Class(o1), t, o1) starting at o1 and continuing until every path terminates (at an object of type t or it terminates prematurely). DJ

  48. class dictionary strategy A = [“x” X] [“r” R]. B = [“b” B] D. R = S. S = [“t” T] C C = D. X = B. T = R. D = . Example A -> T T -> D 0..1 X 0..1 B D A C 0..1 :D :C R S T :A 0..1 class graph object graph “r” :R :S DJ

  49. class dictionary strategy A = [“x” X] [“r” R]. B = [“b” B] D. R = S. S = [“t” T] C C = D. X = B. T = R. D = . Example A -> T T -> D POSS(A,T,a1) = 1 edge POSS(R,T,r1) = 1 edge POSS(S,T,s1) = 0 edges 0..1 X 0..1 B D A C 0..1 :D :C R S T a1:A 0..1 class graph object graph “r” r1:R s1:S DJ

  50. DJ • An implementation of AP using only the DJ library (and the Java Collections Framework) • All programs written in pure Java • Intended as prototyping tool: makes heavy use of introspection in Java • Integrates Generic Programming (a la C++ STL) and Adaptive programming DJ

More Related