Pattern Language for Adaptive Programming (AP)

Slides:



Advertisements
Similar presentations
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Advertisements

February R. McFadyen1 Polymorphism Indirection Pure Fabrication Protected Variations (Law of Demeter) More GRASP Patterns.
INTERPRETER Main Topics What is an Interpreter. Why should we learn about them.
Detailed Design Kenneth M. Anderson Lecture 21
March R. McFadyen1 Principle of Least Knowledge – page 265 Principle: talk only to your immediate friends Also called Law of Demeter (LoD)
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
UML class diagrams and XML schemas Karl Lieberherr UBS AG Northeastern University.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
The chapter will address the following questions:
Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming.
Pattern Language for AP1 Pattern Language for Adaptive Programming (AP) Karl Lieberherr Northeastern University.
Expression evaluation E : S | C. S = int. C = Op E E. Op : A | M. A = “+”. M = “*”.
Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Law of Demeter. What is it: Style Rule for building systems. Proposed by my research group: The Demeter Research Group in 1987, published in Covered.
Not only mark-up languages! There are other many other grammar formalisms and tools than XML. Some of them standardized (ASN). Even XML does not always.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Pattern Language for AP1 Pattern Language for Adaptive Programming (AP) Karl Lieberherr Northeastern University.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
The Interpreter Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
DJ: traversal-visitor-style programming in Java Josh Marshall/ Doug Orleans Want to add more on traversal through collections and Aspectual Components.
Concepts and Realization of a Diagram Editor Generator Based on Hypergraph Transformation Author: Mark Minas Presenter: Song Gu.
AOP/cross-cutting What is an aspect?. An aspect is a modular unit that cross-cuts other modular units. What means cross-cutting? Apply AOP to AOP. Tease.
Pattern Language for AP1 Pattern Language for Adaptive Programming (AP) Karl Lieberherr Northeastern University.
UBC software modularity group 1/14/02 UCSD1 Discussion with Kiczales at UBC Ontology of AOP Ontology is the study of what there is, an inventory of what.
AO Mechanisms in Demeter1 Discussion with Gregor Kiczales at UBC Ontology of AOP Ontology is the study of what there is, an inventory of what exists. An.
Talk only to your friends that share the same concerns (Law of Demeter for Concerns) Midterm Review February 2004.
Pattern Language for AP1 Pattern Language for Adaptive Programming (AP) Karl Lieberherr Northeastern University.
Features of AOP languages AOP languages have the following main elements: –a join point model (JPM) wrt base PL –a specification language for expressing.
Comp 411 Principles of Programming Languages Lecture 3 Parsing
Principles of Programming & Software Engineering
Chapter 0: Introduction
Design Patterns: MORE Examples
Abstract Factory Pattern
Crosscutting Capabilities for Java and AspectJ through DJ
Chapter 10 Design Patterns.
MPCS – Advanced java Programming
Good for DJ over Java Extending traversals for collections (b..*)
A Brief Introduction to Design Patterns
2.3 Collaboration Contracts
Behavioral Design Patterns
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
Principles of Programming and Software Engineering
Structure and Interpretation of an Aspect Language for Datatype
Discussion with Gregor Kiczales at UBC
Lecture 2 Introduction to Programming
Abstract Factory Pattern
Adaptive Object-Oriented Software Development
Presentation by Julie Betlach 7/02/2009
Software Design and Development
ADAPTIVE PROGRAMMING Sezen ERDEM December 2005.
Review lecture AOO/Demeter.
08/15/09 Design Patterns James Brucker.
Software Design and Development
Chapter 20 Object-Oriented Analysis and Design
Third lecture Review Visitor pattern DemeterJ 12/25/2018 SD.
BPMN - Business Process Modeling Notations
Midterm Review CSU 670 Spring 2004.
AP/DJ AP: a generic technology
Lecture 21: Crosscutting Aspect-Oriented Programming Background
Better Separation of Crosscutting Concerns with Aspectual Components
Third lecture Review Visitor pattern DemeterJ 2/17/2019 SD.
Review CSE116 2/21/2019 B.Ramamurthy.
Requirements for better object-oriented design and programming languages Could be organized better.
Week 4 Lecture-2 Chapter 6 (Methods).
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Objects, components, and Frameworks with UML
Informatics 122 Software Design II
Presentation transcript:

