Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Short Introduction to Adaptive Programming (AP) with collaborations and adapters Northeastern Team 11/16/2018 DJ.

Similar presentations


Presentation on theme: "A Short Introduction to Adaptive Programming (AP) with collaborations and adapters Northeastern Team 11/16/2018 DJ."— Presentation transcript:

1 A Short Introduction to Adaptive Programming (AP) with collaborations and adapters
Northeastern Team 11/16/2018 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. 11/16/2018 DJ

3 Approaches In paper mentioned: In AP:
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. 11/16/2018 DJ

4 Example: The Publisher-Subscriber Protocol
Have two collaborating participants: Publisher and Subscriber All subscribers are reachable from publisher. We call this a “complete” version of the pattern (no attach or detach). Describe a specific version of the pattern. 11/16/2018 DJ

5 Publisher-Subscriber-complete
collaboration PublisherSubscriberProtocol { participant Publisher { in TraversalGraph subscribers(); in-out void changeOp(*) { expected(); List subscrs = subscribers().asList(this); for (int i = 0; i < subscrs.size(); i++) {((Subscriber)subscrs.elementAt(i)). newUpdate(this);}} participant Subscriber { in void subUpdate(Publisher publ); protected Publisher publ; out void newUpdate(Publisher aPubl) { publ = aPubl; subUpdate(publ);} } Meta variable: bold Keywords: underscored 11/16/2018 DJ

6 Classes for deployment
Class Point { void set(…) } class InterestedInPointChange { void public notifyChange() { System.out.println("CHANGE ..."); 11/16/2018 DJ

7 Deployment 1 adapter PubSubConn1 { Point is Publisher with
{ changeOp = set*;} { TraversalGraph subscribers() { ClassGraph cg = new ClassGraph(true, false); String s = “from Point via B bypassing C to InterestedInPointChange”; return new TraversalGraph(s,cg); } InterestedInPointChange is Subscriber with { void subUpdate(Publisher publ) { notifyChange(); System.out.println(”on Point object " + ((Point) publ).toString()); } 11/16/2018 DJ

8 Adaptive Deployment Qosketeer
adapter PubSubConn1 { Point is Publisher with { changeOp = set*;} { TraversalGraph subscribers() { ClassGraph cg = new ClassGraph(true, false); String s = // computed from a System property return new TraversalGraph(s,cg); } InterestedInPointChange is Subscriber with { void subUpdate(Publisher publ) { notifyChange(); System.out.println(”on Point object " + ((Point) publ).toString()); } 11/16/2018 DJ

9 Deployment 1 Red: expected replaced Blue: from adapter Publisher Point
changeOp calls newUpdate subscribers set InterestedInPointChange Subscriber subUpdate(Publisher) newUpdate(Publisher) subUpdate(Publisher) notifyChange() 11/16/2018 DJ

10 Deployment 2 adapter PubSubConn2 { TicTacToe is Publisher with {
changeOp = {startGame, newPlayer, putMark, endGame} TraversalGraph subscribers() { ClassGraph cg = new ClassGraph(true, false); String s = “from TicTacToe via B bypassing C to {BoardDisplay, StatusDisplay}”; return new TraversalGraph(s,cg); } }; {BoardDisplay, StatusDisplay} is Subscriber with { void subUpdate(Publisher publ) { setGame((Game) publ); repaint(); } 11/16/2018 DJ

11 Overview DJ introduction AspectJ and DJ
Aspect-oriented Programming in pure Java using the DJ library 11/16/2018 DJ

12 AP Late binding of data structures
Programming without accidental data structure details yet handling all those details on demand without program change 11/16/2018 DJ

13 Concepts needed (DJ classes)
ClassGraph Strategy TraversalGraph ObjectGraph ObjectGraphSlice Visitor 11/16/2018 DJ

14 Strategy ClassGraph ObjectGraph Adaptive Programming is use-case based
Bold names refer to DJ classes. is use-case based abstraction of ClassGraph New: abstractions of class diagrams notice affinity to design patterns: some of them also talk about families of class structures explain strategies defines family of ObjectGraph 11/16/2018 DJ

15 Strategy ObjectGraph ObjectGraphSlice Adaptive Programming
defines traversals of ObjectGraph plus Strategy defines explain strategies ObjectGraphSlice 11/16/2018 DJ

16 Strategy Visitor Adaptive Programming guides and informs 11/16/2018 DJ
explain strategies Visitor 11/16/2018 DJ

17 Software Design and Development with DJ (very brief)
Functional decomposition into generic behavior Decomposition into methods Decomposition into formal traversal graphs Decomposition into visitors Adaptation of generic behavior Identify class graph Identify traversal strategies 11/16/2018 DJ

18 AspectJ DJ Abstract pointcut advice Concrete 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 11/16/2018 DJ

19 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(); } } …} 11/16/2018 DJ

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

21 DJ: Counting Pattern: Abstract Pointcut
class BusRoute { 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();} } 11/16/2018 DJ

22 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); 11/16/2018 DJ

23 Example : Count Aspect Pattern
collaboration Counting { participant Source { import 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;} …) } } participant Target {} } can we really program with strategies? Here is how. Base: Meta variable: bold Keywords: underscore 11/16/2018 DJ

24 DJ Example: TBR Terminal Buffer Rule:
Terminal classes (i.e., classes not defined in the present class graph), if used as a part class, must be the only part part class. Address = String String Number. Address = Streetname CityName ZipCode. StreetName = String. CityName = String. ZipCode = Number. 11/16/2018 DJ

25 Terminal Buffer Rule (TBR)
//class Cd_graph public void TBRchecker( TraversalGraph definedClassNamesT, TraversalGraph allPartsT, TraversalGraph fetchIdentT) { // definedClassNamesT defines // the part of the object graph // that is relevant for finding // all defined classes. // allPartsT defines // that is relevant for checking // the TerminalBufferRule 11/16/2018 DJ

26 TBR: generic behavior // find defined classes DefinedClassVisitor v1 =
new DefinedClassVisitor (fetchIdentT); Vector definedClasses = (Vector) definedClassNamesT. traverse( this, v1); // check for violations TBRVisitor v2 = new TBRVisitor(definedClasses); allPartsT.traverse(this, v2); } 11/16/2018 DJ

27 TBR: DefinedClassVisitor
public class DefinedClassVisitor extends Visitor { ... private Vector vNonTerminals = new Vector(); public void before(Adj o) { Ident idCurrentAdj = fetchIdentT.fetch(o); vNonTerminals.addElement(idCurrentAdj);} public Object getReturnValue() { return vNonTerminals; } 11/16/2018 DJ

28 TBR: Adaptation to a concrete Structure 1
ClassGraph cg = new ClassGraph();//reflection TraversalGraph tg1 = new TraversalGraph( "from Cd_graph to Adj", cg); // The purpose of traversal tg2 // is to visit all parts of all classes TraversalGraph tg2 = new TraversalGraph( "from Cd_graph via Construct to Vertex", cg); TraversalGraph tg3 = … “from Adj through Vertex to Ident” …; CdGraph cdGraph = new CdGraph(...); cdGraph.TBRchecker(tg1,tg2,tg3); 11/16/2018 DJ

29 Example 2: Cd_graph 0..* Entry EParse entries Cd_graph ClassDef BParse
Body parts Part className 0..* super Ident ClassName Concrete Abstract 11/16/2018 DJ

30 Example 2: Adaptation definedClassNamesT "from Cd_graph to ClassDef"
0..* Entry EParse entries Cd_graph ClassDef BParse Body parts Part className 0..* super Ident ClassName Concrete Abstract 11/16/2018 DJ

31 Example 2: Adaptation allPartsT
"from Cd_graph through Part to ClassName" 0..* Entry EParse entries Cd_graph BParse ClassDef Body parts Part className 0..* super Ident ClassName Concrete Abstract 11/16/2018 DJ

32 Example 2: Adaptation fetchIdentT
“from ClassDef through ->className to Ident” 0..* Entry EParse entries Cd_graph ClassDef BParse Body parts Part className 0..* super Ident ClassName Concrete Abstract 11/16/2018 DJ

33 TBR: Adaptation to a concrete Structure 2
ClassGraph cg = new ClassGraph();//reflection TraversalGraph tg1 = new TraversalGraph( "from Cd_graph to ClassDef", cg); // The purpose of traversal tg2 // is to visit all parts of all classes TraversalGraph tg2 = new TraversalGraph( "from Cd_graph through Part to ClassName", cg); TraversalGraph tg3 = … “from ClassDef through ->className to Ident” …; CdGraph cdGraph = new CdGraph(...); cdGraph.TBRchecker(tg1,tg2,tg3); 11/16/2018 DJ

34 Participant Graph: connections are elastic
entries Cd_graph 0..* ClassDef Body parts Part className 0..* Ident ClassName 11/16/2018 DJ

35 AP history Programming with partial data structures = propagation patterns Programming with participant graphs Programming with object slices partial data structures = all the constraints imposed by visitors 11/16/2018 DJ

36 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..* 11/16/2018 DJ

37 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..* 11/16/2018 DJ

38 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..* 11/16/2018 DJ

39 Robustness of Strategy
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..* 11/16/2018 DJ

40 Writing Adaptive Programs with Strategies (DJ=pure Java)
String WPStrategy=“from BusRoute through BusStop to Person” class BusRoute { 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();} } can we really program with strategies? Here is how. 11/16/2018 DJ

41 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); can we really program with strategies? Here is how. 11/16/2018 DJ

42 Writing Adaptive Programs with Strategies (DJ=pure Java)
String WPStrategy=“from BusRoute through BusStop to Person” class BusRoute { int countPersons(TraversalGraph WP) { Integer result = (Integer) WP.traverse(this, new Visitor(){...}); return result.intValue();} } can we really program with strategies? Here is how. ObjectGraph objectGraph = new ObjectGraph(this, classGraph); ObjectGraphSlice objectGraphSlice = new ObjectGraphSlice(objectGraph, WP); objectGraphSlice.traverse(visitor); WP.traverse(this,visitor) <===> 11/16/2018 DJ

43 ObjectGraph BusRoute ( <busStops> BusStopList { BusStop(
<waiting> PersonList { Person() Person()})} <buses> BusList{ Bus( <passengers> PersonList { Person()})}) 11/16/2018 DJ

44 ObjectGraph: in UML notation
Route1:BusRoute :BusList buses busStops :BusStopList Bus15:Bus passengers CentralSquare:BusStop waiting :PersonList :PersonList Joan:Person Paul:Person Seema:Person Eric:Person 11/16/2018 DJ

45 TraversalGraph find all persons waiting at any bus stop on a bus route
from BusRoute through BusStop to Person busStops BusRoute BusStopList buses 0..* BusStop BusList waiting 0..* passengers Bus PersonList Person 0..* 11/16/2018 DJ

46 ObjectGraphSlice BusRoute ( <busStops> BusStopList { BusStop(
<waiting> PersonList { Person() Person()})} <buses> BusList{ Bus( <passengers> PersonList { Person()})}) 11/16/2018 DJ

47 ObjectGraphSlice Route1:BusRoute BusList buses busStops :BusStopList
Bus15:Bus passengers CentralSquare:BusStop waiting :PersonList :PersonList Joan:Person Paul:Person Seema:Person Eric:Person 11/16/2018 DJ

48 Example Short-cut strategy: {A -> B B -> C} class graph strategy
traversal graph ??? c 0..1 b x c B X 0..1 x b c B X x c C C 11/16/2018 DJ

49 Short-cut strategy: {A -> B B -> C} Object graph Traversal graph
start set x1:X :B x 0..1 b x2:X b X X B c1:C x x b c B c2:C finish set C c3:C 11/16/2018 DJ

50 Short-cut ObjectGraphSlice static view dynamic view
ObjectGraph, TraversalGraph, start nodes, finish nodes dynamic view when traverse method gets called traversal history: node sequence of nodes in object graph subgraph of object graph (but only part of the story) visualize it by a movie of traverse 11/16/2018 DJ

51 Short-cut POSS(A,B,a1)= 1 edge strategy: {A -> B B -> C}
Object graph Object graph Traversal graph a1:A A start set x1:X :B x 0..1 b x2:X b X X B c1:C x x b c B c2:C finish set C c3:C Used for token set and currently active object 11/16/2018 DJ

52 Short-cut strategy: {A -> B B -> C} Object graph Traversal graph
start set x1:X :B x 0..1 b x2:X b X X B c1:C x x c b B c2:C finish set C c3:C Used for token set and currently active object 11/16/2018 DJ

53 Short-cut strategy: {A -> B B -> C} Object graph Traversal graph
start set x1:X :B x 0..1 b x2:X b X X B c1:C x x b c B c2:C finish set C c3:C Used for token set and currently active object 11/16/2018 DJ

54 Short-cut strategy: {A -> B B -> C} Object graph Traversal graph
start set x1:X :B x 0..1 b x2:X b X X B c1:C x x b c B c2:C finish set C c3:C Used for token set and currently active object 11/16/2018 DJ

55 Short-cut strategy: {A -> B B -> C} Object graph Traversal graph
start set x1:X :B x 0..1 b x2:X b X X x B c1:C x b c B c2:C finish set C c3:C Used for token set and currently active object 11/16/2018 DJ

56 Short-cut strategy: {A -> B B -> C} Object graph Traversal graph
After going back to x1:X A start set x1:X :B x 0..1 b x2:X b X X B c1:C x x b c B c2:C finish set C c3:C Used for token set and currently active object 11/16/2018 DJ

57 Short-cut strategy: {A -> B B -> C} Object graph Traversal graph
After going back to :A A start set x1:X :B x 0..1 b x2:X b X X B c1:C x x b c B c2:C finish set C c3:C Used for token set and currently active object 11/16/2018 DJ

58 ObjectGraphSlice strategy: {A -> B B -> C} :A x1:X :B x2:X c1:C
11/16/2018 DJ

59 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. 11/16/2018 DJ

60 Example For data member access:
new TraversalGraph(“from A via B to C”, Main.cg).fetch(host); 11/16/2018 DJ

61 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. 11/16/2018 DJ

62 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. 11/16/2018 DJ

63 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. 11/16/2018 DJ

64 Strategy definition: embedded, positive strategies
Given a graph G, a strategy graph S of G is any subgraph of the transitive closure of G. 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}. 11/16/2018 DJ

65 S is a strategy for G F=t F D D E E B B C C S G A = s A

66 Transitive Closure busStops BusRoute BusStopList buses 0..* BusStop
BusList waiting 0..* passengers Bus PersonList Person 0..* 11/16/2018 DJ

67 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. S defines path set in G as follows: PathSetst(S,G) is the set of all s-t paths in G that are expansions of any s-t path in S. 11/16/2018 DJ

68 PathSet S = from BusRoute to Person
BR P PathSetBusRoute,Person (S,G)={(BR BL B PL P),(BR BSL BS PL P)} busStops BusRoute BusStopList buses 0..* BusStop BusList waiting 0..* passengers Bus PersonList Person 0..* 11/16/2018 DJ

69 PathSet S = from BusRoute through BusStop to Person
BR BS P PathSetBusRoute,Person (S,G)={(BR BSL BS PL P)} busStops BusRoute BusStopList buses 0..* BusStop BusList waiting 0..* passengers Bus PersonList Person 0..* 11/16/2018 DJ

70 Expansion PathSet S G (A X B X C) is an expansion of (A B C)
class graph Expansion PathSet strategy: {A -> B B -> C} S A G Traversal graph x c 0..1 A b start set B X x x c 0..1 b b X X C B x x b c B finish set C (A X B X C) is an expansion of (A B C) PathSetA,C(S,G) = {(A X B X (B X)* C)} 11/16/2018 DJ

71 Correspondence A X Bopt B X C A X B X C strategy: {A -> B
B -> C} Object graph strategy :A A B C x1:X class graph e1:Empty :B A x2:X Empty B x x c c1:C b X c2:C A X Bopt B X C A X B X C BOpt c c3:C C 11/16/2018 DJ

72 From Path Set to Object Slices
Effect of a strategy at class graph level: defines a path set that in general cannot be described by a subgraph of the class graph. at object graph level: defines a subgraph of the object graph, called an object graph slice 11/16/2018 DJ

73 From Path Set to Object Slice: Assume class graph is flat
An edge e of an object graph belongs to the object graph slice determined by strategy S=(SS,s,t), if the path from s to e is an alternation reduction of a path in the path set of the class graph and S. An alternation reduction is a path with alternation nodes and edges and inheritance edges deleted. 11/16/2018 DJ

74 New definition View traversal graph only as implementation detail.
11/16/2018 DJ

75 From Strategy to Object Slice: direct definition
Karl Lieberherr: delete A path p starting at s of an object graph belongs to the object graph slice determined by strategy S=(SS,s,t), if p is a prefix of an expansion of a path from s to t in SS with subclasses in p replaced by suitable superclasses. By using sufficiently many (but finitely many) paths, we determine the object graph slice for any object graph. 11/16/2018 DJ

76 From Strategy to Object Slice: direct definition
Karl Lieberherr: delete From Strategy to Object Slice: direct definition A path p starting at s of an object graph belongs to the object graph slice determined by strategy S=(SS,s,t), if there is a map from subclasses to superclasses in G such that the mapped path p is a prefix of an expansion of a path from s to t in SS. 11/16/2018 DJ

77 Summary Start: Path p in object graph.
Karl Lieberherr: delete Summary Start: Path p in object graph. Replace in p: Some subclasses by superclasses. Resulting path p must be prefix of an expansion of a path from s to t in strategy. 11/16/2018 DJ

78 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. 11/16/2018 DJ

79 Special case 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. 11/16/2018 DJ

80 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. 11/16/2018 DJ

81 Example 1 Only node paths shown for space reasons OG : A X R X C
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 x c c1:C b X c2:C OG : A X R X C OG’: A X B X C SG : A B C (CG: A X Bopt B X C) BOpt c c3:C C 11/16/2018 DJ

82 Example 1A Only node paths shown for space reasons early termination
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 x c c1:C b X c2:C OG : A X R X C OG’: A X B X C SG : A B C (CG: A X Bopt B X C) BOpt c c3:C C 11/16/2018 DJ

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

84 Example 2 OG : BR BL DP PL P OG’: BR BL B PL P SG : BR B P
Only node paths shown for space reasons Route1:BusRoute BusList 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 11/16/2018 DJ

85 Example 3 OG : BR BL DP PL P OG’: BR BL B PL P SG : BR B P
Only node paths shown for space reasons early termination Route1:BusRoute BusList buses busStops :BusStopList Bus15:DieselPowered passengers CentralSquare:BusStop waiting :PersonList :PersonList Joan:Person Paul:Person Seema:Person Eric:Person S = from BusRoute via NGasPowered to Person 11/16/2018 DJ

86 More precise 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 11/16/2018 DJ

87 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). 11/16/2018 DJ

88 Example class dictionary strategy A -> T T -> D 0..1 X 0..1 B D
A = [“x” X] [“r” R]. B = [“b” B] D. R = S. S = [“t” T] C C = D. X = B. T = R. D = . strategy 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 11/16/2018 DJ

89 Example class dictionary strategy A -> T T -> D
A = [“x” X] [“r” R]. B = [“b” B] D. R = S. S = [“t” T] C C = D. X = B. T = R. D = . 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 :D :C R S T a1:A 0..1 class graph object graph “r” r1:R s1:S 11/16/2018 DJ

90 Example class dictionary strategy A -> T T -> D
A = [“x” X] [“r” R]. B = [“b” B] D. R = S. S = [“t” T] C C = D. X = B. T = R. D = . strategy Example A -> T T -> D POSS(A,T,a1) = 1 edge POSS(R,T,r1) = 1 edge POSS(S,T,s1) = 1 edge 0..1 X 0..1 B D A C c2:C d2:D 0..1 :D c1:C R S T a1:A 0..1 class graph r1:R s1:S t1:T r2:R 11/16/2018 DJ

91 Object Graph Slice at m Determined by Strategy S
S = (SS,s,t), OGS = subgraph of OG=(N,E) of class graph G. Class(N) subset of Nodes(G). S subset of Nodes(G). m in OGS with Class(m)=s. Select all paths p from m to nodes n with Class(n)=t. p belongs to the object graph slice determined by strategy S if there is a map from subclasses to superclasses in G such that the mapped path p is an expansion of a path from s to t in SS. Subgraph touched by paths p is the object graph slice OGS (assume that object graph is a tree). 11/16/2018 DJ

92 Role of Class Graph OG is an instance of class graph. Superclasses
But: From BR via BS to P: if go through Object for repetition: has effect on what is traversed, i.e., has effect on ogs. ??? Only on partial paths? Not on full paths? Class graph must faithfully represent possibilities in object graph: cannot replace List(BusStop) by List(Object). 11/16/2018 DJ

93 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 11/16/2018 DJ

94 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 11/16/2018 DJ

95 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 11/16/2018 DJ

96 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) 11/16/2018 DJ

97 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 11/16/2018 DJ

98 Fetch Method If you love the Law of Demeter, use fetch as your shovel for digging: Part k1 = (K) classGraph.fetch(a,”from A to K”); The alternative is (digging by hand): Part k1 = a.b().c().d().e().f().g().h().i().k(); DJ will tell you if there are multiple paths to the target (but currently only at run-time). 11/16/2018 DJ

99 Gather Method Returns a list of objects.
Object ClassGraph.gather(Object o, String s) List ks = classGraph.gather(a,”from A to K”); returns a list of K-objects. 11/16/2018 DJ

100 Using DJ traverse(…) returns the v[0] return value. Make sure the casting is done right, otherwise you get a run-time error. If “public Object getReturnValue()” returns an Integer and traverse(…) casts it to a Real: casting error at run-time. Make sure all entries of Visitor[] array are non-null. 11/16/2018 DJ

101 Using multiple visitors
// establish visitor communication aV.set_cV(cV); aV.set_sV(sV); rV.set_aV(aV); Float res = (Float) whereToGo. traverse(this, new Visitor[] {rV, sV, cV, aV}); 11/16/2018 DJ

102 DJ binaryconstruction operations
11/16/2018 DJ

103 Who has traverse, fetch, gather? (number of arguments of traverse)
11/16/2018 DJ

104 Methods returning an ObjectGraphSlice
ClassGraph.slice(Object, Strategy) ObjectGraph.slice(Strategy) TraversalGraph.slice(Object) ObjectGraphSlice(ObjectGraph,Strategy) ObjectGraphSlice(ObjectGraph,TraversalGraph) Blue: constructors 11/16/2018 DJ

105 Traverse method arguments
ClassGraph Object, Strategy, Visitor TraversalGraph Object, Visitor ObjectGraph Strategy, Visitor ObjectGraphSlice Visitor 11/16/2018 DJ

106 Traverse method arguments. Where is collection framework used?
ClassGraph gather(Object, Strategy) / asList(Object, Strategy) TraversalGraph gather(Object) / asList(Object) ObjectGraph gather(Strategy) / asList(Strategy) ObjectGraphSlice gather() / asList() 11/16/2018 DJ

107 Where is collection framework used?
ObjectGraphSlice.asList() a fixed-size List backed by the object graph slice. Is write-through: modifying list will modify object and modifying object will modify list. (Similar to Arrays.asList() in Java.) gather copies the pointers to the objects. 11/16/2018 DJ

108 w:W v:V u:U a:A c4:C c3:C gather() x:X c5:C List v c2:C c1:C
v.get(1).set_j(5); v.set(4, new C()); List v asList() 11/16/2018 DJ

109 Interfaces Interface List Interface ListIterator
ListIterator listIterator() Object set(int index, Object element) Interface ListIterator void set(Object o): replaces the last element returned by next or previous with the specified element. 11/16/2018 DJ

110 Why is asList useful? from Application to X: want to change all F-objects to D-objects. From Application to “predecessors” of D: put in a new object. This is not structure-shy: all the predecessors of X also need to be mentioned. 11/16/2018 DJ

111 Why is asList useful? from Application to X: want to change all X-objects to D-objects. Application = <es> List(E). E = X D. D = X F. X : F | D. F = . D = List(X). List(S) ~ {S}. Sketch: from Application to E: aE.set_x(new D()); aE.get_d().set_x(new D()); from E to D: update list elements from Application to X: set(new D()) 11/16/2018 DJ

112 Why is asList useful? From A to B: want to change a B-object that satisfies some property. Does not matter whether it is in a list. A = B C. C = List(B). 11/16/2018 DJ

113 Some more theory So far:
strategy S for a class graph G: S is subgraph of the transitive closure of G. S defines path set in G as follows: PathSetst(S,G) is the set of all s-t paths in G that are expansions of any s-t path in S. A path p is an expansion of path p’ if p’ can be obtained by deleting some elements from p. 11/16/2018 DJ

114 What is a path? If we only have concrete classes: no problem.
Use the standard graph theoretic definition. 11/16/2018 DJ

115 A Path E (A,B,E) A B 11/16/2018 DJ

116 Traversals with Abstract Classes
from A to E from A to B from A to C from A to D A B C D 11/16/2018 DJ

117 Traversals to Abstract Classes
from A to B = includes =>B,C and :>C,B etc. Motivation: from A to E also includes subclasses of B A B C D 11/16/2018 DJ

118 Path concept Path from A to B include construction edges (forward)
inheritance edge implies subclass edge in opposite direction. include inheritance edges (backward and forward). Backward inheritance edges are called subclass edges. follow rule: after an inheritance edge, you are not allowed to follow a subclass edge. 11/16/2018 DJ

119 Path concept Path from A to B: EI implies EA in opposite direction
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* checking the eight allowed edge pairs: (EI,EC),(EI,EI),(EC,EC),(EA,EI),(EC,EI), (EA,EA),(EC,EA),(EA,EC) 11/16/2018 DJ

120 Equivalence of Regular Expressions
EA* (EI* EC EA*)* EI* ((EI* EC) | EA )* EI* 11/16/2018 DJ

121 Problem with path concept of DJ
X from A to X from X to C from A via X to C A B C D 11/16/2018 DJ

122 Path concept in flat class graphs
Path from A to B: In flat class graph there is never a construction edge following an inheritance edge: (EI* EC) = EC EI implies EA in opposite direction (EC | EA | EI)* but not EA followed by EI ((EI* EC) | EA )* EI* = (EC|EA)* EI* 11/16/2018 DJ

123 Legal Path Path in object graph No path in class graph C
From A via C to D B D A d1:D a1:A b1:B 11/16/2018 DJ

124 Legal path E From A to D A B No path in object graph
No path in class graph C D 11/16/2018 DJ

125 Requirements: for flat class graphs
Paths in class graph turn into paths in object graph by deleting alternation edges and nodes and inheritance edges (Def. of corresponding path). A B E A -> B => C -> E A -> C -> E C D 11/16/2018 DJ

126 Requirements: for flat class graphs
If there is a path in the class graph then there is a legal object of the class graph that has the same corresponding path. A B E A -> B => C -> E A -> C -> E C D Path in class graph => Exists object with path in object graph 11/16/2018 DJ

127 Requirements: for flat class graphs
Path in object graph => Exists corresponding path in class graph B D A d1:D a1:A b1:B 11/16/2018 DJ

128 Requirements: for flat class graphs
For a class graph G and legal object O, if there is a path p in the object graph then there is a path P in the class graph such that p corresponds to P. A B E A -> B => C -> E A -> C -> E C D 11/16/2018 DJ

129 More on semantics of DJ with abstract classes
What is the meaning of a visitor on an abstract class? 11/16/2018 DJ

130 Traversals to Abstract Classes
Class graph A B visitor: before (B){p(“b”);} before (C){p(“c”);} C D 11/16/2018 DJ

131 Visitor Methods on Abstract Classes
from A to E: b, c from A to B: b, c from A to C: b, c visitor: before (B){p(“b”);} before (C){p(“c”);} :A :C 11/16/2018 DJ

132 Visitor Methods on Abstract Classes
from A to E: b from A to B: b from A to C: b visitor: before (B){p(“b”);} before (C){p(“c”);} :A :D 11/16/2018 DJ

133 Visitor Rule When an object of class X is visited, all visitors of ancestor classes of X will be active. The before visitors in the downward order and the after visitors in the upward order. 11/16/2018 DJ

134 Traversals to Abstract Classes
X From A to C A B visitor: before (X){p(“x”);} before (B){p(“b”);} before (C){p(“c”);} C D 11/16/2018 DJ

135 Alternative Visitor Rule: not what programmers want
When an object of class X is visited, all visitors of ancestor classes of X and in the path set of the current traversal will be active. The before visitors in the downward order and the after visitors in the upward order. 11/16/2018 DJ

136 Guidelines In principle can express programs
IF you use the combination of the following pairs and triples for multiple traversals, fetch or gather, introduce the following computation saving objects: (cg,s,o)->ogs (cg,s)->tg (cg,o)->og (tg,o)->ogs cg class graph s strategy tg traversal graph o object og object graph ogs object graph slice v visitor In principle can express programs only with ClassGraph and Strategy and Visitor: cg.traverse(o,s,v); cg.fetch(o,s); cg.gather(o,s); cg.asList(o,s); 11/16/2018 Abbreviations DJ

137 DJ unary construction operations
Class graph from Strategy ClassGraph(Strategy): make a class graph with just the classes and edges in traversal graph determined by strategy. Interpret path set as a graph. Maintain several class graphs cg1, cg2, cg3 that are suitable for expressing what you need. Class graph from all classes in package 11/16/2018 DJ

138 ClassGraph construction
make a class graph from all classes in default package ClassGraph() include all fields and non-void no-argument methods. Static members are not included. ClassGraph(boolean f, boolean m) If f is true, include all fields; if m is true, include all non-void no-argument methods. 11/16/2018 DJ

139 Dynamic features of DJ ClassGraph construction
When a class is defined dynamically from a byte array (e.g., from network) ClassGraph.addClass(Class cl) has to be called explicitly. Class cl is returned by class loader. ClassGraph() constructor examines class file names in default package and uses them to create class graph. 11/16/2018 DJ

140 Dynamic features of DJ ClassGraph construction
ClassGraph.addPackage(String p) adds the classes of package p to the class graph. The package is searched for in the CLASSPATH. How can we control (f,m) options? Uses the same rule as used for the original class graph construction. Java has no reflection for packages. Motivates above solution. 11/16/2018 DJ

141 Adding Nodes and Edges to ClassGraph
addClass(Class cl) add cl and all its members to the class graph, if it hasn’t already been added. addClass(Class cl, boolean aF, boolean aM) add cl to the class graph. If aF, add all its non-static fields as construction edges. If aM, add all its non-static non-void methods with no arguments as derived construction edges. 11/16/2018 DJ

142 Adding Nodes and Edges to ClassGraph
Part addConstructionEdge(Field f) add f as a construction edge. Part addConstructionEdge(Method m) add a no-args method as a construction edge. addConstructionEdge may have in addition a String argument called source. For Impl. And also a Class argument called target. Also for Impl. Should not be public. 11/16/2018 DJ

143 Add other repetition edges
void ClassGraph.addRepetitionEdge(String source, String target) add a repetition edge from source to target Questions what about subclass and inheritance edges what happens if class graph contains edges not in program. Error will occur. 11/16/2018 DJ

144 Design and Implementation
Until summer 1999: Josh Marshall Since then: Doug Orleans Available on the Web from DJ home page Quite complex Viewing an object as a list is done through coroutines to simulate continuation 11/16/2018 DJ

145 Problem with DJ What is coming is not about a problem of DJ but about a problem with Java: the lack of parameterized classes. The lack of parameterized classes forces the use of class Object which, as the mother of all classes, is too well connected. This leads to unnecessary traversals and traversal graphs that are too big. 11/16/2018 DJ

146 Lack of parameterized classes in Java makes DJ harder to use
Consider the traversal: from A to B Let’s assume that in the class graph between A and B there is a Java collection class. The intent is: A = List(B) which we cannot express in Java. Instead we have: A = Vector(Object). Object : A | B. Let’s assume we also have a class X=B. 11/16/2018 DJ

147 Lack of parameterized classes in Java makes DJ harder to use
We have: A = Vector(Object). Object : A | B | X. X = B. If the vector contains an X object it will be traversed!!! Vector * Object A X B 11/16/2018 DJ

148 No X-object is allowed to be in vector A * X B Vector * Object A X B
11/16/2018 DJ

149 Moral of the story If the Collection objects contain only the objects advertised in the nice class graph of the application the traversal done by DJ will be correct. But unecessary traversals still happen. However, if the Collection objects contain additional objects (like an X-object) they will be traversed accidentally. 11/16/2018 DJ

150 Moral of the story Java should have parameterized classes.
Workaround: Use a JSR (Java Specification Request) 31 approach: use a schema notation with parameterization to express class graph and generate Java code from schema. For traversal computation, the schema will be used. 11/16/2018 DJ

151 Size of traversal graph
DJ might create big traversal graphs when collection classes are involved. DJ will plan for all possibilities even though only a small subset will be realized during execution. To reduce the size of the traversal graph, you need to use bypassing. In the example: from A bypassing {A,X} to B. 11/16/2018 DJ

152 Technical Details Using DJ and DemeterJ 11/16/2018 DJ

153 Combining DJ and DemeterJ
DJ is a 100% Java solution for adaptive programming. DemeterJ has XML style data binding facilities: code generation from schema (class dictionary). Its own adaptive programming language. We attempt an optimal integration giving us the strong advantages of both and only few small disadvantages. 11/16/2018 DJ

154 Optimal DJ and DemeterJ Integration
Take all of DJ Take all of DemeterJ class dictionary notation Take a very tiny bit of DemeterJ adaptive programming language (basically only part that allows us to weave methods). 11/16/2018 DJ

155 Combining DJ and DemeterJ
Pros (advantages) Java class generation from class dictionary (getters, setters, constructors). Parser generation. Better packaging of Java code into different files. MUCH MORE POWERFUL. Cons (disadvantages) No longer pure Java solution. need to learn Demeter notation for class dictionaries (similar to XML DTD notation). need to learn how to call DemeterJ and how to use project files. 11/16/2018 DJ

156 Combining DJ and DemeterJ
What do we have to learn about DemeterJ? Class dictionaries: *.cd files Behavior files: *.beh files. Very SIMPLE! A { {{ … }} } defines methods … of class A Project files: *.prj list behavior files *.beh that you are using Commands: demeterj new, demeterj test, demeterj clean 11/16/2018 DJ

157 Combining DJ and DemeterJ
What you might forget in class dictionaries that are used with DJ: import edu.neu.ccs.demeter.dj.*; visitors need to inherit from Visitor parts of visitors need to be defined in class dictionary 11/16/2018 DJ

158 Combining DJ and DemeterJ
Structuring your files put reusable visitors into separate behavior files. put each new behavior into a separate behavior file. Visitors that are not planned for reuse should also go into same behavior file. Update the class dictionary with required structural information for behavior to work. List all *.beh files in .prj file. 11/16/2018 DJ

159 Plans for DJ Continued use and refinement: 50+ students this quarter
Write compile-time checker and partial evaluator for adaptive part. 11/16/2018 DJ

160 Example : Count Aspect Pattern
collaboration Counting { participant Source { expect 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;} …) } } participant Target {} } can we really program with strategies? Here is how. Base: Meta variable: bold Keywords: underscore 11/16/2018 DJ

161 Adapter 1 classGraph1 is fixed and therefore the traversal is fixed
adapter CountingForBusRoute1 { BusRoute is Counting.Source with { TraversalGraph getT() { ClassGraph classGraph1 = new ClassGraph(); return new TraversalGraph(classGraph1, new Strategy(“from BusRoute via BusStop to Person”));} } Person is Counting.Target { } 11/16/2018 DJ


Download ppt "A Short Introduction to Adaptive Programming (AP) with collaborations and adapters Northeastern Team 11/16/2018 DJ."

Similar presentations


Ads by Google