350 likes | 425 Views
Semantics. Connection to Traversal Graphs. Strategy: From C1 to T. o2:C2. o1:C1. declared type of o2 is C3=>C2. e. go down e iff C1 <=.C C3 (=>.<=.C.=>)*.<=) T go down e iff C1 EI* EC C3 (EA*(EI* EC EA*)* EI*) T. go down e iff C1 <=.C C3 (=>.<=.C.=>)*.<=) T. Example 1. strategy:
E N D
Semantics Connection to Traversal Graphs
Strategy: From C1 to T o2:C2 o1:C1 declared type of o2 is C3=>C2 e go down e iff C1 <=.C C3 (=>.<=.C.=>)*.<=) T go down e iff C1 EI* EC C3 (EA*(EI* EC EA*)* EI*) T
go down e iff C1 <=.C C3 (=>.<=.C.=>)*.<=) T 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 c2:C BOpt c c3:C C
Are the two concepts equivalent? • Traversals done by Mitch’s semantics. • Traversals done by the Traversal Methods Algorithm.
Traversal methods algorithmAlgorithm 2 • Idea is to traverse an object graph while using the traversal graph as a road map. • Maintain set of “tokens” placed on the traversal graph. • May have several tokens: path leading to an object may be a prefix of several distinct paths in PathSet[SS,G,N,B].
Traversal methods algorithm • 4. Let Q be the set of labels which appear both on edges outgoing from a node in T’ÎTG and on edges outgoing from this in the object graph. For each field name lÎQ, let Tl = {v|(u,l,v) ÎTG for some uÎT’}. • 5. Call this.l.Traverse(Tl) for all lÎQ, ordered by “<“, the field ordering.
Main Theorem • Let SS be a strategy, let G be a class graph, let N be a name map, and let B be a constraint map. Let TG be the traversal graph generated by Algorithm 1, and let Tsand Tfbe the start and finish sets, respectively.
Main Theorem (cont.) • Let O be an object tree and let o be an object in O. Let H be the sequence of nodes visited when o.Traverse is called with argument Ts , guided by TG. Then traversingOfromoguided byPathSet[SS,G,N,B]producesH.
Complexity of algorithm • Algorithm 1: All steps run in time linear in the size of their input and output. Size of traversal graph: O(|S|2 |G| d0) where d0 is the maximal number of edges outgoing from a node in the class graph. • Algorithm 2: How many tokens? Size of argument T is bounded by the number of edges in strategy graph.
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 each in the strategy graph corresponds to at least one edge in the object graph.
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.
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
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
So far: Remarks about traversals • Traversals are opportunistic: As long as there is a possibility for success (i.e., getting to the target), the branch is taken. • In the TOPLAS 95 paper and my book (page 459): Notice that we let the set of paths guide the traversal as long as possible.
A1 (=>.(<=C=>)*.<=) E2 c1 D1 B2 E2 A1 D2 E1 B1 A2 K2 K1
Definition • POSS(Class c1, Class t, Object o1) = those edges e outgoing from o1 s.t. there is an object graph O (consistent with the class graph C), containing the object o1 of class c1, an object o2 of a class that is a subclass of t, and a path in O from o1 to o2 such that the first edge in the path is e. • POSS: possibility of success
strategy 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 object graph slice :D :C R S T a1:A 0..1 class graph object graph r1:R s1:S
strategy POSS(A,T,a1) = 1 edge POSS(R,T,r1) = 1 edge POSS(S,T,s1) = 1 edge POSS(T,D,t1) = 1 edge POSS(R,D,r2) = 1 edge Example A -> T T -> D a1:A 0..1 :D r1:R X 0..1 B c1:C s1:S D A C s2:S t1:T 0..1 object graph r2:R R S T 0..1 c2:C class graph d2:D
Object Slice • 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 if it terminates prematurely).
Path concept EI: inheritance or is-a edges EA: subclass or alternation edges EC: construction or has-a edges • Path from A to B: • EI implies EA in opposite direction • (EC | EA | EI)* but not EA followed by EI • ((EI* EC) | EA )* EI* • Equivalent: ? • EA* (EI* EC EA*)* EI* • ((EI* EC) | EA )* EI*
Agenda: Add to DJ • Add WandVisitor as a new subclass to Visitor. • In a WandVisitor visitor method activation is delayed until we are at a target. • What are the semantics?
WandVisitor example // where has source A and target C void someMethod(TraversalGraph where) { where.traverse(this, new WandVisitor(“A”,”C”) { void before(A a){print(a);} void before(B b){print(b);} void before(C c){print(c);} }); } Which methods will be executed when a C-object is visited? Not all As and Bs visited since last visit to a C-object?
Visitor Methods forConstruction Edges • void cbefore_x(Source s, Target t); • -> Source,x,Target • void cbefore(Source s, String partName, Target t); • -> Source, **, Target • void cbefore_x(Source s); • -> Source, x, * • void cbefore(Source s, String partName); // * • -> Source, **, *
Visitor Methods forConstruction Edges • void cbefore_x(Target t); // * • -> *,x,Target • void cbefore(String partName, Target t); // * • -> *,**,Target • void cbefore_x(); // * • -> *,x,* • void cbefore(String partName); // * ; all edges • -> *,**,*
CEdgeInfo CEdgeInfo = [<sourceName> String] [<partName> String] [<targetName> String] [<edgeKind> String]. // derived / public, protected, private
SEdgeInfo SEdgeInfo = [<sourceName> String] [<targetName> String].
Visitor has a method EdgeInfo getCEdgeInfo() that returns the EdgeInfo of the current construction edge being traversed.
Visitor Methods forConstruction Edges • void cbefore_x(Source s, Target t); • -> Source,x,Target • void cbefore(Source s, Target t); • -> Source, *, Target • void cbefore_x(Source s); • -> Source, x, * • void cbefore(Source s); • -> Source, *, *
Visitor Methods forConstruction Edges • void cbefore_x(Target t); // * • -> *,x,Target • void cbefore(String partName, Target t); // * • -> *,**,Target • void cbefore_x(); // * • -> *,x,* • void cbefore(String partName); // * ; all edges • -> *,**,*
Visitor Methods forConstruction Edges • void cbefore_x(Source s, EdgeInfo e); • -> Source, x, * • void cbefore(Source s, EdgeInfo e); // * • -> Source, *, *
Visitor Methods forConstruction Edges • void cbefore_x(Target t, EdgeInfo e); // * • -> *,x,Target • void cbefore(Target t, EdgeInfo); // * • -> *,**,Target • void cbefore_x(EdgeInfo e); // * • -> *,x,* • void cbefore(String partName); // * ; all edges • -> *,**,*
Visitor Methods forStrategy Edges • void sbefore(Source s, Target t); // strategy
Programming with strategies check whether currently in scope of subtraversal // may be used in before, cbefore, rbefore, sbefore • // sg a substrategy of current strategy if (sg.contains(getSEdgeInfo())) { // currently in traversal determined by strategy sg • // tg a subgraph of current traversal graph if (tg.contains(getSEdgeInfo())) { // currently in traversal determined by tg
Programming with strategies • check whether currently in scope of substrategy // sg a substrategy of current strategy // may be used in before, cbefore, rbefore, sbefore if (sg.contains(getSEdgeInfo())) { // currently in traversal determined by strategy sg • check whether currently // tg a subgraph of current traversal graph // may only be used in cbefore if (tg.contains(getCEdgeInfo())) { // currently in traversal determined by tg