Towards A Better Organization of Programs with AspectOriented

  • Slides: 39
Download presentation
Towards A Better Organization of Programs with Aspect-Oriented Programming Karl Lieberherr Demeter Research Group

Towards A Better Organization of Programs with Aspect-Oriented Programming Karl Lieberherr Demeter Research Group 3/10/2021 Aspect-Oriented Programming 1

Overview • Aspect-Oriented Programming (AOP): Crosscuting concerns, controlling tangling and scattering. • Example: 3

Overview • Aspect-Oriented Programming (AOP): Crosscuting concerns, controlling tangling and scattering. • Example: 3 concerns: Find undefined entities. • Tools for AOP – Aspect. J from Xerox PARC – DJ from Northeastern • Adaptive Programming and AOP • Summary 3/10/2021 Aspect-Oriented Programming 2

The MIT Technology Review Ten (Jan. /Feb. 2001 issue) • Ten emerging technologies that

The MIT Technology Review Ten (Jan. /Feb. 2001 issue) • Ten emerging technologies that will change the world – Untangling Code - Aspect-Oriented Programming (AOP) – Data Mining – Micro Fluids – Robot Design –. . . 3/10/2021 Aspect-Oriented Programming 3

Northeastern Connection • Crista Lopes wrote the first Ph. D. Thesis on AOP at

Northeastern Connection • Crista Lopes wrote the first Ph. D. Thesis on AOP at Northeastern supported by Xerox PARC. • The Demeter Research Group worked with interesting AOP Systems long before the name AOP was invented. 3/10/2021 Aspect-Oriented Programming 4

Quote: MIT Technology Magazine • “The idea of aspects has been around for many

Quote: MIT Technology Magazine • “The idea of aspects has been around for many years and with many different names. It is called adaptive programming at Northeastern University, and subjectoriented programming at IBM, …” 3/10/2021 Aspect-Oriented Programming 5

AOP • Crosscutting concerns • Example: Logging: record every operation an application performs •

AOP • Crosscutting concerns • Example: Logging: record every operation an application performs • “When adding a new operation to this application, always put a trace statement in” • Keeping track of crosscutting concerns is error-prone 3/10/2021 Aspect-Oriented Programming 6

AOP • Crosscutting concerns, when implemented in an ad-hoc way, lead to a lot

AOP • Crosscutting concerns, when implemented in an ad-hoc way, lead to a lot of tangling and scattering in the program. • Goal of AOP is to control the tangling and scattering in the program. 3/10/2021 Aspect-Oriented Programming 7

Tangling: count color changes ordinary program better program structure-shy functionality structure synchronization 3/10/2021 Aspect-Oriented

Tangling: count color changes ordinary program better program structure-shy functionality structure synchronization 3/10/2021 Aspect-Oriented Programming Components Aspect 1 Aspect 2 8

Scattering: count number of modules to which color goes ordinary program M 1 M

Scattering: count number of modules to which color goes ordinary program M 1 M 2 better program structure-shy functionality structure M 3 synchronization 3/10/2021 Aspect-Oriented Programming Components Aspect 1 Aspect 2 9

Aspect-Oriented Programming: Example • Separating the following cross-cutting concerns: – Object Structure – Traversals

Aspect-Oriented Programming: Example • Separating the following cross-cutting concerns: – Object Structure – Traversals through Objects – Advice on Traversals • Focus on those three concerns only. They appear frequently. 3/10/2021 Aspect-Oriented Programming 10

overall graph: object structure; green graph: traversal; purple: advice Why crosscutting? r=0; Route 1:

overall graph: object structure; green graph: traversal; purple: advice Why crosscutting? r=0; Route 1: Bus. Route bus. Stops buses Bus. List : Bus. Stop. List Central. Square: Bus. Stop waiting : Person. List Bus 15: Diesel. Powered passengers : Person. List Joan: Person Paul: Person r++; 3/10/2021 Seema: Person r++; Aspect-Oriented Programming Eric: Person 11

Keeping Track • When object structure changes at certain places, need to update traversal.

Keeping Track • When object structure changes at certain places, need to update traversal. • Error prone. 3/10/2021 Aspect-Oriented Programming 12

Why aspects: Oblivious • Object Structure – does not have to know about traversals

Why aspects: Oblivious • Object Structure – does not have to know about traversals and advice on traversals • Traversals – don’t have to know about advice on traversals • Advice on Traversals – has to know minimally about object structure and traversals 3/10/2021 Aspect-Oriented Programming 13

Ad-hoc Implementation of three concerns • Leads to lots of tangled and scattered code

Ad-hoc Implementation of three concerns • Leads to lots of tangled and scattered code with numerous disadvantages • The question is not how to eliminate the tangling but how to reduce it • AOP is about tangling control of the implementation of crosscutting concerns • Crosscutting will always lead to some tangling at code level 3/10/2021 Aspect-Oriented Programming 14

