Adaptive and Aspect-Oriented Object-Oriented Software Development

Document Sample
Adaptive and Aspect-Oriented Object-Oriented Software Development Powered By Docstoc
					    Adaptive and Aspect-Oriented
          Object-Oriented
       Software Development
                   Karl Lieberherr
               Demeter Research Group
           For MIT Software Design Seminar
                 Daniel Jackson, host
5/5/2010           AP and AOP/Demeter Research Group   1
               Two alternatives:
           I decided for the second
• Graph Theory of AP/AOP
• Introduction to AP/AOP with highlights of
  theoretical foundations




5/5/2010        AP and AOP/Demeter Research Group   2
           generalization
                                 Learning map:
        other relationships
                             Graph Theory AP/AOP
    numbers: order of coverage
                                8                             correspondences
             1      graph                                     X:class path - concrete path
                                    FROM-TO                   Y:object path - concrete path
                     paths          computation               traversal path - class path
                     labeled

     3                  2               5                 9                  10
         object             class           strategy          traversal           propagation
         graph              graph           graph             graph               graph
4                                                  6                            11 zig-zags
    object traversal defined                           name map
    by concrete path set                               constraint map              short-cuts

7                                             12
    Algorithm 1                                    Algorithm 2
     in: strategy + class graph                     in: traversal + object graph
     out: traversal graph                           out: object traversal
         5/5/2010                    AP and AOP/Demeter Research Group                          3
                               Learning map:
Law of Demeter
                               Intro. AP/AOP
                                      Control
             Adaptive                 Tangling                 Aspect-Oriented
             Programming                                       Programming
                                   Cross-cutting

      Traversal                 APPC
      Strategy Graph            Adaptive Plug-                 Reflection
                                and-Play                       Open Implementation
 Implementations:               Components
 DJ etc.
                                                                 Rondo
                           Software Architecture:
                           Components
                           Connectors
  5/5/2010                 AP and AOP/Demeter Research Group                     4
      Adaptive Programming (AP)
• Programs adapt to interesting context
  changes
• Structure-shy behavior
• Succinct representation of traversals
• Programming in terms of graph constraints



5/5/2010      AP and AOP/Demeter Research Group   5
Cross-cutting
                                 better program
 ordinary program
                                   basic structure


                                                     Aspect 1
                                    structure-shy
                                    functionality

                                                     Aspect 2
                                  synchronization


5/5/2010    AP and AOP/Demeter Research Group              6
               Connection
• Aspect-Oriented Programming (AOP) is a
  generalization of Adaptive Programming
  (AP): In AP traversal strategies must be
  used.




5/5/2010      AP and AOP/Demeter Research Group   7
               Brief History(NU)
 1987
     • Law of Demeter
  1990
     • First traversal specifications, construct for
        AP and AOP
     • Synchronization, data transfer as an aspect
1993/95
     • Fast and general compilation algorithm
1996/97
     •
  1998 APPCs (Adaptive Plug-and-Play
        Components): construct for AOP
    5/5/2010        AP and AOP/Demeter Research Group   8
           A simple example
• Show structure-shyness, adaptiveness
• Show complete program




5/5/2010      AP and AOP/Demeter Research Group   9
               UML Class Diagram
                          busStops
       BusRoute                                      BusStopList

               buses
                                                               0..*
           BusList                                         BusStop

              0..*                                             waiting
                       passengers
             Bus                                     PersonList

                               Person               0..*
5/5/2010               AP and AOP/Demeter Research Group                 10
           Collaborating Classes
  use connectivity in class graph to define them succinctly
  using traversal strategy graphs




   from Company to Employee                  from Customer to Agent

5/5/2010            AP and AOP/Demeter Research Group           11
                                               self-service                  OOAD

customer service   Z        teller service
     center
                                                                        Collab-1

                                                                            C1
                                                                                    C4

                                                                C2         C3
                                                                                   C5    Collab-4
                                                         Collab-2      Collab-3
                   C1
                                             C4

  C2                   C3
                                        C5                                   Implementation

5/5/2010                                     AP and AOP/Demeter Research Group                12
              Collaborating Classes
    find all persons waiting at any bus stop on a bus route
                          busStops
       BusRoute                                      BusStopList

               buses      OO solution:
                          one method                           0..*
           BusList        for each red                     BusStop
                          class
              0..*                                             waiting
                       passengers
             Bus                                     PersonList

                               Person               0..*
5/5/2010               AP and AOP/Demeter Research Group                 13
           Traversal Strategy Graphs