Pattern Language for Adaptive Programming (AP) Karl Lieberherr Northeastern University Meaning of Adaptive: Program is split into at least two parts. P = W(S , B). B makes assumptions on S. P1 = W(S1, B). change first part S of program to S1 and the second, which depends on the first, stays unchanged. The second program P1 is very different from P. The second part B adapts to changes of the first. Lecture 2, 1. class, fall 97, sep 25. Pattern Language for AP

Pattern Language for AP Introduction Four Patterns Structure-shy Traversal Selective Visitor Structure-shy Object Class Graph Structure is viewed as a separate “module”. Pattern Language for AP

Pattern Language for AP On-line information $D = www.ccs.neu.edu/research/demeter $D is Demeter Home Page $SD course home directory Lectures are in: $SD/lectures Pattern Language for AP

Pattern Language for AP Summary Present ideas of AP at a high-level of abstraction. Explain concepts independent of tools and languages. Prepare you for homework 1. Pattern Language for AP

Pattern Language for AP Vocabulary Pattern: Reusable solution to a problem in a context. Class graph = Class diagram: Graph where nodes are classes and edges are relationships between the classes. Design pattern book: Gamma, Helm, Johnson, Vlissides: 23 design patterns Pattern Language for AP

Pattern Language for AP Vocabulary Visitor pattern: Define behavior for classes without modifying classes. Parser: Takes a sequence of tokens and creates a syntax tree or object based on a grammar. Grammar: a class graph annotated with concrete syntax. Pattern Language for AP

Pattern Language for AP Overview Patterns a useful way to write down experience. Use a standard format: Intent, Motivation, Applicability, Solution, Consequences, etc. Patterns are connected and refer to each other. Extended version at: $D/adaptive-patterns/pattern-lang-conv Pattern Language for AP

Pattern Language for AP Connections There are several connections between the AP patterns and other design patterns. Class Graph is the basis for Structure-shy Traversal, Selective Visitor and Structure-shy Object. Pattern Language for AP

Structure-shy Traversal Intent Succinctly represent a traversal to be performed on objects Commit only to navigation strategy and specify navigation details later Pattern Language for AP

Solve Law of Demeter Dilemma Small Method Goat Big Method Goat Pattern Language for AP

Structure-shy Traversal Could also be called: Adaptive Traversal Structure-shy Walker Adaptive Visitor (significantly improves the Visitor pattern) Pattern Language for AP

Structure-shy Traversal Motivation Noise in objects for specific task Focus on long-term intent Don’t want to attach every method to a specific class explicitly. Leads to brittle programs. Small methods problem (example: 80% of methods are two lines long or shorter) Pattern Language for AP

Structure-shy Traversal Applicability Need collaboration of at least two classes. In the extreme case, each data member access is done through a succinct traversal specification. Some subgraphs don’t have a succinct representation, for example a path in a complete graph. More generally: avoid well connected, dense graphs. Pattern Language for AP

Structure-shy Traversal Solution Use succinct subgraph specifications Use succinct path set specifications Pattern Language for AP

Structure-shy Traversal: Solution Traversal Strategy Graphs (Strategies) First stage: A strategy is a graph with nodes and edges. Nodes are labeled with nodes of a class graph. Edges mean: all paths. Second stage: label edges with constraints excluding edges and nodes in class graph Third stage: Encapsulated strategies. Use symbolic elements and map to class graph. Pattern Language for AP

