1.74k likes | 1.94k Views
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.
E N D
DEMETER DHMHTRA AOSD 2002 Tutorial:DemeterAspect-Oriented Programming of Traversal Related Concerns in Java Demeter Research Group Demeter and Aspects
Overview • DJ introduction • AspectJ and DJ • Aspect-oriented Programming in pure Java using the DJ library Demeter and Aspects
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
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
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
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
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
Concepts needed(DJ classes) • ClassGraph • Strategy • Visitor • TraversalGraph • ObjectGraph • ObjectGraphSlice important for advanced programming Demeter and Aspects
AOSD: not every concern fits into a component: crosscutting Goal: find new component structures that encapsulate “crosscutting” concerns Demeter and Aspects
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Example • For data member access: • C c = (C) Main.cg.fetch(this, “from A via B to C”); Demeter and Aspects
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
Simple case: from A to B • See lecture: Navigation in object graphs navig-object-graphs-1205-w02.ppt Demeter and Aspects
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
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
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
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
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
Transitive Closure busStops BusRoute BusStopList buses 0..* BusStop BusList waiting 0..* passengers Bus PersonList Person 0..* Demeter and Aspects
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
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
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
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
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
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
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
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
Allow • strategy: {A -> B B -> C} • class graph: A : B. B : C. C = . • object graph: C Demeter and Aspects
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
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
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
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
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
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
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
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