• Want to define traversals succinctly
• Use graph to express abstraction of class
  diagram
• Express traversal intent: useful for
  documentation of object-oriented programs



5/5/2010         AP and AOP/Demeter Research Group   14
find all persons waiting at any bus stop on a bus route
                     Traversal Strategy
                  first try: from BusRoute to Person
                            busStops
         BusRoute                                      BusStopList

                 buses
                                                                 0..*
             BusList                                         BusStop

                0..*                                             waiting
                         passengers
               Bus                                     PersonList

                                 Person               0..*
  5/5/2010               AP and AOP/Demeter Research Group                 15
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..*
             BusList                                         BusStop

                0..*                                             waiting
                         passengers
               Bus                                     PersonList

                                 Person               0..*
  5/5/2010               AP and AOP/Demeter Research Group                 16
find all persons waiting at any bus stop on a bus route
                     Traversal Strategy
 Altern.: from BusRoute bypassing Bus to Person

                            busStops
         BusRoute                                      BusStopList

                 buses
                                                                 0..*
             BusList                                         BusStop

                0..*                                             waiting
                         passengers
               Bus                                     PersonList

                                 Person               0..*
  5/5/2010               AP and AOP/Demeter Research Group                 17
               Implementation
           (simplified OCL style)
class BusRoute {
   Persons findWaiting() {
     return this.busStops().waiting();
   }
}




5/5/2010        AP and AOP/Demeter Research Group   18
find all persons waiting at any bus stop on a bus route

             Robustness of Strategy
 from BusRoute bypassing Bus to Person

                          villages
         BusRoute                                      BusStopList

                 buses   VillageList
                                                    busStops           0..*
                                 0..*
             BusList                                         BusStop
                           Village
                0..*                                             waiting
                         passengers
               Bus                                     PersonList

                                 Person               0..*
  5/5/2010               AP and AOP/Demeter Research Group                    19
               Implementation
           (simplified OCL style)
class BusRoute {
   Persons findWaiting() {
     return this.villages().busStops().waiting();
   }
}




5/5/2010        AP and AOP/Demeter Research Group   20
  Filter out noise in class diagram
                    •only three out of seven classes
                      are mentioned in traversal
                      strategy!


 from BusRoute through BusStop to Person
     replaces traversal methods for the classes
BusRoute VillageList Village BusStopList BusStop
PersonList Person


5/5/2010         AP and AOP/Demeter Research Group     21
     find all persons waiting at any bus stop on a bus route
    Even better: Participant graph
           from BusRoute through BusStop to Person

                                 busStops
       BusRoute                                          BusStop
                                        0..*



                                               waiting       0..*
   buses      0..*
                                passengers
            Bus                                          Person
                                         0..*

5/5/2010             AP and AOP/Demeter Research Group              22
         Map Participant graph to
         application class graph
   from BusRoute through BusStop to Person
                           villages
          BusRoute                                      BusStopList

                  buses   VillageList
                                                     busStops           0..*
                                  0..*
              BusList                                         BusStop
                            Village
                 0..*                                             waiting
                          passengers
                Bus                                     PersonList
edge -> path                      Person               0..*
   5/5/2010               AP and AOP/Demeter Research Group                    23
         Map Participant graph to
         application class graph
   from BusRoute through BusStop to Person
                          villages
          BusRoute                                     BusStopList

                         VillageList
                                                    busStops           0..*
                                 0..*
                                                             BusStop
                           Village

                                      busStops
              BusRoute                                       BusStop
                                             0..*
edge -> path
   5/5/2010              AP and AOP/Demeter Research Group                    24
       Benefits of Participant graph
• Shields program from details of application
  class graph
• Makes program more robust and simpler




5/5/2010       AP and AOP/Demeter Research Group   25
 The Nature of Software Systems
    • Layered systems are ubiquitous




5/5/2010       AP and AOP/Demeter Research Group   26
             AP Approach
• Formulate specification of outer layer with
  approximate knowledge of inner layer.
• Decouples outer layer from inner layer.
• Define outer layer for a family of inner layers
• AP generates implementation of outer layer
  using specification and information about the
  particular inner layer.

5/5/2010      AP and AOP/Demeter Research Group   27
             AP Approach
• If inner layer changes, AP can generate a new
  implementation of outer layer.
• This allows programs written to the outer layer
  specification to run unchanged.




5/5/2010      AP and AOP/Demeter Research Group   28
 AP Idea: X is robust: works with
          red and blue
           inner layer
           X                                   X




