Demeter J prj Structureshy Traversal Selective Visitor Patterns














































































- Slides: 78
Demeter. J. prj Structure-shy Traversal / Selective Visitor Patterns Collaborative Behavior (using DJ). beh (99% Java) Project. Control Class dictionary generate . cd Classes compile . java . class follow Object. Descriptions Structure-shy Object Pattern 12/28/2021 . input parse Objects print AOO/Demeter Java Virtual Machine 1
Growth plan pattern • Intent – Build your adaptive programs incrementally. Use structural and behavioral simplifications which allow, ideally, for growth by addition and refinement. • Could also be called: – Evolutionary development 12/28/2021 AOO/Demeter 2
Earlier Patterns Four Patterns • Structure-shy Traversal • Selective Visitor • Structure-shy Object • Class Graph 12/28/2021 AOO/Demeter 3
How your project/hw directory should look like • /personality-proj – /growth-phase 1 – /growth-phase 2 – /growth-phase 3 –… • Each directory contains a running program. 12/28/2021 AOO/Demeter 4
Growth plan • Motivation: It is useful to have at all times a simplified version of the program running. – good for your self-confidence – good for your customers: feedback Build applications in growth phases, where a phase next can do more than a previous phase previous. 12/28/2021 AOO/Demeter 5
Growth plan • Motivation (continued): We want to build next as much as possible out of previous by adding or refining, not by modifying previous. next ideally reuses the test inputs of previous. • Application: Use this pattern when you build applications involving more than a small number of classes (say 5). 12/28/2021 AOO/Demeter 6
Growth plan • Solution: A good strategy is to build a relative big chunk of the class dictionary of the application and to test it with several input sentences. Then build a structural shrinking plan (whose inverse is a structural growth plan) consisting of a decreasing (in size) sequence of class dictionaries. 12/28/2021 AOO/Demeter 7
Growth plan • Solution (continued): For the smallest class dictionary you should be able to implement some interesting behavior. For each phase in the structural growth plan you implement increasingly more complex behavior. 12/28/2021 AOO/Demeter 8
Growth plan • Solution (continued): The structural growth steps should ideally be language extending so that the inputs of phase i also are legal inputs for phase i+1. • The behavioral growth steps should ideally be additive and should require only small modifications. 12/28/2021 AOO/Demeter 9
Growth plan behavior phases P 4 P 3 P 2 P 1 P 0 L 1 Í (P 0, P 1) 12/28/2021 L 2 Í (P 2, P 3) AOO/Demeter L 3 (P 4) structure, grammar phases 10
OS simulation example • Phase 1: – Structure: Start with full class dictionary and use the following slices: tg 1 = from File. System to * and tg 2 = from Commands to Create. Empty. File. – Behavior: Main. cdir. Commands: : process(tg 2){ Command. Visitor cv =. . . ; tg 2. traverse(this, cv); } Command. Visitor: : before(Create. Empty. File h){ create Simple. File sf; add. Element(sf); } 12/28/2021 AOO/Demeter 11
OS simulation example • Phase 1: – Why useful? What does it test? We can check whether simple files that are created anywhere in the input, are properly added to the root directory. – Program already shows some of the right behavior: if we only have touch commands. – But also on other inputs it shows useful behavior. 12/28/2021 AOO/Demeter 12
Phase 1: class dictionary File. System = <root> Compound. File EOF. File : Simple. File | Compound. File common <f> File. Name. Simple. File = "simple". Compound. File = "compound" <contents> PList(File) [<parent> Compound. File]. 12/28/2021 AOO/Demeter 13
Phase 1: class dictionary Commands = List(Command) EOF. Command : Simple : Make. Directory | Change. Directory. Up | Change. Directory. Down | Recursive. Copy | Disk. Usage | Find | Echo | Symbolic. Link | Remove. Directory | Create. Empty. File | Remove. File. Make. Directory = "mkdir" Directory. Name. Change. Directory. Up = "cd. . ". Change. Directory. Down = "cd" Directory. Name. Recursive. Copy = "cp -r. . /*. ". Disk. Usage = "du. ". Symbolic. Link = "ln -s" <from> File. Name <to> File. Name. Remove. Directory = "rmdir" Directory. Name. 12/28/2021 AOO/Demeter 14
Phase 1: class dictionary // "touch f" creates an empty file called f. Create. Empty. File = "touch" File. Name. Remove. File = "rm" File. Name. Find = "find. -name" Directory. Name "-print". Echo = "echo" Message. File. Name = Ident. Directory. Name = Ident. Message = String. PList(S) ~ "(" {S} ")". List(S) ~ {S}. Main =. 12/28/2021 AOO/Demeter 15
Desired behavior (example) File. System fs = new File. System( new Compound. File(new File. Name( new Ident ("root")), new File_PList() ) ); File_PList filelist 1 = fs. get_root(). get_contents(); Compound. File a = new Compound. File( new File. Name( new Ident ("a")), new File_PList()); filelist 1. add. Element(a); 12/28/2021 AOO/Demeter 16
Desired behavior (example) Compound. File b = new Compound. File( new File. Name( new Ident ("b") ), new File_PList()); filelist 1. add. Element(b); 12/28/2021 AOO/Demeter 17
Desired behavior (example) File_PList filelist 2 = a. get_contents(); Compound. File c = new Compound. File( new File. Name( new Ident ("c") ), new File_PList()); filelist 2. add. Element(c); 12/28/2021 AOO/Demeter 18
Phase 1 Commands { {{ void process(Traversal. Graph where) { Command. Visitor c. V = new Command. Visitor(); where. traverse(this, c. V); } }} } 12/28/2021 AOO/Demeter 19
Phase 1 Command. Visitor { {{ void before(Create. Empty. File host){ Simple. File sf = Simple. File. parse("simple " + (Ident) Main. cg. fetch(host, "from Create. Empty. File to" + Main. FIdent); // host. get_filename(). get_ident()); // Simple. File sf = new Simple. File( // host. get_filename()); Main. cdir. get_contents(). add. Element(sf); System. out. println(" Create. Empty. File "); } }} } 12/28/2021 AOO/Demeter 20
Phase 1 Main { (@ static Compound. File cdir; static Class. Graph cg; static String FIdent = " edu. neu. ccs. demeter. Ident "; static public void main(String args[]) throws Exception { Commands cs = Commands. parse(System. in); cs. print(); System. out. println(); File. System fs = File. System. parse( "compound () root"); 12/28/2021 AOO/Demeter 21
Phase 1: Main continued cdir = fs. get_root(); cg = new Class. Graph(true, false); Class. Graph cg. Commands. Without. Tail = new Class. Graph(Main. cg, "from Commands bypassing -> *, tail, * to *"); cs. process(new Traversal. Graph ("from Commands to *”, cg. Commands. Without. Tail)); 12/28/2021 AOO/Demeter 22
Input for testing phase 1 touch mkdir touch cd x touch 12/28/2021 a b x c d AOO/Demeter 23
OS simulation example • Phase 2: – Structure: Use larger part of class dictionary: tg 2 = from Commands to {Create. Empty. File, Make. Directory}. – Command. Visitor: : before(Make. Directory h){ create Compound. File cf; add. Element(cf); } 12/28/2021 AOO/Demeter 24
OS simulation example • Phase 2: – Why useful? What does it test? We can check whether simple files and directories that are created anywhere in the input, are properly added to the root directory. – Program already shows some of the right behavior: if we only have touch and mkdir commands. – But also on other inputs it shows useful behavior. 12/28/2021 AOO/Demeter 25
Input for testing phases 1 and 2 touch mkdir touch cd x touch 12/28/2021 a b x c d AOO/Demeter 26
Phase 2 Command. Visitor { {{ void before(Make. Directory host){ Ident id = (Ident) Main. cg. fetch(host, "from Make. Directory to" + Main. FIdent); Compound. File cf = Compound. File. parse("compound ()" + id); cf. set_parent(Main. cdir); // new Compound. File( // new File. Name(host. get_directoryname(). get_ident()), // new File_PList(), Main. cdir); Main. cdir. get_contents(). add. Element(cf); System. out. println(" Make. Directory "); } 12/28/2021 AOO/Demeter 27
What is next? • Change. Directory. Down • Change. Directory. Up • Which one is easier to test? It is very important that we can test each phase to get immediate gratification whether we did it right. 12/28/2021 AOO/Demeter 28
OS simulation example • Phase 3: – Structure: Use larger part of class dictionary: tg 2 = from Commands to {Create. Empty. File, Make. Directory, Change. Directory. Down}. – Change Command. Visitor: entry for Change. Directory. Down; Search through current directory to find directory to enter. Update Main. cdir. 12/28/2021 AOO/Demeter 29
Input for testing phases 1, 2, 3 touch mkdir touch cd x touch 12/28/2021 a b x c d AOO/Demeter 30
OS simulation example • Phase 4: – Structure: Use larger part of class dictionary: tg 2 = from Commands to {Create. Empty. File, Make. Directory, Change. Directory. Down, Change. Directory. Up}. – Change class dictionary: parent field. – Change display: infinite loop: 2 options. – Change Command. Visitor: entry for Change. Directory. Up; update entry for Make. Directory: maintain parent. 12/28/2021 AOO/Demeter 31
Input for testing phase 4 mkdir cd x mkdir cd y touch cd. . touch 12/28/2021 x y d c AOO/Demeter 32
For phases 3 and 4: List Structure • List(X) first X_List Non. Empty_X_List it next X 12/28/2021 AOO/Demeter 33
Iterating through a Demeter. J list • Have an X_List xlist; java. util. Enumeration en = xlist. elements(); while (en. has. More. Elements()) { if (e. equals((X) en. next. Element())) { found = true; } Enumeration Interface found = false; boolean has. More. Elements(); Object next. Element(); } 12/28/2021 AOO/Demeter 34
Iterating through a Demeter. J list: in class X_List public java. util. Enumeration elements() { return new X_List(first); } public Object next. Element() { X car = first. get_it(); first = first. get_next(); return (Object) car; } 12/28/2021 AOO/Demeter 35
Iterating through a Demeter. J list: in class X_List public boolean has. More. Elements() { return (first != null); } 12/28/2021 AOO/Demeter 36
Enumeration interface is old fashioned • Use Iterator interface instead – boolean has. Next(); – Object next(); – void remove(); (optional operation) • Compare to Enumeration Interface boolean has. More. Elements(); Object next. Element(); 12/28/2021 AOO/Demeter 37
Enumeration interface is old fashioned • List. Iterator is a subinterface of Iterator – boolean has. Next(); – Object next(); – void remove(); (optional operation) – add, has. Previous, previous. Index, next. Index, previous, set 12/28/2021 AOO/Demeter 38
Java documentation • The functionality of the Enumeration interface is duplicated by the Iterator interface. … shorter method names. . . New implementations should consider using Iterator in preference to Enumeration. 12/28/2021 AOO/Demeter 39
Traversal with a List. Iterator void traverse_max. Size(Integer. Ref m){ for (List. Iterator i=this. list. Iterator(); i. has. Next(); ) { Directory. Entry de = (Directory. Entry) i. next(); de. traverse_max. Size(m); } 12/28/2021 AOO/Demeter 40
How can you get an Iterator? • Interface Collection: – Iterator iterator(); • Example: – class Vector implements interface List – interface List extends interface Collection – Therefore: Use the Iterator interface to go through a vector 12/28/2021 AOO/Demeter 41
Change display() method : File. System root f : Compound. File : File. Name contents : File_PList first : Non. Empty_File_PList next it parent : Compound. File f contents 12/28/2021 AOO/Demeter 42
Testing for Type in Java if (f instanceof Compound. File) { cf = (Compound. File) f; . . . } USE SPARINGLY! 12/28/2021 AOO/Demeter 43
OS simulation example • Phase 5: – Structure: Use larger part of class dictionary: tg 2 = from Commands to {Create. Empty. File, Make. Directory, Change. Directory. Down, Change. Directory. Up, Disk. Usage}. – Change Command. Visitor: entry for Disk. Usage: requires itself a traversal. 12/28/2021 AOO/Demeter 44
Output to expect • In a Demeter. J diretory: du. . /gen/classes. /gen. 12/28/2021 AOO/Demeter 45
OS simulation example • Phase 5: – Traversal in visitor: from Compound. File to File – Visitor: before Compound. File: print path from current directory 12/28/2021 AOO/Demeter 46
Growth plan • Solution (continued): For the smallest class dictionary you should be able to implement some interesting behavior. For each phase in the structural growth plan you implement increasingly more complex behavior. The structural growth steps should fall into one or both of the following categories: 12/28/2021 AOO/Demeter 47
cd = class dictionary Growth plan • Solution (continued): – weakly object extending cd transformations • The next class dictionary defines more objects but does not invalidate any existing objects. What runs now should run later. Reuse of test objects. – language extending cd transformations • The next cd defines a super language of the current cd. 12/28/2021 AOO/Demeter 48
Object-extending transformations • relations on class graphs, associated with transformations, fundamental for reuse – object-equivalence • preserves the set of objects – weak extension • enlarges the set of objects – extension • enlarges and augments the set of objects 12/28/2021 AOO/Demeter 49
Part clusters • What can be put into parts? • Part. Clusters of a class v is a list of pairs, one for each induced part of v. Each pair consists of the part name and the set of construction classes whose instances can be assigned to the part • Part. Clusters. Furnace(Temp. Sensor) = {temp {Kelvin, Celsius}, trigger {Integer}} 12/28/2021 AOO/Demeter 50
Object-equivalence • Let G 1 and G 2 be two class graphs. G 1 is object-equivalent to G 2 if for the concrete classes VC 1 of G 1 and the concrete classes VC 2 of G 2: – VC 1 = VC 2 – and for all v in VC 1: Part. Clusters. G 1(v)= Part. Clusters. G 2(v). 12/28/2021 AOO/Demeter 51
Covered • Let PC 1 and PC 2 be two part clusters. PC 1 is covered by PC 2 if for each pair (l, T 1) in PC 1 there exists a pair (l, T 2) in PC 2 such that T 1 Í T 2. • Tightly covered means: covered and |PC 1| = |PC 2|. 12/28/2021 AOO/Demeter 52
Weak extension • Let G 1 and G 2 be two class graphs. G 1 is a weak extension of G 2 if for the concrete classes VC 1 of G 1 and the concrete classes VC 2 of G 2: – VC 1 Í VC 2 and for all v in VC 1: – Part. Clusters. G 1(v) is tightly covered by Part. Clusters. G 2(v). 12/28/2021 AOO/Demeter 53
Extension • Let G 1 and G 2 be two class graphs. G 1 is an extension of G 2 if for the concrete classes VC 1 of G 1 and the concrete classes VC 2 of G 2: – VC 1 Í VC 2 and – for all v in VC 1: Part. Clusters. G 1(v) is covered by Part. Clusters. G 2(v). 12/28/2021 AOO/Demeter 54
Properties • The three class graph relations have the following inclusion properties: – object-equivalence Í – weak-extension Í – extension 12/28/2021 AOO/Demeter 55
H Del. A* Abs. R* Rep. R* Dis. R* Add. A* H F D A 12/28/2021 object-equivalent G inheritance G A E B F C B C E AOO/Demeter 56
Primitive Transformations • Addition of Abstract Class (Add. A) • Deletion of Abstract Class (Del. A) • Abstraction of Common Reference (Abs. R) • Distribution of Common Reference (Dis. R) • Replacement of Reference (Rep. R) object-equivalence = Del. A* Abs. R* Rep. R* Dis. R* Add. A*. 12/28/2021 AOO/Demeter 57
Primitive Transformations • Addition of Abstract Class (Add. A) – adds an abstract class u and subclass edges outgoing from u. u must not have any outgoing construction edges. • Deletion of Abstract Class (Del. A) – inverse of Add. A. Deletes an abstract class u and all its subclass edges. u must not have any incoming construction or subclass edges nor any outgoing construction edges. 12/28/2021 AOO/Demeter 58
Primitive Transformations • Abstraction of Common Reference (Abs. R) – moves a construction edge common to a set of sibling classes up to their direct superclass. • Distribution of Common Reference (Dis. R) – moves a construction edge to the direct subclasses. 12/28/2021 AOO/Demeter 59
Primitive Transformations • Replacement of Reference (Rep. R) – reroutes a construction edge (v, l, u 1) to a new target (v, l, u 2) where u 1 and u 2 have the same set of concrete subclasses. 12/28/2021 AOO/Demeter 60
Primitive Transformations • Addition of Concrete Class (Add. C) • Generalization of Reference (Gen. R) • Addition of Reference (Add. R) • Equiv = object equivalence weak-extension = (Equiv((Gen. R)Equiv)*Add. C* extension = (Equiv((Add. R|Gen. R)Equiv)*Add. C* 12/28/2021 AOO/Demeter 61
Primitive Transformations • Addition of Concrete Class (Add. C) – adds an “empty” concrete class • Generalization of Reference (Gen. R) – reroutes a construction edge (v, l, u 1) to a new target (v, l, u 2), where u 2 is a direct superclass of u 1. 12/28/2021 AOO/Demeter 62
Primitive Transformations • Addition of Reference (Add. R) – adds a new construction edge between existing vertices of the class graph. 12/28/2021 AOO/Demeter 63
Warning • In the following viewgraphs is-a and has-a edges should be switched. 12/28/2021 AOO/Demeter 64
H Del. A* Abs. R* Rep. R* Dis. R* Add. A* H F D A 12/28/2021 object-equivalent G inheritance G A E B F C B C E AOO/Demeter 65
H F 2 Dis. R* Add. A* F G H D 2 F D A 12/28/2021 G A E B C E AOO/Demeter 66
H F 2 Rep. R* Dis. R* Add. A* F G H D 2 F D A 12/28/2021 G A E B C E AOO/Demeter 67
H Abs. R* Rep. R* Dis. R* Add. A* F 2 F G H D 2 F D A 12/28/2021 G A E B C E AOO/Demeter 68
H Del. A* Abs. R* Rep. R* Dis. R* Add. A* F 2 G H D 2 F D A 12/28/2021 G A E B C E AOO/Demeter 69
Connections • weak extension implies language extension – If two cds are in a weakly object-extending relationship they can be brought to a language extending relationship with appropriate syntax. • object-equivalent extension implies language-equivalent extension – similar 12/28/2021 AOO/Demeter 70
Growth plan • behavior transformations should ideally extend the program by addition instead of modifying it: – use inheritance between visitor classes – add methods, traversals, visitors – refine methods and visitors – refine strategies (add more constraints) 12/28/2021 AOO/Demeter 71
Growth plan behavior phases P 4 P 3 P 2 P 1 P 0 L 1 Í (P 0, P 1) 12/28/2021 L 2 Í (P 2, P 3) AOO/Demeter L 3 (P 4) structure, grammar phases 72
Growth plan • Consequences: Following Growth plan has a number of benefits: – Gradual building of confidence in your software development skills. – Show prototypes to your customers. – Simplified testing. Find earliest phase where a bug shows up. – faster compilation and generation 12/28/2021 AOO/Demeter 73
Growth plan • Implementation: Create separate directories for each growth phase. Document changes. See chapter 13 in AP book for study of class graph extension. Also follow the pages listed under index entry growth plan. 12/28/2021 AOO/Demeter 74
Example Same adaptive program for Terminal Buffer Rule checking works for both phases. Faster for phase 1 Cd_graph = <first> Adj = <vertex> Vertex <ns> Construct “. ”. Construct = “=“ <l 1> Labeled_vertex <l 2> Labeled_vertex = “<“ <label_name> Label “>“ <class_name> Vertex = <name> Ident. Label = <name> Ident. Cd_graph = <first> Adj <rest> Adj_list. phase 2 Adj = <vertex> Vertex <ns> Neighbors “. ”. NOT even Neighbors : Construct | Alternat. Construct = “=“ <c_ns> Any_vertex_list. an extension Labeled_vertex = “<“ <label_name> Label “>“ <class_name> Vertex = <name> Ident. Any_vertex_list = <first> Any_vertex <rest> Any_vertex_list. Any-Vertex : Labeled_vertex | Syntax_vertex. . 12/28/2021 AOO/Demeter 75
Conclusion • Using Adaptive Programming you can apply the Growth Plan pattern effectively. You can start with simple structures and generalize your program to more general structures easily. 12/28/2021 AOO/Demeter 76
Further information • Paul Bergstein’s OOPSLA 91 paper • Walter Huersch’s Ph. D. thesis • Linda Seiter’s Ph. D. thesis 12/28/2021 AOO/Demeter 77
End of lecture 12/28/2021 AOO/Demeter 78