Structure-shy Traversal: Solution Traversal Strategy Graphs (Strategies) Simplest useful strategy: One Edge. Possible syntax: from Company to Salary or {Company -> Salary} Line graph. Several edges in a line. Possible syntax: From Company via Employee to Salary {Company -> Employee Employee -> Salary} Pattern Language for AP

Structure-shy Traversal: Solution Traversal Strategy Graphs (Strategies) Star graph From Company to {Personnel, Travel, Employee} Personnel Travel Company Employee Pattern Language for AP

Pattern Language for AP UML Class Diagram busStops BusRoute BusStopList buses 0..* BusStop BusList Example for developing strategies and writing adaptive programs waiting 0..* passengers Bus PersonList Person 0..* Pattern Language for AP

Pattern Language for AP 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..* Pattern Language for AP

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..* Pattern Language for AP

Structure-shy Traversal Consequences Programs become shorter and more powerful. A paradox. With less work we achieve more. Polya’s inventor paradox. Program will adapt to many changes in class structure. Pattern Language for AP

Structure-shy Traversal Implementation Many different models for succinct traversal specifications. Best one: Strategies Correct implementation of strategies is tricky. See paper by Lieberherr/Patt-Shamir strategies.ps in my FTP directory. Pattern Language for AP

Structure-shy Traversal Known Uses Adaptive Programming: Demeter/C++, DemeterJ, Dem/Perl, Dem/CLOS etc. Databases (limited use): Structure-shy queries: See Cole Harrison’s Master’s Thesis (Demeter Home Page) XML: XPath Artificial Intelligence (limited use): Minimal ontological commitment Pattern Language for AP

Pattern Language for AP Nature Analogy same strategy in different class graphs: similar traversals same seeds in different climates: similar trees warm climate cold climate Pattern Language for AP

Pattern Language for AP same cone different planes define similar point sets same strategy different class graphs define similar path sets Mathematical Analogy Pattern Language for AP

Pattern Language for AP Selective Visitor Intent Loosely couple behavior modification to behavior and structure. Would like to write an editing script to modify traversal code instead of modifying the traversal code manually. Pattern Language for AP

Pattern Language for AP Selective Visitor Could also be called: Structure-shy Behavior Modification Event-based Coupling Traversal Advice Pattern Language for AP

Pattern Language for AP Selective Visitor Motivation: Avoid tangling of code for one behavior with code for other behaviors. Localize code belonging to one behavior. Compose behaviors. Modify the behavior of a traversal call (traversals only traverse). Pattern Language for AP

Pattern Language for AP Selective Visitor Applicability: Need to add behavior to a traversal. Pattern Language for AP

Pattern Language for AP Selective Visitor Solution: Use visitor classes and objects. Pass visitor objects as arguments to traversals. Either use naming conventions for visitor methods (e.g., before_A()) or extend object-oriented language (e.g. before A, before is a new key word). Pattern Language for AP

Pattern Language for AP Selective Visitor Solution: before, after methods for nodes and edges in the class graph Activated during traversal as follows: Execute before methods Traverse Execute after methods Pattern Language for AP

Visitor visits objects following strategy Visitor collects information in suitcase (variables) Pattern Language for AP

Pattern Language for AP Selective Visitor Solution: Focus on what is important. SummingVisitor { {{ int total; }} init {{ total = 0; }} before Salary {{ total = total + host.get_v(); }} return {{ total }} } host is object visited Code between {{ and }} is Java code Pattern Language for AP

Pattern Language for AP Selective Visitor Solution: Use of visitor Company { traversal allSalaries(UniversalVisitor) {do S;} int sumSalaries() {{ SummingVisitor s = new SummingVisitor(); this.allSalaries(s); return s.get_return_val(); }} } Pattern Language for AP

Pattern Language for AP Selective Visitor Consequences Easy behavior adjustments: Add visitor Reuse of visitors Pattern Language for AP