Example • Check whether all used entities are defined. • Object structure, traversal, advice

Example • Check whether all used entities are defined. • Object structure, traversal, advice on traversal 3/10/2021 Aspect-Oriented Programming 15

Find undefined things System defined. Things used. Things * * Used. Things. Holder *

Find undefined things System defined. Things used. Things * * Used. Things. Holder * Thing * * defined. Things= from System bypassing Used. Things. Holder to Thing used. Things = from System through Used. Things. Holder to Thing 3/10/2021 Aspect-Oriented Programming 16

Name map 3/10/2021 Aspect-Oriented Programming 17

Name map 3/10/2021 Aspect-Oriented Programming 17

Java Program with less tangling class Cd_graph{ String vi = “from Vertex to edu.

Java Program with less tangling class Cd_graph{ String vi = “from Vertex to edu. neu. ccs. demeter. Ident”; void is. Defined(Class. Graph cg){ check. Defined(cg, get. Classes(cg)); } Hash. Set get. Classes(Class. Graph cg){ String defined. Things = "from Cd_graph bypassing Neighbors to Vertex"; Visitor v = new Visitor(){ Hash. Set return_val = new Hash. Set(); void before(Vertex v 1){ return_val. add(cg. fetch(v 1, vi) ); } public Object get. Return. Value(){return_val; }}; cg. traverse(this, defined. Things, v); green: traversal return (Hash. Set)v. get. Return. Value(); black bold: structure } 3/10/2021 Aspect-Oriented Programming purple: advice red: parameters 18

Java Program with less tangling void check. Defined(Class. Graph cg, final Hash. Set class.

Java Program with less tangling void check. Defined(Class. Graph cg, final Hash. Set class. Hash){ String used. Things = ”from Cd_graph through Neighbors to Vertex"; cg. traverse(this, used. Things, new Visitor(){ void before(Vertex v){ Ident vn = cg. fetch(v, vi); if (!class. Hash. contains(vn)){ System. out. println("The object "+ vn + " is undefined. "); }}}); } } 3/10/2021 Aspect-Oriented Programming 19

Tangling is localized • Instead of having code spread across several classes, it is

Tangling is localized • Instead of having code spread across several classes, it is localized in one class. • Java program is describing the abstract pattern behind the computation of interest: checking whether used entities are defined. • Tangling control through abstraction of patterns. 3/10/2021 Aspect-Oriented Programming 20

defined. Things = from Class. G bypassing Body to Class. Name UML class diagram

defined. Things = from Class. G bypassing Body to Class. Name UML class diagram Class. G 0. . * Entry EParse entries Class. G BParse Class. Def Body parts Part class. Name 0. . * Concrete 3/10/2021 Class. Name Abstract Aspect-Oriented Programming 21

used. Things = from Class. G through Body to Class. Name UML class diagram

used. Things = from Class. G through Body to Class. Name UML class diagram Class. G 0. . * Entry EParse entries Class. G BParse Class. Def Body parts Part class. Name 0. . * Concrete 3/10/2021 Class. Name Abstract Aspect-Oriented Programming 22

Example: x = 1. 0. y = (+ x 4. 0). z = (*

Example: x = 1. 0. y = (+ x 4. 0). z = (* x y). used. Things = from Equation. System through Expression to Variable Equation System Equation. System = <equations> List(Equation). Equation = <lhs> Variable “=“ <rhs> Expression “. ”. Variable = Ident. Expression : Simple | Compound. Simple : Variable | Numerical. Compound = “(“ Op <args> List(Expression) “)”. Op : Add | Mul. Add = “+”. Mul = “*”. Numerical = float. 3/10/2021 Aspect-Oriented Programming 23

Example: x = 1. 0. y = (+ x 4. 0). z = (*

Example: x = 1. 0. y = (+ x 4. 0). z = (* x y). defined. Things= from Equation. System bypassing Expression to Variable Equation System Equation. System = <equations> List(Equation). Equation = <lhs> Variable “=“ <rhs> Expression “. ”. Variable = Ident. Expression : Simple | Compound. Simple : Variable | Numerical. Compound = “(“ Op <args> List(Expression) “)”. Op : Add | Mul. Add = “+”. Mul = “*”. Numerical = float. 3/10/2021 Aspect-Oriented Programming 24

Aspect. J (Xerox PARC) • A general aspect-oriented programming language for Java. • An

Aspect. J (Xerox PARC) • A general aspect-oriented programming language for Java. • An aspect is like a class and may contain pointcut definitions defining a set of join points and advice saying what to do before, after or instead of the join points. 3/10/2021 Aspect-Oriented Programming 25

DJ (Northeastern) • Is a Java package that supports AOP for the three concerns:

DJ (Northeastern) • Is a Java package that supports AOP for the three concerns: class structure, traversals, and traversal advice. • Connection to Aspect. J: both can be used simultaneously. • DJ provides an implementation of Adaptive Programming (AP). 3/10/2021 Aspect-Oriented Programming 26

Principle behind AP: Polya’s Inventor Paradox • Polya observed that it is often easier

Principle behind AP: Polya’s Inventor Paradox • Polya observed that it is often easier to solve a more general problem than the one at hand then to use the solution of the general problem to solve the specific problem. The hard work consists of finding the appropriate generalization. • Programs become shorter and more powerful. A paradox. With less work we achieve more. 3/10/2021 Aspect-Oriented Programming 27

Apply Polya to Programming • Generalization: Don’t write the program for a specific data

Apply Polya to Programming • Generalization: Don’t write the program for a specific data structure, write it for an abstract data structure. 3/10/2021 Aspect-Oriented Programming 28

Concepts needed (DJ classes) • • • Class. Graph Strategy Object. Graph. Slice Visitor

Concepts needed (DJ classes) • • • Class. Graph Strategy Object. Graph. Slice Visitor 3/10/2021 Aspect-Oriented Programming 29

Adaptive Programming Bold names refer to DJ classes. Strategy is use-case based abstraction of

Adaptive Programming Bold names refer to DJ classes. Strategy is use-case based abstraction of Class. Graph defines family of Object. Graph 3/10/2021 Aspect-Oriented Programming 30

Adaptive Programming Strategy defines traversals of Object. Graph plus Strategy defines Object. Graph. Slice

Adaptive Programming Strategy defines traversals of Object. Graph plus Strategy defines Object. Graph. Slice 3/10/2021 Aspect-Oriented Programming 31

Adaptive Programming Strategy guides and informs Visitor 3/10/2021 Aspect-Oriented Programming 32

Adaptive Programming Strategy guides and informs Visitor 3/10/2021 Aspect-Oriented Programming 32

Aspect. J (Xerox) • Abstract pointcut – set of execution points – where to

Aspect. J (Xerox) • Abstract pointcut – set of execution points – where to watch • Advice • Abstract object slice – set of entry/exit points – where to go • Visitor – what to do • Concrete pointcut – set notation using regular expressions 3/10/2021 DJ (NEU) • Actual object slice – traversal strategies Aspect-Oriented Programming 33

AOP AP • Program with aspects that correspond to the concerns of the programmer.

AOP AP • Program with aspects that correspond to the concerns of the programmer. 3/10/2021 • Relieve the programmer from the details of some concern. • Create robustness to changes in an aspect. • AP is about join point reduction. • Example: structureshyness Aspect-Oriented Programming 34

Technology Evolution Object-Oriented Programming Other Technologies Tangled structure/behaviors robustness to structure changes Adaptive Programming

Technology Evolution Object-Oriented Programming Other Technologies Tangled structure/behaviors robustness to structure changes Adaptive Programming Tangled concerns in general (synchronization, etc. ) Aspect-Oriented Programming robustness to aspect changes Aspect-Oriented Programming II 3/10/2021 Aspect-Oriented Programming 35

Benefits of Adaptive Programming • robustness to changes • shorter programs • design matches

Benefits of Adaptive Programming • robustness to changes • shorter programs • design matches program, more understandable code • partially automated evolution • keep all benefits of OO technology • improved productivity Applicable to design and documentation of your current systems. 3/10/2021 Aspect-Oriented Programming 36

Summary • AOP getting a lot of attention. Addresses an important problem: how to

Summary • AOP getting a lot of attention. Addresses an important problem: how to program crosscutting concerns. • AP is about relieving the programmer from the details of a concern: traditionally from the structural concern. • Want to learn more: Take COM 3360: Adaptive Object-Oriented Software Development and attend the Demeter Seminar. 3/10/2021 Aspect-Oriented Programming 37

Current Work • DARPA grant with BBN (Prof. Wand, Lieberherr) on AOP for real-time

Current Work • DARPA grant with BBN (Prof. Wand, Lieberherr) on AOP for real-time systems. • Doug Orleans: Ph. D thesis, in progress: Design and Implementation of Aspect. Oriented Languages. • Johan Ovlinger: Ph. D thesis, in progress: The Collaboration/Adapter Approach to AOP. Works with industry: SKYVA. 3/10/2021 Aspect-Oriented Programming 38

Critical Mass • Professors: Lorenz, Wand, Lieberherr, Clinger • Ph. D. students: Orleans, Ovlinger,

Critical Mass • Professors: Lorenz, Wand, Lieberherr, Clinger • Ph. D. students: Orleans, Ovlinger, Wu • Master’s students working on JSR 31 project: Prasenijt Adak, Huichan He, John Sung • Undergraduate student: Jon Kelley • Courses: COM 3360 (graduate), COM 1205 (undergraduate) 3/10/2021 Aspect-Oriented Programming 39