5/5/2010   AP and AOP/Demeter Research Group       29
           Why Traversal Strategies?
       • Law of Demeter: a method should talk only to its
         friends:
           arguments and part objects (computed or stored)
           and newly created objects

       • Dilemma:
           •Small method problem of OO (if followed) or
           •Unmaintainable code (if not followed)

       •Traversal strategies are the solution to this dilemma


5/5/2010              AP and AOP/Demeter Research Group         30
           Law of Demeter Principle
• Each unit should have only limited
  knowledge about other units: only about
  units “closely” related to the current unit.
• “Each unit should only talk to its friends.”
  “Don’t talk to strangers.”
• Main Motivation: Control information
  overload. We can only keep a limited set of
  items in short-term memory.
5/5/2010         AP and AOP/Demeter Research Group   31
           Law of Demeter
                               FRIENDS




5/5/2010    AP and AOP/Demeter Research Group   32
                 Application to OO
• Unit = method
     – closely related =
           • methods of class of this/self and other
             argument classes
           • methods of immediate part classes (classes that are
             return types of methods of class of this/self)
• In the following we talk about this
  application of the Law of Demeter Principle
  to OO
5/5/2010               AP and AOP/Demeter Research Group           33
       Rumbaugh and the Law of
             Demeter
Quote: Avoid traversing multiple links or
 methods. A method should have limited
 knowledge of an object model. A method
 must be able to traverse links to obtain its
 neighbors and must be able to call
 operations on them, but it should not
 traverse a second link from the neighbor to
 a third class.
 5/5/2010      AP and AOP/Demeter Research Group   34
               Adaptive Following LoD                                 C

A                                         FRIENDS
a
                                     S
                                                           X
                                                                       c
b

                  a:From S to A
                  b:From S to B                  c:From S via X to C
    5/5/2010           AP and AOP/Demeter Research Group         35
           Adaptive Programming
• How can we use strategies to program?
• Need to do useful work besides traversing:
  visitors
• Incremental behavior composition using
  visitors



5/5/2010       AP and AOP/Demeter Research Group   36
  Writing Adaptive Programs with
    Strategies (DJ=pure Java)