Pattern Language for AP summing counting Selective Visitor Consequences: Easy behavior enhancement Company { // enhancements in red traversal allSalaries(UniversalVisitor, UniversalVisitor) {do S;} {{ float averageSalaries() { SummingVisitor s = new SummingVisitor(); CountingVisitor c = new CountingVisitor(); this.allSalaries(s, c); return s.get_return_val() / c.get_return_val(); } }} } Pattern Language for AP

Writing Programs with Strategies Example of Adaptive Program strategy: from BusRoute through BusStop to Person BusRoute { traversal waitingPersons(PersonVisitor) { through BusStop to Person; } // from is implicit int printWaitingPersons() // traversal/visitor weaving instr. = waitingPersons(PrintPersonVisitor); PrintPersonVisitor { before Person {{ … }} … } PersonVisitor {init {{ r = 0 }} … } can we really program with strategies? Here is how. Extension of Java: keywords: traversal init through bypassing to before after etc. Pattern Language for AP

Pattern Language for AP Selective Visitor Consequences: Can reuse SummingVisitor and CountingVisitor in other applications. Pattern Language for AP

Pattern Language for AP Selective Visitor Implementation Translate to object-oriented language. See DemeterJ, for example. Stopped after first lecture. Pattern Language for AP

Pattern Language for AP Selective Visitor Known uses Propagation patterns use inlined visitor objects (see AP book). DemeterJ. The Visitor Design Pattern from the design pattern book uses a primitive form of Selective Visitor. Pattern Language for AP

Differences to Visitor pattern Focus selectively on important classes. Don’t need a method for each traversed class. Finer control: not only one accept method but before and after methods for both nodes and edges. Pattern Language for AP

Pattern Language for AP Structure-shy Object Intent Make object descriptions for tree objects robust to changes of class structure. Make object descriptions for tree objects independent of class names. Pattern Language for AP

Pattern Language for AP Example Tree = “tree” [“left“ <left> Tree] [“right” <right> Tree] <n> Node. Node = int. tree left tree 8 right tree 7 3 1 1 3 8 7 new Tree( new Tree(null, null, new Node(8)), new Tree(null, null, new Node(7)), null, new Node(3)), new Node(1)) Pattern Language for AP

Pattern Language for AP Example BT = “tree” [“left“ <left> BT] [“right” <right> BT] <n> int. tree left tree 8 right tree 7 3 1 1 3 8 7 new Tree( new Tree(null, null, new Node(8)), new Tree(null, null, new Node(7)), null, new Node(3)), new Node(1)) COMPLETELY BROKEN STILL CORRECT Pattern Language for AP

Pattern Language for AP Flexibility There are many ways to clothe a tree!! clothing = syntax Two goals with clothing: objects look nice avoid ambiguity: two distinct objects must look different Pattern Language for AP

Pattern Language for AP Example Tree = “(“ <subtrees> TreeList <n> Node “)”. TreeList ~ {Tree}. Node = int. ((8) ((7) 3) 1) 1 3 8 7 Pattern Language for AP

Pattern Language for AP Example: bare Tree Tree = <subtrees> TreeList <n> Node. TreeList ~ {Tree}. Node = int. 8 7 3 1 ((8) ((7) 3) 1) ((8) (7) (3) 1) 1 3 8 1 7 3 8 7 Pattern Language for AP

Pattern Language for AP Structure-shy Object Could also be called: Object Parsing Grammar Abstract=Concrete Syntax Pattern Language for AP

Pattern Language for AP Structure-shy Object Motivation Data maintenance a major problem when class structure changes Tedious updating of constructor calls The creational patterns in the design pattern book also recognize need Concrete syntax is more abstract than abstract syntax! Pattern Language for AP

Pattern Language for AP Structure-shy Object Applicability Useful in object-oriented designs of any kind. Especially useful for reading and printing objects in user-friendly notations. Ideal if you control notation. If you see many constructor calls: think of Structure-shy Object. End first class, Sep. 25, 1997. Pattern Language for AP

