47
1 Shyness in Programming Karl Lieberherr Demeter Research Group Northeastern University Boston

Shyness in Programming

Embed Size (px)

DESCRIPTION

DEMETER. DHMHTRA. Shyness in Programming. Karl Lieberherr Demeter Research Group Northeastern University Boston. Coupling Aspect-Oriented and Adaptive Programming: Shyness in Programming. PhD Visitation Weekend 2003. Our Intuition behind AOP. - PowerPoint PPT Presentation

Citation preview

Page 1: Shyness in Programming

1

Shyness in Programming

Karl Lieberherr

Demeter Research Group

Northeastern University

Boston

Page 2: Shyness in Programming

2

Coupling Aspect-Oriented and Adaptive Programming:

Shyness in Programming

PhD Visitation Weekend 2003

Page 3: Shyness in Programming

3

Our 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").

Page 4: Shyness in Programming

4

MIT Technology Review2001

• Aspect-oriented programming is called “adaptive programming” at Northeastern University.

Page 5: Shyness in Programming

5

Definitions

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.

Page 6: Shyness in Programming

6

Examples 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?

Page 7: Shyness in Programming

7

Scattering and Tangling: Static• aspecti is scattered across many classes (i = 1,2,3)

• class X tangles aspects 1, 2 and 3

aspect1 aspect2 aspect3class A consistingof three aspects

classes foraspect1 classes for

aspect2

classes foraspect3

Class X

class diagram

Adding to classes

Page 8: Shyness in Programming

8

Scattering and Tangling: Dynamicprogram executioninvolving three aspects (colors r b g)

program call tree(classesexecuting method calls)

•Each aspect (colors) is scattered across many classes (shapes)•Class tangles all three aspects

At those calls the aspect enhances the behavior

classes

this(s)

target(t)f(..)

Enhancing callst.f(..);

Page 9: Shyness in Programming

9

AP-Concern-shy

• Program with multiple building blocks, say b1:B1 and b2:B2, that are woven together: b1*b2. b1 relies only on partial information about b2 which makes b1 more robust and reusable. Goal: b1 should be loosely coupled to b2.

• AP-Concern-shy

– AP-Structure-shy

• AP-WildCard

• AP-Strategy

– AP-Call

– AP-Demeter

Page 10: Shyness in Programming

10

Law 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 Engineeringat Kronos Incorporated in Chelmsford, MA. 2200 empls.

Page 11: Shyness in Programming

11

Adaptive 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)

Best of both worlds

50 pages of theory

Page 12: Shyness in Programming

12

Adaptive Programming.

• AP-Concern-shy

– AP-Structure-shy

• AP-WildCard

• AP-Strategy

– AP-Call

– AP-Demeter

• AP-Concern-shy

– AP-Graph-shy

• AP-CallGraph-shy

• AP-ClassGraph-shy

– AP-WildCard

– AP-Strategy

Alternativeorganization:

X-shysubX-shyMechanism-to-achieve-shyness

Page 13: Shyness in Programming

13

AP-Structure-shy

• Program with multiple building blocks including b1and b2:Graph, 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.

• AP-Concern-shy

– AP-Structure-shy

• AP-WildCard

• AP-Strategy

– AP-Call

– AP-Demeter

Page 14: Shyness in Programming

14

AP-WildCard

• Program with multiple building blocks including b1 and b2:Graph, that are woven together. b1 uses wildcard techniques which makes b1 more robust and reusable. Goal: b1 should be loosely coupled to b2.

• AP-Concern-shy

– AP-Structure-shy

• AP-WildCard

• AP-Strategy

– AP-Call

– AP-Demeter

Page 15: Shyness in Programming

15

AP-Strategy

• Program with multiple building blocks including b1:Strategy and b2:Graph, 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.

• AP-Concern-shy

– AP-Structure-shy

• AP-WildCard

• AP-Strategy

– AP-Call

– AP-Demeter

