1 / 174

AOSD 2002 Tutorial: Demeter Aspect-Oriented Programming of Traversal Related Concerns in Java

DEMETER. DHMHTRA. AOSD 2002 Tutorial: Demeter Aspect-Oriented Programming of Traversal Related Concerns in Java. Demeter Research Group. Overview. DJ introduction AspectJ and DJ Aspect-oriented Programming in pure Java using the DJ library . Problem addressed.

skipper
Download Presentation

AOSD 2002 Tutorial: Demeter Aspect-Oriented Programming of Traversal Related Concerns in Java

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. DEMETER DHMHTRA AOSD 2002 Tutorial:DemeterAspect-Oriented Programming of Traversal Related Concerns in Java Demeter Research Group Demeter and Aspects

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

  3. Problem addressed • Encapsulation of traversal-related concerns • Those are concerns that involve a group of collaborating objects that are connected by has-a relationships or zero-argument methods. • Control scattering of traversal-related concerns and tangling with other concerns • Construct useful programs without knowing exactly what data types are involved Demeter and Aspects

  4. How is the problem solved? • Need to talk about a traversal join point model: • sets of points (called join points) in the execution of a traversal where additional behavior is executed (called hooks or pointcuts). • the behavior to be executed (called an enhancement or advice). Demeter and Aspects

  5. An AOP System • has 3 critical elements • what are the join points • in AspectJ • points in runtime • means of identifying join points • in AspectJ • signatures (plus …) • means of specifying semantics at join points • in AspectJ • advice • define members Demeter and Aspects

  6. An AOP System (in more detail) • what is the set of all join points (SJP) • means of identifying join points(IJP) • means of specifying semantics at join points (SJP) • encapsulated units combining JPS and BJP (CSB) • method of attachment of units (AU) JPS and BJP are sometimes overlapping. JPS might already define an initial behavior plus a set of join points in that behavior. Demeter and Aspects

  7. 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 Demeter and Aspects

  8. Concepts needed(DJ classes) • ClassGraph • Strategy • Visitor • TraversalGraph • ObjectGraph • ObjectGraphSlice important for advanced programming Demeter and Aspects

  9. AOSD: not every concern fits into a component: crosscutting Goal: find new component structures that encapsulate “crosscutting” concerns Demeter and Aspects

  10. a reusable aspect abstractpublicaspect RemoteExceptionLogging { abstractpointcut logPoint(); after() throwing(RemoteException e): logPoint() { log.println(“Remote call failed in: ” + thisJoinPoint.toString() + “(” + e + “).”); } } abstract publicaspect MyRMILogging extends RemoteExceptionLogging { pointcut logPoint(): call(* RegistryServer.*.*(..)) || call(private * RMIMessageBrokerImpl.*.*(..)); } Demeter and Aspects

  11. 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();} } Demeter and Aspects

  12. 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); Demeter and Aspects

  13. 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 Demeter and Aspects

  14. AP history • Programming with partial data structures = propagation patterns • Programming with participant graphs = high-level class graphs • Programming with object slices • partial objects = all the constraints imposed by visitors Demeter and Aspects

  15. 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..* Demeter and Aspects

  16. 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..* Demeter and Aspects

  17. 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..* Demeter and Aspects

  18. 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();} } Demeter and Aspects

  19. 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); Demeter and Aspects

  20. 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) <===> Demeter and Aspects

  21. 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 Demeter and Aspects

  22. 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..* Demeter and Aspects

  23. ObjectGraphSlice BusList Route1:BusRoute buses busStops :BusStopList Bus15:Bus passengers CentralSquare:BusStop waiting :PersonList :PersonList Joan:Person Paul:Person Seema:Person Eric:Person Demeter and Aspects

  24. 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 TraditionalOptimized (TraversalGraphs) • strategies are a parameter. Reuse traversal graphs. • AdaptiveProgramDJ(ObjectGraphSlices) • strategies are a parameter. Reuse traversal graph slices. Demeter and Aspects

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

  26. 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. Demeter and Aspects

  27. Simple case: from A to B • See lecture: Navigation in object graphs navig-object-graphs-1205-w02.ppt Demeter and Aspects

  28. 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. Demeter and Aspects

  29. 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. Demeter and Aspects

  30. 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. Demeter and Aspects

  31. 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}. Demeter and Aspects

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

  33. Transitive Closure busStops BusRoute BusStopList buses 0..* BusStop BusList waiting 0..* passengers Bus PersonList Person 0..* Demeter and Aspects

  34. 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. Demeter and Aspects

  35. 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. Demeter and Aspects

  36. 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. Demeter and Aspects

  37. 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 Demeter and Aspects

  38. 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 (CG: A X Bopt B X) c2:C BOpt c c3:C C Demeter and Aspects

  39. 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 Demeter and Aspects

  40. 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 Demeter and Aspects

  41. 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 Demeter and Aspects

  42. Allow • strategy: {A -> B B -> C} • class graph: A : B. B : C. C = . • object graph: C Demeter and Aspects

  43. 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). Demeter and Aspects

  44. 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 Demeter and Aspects

  45. 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 Demeter and Aspects

  46. 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 Demeter and Aspects

  47. Integration of Generic and Adaptive Programming • A traversal specification turns an object graph into a list. • Can invoke generic algorithms on those lists. Examples: contains, containsAll, equals, isEmpty, contains, etc. add, remove, etc. throws operation not supported exception. • What is gained: genericity not only with respect to data structure implementations but also with respect to class graph Demeter and Aspects

  48. Sample DJ code // Find the user with the specified uid List libUsers = classGraph.asList(library, "from Library to User"); ListIterator li = libUsers.listIterator(); // iterate through libUsers Demeter and Aspects

  49. Methods provided by DJ • On ClassGraph, ObjectGraph, TraversalGraph, ObjectGraphSlice: traverse, fetch, gather • traverse is the important method; fetch and gather are special cases • TraversalGraph • Object traverse(Object o, Visitor v) • Object traverse(Object o, Visitor[] v) Demeter and Aspects

  50. Traverse method: excellent support for Visitor Pattern // class ClassGraph Object traverse(Object o, Strategy s, Visitor v); traverse navigates through Object o following traversal specification s and executing the before and after methods in visitor v ClassGraph is computed using introspection Demeter and Aspects

More Related