Title: AOSD and the Law of Demeter: Shyness in Programming
1AOSD and the Law of Demeter Shyness in
Programming
- Karl Lieberherr
- Demeter Research Group
- Northeastern University
- Boston
2Coupling Aspect-Oriented and Adaptive
ProgrammingShyness in Programming
- PhD Visitation Weekend 2003
3Our Intuition behind AOP
- Our Aspect-Oriented Programming (AOP) intuition
has been "adaptiveness". It comes in two
flavors adaptiveness - to the class graph ("painting the class graph in
broad strokes with code") and - to the call graph of the traversal ("picking
points in the graph where additional code gets
called").
4MIT Technology Review2001
- Aspect-oriented programming is called adaptive
programming at Northeastern University.
5Definitions
- y is x-shy if
- (1) y relies only on minimal information of x
- (2) y can adapt to small changes in x
- (3) y is loosely coupled with x
- (4) y can work with x1, x2, ... which are close
or similar to x. - What is a concern? A concern is something that
the programmer cares about.
6Examples of concerns the programmer has to deal
with
- Production concerns
- How do I compute the price allowing for multiple
pricing schemes? - Non-Production concerns
- What do I have to print to understand why this
program does not work? - Are all objects of class A created in class
Afactory?
7Scattering and Tangling Static
- aspecti is scattered across many classes (i
1,2,3) - class X tangles aspects 1, 2 and 3
class A consisting of three aspects
aspect1
aspect2
aspect3
class diagram
Class X
classes for aspect1
classes for aspect3
classes for aspect2
Adding to classes
8Scattering and Tangling Dynamic
- Each aspect (colors) is scattered across many
classes (shapes) - Class tangles all three aspects
program execution involving three aspects
(colors r b g)
this(s)
f(..)
target(t)
program call tree (classes executing method
calls)
Enhancing calls
t.f(..)
classes
At those calls the aspect enhances the behavior
9AP-Concern-shy
- AP-Concern-shy
- AP-Structure-shy
- AP-WildCard
- AP-Strategy
- AP-Call
- AP-Demeter
- Program with multiple building blocks, say b1B1
and b2B2, that are woven together b1b2. b1
relies only on partial information about b2 which
makes b1 more robust and reusable. Goal b1
should be loosely coupled to b2.
10Law of Demeter (LoD) for Concerns
- A concern implementation should not rely on too
much information about other concern
implementations. - (Classic LoD A method should not rely on too
much information about other classes/objects.)
Ian Holland PhD 1992 Vice President of
Architecture and Systems Engineering at Kronos
Incorporated in Chelmsford, MA. 2200 empls.
11Adaptive Programming.
- AP-Concern-shy concerns shy of other concerns
- AP-Structure-shy concerns shy of graph structure
- AP-WildCard aspects shy through wildcards
- AspectJ , .., this, target, args, call,
execution, (call graph) - AP-Strategy three level model using strategies
- AP-Call aspects shy of call graph using
strategies - AspectJ cflow
- AP-Demeter behavior shy of class graph using
strategies advice on traversal - AP-DJ ClassGraph, Strategy, Visitor (in Java)
- AP-DAJ Strategy enhances ClassGraph Visitor
enhances traversal defined by Strategy (in
AspectJ) - AP-DemeterJ (new programming language)
50 pages of theory
Best of both worlds
12Adaptive Programming.
- AP-Concern-shy
- AP-Structure-shy
- AP-WildCard
- AP-Strategy
- AP-Call
- AP-Demeter
Alternative organization
- AP-Concern-shy
- AP-Graph-shy
- AP-CallGraph-shy
- AP-ClassGraph-shy
- AP-WildCard
- AP-Strategy
X-shy subX-shy Mechanism-to-achieve-shyness
13AP-Structure-shy
- AP-Concern-shy
- AP-Structure-shy
- AP-WildCard
- AP-Strategy
- AP-Call
- AP-Demeter
- Program with multiple building blocks including
b1and b2Graph, that are woven together. b1
relies only on partial information about b2 which
makes b1 more robust and reusable. Goal b1
should be loosely coupled to b2. b1 enhances b2
at nodes and edges.
14AP-WildCard
- AP-Concern-shy
- AP-Structure-shy
- AP-WildCard
- AP-Strategy
- AP-Call
- AP-Demeter
- Program with multiple building blocks including
b1 and b2Graph, that are woven together. b1 uses
wildcard techniques which makes b1 more robust
and reusable. Goal b1 should be loosely coupled
to b2.
15AP-Strategy
One contributor Ignacio Silva-Lepe PhD
1994 Currently at IBM Watson Research Lab
- AP-Concern-shy
- AP-Structure-shy
- AP-WildCard
- AP-Strategy
- AP-Call
- AP-Demeter
- Program with multiple building blocks including
b1Strategy and b2Graph, that are woven
together. b1 reveals only partial information
about b2 which makes b1 more robust and reusable.
b1 is written against an abstraction of b2 so
that the application to b2 is well defined. Goal
b1 should be loosely coupled to b2.
16A General Strategy-based Adaptive Mechanism
From TOPLAS 2003 paper (Lieberherr, Patt-Shamir,
Orleans)
- Three layers of graphs Bottom, Middle, Top
- Bottom layer trees to select subtrees guided by
top layer. Each bottom layer tree has a graph
from the - Middle layer associated with it that contains
meta-information about the bottom layer tree.
Acts as an abstraction barrier between the top
and bottom layers. Used to reduce search space. - Top layer graph is basically a subgraph of the
transitive closure of the middle layer graph,
decorated with additional information attached to
the edges.
17Top graph subgraph of transitive closure of
middle layer
B
A
C
Middle graph Abstraction barrier
B
A
C
Bottom tree select subtrees
B
c1C c2C c3C
A
18Strategy-based adaptiveness
- The call graph application (AspectJ)
- Top computational pattern,
- Middle static call graph,
- Bottom call tree.
- The standard application (Demeter)
- Top strategy graph,
- Middle class graph,
- Bottom object trees.
19AP-Call
- AP-Concern-shy
- AP-Structure-shy
- AP-WildCard
- AP-Strategy
- AP-Call
- AP-Demeter
- Program with multiple building blocks including
b1CrossCut and b2CallGraph, that are woven
together. b1 reveals only partial information
about b2 which makes b1 more robust and reusable.
Advice on b2 at b1.
20AP-AspectJ
- AP-Concern-shy
- AP-Structure-shy
- AP-WildCard
- AP-Strategy
- AP-Call
- AP-Demeter
- Program with multiple building blocks including
b1PointCut and b2CallGraph, that are woven
together. b1 reveals only partial information
about b2 which makes b1 more robust and reusable.
Advice on b2 at b1.
21AP-AspectJ
- Many AspectJ programs are adaptive (designed for
a family of Java programs) - Context Java program or its execution tree
(lexical joinpoints or dynamic join points) - Features enabling adaptiveness
- , .. (wildcards)
- cflow, (graph transitivity)
- this(s), target(s), args(a), call (),
(inheritance as wild card) - pc(Object s, Object t)
- this(s) target(t) call( f )
22AP-COOL
- AP-Concern-shy
- AP-Structure-shy
- AP-WildCard
- AP-Strategy
- AP-Call
- AP-Demeter
- Program with multiple building blocks including
b1Coordinator and b2CallGraph, that are woven
together. b1 reveals only partial information
about b2 which makes b1 more robust and reusable.
Advice on b2 at b1.
Crista Lopes, PhD 1997 Assistant Professor at UC
Irvine, first PhD thesis on AOP.
23AP-Demeter
- AP-Concern-shy
- AP-Structure-shy
- AP-WildCard
- AP-Strategy
- AP-Call
- AP-Demeter
- Program with multiple building blocks including
b1Strategy and b2ClassGraph, that are woven
together. b1 reveals only partial information
about b2 which makes b1 more robust and reusable.
Advice on traversal defined by b1 and b2.
24AP-DJ
- AP-Concern-shy
- AP-Structure-shy
- AP-WildCard
- AP-Strategy
- AP-Call
- AP-Demeter
- Program in terms of ClassGraph-, Strategy- and
Visitor-objects. - Example
- in Java cg.traverse(o, s, v)
The easiest tool to learn good structure-shy progr
amming
25AP-DAJ
- AP-Concern-shy
- AP-Structure-shy
- AP-WildCard
- AP-Strategy
- AP-Call
- AP-Demeter
- Program in terms of Strategy-objects that
introduce traversal methods into
ClassGraph-objects (adaptiveness to class graph). - Enhance the execution of the traversal methods
with Visitor-objects that may modify the run-time
traversal (adaptiveness to the traversal
execution). - Example In AspectJ declare traversal f() s V
o.f()
26crosscutting
base
Adaptive to family of bases
Connected join points
Isolated join points
27Demeter crosscutting I
Class graph or Object graph
From Company to Salary
Adaptive to family of class graphs
From BusRoute via BusStop to Person
Connected join points
28Demeter crosscutting II
Static call graph or Dynamic call graph
From Company to Salary
Adaptive to family of traversal call graphs
_salary(Employee, Object)
Connected join points
Isolated join points
29AspectJ crosscutting I
Class graph or Object graph
Company.cache()
Vector BusRoute.busses
Isolated join points
30Static call graph or Dynamic call graph
AspectJ crosscutting II
Adaptive to family of call graphs
cflow
target(Employee)
Connected join points
Isolated join points
31Demeter crosscutting
Graph
From BusRoute via Bus to Person
Adaptive to family of graphs
_salary(Employee, Object)
Connected join points
Isolated join points
32How are AP and AOP coupled?
- AOP module-shy programming
- Modularize programs that cut across modules (with
minimal reliance on information in modules). - Programming is module-shy if the modular
structure of the program does not prevent
concerns that cut across other concerns to be
modularized. - AP concern-shy programming
Can we view concern implementations as modules?
33Many open questions
- Doug Orleans Simple model of AOP Fred
- Johan Ovlinger Modules and Aspects
- Pengcheng Wu Statically Executable Advice
- Theo Skotiniotis Contracts for Aspects
34The End
35Crosscutting in Demeter
generated Java program
Demeter program
structure-shy functionality
structure
replicated!
synchronization
36range of AOP languages
means of join points
JPM
join points
identifying
specifying semantics at
AspectJ dynamic JPM
points in execution call, get, set
signaturesw/ wildcards other properties of JPs
advice
add members
signatures
class members
static JPM
- DemeterJ, DAJ
- dynamic JPM
- static JPM 1
- static JPM 2
-
- static JPM 3
when traversal reaches object or edge class
members class members class members
visitor method signatures traversal spec. s class
graph g class names class graph
visitor method bodies s g (result traversal
implementation) add members class graph with
tokensgrammar (result parsing and printing
implementation)
37Adaptiveness
- The next 7 viewgraphs show how two traversals
(parts of an adaptive program) adapt to two
different class graphs.
38Class graph Find undefined things
Ident
definedThings
System
Thing
usedThings
def
S
Definition
T
body
Body
D
B
definedThings from System bypassing Body to
Thing usedThings from System through Body to
Thing
39M1 Equation System
EquationSystem
equations
Equation_List
Ident
Variable
lhs
Equation
Numerical
rhs
Expression_List
Simple
args
Expression
op
Add
Compound
40M1 Equation System
definedThings from EquationSystem bypassing
Expression to Variable
EquationSystem
equations
Equation_List
Ident
lhs
Equation
Variable
Numerical
rhs
Simple
args
Expression_List
Expression
S
T
op
Add
Compound
D
B
41M1 Equation System
usedThings from EquationSystem through
Expression to Variable
EquationSystem
equations
Equation_List
Ident
lhs
Equation
Variable
Numerical
rhs
Simple
args
Expression_List
Expression
S
T
op
Add
Compound
D
B
42CS1 Grammar
Entry
0..
EParse
entries
Grammar
BParse
Production
rhs
Body
Part
parts
lhs
NonTerm
0..
Concrete
Abstract
43CS1 Grammar
definedThings from Grammar bypassing Body to
NonTerm
Entry
0..
EParse
entries
Grammar
BParse
Production
rhs
Body
Part
parts
lhs
NonTerm
0..
S
T
Concrete
Abstract
D
B
44CS1Grammar
usedThings from Grammar through Body to NonTerm
Entry
0..
EParse
entries
Grammar
BParse
Production
Body
rhs
Part
parts
lhs
NonTerm
0..
S
T
Concrete
Abstract
D
B
45Software Structure with ACs
Software Structure with ACs
Software Structure with ACs
P1
P1
P1
P2
P2
P2
P3
P3
P3
P1
P1
P1
P4
P4
P4
P3
P3
P3
P5
P5
P5
P2
P2
P2
P2
P2
P2
P6
P6
P6
P1
P1
P1
45
45
45
46AP-structure-shy notion of crosscutting
- B2 program and its execution trees or an
abstraction thereof UML class diagram and its
object diagrams. - A program b1b2 is aspect-oriented if it is
crosscutting. - Examples Adaptive, aspect-oriented programs
Policies (Concurrency, Distribution,
Authentication, Logging), Adaptive Method, Law of
Demeter Checker in AspectJ
47Scattering
- B2 is a graph. Count number of nodes and edges
that are enhanced. Scat(b1b2) number of nodes
and edges in b2 enhanced by b1. The higher the
number, the more the crosscutting. - A program b1B2 is crosscutting if there is an
infinite sequence R1, R2, of B2 so that
Scat(b1R1), Scat(b1R2), is strictly
increasing.