390 likes | 408 Views
This article provides an overview of aspect-oriented programming (AOP) and its benefits in controlling tangling and scattering in programs. It also showcases examples and tools for AOP.
E N D
Towards A Better Organization of Programs withAspect-Oriented Programming Karl Lieberherr Demeter Research Group Aspect-Oriented Programming
Overview • Aspect-Oriented Programming (AOP): Crosscuting concerns, controlling tangling and scattering. • Example: 3 concerns: Find undefined entities. • Tools for AOP • AspectJ from Xerox PARC • DJ from Northeastern • Adaptive Programming and AOP • Summary Aspect-Oriented Programming
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 • ... Aspect-Oriented Programming
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. Aspect-Oriented Programming
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 subject-oriented programming at IBM, …” Aspect-Oriented Programming
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 Aspect-Oriented Programming
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. Aspect-Oriented Programming
Tangling: count color changes ordinary program better program structure-shy functionality Components structure Aspect 1 synchronization Aspect 2 Aspect-Oriented Programming
Scattering: count number of modules to which color goes ordinary program better program structure-shy functionality M1 Components structure Aspect 1 M2 M3 synchronization Aspect 2 Aspect-Oriented Programming
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. Aspect-Oriented Programming
overall graph: object structure; green graph: traversal; purple: advice Why crosscutting? r=0; BusList Route1:BusRoute buses busStops :BusStopList Bus15:DieselPowered passengers CentralSquare:BusStop waiting :PersonList :PersonList Joan:Person Paul:Person Seema:Person r++; r++; Eric:Person Aspect-Oriented Programming
Keeping Track • When object structure changes at certain places, need to update traversal. • Error prone. Aspect-Oriented Programming
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 Aspect-Oriented Programming
Ad-hoc Implementationof 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 Aspect-Oriented Programming
Example • Check whether all used entities are defined. • Object structure, traversal, advice on traversal Aspect-Oriented Programming
Find undefined things definedThings * System * Thing usedThings * * * UsedThingsHolder definedThings= from System bypassing UsedThingsHolder to Thing usedThings = from System through UsedThingsHolder to Thing Aspect-Oriented Programming
Name map Aspect-Oriented Programming
Java Program with less tangling class Cd_graph{ String vi = “from Vertex to edu.neu.ccs.demeter.Ident”; void isDefined(ClassGraph cg){ checkDefined(cg, getClasses(cg));} HashSet getClasses(ClassGraph cg){ String definedThings = "fromCd_graphbypassingNeighborstoVertex"; Visitor v = new Visitor(){ HashSet return_val = new HashSet(); void before(Vertexv1){ return_val.add(cg.fetch(v1, vi) );} public Object getReturnValue(){return return_val;}}; cg.traverse(this,definedThings, v); return (HashSet)v.getReturnValue(); } green: traversal black bold: structure purple: advice red: parameters Aspect-Oriented Programming
Java Program with less tangling void checkDefined(ClassGraph cg, final HashSet classHash){ String usedThings = ”fromCd_graphthroughNeighborstoVertex"; cg.traverse(this, usedThings, new Visitor(){ void before(Vertexv){ Ident vn = cg.fetch(v, vi); if (!classHash.contains(vn)){ System.out.println("The object "+ vn + " is undefined."); }}});} } Aspect-Oriented Programming
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. Aspect-Oriented Programming
definedThings = from ClassG bypassing Body to ClassName UML class diagram ClassG 0..* Entry EParse entries ClassG BParse ClassDef Body parts Part className 0..* ClassName Concrete Abstract Aspect-Oriented Programming
usedThings = from ClassG through Body to ClassName UML class diagram ClassG 0..* Entry EParse entries ClassG BParse ClassDef Body parts Part className 0..* ClassName Concrete Abstract Aspect-Oriented Programming
usedThings = from EquationSystem through Expression to Variable Example: x = 1.0 . y = (+ x 4.0). z = (* x y). Equation System EquationSystem = <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. Aspect-Oriented Programming
definedThings= from EquationSystem bypassing Expression to Variable Example: x = 1.0 . y = (+ x 4.0). z = (* x y). Equation System EquationSystem = <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. Aspect-Oriented Programming
AspectJ (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. Aspect-Oriented Programming
DJ (Northeastern) • Is a Java package that supports AOP for the three concerns: class structure, traversals, and traversal advice. • Connection to AspectJ: both can be used simultaneously. • DJ provides an implementation of Adaptive Programming (AP). Aspect-Oriented Programming
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 and 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. Aspect-Oriented Programming
Apply Polya to Programming • Generalization: Don’t write the program for a specific data structure, write it for an abstract data structure. Aspect-Oriented Programming
Concepts needed(DJ classes) • ClassGraph • Strategy • ObjectGraph • ObjectGraphSlice • Visitor Aspect-Oriented Programming
Adaptive Programming Strategy Bold names refer to DJ classes. is use-case based abstraction of ClassGraph defines family of ObjectGraph Aspect-Oriented Programming
Adaptive Programming Strategy defines traversals of ObjectGraph plus Strategy defines ObjectGraphSlice Aspect-Oriented Programming
Adaptive Programming Strategy guides and informs Visitor Aspect-Oriented Programming
Abstract pointcut set of execution points where to watch Advice what to do Concrete pointcut set notation using regular expressions Abstract object slice set of entry/exit points where to go Visitor what to do Actual object slice traversal strategies AspectJ (Xerox) DJ (NEU) Aspect-Oriented Programming
Program with aspects that correspond to the concerns of the programmer. Relieve the programmer from the details of some concern. Create robustness to changes in an aspect. AP is about join point reduction. Example: structure-shyness AOP AP Aspect-Oriented Programming
Technology Evolution Object-Oriented Programming Tangled structure/behaviors robustness to structure changes Other Technologies Adaptive Programming Tangled concerns in general (synchronization, etc.) Aspect-Oriented Programming robustness to aspect changes Aspect-Oriented Programming II Aspect-Oriented Programming
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. Aspect-Oriented Programming
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. Aspect-Oriented Programming
Current Work • DARPA grant with BBN (Prof. Wand, Lieberherr) on AOP for real-time systems. • Doug Orleans: PhD thesis, in progress: Design and Implementation of Aspect-Oriented Languages. • Johan Ovlinger: PhD thesis, in progress: The Collaboration/Adapter Approach to AOP. Works with industry: SKYVA. Aspect-Oriented Programming
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) Aspect-Oriented Programming