Title: Adaptive Object-Oriented Software Development
1Adaptive Object-OrientedSoftware Development
www.ccs.neu.edu/research/demeter/course/f00/f00.ht
ml
2Software Design and Development
3Introduction
- Software engineering, modeling, design, software
architecture - Programming languages
- Hands-on, practical, useful
4Summary of Course
- How to design and implement flexible
object-oriented software using the principles of
adaptiveness in combination with UML and Java.
Learning principles behind XML. - How to design and implement flexible 100 pure
Java software.
5Who is in Attendance?
- Software developers who have some experience with
object-oriented programming. - Should know concepts of OOP, like classes,
methods, and late binding.
6Agenda
Adaptive Programming Aspect-Oriented Progr.
DemeterJ Java and DJ Java environment UML XML
strategy graphs class graphs object graphs state
graphs
principles heuristics patterns idioms theorems alg
orithms
requirements domain analysis design implementation
use cases interfaces traversals visitors packages
Demeter Method iterative development spiral
model Extreme Programming
7Agenda
- UML class diagrams. Perspective analysis,
design, implementation - Class graphs as special cases of UML class
diagrams - Textual representation of class graphs
- Default implementation of UML class diagrams as
class graphs
8Agenda
- UML interaction diagrams
- sequence diagrams
- collaboration diagrams
- improving interaction diagrams to make them
specification languages - XML data type definitions and documents
9Agenda
- Class graphs in textual form (construction,
alternation) - object graphs (graphical and textual form)
- object graphs defined by class graphs
- add repetition classes and optional parts
- translating class graphs to Java
10Agenda
- annotating class graph with syntax class
dictionary - printing objects and language defined by class
dictionary - grammars, parsing, ambiguous grammars,
undecidable problem - LL(1) grammars, robustness of sentences
- etc.
11Overview
- good separation of concerns is the goal
- concerns should be cleanly localized
- programs should look like designs
12Adaptive Programming
- Programs adapt to interesting context changes
- Structure-shy behavior
- Succinct representation of traversals
- Programming in terms of graph constraints
13Cross-cutting of components and aspects
better program
ordinary program
structure-shy functionality
Components
structure
Aspect 1
synchronization
Aspect 2
14Aspect-Oriented Programming
components and aspect descriptions
High-level view, implementation may be different
Source Code (tangled code)
weaver (compile- time)
15Examples of Aspects
- Synchronization of methods across classes
- Remote invocation (using Java RMI)
- Quality of Service (QoS)
- Failure handling
- External use (e.g., being a Java bean)
- Replication, Migration
- etc.
16Connections
- explain adaptive programming in terms of
patterns - Aspect-Oriented Programming (AOP) is a
generalization of Adaptive Programming (AP) - correspondence
- adaptive program object-oriented program
sentence object graph
17Vocabulary
- Graph, nodes, edges, labels
- Class graph, construction, alternation
- Object graph, satisfying class graph
- UML class diagram
- Grammar, printing, parsing
18Vocabulary
- Traversals, visitors
- Strategy graphs, path set
19Overview this lecture
- Basic UML class diagrams
- Traversals/Collaborating classes
- Traversal strategy graphs
- Adaptive programming
- Tools for adaptive programming
- DemeterJ and AP/Studio
201 Basic UML class diagrams
- Graph with nodes and directed edges and labels
for nodes and edges - Nodes classes, edges relationships
- labels class kind, edge kind, cardinality
21UML Class Diagram
busStops
BusRoute
BusStopList
buses
0..
BusStop
BusList
waiting
0..
passengers
Bus
PersonList
Person
0..
222 Traversals / Collaborating classes
- To process objects we need to traverse them
- Traversal can be specified by a group of
collaborating classes
23Collaborating Classes
use connectivity in class graph to define them
succinctly using strategy graphs
from Customer to Agent
from Company to Employee
24What's the problem? TANGLING
OOAD
Collab-1
C1
C4
C2
C3
C5
Collab-4
Collab-2
Collab-3
C1
C4
C2
C3
Implementation
C5
25Collaborating Classes
find all persons waiting at any bus stop on a bus
route
busStops
BusRoute
BusStopList
OO solution one method for each red class
buses
0..
BusStop
BusList
waiting
0..
passengers
Bus
PersonList
Person
0..
263 Traversal Strategy Graphs
- Want to define traversals succinctly
- Use strategy graphs to express abstraction of
class diagram - Express traversal intent useful for
documentation of object-oriented programs
27Traversal Strategy
find all persons waiting at any bus stop on a bus
route
first try from BusRoute to Person
busStops
BusRoute
BusStopList
buses
0..
BusStop
BusList
waiting
0..
passengers
Bus
PersonList
Person
0..
28Traversal Strategy
find all persons waiting at any bus stop on a bus
route
from BusRoute through BusStop to Person
busStops
BusRoute
BusStopList
buses
0..
BusStop
BusList
waiting
0..
passengers
Bus
PersonList
Person
0..
29Traversal Strategy
find all persons waiting at any bus stop on a bus
route
Altern. from BusRoute bypassing Bus to Person
busStops
BusRoute
BusStopList
buses
0..
BusStop
BusList
waiting
0..
passengers
Bus
PersonList
Person
0..
30Robustness of Strategy
find all persons waiting at any bus stop on a bus
route
from BusRoute through BusStop to Person
villages
BusRoute
BusStopList
busses
VillageList
busStops
0..
0..
BusStop
BusList
Village
waiting
0..
passengers
Bus
PersonList
Person
0..
31Filter 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
32Why 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
334 Adaptive Programming
- How can we use strategies to program?
- Need to do useful work besides traversing
visitors - Incremental behavior composition using visitors
34Writing Adaptive Programs with Strategies
(DJpure Java)
String WPStrategyfrom BusRoute through BusStop
to Person
class BusRoute int countPersons(TraversalGraph
WP) Integer result (Integer)
WP.traverse(this, new Visitor() int r
public void before(Person host) r
public void start() r 0 public
Object getReturnValue() return new
Integer ( r) ) return
result.intValue()
35Writing Adaptive Programs with Strategies
(DJpure Java)
String WPStrategyfrom BusRoute through BusStop
to Person
// Prepare the traversal for the current class
graph ClassGraph classGraph new
ClassGraph() TraversalGraph WPTraversal new
TraversalGraph (WPStrategy, classGraph) int
r aBusRoute.countPersons(WPTraversal)
36Writing Adaptive Programs with Strategies
(DJpure Java)
class Utility static int
countPersons(ObjectGraphSlice countSlice)
Integer result (Integer)
countSlice.traverse(new Visitor() int r
public void before(Person host) r
public void start() r 0 public
Object getReturnValue() return new
Integer ( r) ) return
result.intValue()
37Writing Adaptive Programs with Strategies
(DJpure Java)
String WPStrategyfrom BusRoute through BusStop
to Person
// Prepare the slice for the current class
graph ClassGraph classGraph new
ClassGraph() ObjectGraph objectGraph new
ObjectGraph(aBusRoute) ObjectGraphSlice
whatToCount new ObjectGraphSlice(objectGraph,
WPStrategy) int r Utility.countPersons(whatT
oCount)
38Writing Adaptive Programs with Strategies
(DemeterJ)
strategy from BusRoute through BusStop to Person
BusRoute traversal waitingPersons(PersonVis
itor) through BusStop to Person //
from is implicit int printCountWaitingPersons(
) // traversal/visitor weaving
waitingPersons(PrintPersonVisitor) PrintPersonVis
itor before Person
PersonVisitor init r 0
Extension of Java keywords traversal
init through bypassing to before after etc.
39Taxi 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
40Programming in Large Families
Two adaptive programs
A1
Class Graphs
A2
A1 family
Object-Oriented Programs
A2 family
41Adaptive Programming
Strategy
Bold names refer to DJ classes.
is use-case based abstraction of
ClassGraph
defines family of
ObjectGraph
42Adaptive Programming
Strategy
defines traversals of
ObjectGraph
43Adaptive Programming
Strategy
guides and informs
Visitor
44Strategies
BusRoute BusStop Person
- Nodes positive information Mark corner stones
in class graph Overall topology - of collaborating classes. 3 nodes
- from BusRoute
- through BusStop
- to Person
45Strategies
bypassing edges incident with Bus
BusRoute
Person
Edges negative information Delete edges from
class graph.
from BusRoute bypassing Bus to Person
465 Tools for Adaptive Programming
- free and commercial tools available
47Free Tools on WWW
- DJ and AP Library
- Demeter/C
- DemeterJ
- Demeter/StKlos
- Dem/Perl5
- Dem/C
- Dem/CLOS
- Demeter/Object Pascal
last four developed outside our group
48Commercial Tools available on WWW
StructureBuilder from Tendril Software Inc.
(bought by WebGain) has support for DJ style
actions
www.webgain.com
49Benefits 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.
50DemeterJ
www.ccs.neu.edu/research/demeter
- class diagrams
- functionality
- strategies
- visitors
- etc.
Executable Java code for your favorite commercial
Java Software Development Environment
weaver
51DemeterJ in DemeterJ
structure (.cd) class diagrams
focus of this lecture
compiler/ weaver
structure-shy behavior (.beh) strategies
and visitors
structure-shy communication (.ridl) distribution
structure-shy object description (.input, at
runtime)
synchronization (.cool) multi threading
52Cross-cutting in DemeterJ
generated Java program
DemeterJ program
structure-shy functionality
structure
replicated!
synchronization
53AP Studio
- visual development of traversal strategies
relative - to class diagram
- visual feedback about collaborating classes
- visual development of annotated UML class diagrams
54Strengths of DemeterJ
- Theory
- Novel algorithms for strategies
- Formal semantics
- correctness theorems
- Practice
- Extensive feedback (8 years)
- Reflective implementation
55Meeting the Needs
- DemeterJ
- Easier evolution of class diagrams (with strategy
diagrams) - Easier evolution of behavior (with visitors)
- Easier evolution of objects (with sentences)
56Real Life
- Used in several commercial projects
- Implemented by several independent developers
- Used in several courses, both academic and
commercial
57Summary
- What has been learned Simple UML class diagrams,
strategies and adaptive programs - How can you apply
- DemeterJ takes adaptive programs as input
- Document object-oriented programs with strategies
- Design in terms of traversals and visitors
58Where to get more information
- Adaptive Programming book
- DJ home page
- UML Distilled
- DemeterJ home page
- Course home page
- www.ccs.neu.edu/research/demeter/
59Feedback
- Request feedback of training session
60A Short Introduction to Adaptive Programmingfor
Java Programmers
- Karl Lieberherr
- Doug Orleans
61Concepts needed(DJ classes)
- ClassGraph
- Strategy
- TraversalGraph
- ObjectGraph
- ObjectGraphSlice
- Visitor
62Adaptive Programming
Strategy
Bold names refer to DJ classes.
is use-case based abstraction of
ClassGraph
defines family of
ObjectGraph
63Adaptive Programming
Strategy
defines traversals of
ObjectGraph
results in
ObjectGraphSlice
64Adaptive Programming
Strategy
guides and informs
Visitor
65Collaborating Classes
find all persons waiting at any bus stop on a bus
route
busStops
BusRoute
BusStopList
OO solution one method for each red class
buses
0..
BusStop
BusList
waiting
0..
passengers
Bus
PersonList
Person
0..
66Traversal Strategy
find all persons waiting at any bus stop on a bus
route
from BusRoute through BusStop to Person
busStops
BusRoute
BusStopList
buses
0..
BusStop
BusList
waiting
0..
passengers
Bus
PersonList
Person
0..
67Traversal Strategy
find all persons waiting at any bus stop on a bus
route
from BusRoute through BusStop to Person
busStops
BusRoute
BusStopList
buses
0..
BusStop
BusList
waiting
0..
passengers
Bus
PersonList
Person
0..
68Robustness of Strategy
find all persons waiting at any bus stop on a bus
route
from BusRoute through BusStop to Person
villages
BusRoute
BusStopList
buses
VillageList
busStops
0..
0..
BusStop
BusList
Village
waiting
0..
passengers
Bus
PersonList
Person
0..
69Writing Adaptive Programs with Strategies
(DJpure Java)
String WPStrategyfrom BusRoute through BusStop
to Person
class BusRoute int countPersons(TraversalGraph
WP) Integer result (Integer)
WP.traverse(this, new Visitor() int r
public void before(Person host) r
public void start() r 0 public
Object getReturnValue() return new
Integer ( r) ) return
result.intValue()
70Writing Adaptive Programs with Strategies
(DJpure Java)
String WPStrategyfrom BusRoute through BusStop
to Person
// Prepare the traversal for the current class
graph ClassGraph classGraph new
ClassGraph() TraversalGraph WPTraversal new
TraversalGraph (WPStrategy, classGraph) int
r aBusRoute.countPersons(WPTraversal)
71Writing Adaptive Programs with Strategies
(DJpure Java)
String WPStrategyfrom BusRoute through BusStop
to Person
class BusRoute int countPersons(TraversalGraph
WP) Integer result (Integer)
WP.traverse(this, new Visitor()...)
return result.intValue()
ObjectGraph objectGraph new
ObjectGraph(this, classGraph) ObjectGraphSlice
objectGraphSlice new ObjectGraphSlice(objectG
raph, WP) objectGraphSlice.traverse(visitor)
WP.traverse(this,visitor) ltgt
72ObjectGraph
BusRoute ( ltbusStopsgt BusStopList
BusStop( ltwaitinggt PersonList
Person() Person()) ltbusesgt BusList
Bus( ltpassengersgt PersonList
Person() Person()))
73TraversalGraph
find all persons waiting at any bus stop on a bus
route
from BusRoute through BusStop to Person
busStops
BusRoute
BusStopList
buses
0..
BusStop
BusList
waiting
0..
passengers
Bus
PersonList
Person
0..
74ObjectGraphSlice
BusRoute ( ltbusStopsgt BusStopList
BusStop( ltwaitinggt PersonList
Person() Person()) ltbusesgt BusList
Bus( ltpassengersgt PersonList
Person() Person()))
75Example Short-cut
strategy A -gt B B -gt C
class graph
strategy
A
B
C
A
A
x
traversal graph ???
c
0..1
b
x
c
B
X
0..1
x
b
c
B
X
x
c
C
C
76Short-cut
strategy A -gt B B -gt C
Object graph
Traversal graph
A( ltxgt X( ltbgt B( ltxgt X( ltcgt
C())) ltcgt C()) ltcgt C())
A
start set
x
0..1
b
b
X
X
B
x
x
c
b
B
finish set
C
77Short-cut
- ObjectGraphSlice
- static view
- ObjectGraph, TraversalGraph, start nodes, finish
nodes - dynamic view
- when traverse method gets called
- traversal history node sequence of nodes in
object graph - subgraph of object graph (but only part of the
story) - visualize it by a movie of traverse
78Short-cut
strategy A -gt B B -gt C
Object graph
Traversal graph
A( ltxgt X( ltbgt B( ltxgt X( ltcgt
C())) ltcgt C()) ltcgt C())
A
start set
x
0..1
b
b
X
X
B
x
x
c
b
B
finish set
C
Used for token set and currently active object
79Short-cut
strategy A -gt B B -gt C
Object graph
Traversal graph
A( ltxgt X( ltbgt B( ltxgt X( ltcgt
C())) ltcgt C()) ltcgt C())
A
start set
x
0..1
b
b
X
X
B
x
x
c
b
B
finish set
C
Used for token set and currently active object
80Short-cut
strategy A -gt B B -gt C
Object graph
Traversal graph
A( ltxgt X( ltbgt B( ltxgt X( ltcgt
C())) ltcgt C()) ltcgt C())
A
start set
x
0..1
b
b
X
X
B
x
x
c
b
B
finish set
C
Used for token set and currently active object
81Short-cut
strategy A -gt B B -gt C
Object graph
Traversal graph
A( ltxgt X( ltbgt B( ltxgt X( ltcgt
C())) ltcgt C()) ltcgt C())
A
start set
x
0..1
b
b
X
X
B
x
x
c
b
B
finish set
C
Used for token set and currently active object
82Short-cut
strategy A -gt B B -gt C
Object graph
Traversal graph
A( ltxgt X( ltbgt B( ltxgt X( ltcgt
C())) ltcgt C()) ltcgt C())
A
start set
x
0..1
b
b
X
X
B
x
x
c
b
B
finish set
C
Used for token set and currently active object
83Short-cut
strategy A -gt B B -gt C
Object graph
Traversal graph
A( ltxgt X( ltbgt B( ltxgt X( ltcgt
C())) ltcgt C()) ltcgt C())
A
start set
x
0..1
b
b
X
X
B
x
x
c
b
B
finish set
C
Used for token set and currently active object
After going back to X
84ObjectGraphSlice
strategy A -gt B B -gt C
A( ltxgt X( ltbgt B( ltxgt X( ltcgt
C())) ltcgt C()) ltcgt C())
85Strategy definitionembedded, positive strategies
- Given a graph G, a strategy graph S of G is any
subgraph of the transitive closure of G. - The transitive closure of G(V,E) is the graph
G(V,E), where E(v,w) there is a path from
vertex v to vertex w in G.
86S is a strategy for G
Ft
F
D
D
E
E
B
B
C
C
S
G
A s
A
87Transitive Closure
busStops
BusRoute
BusStopList
buses
0..
BusStop
BusList
waiting
0..
passengers
Bus
PersonList
Person
0..
88Key concepts
Strategy graph and base graph are directed graphs
- Strategy graph S with source s and target t of a
base graph G. Nodes(S) subset Nodes(G) (Embedded
strategy graph). - A path p is an expansion of path p if p can be
obtained by deleting some elements from p. - S defines path set in G as follows
PathSetst(G,S) is the set of all s-t paths in G
that are expansions of any s-t path in S.
89Expansion
S from BusRoute through BusStop to Person
(BR BSL BS PL P) is an expansion of (BR BS P)
busStops
BusRoute
BusStopList
buses
0..
BusStop
BusList
waiting
0..
passengers
Bus
PersonList
Person
0..
90PathSet
S from BusRoute through BusStop to Person
PathSetBusRoute,Person (S,G)(BR BL B PL P),(BR
BSL BS PL P)
busStops
BusRoute
BusStopList
buses
0..
BusStop
BusList
waiting
0..
passengers
Bus
PersonList
Person
0..
91ExpansionPathSet
class graph
strategy A -gt B B -gt C
A
Traversal graph
x
c
0..1
A
start set
b
B
X
x
x
c
0..1
b
b
X
X
C
B
x
x
c
b
B
finish set
C
(A X B X C) is an expansion of (A B
C) PathSetA,C(S,G) (A X B X (B X) C)
92Technical Details
93Combining DJ and DemeterJ
- DJ is a 100 Java solution for adaptive
programming. - DemeterJ has
- XML style data binding facilities code
generation from schema (class dictionary). - Its own adaptive programming language.
- We attempt an optimal integration giving us the
strong advantages of both and only few small
disadvantages.
94Optimal DJ and DemeterJ Integration
- Take all of DJ
- Take all of DemeterJ class dictionary notation
- Take a very tiny bit of DemeterJ adaptive
programming language (basically only part that
allows us to weave methods).
95 Combining DJ and DemeterJ
- Pros (advantages)
- Java class generation from class dictionary
(getters, setters, constructors). - Parser generation.
- Better packaging of Java code into different
files. - MUCH MORE POWERFUL.
- Cons (disadvantages)
- No longer pure Java solution.
- need to learn Demeter notation for class
dictionaries (similar to XML DTD notation). - need to learn how to call DemeterJ and how to use
project files.
96Combining DJ and DemeterJ
- What do we have to learn about DemeterJ?
- Class dictionaries .cd files
- Behavior files .beh files. Very SIMPLE!
- A defines methods of class A
- Project files .prj
- list behavior files .beh that you are using
- Commands
- demjava new, demjava test,
- demjava clean
97Combining DJ and DemeterJ
- What you might forget in class dictionaries that
are used with DJ - import edu.neu.ccs.demeter.dj.
- visitors need to inherit from Visitor
- parts of visitors need to be defined in class
dictionary
98Combining DJ and DemeterJ
- Structuring your files
- put reusable visitors into separate behavior
files. - put each new behavior into a separate behavior
file. Visitors that are not planned for reuse
should also go into same behavior file. Update
the class dictionary with required structural
information for behavior to work. - List all .beh files in .prj file.
99Using DJ
- traverse() returns the visitor 0 return value.
Make sure the casting is done right, otherwise
you get a run-time error. If public Object
getReturnValue() returns an Integer and
traverse() casts it to a Real casting error at
run-time. - Make sure all entries of Visitor array are
non-null.
100AspectJ DJ
- 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
- path set notation using traversal strategies
101AspectJ Observer Pattern Abstract Pointcut
public abstract aspect Subject ... abstract
pointcut stateChanges() after()
stateChanges() for (int i 0 i lt
observers.size() i)
((Observer)observers.elementAt(i)).update()
102AspectJ Observer PatternConcrete Pointcut
aspect ColoredNumberAsSubject extends Subject
of eachobject(instanceof(ColoredNumber))
pointcut stateChanges()
(receptions(void setValue(..))
receptions(void setColor(..)))
103DJ Counting PatternAbstract Pointcut
class BusRoute int countPersons(TraversalGraph
WP) Integer result (Integer)
WP.traverse(this, new Visitor() int r
public void before(Person host) r
public void start() r 0 public
Object getReturnValue() return new
Integer ( r) ) return
result.intValue()
104DJ Counting PatternConcrete Pointcut
// Prepare the traversal for the current class
graph ClassGraph classGraph new
ClassGraph() TraversalGraph WPTraversal new
TraversalGraph (from BusRoute via BusStop,
classGraph) int r aBusRoute.countPersons(WPTr
aversal)
105AP history
- Programming with partial data structures
propagation patterns - Programming with participant graphs
- Programming with object slices
- partial data structures all the constraints
imposed by visitors
106Iterators
- Traverser class, trave
- Coroutines to simulate continuation
107Dynamic features
- When class is deined dynamically from a byte
array (e.g., from etwork) GlassGraph.addClass.
Class returned by class loader. - addPackage
- no reflection for packages
108Interfaces
interface PointI void move(int dx, int
dy) interface RealPointI extends PointI
void move(float dx, float dy) void move(double
dx, double dy) class MyPoint implements
RealPointI void move(int dx, int dy)
void move(float dx, float dy) void
move(double dx, double dy)