Pattern Language for AP Structure-shy Object Solution Extend the class structure definitions to define the syntax of objects. Each class will define a parse function for reading objects and a print visitor for printing all or parts of an object. Pattern Language for AP

Pattern Language for AP Structure-shy Object Solution Start with familiar grammar formalism and change it to make it also a class definition formalism. In the Demeter group we use Wirth’s EBNF formalism. Use a parser generator (like YACC or JavaCC) or a generic parser. Pattern Language for AP

Parsers weave sentences into objects Structure-shy Object Parsers weave sentences into objects Problem in OO programs: Constructor calls for compound objects are brittle with respect to structure changes. Solution: Replace constructor calls by calls to a parser. Annotate class diagram to make it a grammar. Benefit: reduce size of code to define objects, object descriptions are more robust Object Description aspect Correspondence: Sentence defines a family of objects. Adaptive program defines family of object-oriented programs. In both cases, family member is selected by (annotated) class graph. Pattern Language for AP

Run-time weaving: Description Structure-shy Object Run-time weaving: Description Object as tree C Sentence * 3 + 4 5 C S M S S I A Grammar Compound=Op <f>Exp <s>Exp. Simple=Integer. Exp : Simple | Compound. Mult=“*”. Add=“+”. Op : Add|Mult. * I I + 3 5 4 Object in linear form (Constructor calls) Object Description aspect C M * S I 3 C A + S I 4 S I 5 SENTENCE IS MORE ROBUST THAN OBJECT Grammar defined by annotating UML class diagram Pattern Language for AP

Pattern Language for AP Structure-shy Object Consequences more robust and shorter object descriptions Need to deal with unique readability with respect to an efficient parsing algorithm Can guarantee unique readability by adding more syntax debug class structures by reading objects Pattern Language for AP

Pattern Language for AP Structure-shy Object Related patterns Creational patterns in design pattern book. Interpreter pattern uses similar idea but fails to propose it for general object-oriented design. Structure-shy Object useful in conjunction with Prototype pattern. Pattern Language for AP

Pattern Language for AP Structure-shy Object Known uses Demeter Tools since 1986, T-gen, applications of YACC, programming language Beta and many more. Pattern Language for AP

Pattern Language for AP Structure-shy Object References Chapters 11 and 16 of AP book describe details. Exercise Use your favorite grammar notation and modify it to also make it a class graph notation. Pattern Language for AP

Pattern Language for AP Class Graph Intent Write class relationships once and reuse them many times. Generate a visitor library from class graph for copying, displaying, printing, checking, comparing and tracing of objects. Pattern Language for AP

Pattern Language for AP Class Graph Could also be called: Class diagram Class dictionary Pattern Language for AP

Pattern Language for AP Class Graph Applicability For every application having more than one class. Implementation Preferred: Use UML class graph model and notation Use tool to generate visitor library (see DemeterJ). Pattern Language for AP

Pattern Language for AP UML Class Diagram busStops BusRoute BusStopList buses 0..* BusStop BusList Example for developing strategies and writing adaptive programs waiting 0..* passengers Bus PersonList Person 0..* Pattern Language for AP

Pattern Language for AP Class Graph Known uses: Almost all object-oriented design methods use some form of class diagram. Only DemeterJ generates visitor library and allows strategies to refer to the class graph. References UML class graphs, see UML doc Demeter class graphs, see chapter 6 of AP book Pattern Language for AP

Pattern Language for AP Summary State what has been learned: Principles of AP in high-level form. How to apply: Do homework one and recognize those patterns in the thousands of lines Java code. Pattern Language for AP

Where to get more information Those patterns will be discussed in much more detail. AP book covers the concepts. UML documentation See $D for more information. Pattern Language for AP

Pattern Language for AP Feedback Please see me after class or send me email if you have improvements to those patterns. lieberherr@ccs.neu.edu Pattern Language for AP