tools1999 - College of Computer and Information Science

Download Report

Transcript tools1999 - College of Computer and Information Science

Interaction Schemata:
Compiling Interactions to Code
Neeraj Sangal, Edward Farrell, Karl Lieberherr, and David H. Lorenz
Tendril Software, Inc,
Westford, MA
http://www.tendril.com
College of Computer Science
Northeastern University
Boston, MA 02115
TOOLS USA '99
Outline
 Motivation
 Generate Code From Interaction Diagrams
 The Problem
 Diagrams are not precise
 Concept Formulization
 Interaction Schemata
 Two Implementation Approaches
 Code generation approach
 Structure Builder (SB)
 Generative approach
 DJ (Demeter Java)
Northeastern University
Motivation: Round Trip
Northeastern University
Interactions Diagrams
// Class: Library
// File: Library.java
/**
* (c) Northeastern University
* @param book
* @author dl
* @SBGen Generated Method (2)
*/
void upwardTransportationExample(Book book) {
// SBgen: Calls generated method on Book (2)
SBObject sbsecondUser = new SBObject();
User firstUser = book.getNextUser(sbsecondUser);
User secondUser = (User)sbsecondUser.getValue();
// SBgen: End Call
// SBgen: Action Execute method on User (5)
firstUser.notifyBookReady();
// SBgen: Action Execute method on User (6)
secondUser.alertYouAreNext();
}
// Class: Book
// File: Book.java
/** @SBGen Generated Method (4), created by Library.up(Book)
(Library.2,-2) */
User getNextUser(SBObject sbsecondUser) {
// SBgen: Action Execute method on Book (3)
User firstUser = getFirstUser();
// SBgen: Action Execute method on Book (4)
 Goal: Generating Code From
Interaction Diagrams
Northeastern University
The Problem
 Problem
 UML interaction diagrams are not precise enough to
prescribe code.
 UML interaction diagrams are difficult to keep up-to-date with
the code.
 Solution Approaches
 Code generation approach
 Structure Builder: A Java design tool
 Method generated with mark-ups
 Generic approach
 DJ: A research project
 Interactive traversals using JGL and Reflection
Northeastern University
What's Missing in
Interaction Diagrams




No object "book-keeping"
Ignores scope and visibility issues
Ignores object transportation issues
Other details: method parameters, return types,
collections to iterate over, etc.
 Sub-Goal: Make Interaction Diagrams
Complete
and then
Code Generation
Approach:
Structure Builder (SB)
or
Generative
Approach:
DJ (Demeter Java)
Northeastern University
Example: Library System
Class
Association
Role
Cardinality
Northeastern University
Object "Book-Keeping"
 Library.checkIn(Copy copy, UID uid)
Where did user
come from?
What is the signature of
removeCopy?
 Generated methods must have
correct signature and return type.
Northeastern University
Scope and Visibility
What is the
scope of
book?
book is out of
scope!
 Objects have scope
even in Interaction Diagrams
Northeastern University
Downward Object Transportation
Where did copy
come from?
Returned by
findCopy()
Used by
release()
 copy must be transported down
through removeCopy()
Northeastern University
Upward Object Transportation
Where did resUser
come from?
Generated by
getFirstOnReservationList()
Called for
notifyBookReady()
 resUser must be transported up
through getNextUser()
Northeastern University
Object Transportation - Wrapper
firstUser and secondUser
have to be transported up
through getNextUser()
 Second object is passed back
through a wrapper.
Northeastern University
Textual Representation

Nested Methods
a.m1() {
b.m2()
}

Conditional
if (test) {
a.m1();
}
Northeastern University
Formalization of Actions
 Syntax for actions
 [interactor  interactor  ...].actionName(exp1,exp2,...)
return(type1 retexp1, type2 retexp2,...) {...}
 Basic actions
 Method call
 Conditional
The scope of each returned variable
is limited to being inside the
innermost enclosing conditional or
iterative action.
Can contain
other actions
 Iteration
 Additional predefined actions on collections
 Find
 Add
 Remove
 User defined actions
 Anything
Northeastern University
Sequence Diagram for CheckIn
Northeastern University
Interaction Schema for CheckIn
Library.checkIn(UID uid, Copy copyId)
{
[libraryusersuid].find(uid'current == uid)
return (User users'current as theUser)
[theUserborrowscopyId].remove(copyId'current == copyId)
return (Copy borrows'current as theCopy)
[theCopy].getBook()
return (Book book as theBook)
[theBookreserves].remove(reserves'index == 0)
return (User reserves'current as theReserver)
if (theReserver != null) {
[theReserverholds].add(theCopy)
[theReserver].notify(theCopy);
}
}
Northeastern University
Interactions Involve Multiple Classes
Interact-1
C1
C4
C2
C3
C5
Interact-2
C1
Interact-4
Interact-3
C4
C2
C3
C5
Northeastern University
Method Generation Approach
 Four methods generated in
three classes
User checkIn()
{
 Library
 User
 Book
User resUser = null;
int size = reserves.size();
if (size > 0) {
resUser = reserves.elementAt(0);
reserves.removeElementAt(0);
}
class
Book
return resUser;
}
void checkIn(UID uid, Copy copyId)
Copy checkIn(Copy copyId)
{
{
User user = null;
Object tmpKey;
Enumeration i = users.keys();
while(i.hasMoreElements()) {
tmpKey = i.nextElement();
user = (User)users.get(tmpKey);
if (user.uid==uid)
break;
}
Copy copy = user.checkIn(copyId);
Book book = copy.getBook();
User resUser = book.checkIn();
if (resUser != null) {
resUser.checkIn0(copy);
resUser.notify(copy);
}
}
Method
generated
in class
Library
Copy copy = null;
int i, size = borrows.size();
for (i=size-1; i>=0; i--) {
Copy tmpVar = (Copy)borrows.elementAt(i);
if (tmpVar.getId() == copyId) {
copy = tmpVar;
borrows.removeElementAt(i);
break;
}
return copy;
}
class
User
void checkIn0(Copy copy)
{
holds.addElement(copy);
}
Northeastern University
StructureBuilder Approach:
Generated Method Dialog
Available
Objects
Actions
Action
Properties
Action on
Selected
Object
Northeastern University
Object Transportation
firstUser.notifyBookReady()
is missing information: red color
indicates a problem
Northeastern University
Object Transportation
User links the missing
information:
firstUser
Northeastern University
DJ Approach
 Build on Demeter/Java experience
 AP Library, visitor organization
 Create class graph from Java programs
 Generalized traversals: find, add, ...
Better to have an easyto-use less-powerful
system than a harder-touse more-powerful
system.
 Name traversals
 new TravSpec("From Library to User").container(this));
 Compute traversals dynamically
 Container c = new TraversalGraph(Main.classGraph,
new TravSpec(…));
 Main observation
 Actions like add, find, and delete appear in Generic Programming
(GP) as methods of container interfaces (e.g., JGL).
 DJ attempts to reuse those generic algorithms.
Northeastern University
The
checkIn
Method
in
DJ
void checkIn(UID uid, Copy copyId)
{
Container LibraryToUserContainer = new TraversalGraph(
Main.classGraph,
new TravSpec("From Library to User").container(this));
User user = Finding.findIf(
LibraryToUserContainer, new FieldEquals("uid",uid));
if (user == null)
return;
Container UserToCopyContainer = new TraversalGraph(
Main.classGraph,
new TravSpec("From User through borrows to Copy").container(user));
Copy copy = Finding.findIf(
UserToCopyContainer, new FieldEquals("copyId",copyId));
if (copy == null)
return;
Removing.remove(UserToCopyContainer,copy);
Container CopyToUser = new TraversalGraph (…) ;
Container UserToCopyHoldsCont = new TraversalGraph(
Main.classGraph,
new TravSpec("From User through holds to Copy").container(user));
User reserver =
(User)CopyToUser.remove(CopyToUser.elements());
if (reserver != null) {
UserToCopyHoldsCont.add(copy);
reserver.notify(copy);
}
}
Northeastern University
Conclusions
 Possible to generate code from interaction diagrams.
 Interactions must be made complete.
 Possible to preserve simplicity of interaction diagrams and
yet make them complete.
 Interaction diagrams can be made adaptive by addition of
traversal semantics.
 Actions allow programmers to try out different types of data
structures in a structure-shy manner.
 Diagrams like programs need to deal with basic
programming issues like scope, visibility, and
transportation.
Northeastern University
Future




Combine the two approaches.
Domain specific action types.
Improved object caching techniques.
Suggestions for changes to internal data structures for
improved performance.
Northeastern University
Additional Information
 StructureBuilder @ Tendtril Software
 Free product evaluation download
 www.tendril.com
 DJ @ Northeastern University
 Download papers and source code
 www.ccs.neu.edu/research/demeter
Northeastern University
The End
Code for Transporting One Object Up
// Class: Book
// File: Book.java
/** @SBGen Generated Method (4) */
User getNextUser() {
// Class: Library
// Action Execute method on Book (3)
// File: Library.java
User firstUser = getFirstUser();
/**
// Return firstUser
* (c) Northeastern University
return (firstUser);
* @param book
}
* @author dl
* @SBGen Generated Method (2)
*/
void up(Book book) {
// Call generated method on Book (2)
User firstUser = book.getNextUser();
// Action Execute method on User (5)
firstUser.notifyBookReady();
}
Northeastern University
Generated Code for Upward
Transportation of Two Objects
// Class: Library
// File: Library.java
/**
* (c) Northeastern University
* @param book
* @author David Lorenz
* @SBGen Generated Method (2)
*/
void upwardTransportationExample(Book book) {
// SBgen: Calls generated method on Book (2)
SBObject sbsecondUser = new SBObject();
User firstUser = book.getNextUser(sbsecondUser);
User secondUser = (User)sbsecondUser.getValue();
// SBgen: End Call
// SBgen: Action Execute method on User (5)
firstUser.notifyBookReady();
// Class: Book
// File: Book.java
/** @SBGen Generated Method (4), created by
Library.up(Book) (Library.2,-2) */
User getNextUser(SBObject sbsecondUser) {
// SBgen: Action Execute method on Book (3)
User firstUser = getFirstUser();
// SBgen: Action Execute method on Book (4)
User secondUser = getSecondUser;
// SBgen: Returns firstUser
sbsecondUser.setValue(secondUser);
return firstUser;
// SBgen: End Return
}
// SBgen: Action Execute method on User (6)
secondUser.alertYouAreNext();
}
Northeastern University