One contributor: Ignacio Silva-Lepe PhD 1994Currently at IBM Watson Research Lab

Page 16: Shyness in Programming

16

A General Strategy-based Adaptive Mechanism

• 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.

From TOPLAS 2003 paper (Lieberherr, Patt-Shamir, Orleans)

Page 17: Shyness in Programming

17

Middle graph: Abstraction barrier

Bottom tree: select subtrees

A B

A B

C

Top graph: subgraph of transitive closure of middle layer

C

AB c1:C

c2:Cc3:C

Page 18: Shyness in Programming

18

Strategy-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.

Page 19: Shyness in Programming

19

AP-Call

• Program with multiple building blocks including b1:CrossCut and b2:CallGraph, that are woven together. b1 reveals only partial information about b2 which makes b1 more robust and reusable. Advice on b2 at b1.

• AP-Concern-shy

– AP-Structure-shy

• AP-WildCard

• AP-Strategy

– AP-Call

– AP-Demeter

Page 20: Shyness in Programming

20

AP-AspectJ

• Program with multiple building blocks including b1:PointCut and b2:CallGraph, that are woven together. b1 reveals only partial information about b2 which makes b1 more robust and reusable. Advice on b2 at b1.

• AP-Concern-shy

– AP-Structure-shy

• AP-WildCard

• AP-Strategy

– AP-Call

– AP-Demeter

Page 21: Shyness in Programming

21

AP-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 …)

Page 22: Shyness in Programming

22

AP-COOL

• Program with multiple building blocks including b1:Coordinator and b2:CallGraph, that are woven together. b1 reveals only partial information about b2 which makes b1 more robust and reusable. Advice on b2 at b1.

• AP-Concern-shy

– AP-Structure-shy

• AP-WildCard

• AP-Strategy

– AP-Call

– AP-Demeter

Crista Lopes, PhD 1997Assistant Professor at UC Irvine, first PhD thesis on AOP.

Page 23: Shyness in Programming

23

AP-Demeter

• Program with multiple building blocks including b1:Strategy and b2:ClassGraph, 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.

• AP-Concern-shy

– AP-Structure-shy

• AP-WildCard

• AP-Strategy

– AP-Call

– AP-Demeter

Page 24: Shyness in Programming

24

AP-DJ

• Program in terms of ClassGraph-, Strategy- and Visitor-objects.

• Example:– in Java: cg.traverse(o, s, v);

• AP-Concern-shy

– AP-Structure-shy

• AP-WildCard

• AP-Strategy

– AP-Call

– AP-Demeter

The easiest tool to learn good structure-shyprogramming

Page 25: Shyness in Programming

25

AP-DAJ

• 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();

• AP-Concern-shy

– AP-Structure-shy

• AP-WildCard

• AP-Strategy

– AP-Call

– AP-Demeter

Page 26: Shyness in Programming

26Isolated join points

crosscutting

Connected join points

base

Adaptive tofamily of bases

Page 27: Shyness in Programming

27

Demeter crosscutting I

Connected join points

Class graph orObject graph

From Company to Salary

From BusRoute via BusStop to Person

Adaptive tofamily of class graphs

Page 28: Shyness in Programming

28Isolated join points

Demeter crosscutting II

Connected join points

Static call graph orDynamic call graph

…_salary(Employee, Object)

From Company to Salary

Adaptive tofamily of traversal callgraphs

Page 29: Shyness in Programming

29Isolated join points

AspectJ crosscutting I Class graph orObject graph

Company.cache(){} Vector BusRoute.busses;

Page 30: Shyness in Programming

30Isolated join points

AspectJ crosscutting II

Connected join points

Static call graph orDynamic call graph

target(Employee)

cflow

Adaptive tofamily of call graphs

Page 31: Shyness in Programming

31

Demeter crosscutting

Connected join points

GraphFrom BusRoute via Bus to Person

Isolated join points

…_salary(Employee, Object)

Adaptive tofamily of graphs

