Title: DJ: traversalvisitorstyle programming in Java
1DJ traversal-visitor-style programming in Java
- Josh Marshall/ Doug Orleans
- Want to add more on
- traversal through collections
- and Aspectual Components
2History
- Fall 1996 Lars Hansen early proposal
- Johan Ovlinger TAO
- Traversal automata by Wand and Ovlinger
- Doug Orleans AP Library makes DJ possible
- David Wagstaff (Novell) Pure Java AP
3Motivation
- Better to have an easy to use less powerful
system than a harder to use more powerful system
to get initial users. - Build on Demeter/Java experience
- through AP Library
- visitor organization
- Extend Demeter/Java generalize traversals find,
add, delete, ...
4DJ Features
- Name traversals
- Provide abstract visitor class
- Create class graph from Java source
- Compute traversals dynamically
5Comparison
- DJ
- interprets traversal strategies slower, dynamic
- only requires DJ package
- Demeter/Java
- generates traversal code faster, static
- new programming language
6Good for DJ over Java
- Extending traversals for collections (b..)
- returning a single object instead of a collection
(Find) - modifying the collection (Add, Delete)
- implementations are different for different
implementations of collections
7Embedding versus path control
- Embedding does mapping work once but requires
mapping construct - Path control may repeat mapping work
8Reuse of an AC written with DJ
- Copy AC
- rename everything
- change path control assumes original AC is
written with path control - Leads to duplication of code
9UML Class Diagram
BookName
bN
Book
Copy
Library
copies
books
0..
0..
avail
book
0..
checkOutItems
users
0..
uId
User
Boolean
UserId
10UML Class Diagram
BookName
bN
Book
Copy
Library
copies
books
0..
book
0..
avail
copy
checkOutItems
0..
users
CheckOutItem
0..
uId
User
Boolean
UserId
11How can addition be expressed with ACs
- Required participant graph
- Provided modified participant graph has all the
paths in original but may have more
12Interaction schema
Checkout a book for a user
- CheckOut
- Library(
- Find-gt Book Find(availtrue)-gt cCopy
- Find-gt uUser)
- addCopy
- uUser Add-gt Copy
- cCopy Set(avail,false)-gt Boolean
-
- //participants Library, Book, Copy, User
13Compiler asks for information or determines it by
rules
- CheckOut (Library lib,BookName bN, UserId uId)
- libLibrary(
- Find(this.bNbN)-gt Book
- Find(availtrue)-gtcCopy
- Find(this.uIduId)-gt uUser)
- addCopy
- uUser Add(c)-gt Copy
- cCopy Set(avail,false)-gt Boolean
-
14Compiler asks for information or determines it by
rules
- class Library
- Copy CheckOut
- (Library lib,BookName bN, UserId uId)
- Book b lib.Find(Book,bN,bN)
- Copy c b.Find (Copy,avail,true)
- User u lib.Find(User,uId,uId)
- u.Add(Copy,c)
- c.Set(avail,false) return c
-
15For DJ
- class Library
- Copy CheckOut
- (Library lib,BookName bN, UserId uId)
- Book b cg.Find(lib,to Book,bN,bN)
- Copy c cg.Find (b,to Copy,avail,true)
- User u cg.Find(lib,to User,uId,uId)
- cg.Add(u,to Copy,c)
- cg.Set(c,to Boolean,avail,false)
- return c
-
16Dougs proposal
- Book b cg.fetch(
- lib, new Strategy (to Book),
- new Predicate()
- boolean match(Object obj)
- return((Book) obj).get_isbn()
- 678)
-
-
- )
Instead of Book b cg.Find(lib,to
Book,isbn,678)
17Dougs proposal
More flexible but more verbose
- Book b cg.fetch(
- lib, new Strategy (to Book),
- new Predicate()
- boolean match(Object obj)
- return((Book) obj).get_isbn()
- 678)
-
-
- )
Instead of Book b cg.Find(lib,to
Book,isbn,678) interface Predicate
boolean match(Object) Anonymous object of
anonymous class
18For DJ/paper
- Book b cg.Find(lib,to Book,bN,bN)
- Copy c cg.Find (b,to Copy,avail,true)
- User u cg.Find(lib,to User,uId,uId)
- cg.Add(u,to Copy,c)
- Copy cg.Delete(u,to Copy, avail,true)
- cg.Set(c,to Boolean,avail,false)
- Boolean cg.Get(c,to Boolean,avail)
- int cg.Traverse(company,to Salary,v)
19For DJ/paper/constraints
- 0.., 1..
- Book b cg.Find(lib,to Book,bN,bN)
- cg.Add(u,to Copy,c)
- cg.Delete(u,to Copy, avail,true)
- cg.Traverse(company,to Salary,v)
- cg.Gather(company,to Salary)
- 0..1, 1..1
- cg.Set(c,to Boolean,avail,false)
- cg.Get(c,to Boolean,avail)
20For DJ/paper/constraints
- 0.., 1..
- Book cg.Find(lib,to Book,bN,bN)
- Find in library lib a book with data member
- bN bN.
- void cg.Add(u,to Copy,c)
- Add to user u a copy c
- Copy cg.Delete(u,to Copy, avail,true)
- Delete from user u a copy with avail true
-
21For DJ/paper/constraints
- 0.., 1..
- int cg.Traverse(company,to Salary,v)
- traverse from company to Salary and perform
- visiting actions of v.
- Vector cg.Gather(company,to Salary)
- collect all Salary-objects reachable from
- company
- 0..1, 1..1
- Boolean cg.Set(c,to Boolean,avail,false)
- Boolean cg.Get(c,to Boolean,avail)
22Aspect Language dont use
- CheckOut
- Find(this.bNbN)-gt Book
- Find(availtrue)-gtCopy
- Find(this.uIduId)-gt User
- addCopy
- uUser Add(c)-gt Copy
-
23Compiler asks for information or determines it by
rules
Definition an interaction schema is a
sequence of navigation statements of the
form o1T1 -Action1-gt o2T2 -Action2-gt o3T3 ...
- CheckOut (lib,bN,uId)
- libLibrary(
- Find(this.bNbN)-gt Book
- Find(availtrue)-gt cCopy
- Find(this.uIduId)-gt uUser)
- addCopy only one choice
- uUser Add(c)-gt Copy
- cCopy Set(avail,false)-gt Boolean
-
24Advantages
- High-level description of behavior in terms of an
ideal UML class diagram - Behavior can be adapted to many concrete class
diagrams - Some details of action parameters may be filled
in under compiler control
25Generalized Traversals
- Library Find-gt Book
- Traversal from Library to Book must have upper
cardinality gt 1, e.g., 0.., 1... - Compiler will ask for properties to select a
book. Rule if an object of the class of a data
member of Book is available, it will choose that
one as default
26Generalized traversals
- libLibrary Find-gt bBook
- DJ
- Book b cg.Find(lib,
- new Strategy(from Library to Book),
- property bN,bookName)
- Book b cg.Find(lib,
- new Strategy(from Library to Book),
- property isbn,isbnNumber)
27Connection actions/class graph
- libLibrary Find-gt bBook
- uUser Add(c)-gt Copy
- libLibrary Delete-gt bBook
- 0.., 1..
28Connection actions/class graph
- cCopy Set(avail,false)-gt Boolean
- cCopy Get(avail)-gt bBoolean
- cCopy Fetch(avail)-gt bBoolean
- 1..1, 0..1
- --- traverse a little different
- cCompany Traverse(v1)-gt rResult
- traverses to all classes mentioned in v1
- and performs visiting action. There must be a
path to each such class?
29Generalized traversals
- libLibrary Find-gt bBook
- DJ
- Book b cg.Find(lib,
- new Strategy(from Library to Book),
- property author,authorName)
- Find is like a generalized fetch
30Generalized traversals
- uUser Add(c)-gt Copy
- cg.Add(u,new Strategy
- (from User to Copy), c)
- Expects a unique path from User to Copy
- that is insertable and it adds c at
- end. Insertable means ...
31New Operations for DJ
- Find, Delete, Add
- Set, GetFetch
- Traverse, Gather
- cCopy Set(avail,false)-gt Boolean
- cg.Set(c, new Strategy(
- from Copy through -gt ,avail,
- to Boolean), false)
32DJ dealing with vectors
- How can DJ deal with Java vectors and other
collection classes? - A B C. //from A via B to C
- B Vector.
- R Q. Q C.
- S T. T .
33DJ dealing with vectors
- A B C. //from A via B to C
- B Vector.
- R Q. Q C.
- S T. T .
Q
A
C
R
T
S
Vector
B
34DJ dealing with vectors
- A B C. //from A via B to C
- B Vector.
- R Q. Q C.
- S T. T .
Q
A
C
R
T
S
Vector
B
35Dealing with Vector
- Enlarge the class graph by drawing a subclass
edge from Vector to every node. - Might create many edges
- Use the generality of the strategy compilation
algorithm
36Parametric types
- Essential for code reuse
- program fragment viewed in different ways
- intersection/union types derived automatically
analysis - principal type expect less for free variables,
more for result
37- Subtraversal s as argument
s.apply(a)