semantics connection to traversal graphs. strategy: from c1 to t o1:c1 o2:c2 e go down e iff c1. )*
TRANSCRIPT
Semantics
Connection to Traversal Graphs
Strategy: From C1 to T
o1:C1 o2:C2e
go down e iff C1 <=.C C3 (=>.<=.C.=>)*.<=) Tgo down e iff C1 EI* EC C3 (EA*(EI* EC EA*)* EI*) T
declared type of o2 is C3=>C2
Example 1strategy:{A -> B B -> C}
A
B
C
X
xx
b
c
class graph
A B C
Strategy s t
c
BOpt
Empty
Object graph
:A
c2:C
x1:X
:R
x2:X
c1:C
c3:C
e1:Empty SR
go down e iff C1 <=.C C3 (=>.<=.C.=>)*.<=) T
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 lQ, let
Tl = {v|(u,l,v) TG for some uT’}.
– 5. Call this.l.Traverse(Tl) for all lQ, 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 Ts
and Tf be 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 traversing
O from o guided by PathSet[SS,G,N,B] produces H.
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.
Explain directly in termsof paths in object 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.
Example 1strategy:{A -> B B -> C}
A
B
C
X
xx
b
c
class graph
A B C
Strategy s t
c
BOpt
Empty
Object graph
OG : A X R X COG’: A X B X CSG : A B C(CG: A X Bopt B X C)
:A
c2:C
x1:X
:R
x2:X
c1:C
c3:C
e1:Empty SR
Only node paths shown for space reasons
Example 1Astrategy:{A -> S S -> C}
A
B
C
X
xx
b
c
class graph
A S C
Strategy s t
c
BOpt
Empty
Object graph
OG : A X R X OG’: A X B X SG : A B (CG: A X Bopt B X )
:A
c2:C
x1:X
:R
x2:X
c1:C
c3:C
e1:Empty SR
Only node paths shown for space reasons
early termination
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
A1
A2
K1
B1
B2 D1c1
D2
K2
E1
E2
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
Example
A
R
BX
S
D
0..1
0..1
0..1
C
T0..1
A -> TT -> D
a1:A
r1:R s1:S
:C :D
classgraph
strategy
object graph
POSS(A,T,a1) = 1 edgePOSS(R,T,r1) = 1 edgePOSS(S,T,s1) = 0 edges
object graph slice
Example
A
R
BX
S
D
0..1
0..1
0..1
C
T0..1
A -> TT -> D
a1:A
r1:R
s1:Sc1:C
:D
classgraph
strategyPOSS(A,T,a1) = 1 edgePOSS(R,T,r1) = 1 edgePOSS(S,T,s1) = 1 edgePOSS(T,D,t1) = 1 edgePOSS(R,D,r2) = 1 edge
t1:T
r2:R
c2:C
d2:D
s2:S
object graph
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
• 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*
EI: inheritance or is-a edgesEA: subclass or alternation edgesEC: construction or has-a edges
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 Cvoid 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-objectis 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