String WPStrategy=“from BusRoute through BusStop to Person”

 class BusRoute {
    TraversalGraph WP = new TraversalGraph(Womble.classGraph,
      new Strategy(WPStrategy));
     int printCountWaitingPersons(){ // traversal/visitor weaving
      WP.traverse(this, new Visitor(){ int r;
       public void before(Person host){ r++; … }
       public void start() { r = 0;}
       ...
    }
 }
 // ClassGraph classGraph = new ClassGraph();
   5/5/2010          AP and AOP/Demeter Research Group      37
           Taxi driver analogy
• Streets and intersections correspond to class
  graph
• Traversal strategy determines how the taxi
  will navigate through the streets
• You can take pictures before and after
  intersections
• You can veto sub traversals

5/5/2010       AP and AOP/Demeter Research Group   38
  Programming in Large Families
Two adaptive programs
  A1
                                                        Class Graphs
  A2




                                                               A1 family
           Object-Oriented Programs

                                                   A2 family
5/5/2010            AP and AOP/Demeter Research Group                  39
 A very simplified quick intuition
  behind compilation algorithms
• Apply automata theory to compiling AP




5/5/2010     AP and AOP/Demeter Research Group   40
       Intersection of NDFA is similar
        to traversal graph construction
                b                                           a         q4
      q1             q2                      q3


a                                                     b
               a*b                                              ab*

                             a               b


                     q1q3           q1q4             q2q4


                            Intersection of two DFAs


    5/5/2010          AP and AOP/Demeter Research Group                    41
         Traversal Graph Construction
         s1        q1       q2        q3          t1
                                                                any     any
              A         B        C           D
A=B D.                                                   s2        q6       q7       t2
B=C.              D
C=D.
                                                               A        C        D
D=.
                                                           from A via C to D
      s1,s2       q1,q6 q2,q6        q3,q7       t1,t2


              A         B        C         D




  5/5/2010                       AP and AOP/Demeter Research Group                        42
   Integrated view of algorithms 1
       and 2: for path existence
• Both are similar to the intersection of two
  NDFAs
     – Algorithm 1: NDFA for strategy graph and
       NDFA for class graph: results in NDFA for
       traversal graph.
     – Algorithm 2: NDFA for traversal graph and
       NDFA for object graph: results in NDFA which
       tells us whether there is a non-empty traversal

5/5/2010          AP and AOP/Demeter Research Group   43
    Recall: Intersection of NDFAs
• An NDFA is a 5-tuple: M=(S,A,d,p0,F), S
  finite set of states, A is input alphabet, d is a
  state transition function which maps Ax(S
  union epsilon) to the set of subsets of S, p0 is
  the initial state, and F is the set of final states.




5/5/2010        AP and AOP/Demeter Research Group   44
    Recall: Intersection of NDFAs
• M1=(S1,A,d1,p0,F1) and
  M2=(S2,A,d2,q0,F2).
• The NDFA for M1 intersect M2 is
  I=(S1xS2,A,d,(p0,q0),F1xF2), where for a in
  A, (p2,q2) in d((p1,q1),a) if and only if p2 in
  d1(p1,a) and q2 in d2(q1,a).


5/5/2010       AP and AOP/Demeter Research Group   45
     In reality: more complex than
                 automata
• Deal with inheritance.
• Want to minimize the NDFA while
  preserving its structure. Cannot use general
  purpose automata minimization.
• Three levels of graphs: strategy-class-
  object; not just automata defining
  languages.

5/5/2010       AP and AOP/Demeter Research Group   46
       Application to OCL:
    UML class diagram ClassGraph
                            0..*                Entry
                                                                     EParse
                          entries
             ClassGraph
                                                                BParse
                    ClassDef
      Body
                     parts             Part       className

                      0..*
                                                            ClassName

                                       super
                               ClassGraph -- concrete classes
Concrete          Abstract       self.{to Concrete}->size
  5/5/2010                  AP and AOP/Demeter Research Group                 47
ClassGraph self.entries->
        select(c:Entry|c.
         oclIsTypeOf(ClassDef))->
          collect(body)->
           select (b:Body|b.
            oclIsTypeOf(Concrete))
     ->size -- count concrete classes

ClassGraph -- count concrete classes
  self.{to Concrete}->size

           Which one is easier to write?
5/5/2010        AP and AOP/Demeter Research Group   48
            Towards AOP
• Functional decomposistion
• Object decomposition
• Combining them




5/5/2010     AP and AOP/Demeter Research Group   49
Problems with Software Structuring
                                             Software =
                                                Data (Shapes)
                                                          +
                                                Functions (Colors)

 1st Generation
 Spaghetti-Code




                   2nd & 3rd Generation :
                  functional decomposition



                                               4th Generation
                                             object decomposition
Problems with Functional Decomposition
  Advantage:         Disadvantage: Data spread around
  easy integration
                        • integration of new data types ==>
  of new functions
                         modification of several functions

                        • functions tangled due to use of shared
                          data

                             Difficult to localize changes !
Problems with Object Decomposition
Advantage:         Disadvantage: functions spread around
easy integration
of new data           • integration of new functions ==>
                        modifikation of several objects

                      • objects tangled due to higher-level
                        functions involving several classes

                         Difficult to localize changes !
       Reconciling objects and functions:
          the intuition behind APPC
                                       modification




          expected     provided                       result
                                  connectors
Concrete application
           Informal aspect description:
                 ShowRAccess
``For any data type in an application, say RDataToAccess,
   any read access operation, AnyType readOp() defined
   for RDataToAccess, and any invocation of this
   operation on an instance of RDataToAccess,
   dataInstance, display Read access on <string
   representation of dataInstance>}´´.




5/5/2010          AP and AOP/Demeter Research Group   54
 Example of an APPC (Adaptive
 Plug-and-Play Component) for
        ShowRAccess
component ShowRAccess {
     participant RDataToAccess {
         expect Object readOp();
         replace Object readOp() {
           System.out.println("Read access on "
             + this.toString());
           return expected(); // this calls the
              // expected version of readOp()
         }
     }
5/5/2010           AP and AOP/Demeter Research Group 55
  Example of an APPC (Adaptive
  Plug-and-Play Component) for
         ShowRAccess
class ShowRAccess {
  class RDataToAccess {// one participant
    Object expected_readOp();
    Object replaced_readOp() {
      System.out.println("Read access on "
        + this.toString());
      return expected_readOp();
    }
  }
5/5/2010     AP and AOP/Demeter Research Group   56
      Concrete class graph: in Java
   class Point {
     private int x = 0;
     private int y = 0;
     void set(int x,int      y) {this.x = x;this.y = y;}
     void setX(int x) {      this.x = x; }
     void setY(int y) {      this.y = y; }
     int getX(){ return      this.x; }
     int getY(){ return      this.y; }
   }
   class Line { ... }
   class Rectangle {...      }

5/5/2010        AP and AOP/Demeter Research Group     57
             Deployment
connector ShowRAccessConn1 {
   Point is ShowRAccess.RDataToAccess
     with {readOp = get*};
  }
connector ShowReadAccessConn3 {
   {Point, Line, Rectangle}
   is ShowRAccess.RDataToAccess
     with {readOp = get*; }
}

5/5/2010     AP and AOP/Demeter Research Group   58
   Inheritance between components
class ShowRWriteAccess extends ShowReadAccess {
  participant RWDataToAccess extends RDataToAccess{
     expect void writeOp(Object[] args);
     replace void writeOp(Object[] args){
        System.out.println(
        "Write access on " +
            this.toString());
        expected(args);}}}




  5/5/2010        AP and AOP/Demeter Research Group   59
 Inheritance between components
class ShowRWAccess extends ShowRAccess{
  class RWDataToAccess extends RDataToAccess{
     void expected_writeOp(Object[] args);
     void replaced_writeOp(Object[] args){
       System.out.println("Write access on " +
            this.toString());
     expected_writeOp(args);
  }
}



5/5/2010       AP and AOP/Demeter Research Group   60
   Inheritance between connectors
connector ShowReadWriteAccessConn2
  extends ShowReadAccessConn3 {
   {Point,Line,Rectangle}
    is DataToAccess with {
      writeOp = set*;
   }




5/5/2010     AP and AOP/Demeter Research Group   61
               What are APPC?

• APPC are language constructs that capture behaviour
  involving several classes (cross-cuts class boundaries)

• the programmer uses classes to implement the primary data
  (object) structure

• the programmer uses APPC to implement higher-level
  behavior cross-cutting the primary structure in a modular way




5/5/2010           AP and AOP/Demeter Research Group        62
              What are APPC ?
• APPC have provided and expected interfaces

• The expected interface consists of an ideal class graph
  (Participant Graph, PG) to enable defining one aspect of the
  system with limited knowledge about the object model and/or
  other aspects defined by other components

• APPC can be deployed into PGs or concrete class graphs
  and/or composed/refined by 3rd parties (reuse) by mapping
  interfaces via explicit connectors



5/5/2010           AP and AOP/Demeter Research Group      63
APPC
      minimal
  assumptions on
application structure                  P1
                                            P3
                             P2
         +
 expected interfaces

                        P
                        P1
                             meth                add new functionality
                             ... 1,1                      +
    written to the PG
                             meth                enhance the expected
   similar to an OO              1,k
  program is written
  to a concrete class
          graph         P3                            provided
                             meth                         =
                                 3,1
                              ...                everything declared
                             meth                      public
                                 3,j
                             APPC
• A set of participants forming a graph called
  the participant graph (represented, e.g., by a
  UML class diagram). Participant
     –     formal argument to be mapped
     –     expects function members (keyword expect)
     –     reimplementations (keyword replace)
     –     local data and function members


5/5/2010             AP and AOP/Demeter Research Group   65
           APPC (continued)
• Local classes: visibility: APPC
• APPC-level data and function members.
  There is a single copy of each global data
  member for each deployment




5/5/2010      AP and AOP/Demeter Research Group   66
      Benefits of following Law of
       Demeter in Demeter Style
 • robustness to changes
 • shorter programs
 • design matches program
     more understandable code, intent in program
 • partially automated evolution
 • keep all benefits of OO technology
 • improved productivity

           Applicable to design and documentation
           of your current systems with immediate benefits.

5/5/2010             AP and AOP/Demeter Research Group        67
                     Summary
1987
     • Law of Demeter
1990 • First traversal specifications, key

        subconstruct for AP and AOP
1993 • Synchronization as an aspect

     • Fast and general compilation algorithm
1996/97
     • APPCs (Adaptive Plug-and-Play
1998
        Components): general construct for AOP
       5/5/2010   AP and AOP/Demeter Research Group   68
                            Discussion
•   Womble works fine for class graph extractions; it does not get cardinalities of
    relationships always right.
•   Terminal wrapper rule relevant in Daniel Jackson’s work.
•   2 kinds of satisfiability algorithms: Davis-Putnam style, and random walk.
•   Has tool that finds objects satisfying class graph and constraints
•   Same tool: takes class graph and constraints and method and creates pair of
    states that satisfy method and class graph and constraints
•   Nested polymorphic containers: Polymorphism not needed if good traversal
    technology available. Law of Demeter and nested containers. Check with DJ.




5/5/2010                   AP and AOP/Demeter Research Group                      69