Page 32: Shyness in Programming

32

How 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 programmingCan we view concern implementations as modules?

Page 33: Shyness in Programming

33

Many open questions

• Doug Orleans: Simple model of AOP: Fred

• Johan Ovlinger: Modules and Aspects

• Pengcheng Wu: Statically Executable Advice

• Theo Skotiniotis: Contracts for Aspects

Page 34: Shyness in Programming

34

The End

Page 35: Shyness in Programming

35

Crosscutting in Demeter

generatedJava program

structure-shyfunctionality

structure

synchronization

Demeter program

replicated!

Page 36: Shyness in Programming

36

range of AOP languagesmeans of … join points

add memberssignaturesclass members static JPM

DemeterJ, DAJ

dynamic JPM

static JPM 1

static JPM 2

static JPM 3

AspectJ dynamic JPM

JPM

when traversal reaches object or edge

class members

class members

class members

points in execution call, get, set…

join points

visitor method signatures

traversal spec. sclass graph g

class names

class graph

signaturesw/ wildcards & other properties of JPs

identifying

visitor method bodies

s + g (result = traversal implementation)

add members

class graph with tokens=grammar (result = parsing and printing implementation)

advice

specifying semantics at

Page 37: Shyness in Programming

37

Adaptiveness

• The next 7 viewgraphs show how two traversals (parts of an adaptive program) adapt to two different class graphs.

Page 38: Shyness in Programming

38

Class graph: Find undefined things

System

Body

Thing

* *

*definedThings

usedThings

definedThings= from System bypassing Body to ThingusedThings = from System through Body to Thing

*

Definitiondef

body

Ident

S

D

T

B

Page 39: Shyness in Programming

39

M1: Equation SystemEquationSystem

Equation_List

EquationVariable

equations

*lhs

rhs

ExpressionSimple

Compound

Numerical

Expression_List

* Addop

args

Ident

Page 40: Shyness in Programming

40

M1: Equation System

definedThings = from EquationSystem bypassing Expression to Variable

EquationSystem

Equation_List

Equation Variable

equations

*lhs

rhs

ExpressionSimple

Compound

Numerical

Expression_List

*Addop

args

Ident

S

D

T

B

Page 41: Shyness in Programming

41

M1: Equation System

usedThings = from EquationSystem through Expression to Variable

EquationSystem

Equation_List

Equation Variable

equations

*lhs

rhs

ExpressionSimple

Compound

Numerical

Expression_List*

Addop

args

Ident

S

D

T

B

Page 42: Shyness in Programming

42

CS1: Grammar

Grammar

EParse

BParseProduction

Entry0..*

entries

BodyPart

NonTerm0..*

parts

Concrete Abstract

lhs

rhs

Page 43: Shyness in Programming

43

CS1: Grammar

Grammar

EParse

BParseProduction

Entry0..*

entries

BodyPart

NonTerm0..*

parts

Concrete Abstract

lhs

definedThings = from Grammar bypassing Body to NonTerm

rhs

S

D

T

B

Page 44: Shyness in Programming

44

CS1:Grammar

Grammar

EParse

BParseProduction

Entry0..*

entries

BodyPart

NonTerm0..*

parts

Concrete Abstract

lhs

usedThings = from Grammar through Body to NonTerm

rhs

S

D

T

B

Page 45: Shyness in Programming

45

Page 46: Shyness in Programming

46

AP-structure-shy: notion of crosscutting

• B2 = program and its execution trees or an abstraction thereof: UML class diagram and its object diagrams.

• A program b1*b2 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

Page 47: Shyness in Programming

47

Scattering

• B2 is a graph. Count number of nodes and edges that are enhanced. Scat(b1*b2) = number of nodes and edges in b2 enhanced by b1. The higher the number, the more the crosscutting.

• A program b1*B2 is crosscutting if there is an infinite sequence R1, R2, … of B2 so that Scat(b1*R1), Scat(b1*R2), … is strictly increasing.