AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

39
AP/DJ • AP: a generic technology • DJ: a low-learning-curve implementation of AP

Transcript of AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Page 1: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

AP/DJ

• AP: a generic technology

• DJ: a low-learning-curve implementation of AP

Page 2: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Law of Demeter Principle

• Each unit should only have limited knowledge about other units: only about units “closely” related to the current unit.

• “Each unit should only talk to its friends.” “Don’t talk to strangers.”

• Main Motivation: Control information overload. We can only keep a limited set of items in short-term memory.

Page 3: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Law of DemeterFRIENDS

Page 4: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Application to OO

• Unit = method– closely related =

• methods of class of this/self and other argument classes

• methods of immediate part classes (classes that are return types of methods of class of this/self)

• In the following we talk about this application of the Law of Demeter Principle to OO

Page 5: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Rumbaugh and the Law of Demeter

Quote: Avoid traversing multiple links or methods. A method should have limited knowledge of an object model. A method must be able to traverse links to obtain its neighbors and must be able to call operations on them, but it should not traverse a second link from the neighbor to a third class.

Page 6: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Law of Demeter(alternative formulation)

A method should have limited knowledge of an object model.

AP is a reaction to this view of the Law of Demeter

Page 7: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Agreement that LoD Good Idea

• How to follow LoD: good solutions exist but not widely known. Two approaches to following LoD:– OO approach– Adaptive approaches

• DJ

• APPC

• Demeter/Java

Page 8: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

What if your friends are far away?

• You pay them to travel to you or you send an agent to them to collect the information you need.– Approximate Directions: You give them or your

agent directions about what kind of information to collect but you don’t care about accidental details of the travel.

– Detailed Directions: You give them or your agent detailed travel directions.

Page 9: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Adaptive Following LoDFRIENDS

S

A

b

C

X

a:From S to Ab:From S to B c:From S via X to C

a

c

Page 10: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Traversal specifications create friends

• Class ClassGraph is an intermediate class between classes that need to communicate

ClassGraph.fetch(Object o, TravSpec s)

Page 11: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

P1

P2P3

P1

P6P2

P5 P3

P4P1

P2

Adaptive Following of LoD: Key Idea

Page 12: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

DJ

• An implementation of AP using only the DJ library and the Java Generic Library (JGL)

• All programs written in pure Java

• Intended as prototyping tool: makes heavy use of introspection in Java

• Integrates Generic Programming (a la STL) and Adaptive programming

Page 13: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Integration of Generic and Adaptive Programming

• A traversal specification turns an object graph into a container.

• Can invoke 50+ generic algorithms on those containers. Examples: add, delete, find, etc.

• What is gained: genericity not only with respect to data structure implementations but also with respect to class graph

Page 14: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Algorithm Classes

Applying Applies a function to every

element of a sequence

Comparing Mismatches, performs equality

tests, performs lexicographical comparisons

Copying Copies a sequence

Counting Counts unconditionally and conditionally

Filling Fills a sequence with a single element

Filtering Filters a sequence

Finding Finds an object or an element

that satisfies a predicate

Page 15: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Algorithm Classes

Hashing Contains generic hashing algorithms

Heap Makes, pushes, pops, and sorts a heap

MinMax Finds the min and max of a sequence

OrderSetOperations

Contains Generic set operation algorithms

Permuting Cycles through permutations of a sequence

Printing Prints sequences and containers

Removing Removes an object or element that satisfies a predicate

Page 16: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Algorithm Classes

Replacing Replaces an object or element that satisfies a predicate

Reversing Reverses a sequence

Rotating Rotates a sequence

SetOperations Union, intersection, difference, and inclusion

Shuffling Shuffles a sequence

Sorting Sorts a sequence

Swapping Swaps elements or sequences

Transforming Maps one sequence to another

Page 17: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Collections

JGL includes 11 highly optimized data structures that

satisfy most programmer needs. These data structures

have been engineered for performance and ease of use,

and their performance either meets or beats every

other commercially available Java container library. In

addition, all JGL containers work correctly in

multithreaded situations.

Page 18: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Collection Interface

add(Object) Add an object to myself.

clear() Remove all of my objects.

clone() Return a shallow copy of myself.

elements() Return an Enumeration of the components in this container

equals(Object)

Return true if I'm equal to a specified object.

finish() Return an iterator positioned immediately after my last item.

Page 19: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Collection Interface

isEmpty() Return true if I contain no objects.

maxSize() Return the maximum number of objects that I can contain.

remove(Enumeration)

Remove the element at a particular position.

remove(Enumeration, Enumeration)

Remove the elements in the specified range.

size() Return the number of objects that I contain.

start() Return an iterator positioned at my first item.

toString() Return a string that describes me.

Page 20: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

com.objectspace.jgl.algorithms.Filtering

reject(Container, UnaryPredicate)

Reject elements in a container.

reject(InputIterator, InputIterator, UnaryPredicate)

Reject elements in a range.

select(Container, UnaryPredicate)

Select elements in a container.

select(InputIterator, InputIterator, UnaryPredicate)

Select elements in a range.

Page 21: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

com.objectspace.jgl.algorithms.Permuting

nextPermutation(BidirectionalIterator, BidirectionalIterator, BinaryPredicate)

Arrange a sequence to become its next permutation.

nextPermutation(Container, BinaryPredicate)

Arrange a container to become its next permutation.

prevPermutation(BidirectionalIterator, BidirectionalIterator, BinaryPredicate)

Arrange a sequence to become its previous permutation.

prevPermutation(Container, BinaryPredicate)

Arrange a container to become its previous permutation.

Page 22: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

com.objectspace.jgl.algorithms.Transforming

collect(Container, UnaryFunction) Return a container that is the same class as the original and contains the result of applying the given unary function to each element in the original.

transform(Container, Container, Container, BinaryFunction)

Traverse two containers and add the results of invoking a BinaryFunction on corresponding elements to another container.

Page 23: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Sample DJ code

// Find the user with the specified uid

Container libUsers = new

Container(library,

"from Library to User");

User user = libUsers.find("uid", uid);

Page 24: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Methods provided by DJ

• On all objects: traverse, fetch, gather

• On containers: Java Generic Library algorithms (in progress)

• traverse is the important method; fetch and gather are special cases

Page 25: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Traverse method: excellent support for Visitor Pattern

// class ClassGraph

Object traverse(Object o,

TravSpec s, Visitor v);

traverse navigates through Object o following traversal specification s and executing the before and after methods in visitor v

ClassGraph is computed using introspection

Page 26: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Guidelines

IF you plan to use

1 sg with 1 o and several v THEN freeze(cg,sg,o)->ogs

1 sg with several (o or v) THEN freeze(cg,sg)->tg

1 o with several (sg or v) THEN freeze(cg,o)->og

1 tg with 1 o and several v THEN freeze(tg,o)->ogs

1 sg with 1 (o and v) THEN inline

cg class graph

sg strategy graph

tg traversal graph

o object

og object graph

v visitor or generic algorithm

Abreviations

Page 27: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Lack of parameterized classes in Java makes DJ harder to use

• Consider the traversal: from A to B

• Let’s assume that in the class graph between A and B there is a Java collection class. The intent is: A = List(B) which we cannot express in Java. Instead we have: A = Vector(Object). Object : A | B. Let’s assume we also have a class X=B.

Page 28: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Lack of parameterized classes in Java makes DJ harder to use

• We have: A = Vector(Object). Object : A | B | X. X = B.

• If the vector contains an X object it will be traversed!!!

A

X

Object

B

Vector*

Page 29: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

A

X

Object

B

Vector*

A

X B

*

No X-object is allowed to be in A-object

Page 30: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Moral of the story

• If the Collection objects contain only the objects advertised in the nice class graph of the application the traversal done by DJ will be correct.

• However, if the Collection objects contain additional objects (like an X-object) they might be traversed accidentally.

Page 31: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Size of traversal graph

• DJ might create big traversal graphs when collection classes are involved. DJ will plan for all possibilities even though only a small subset will be realized during execution.

• To reduce the size of the traversal graph, you need to use bypassing. In the example: from A bypassing {A,X} to B.

Page 32: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

DJ and collaborations

• Collaborations have a participant graph and are used with connectors.

• DJ allows us to write the behavior of collaborations such that the connectors only need to map roles to classes. Mapping of edges will be done by behavior.

• Traversal strategy graphs can be viewed as participant graphs.

Page 33: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Expressing collaborations in DJ

ClassGraph cg=new ClassGraph();

TraversalGraph tg =

TraversalGraph.compute(cg,

new Strategy("from UMLDiagram via

Abstract to Vertex"));

CountInhRelsVisitor v = new CountInhRelsVisitor();

tg.traverse( cg, v);

Page 34: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Expressing collaborations in DJ

public class CountInhRelsVisitor extends Visitor

{ public int iCount;

public void start() {

System.out.println("begin");

iCount = 0;}

public void finish() {

System.out.println("The total count = " + iCount);

System.out.println("end"); }

public void before(Vertex o) { iCount++; }

…}

Page 35: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Traversal-based collaborations

• Where are the participants? Three of them.–from UMLDiagram via Abstract to Vertex

• Where is the participant graph?

UMLDiagram Abstract Vertex

* *

Page 36: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Traversal-based collaborations

• Where is the behavior of the collaboration? – It is split into three parts:

• The traversal graph. Has the meaning of traversing entire participant graph–from UMLDiagram via Abstract to Vertex

• The visitor and

• The gluing of traversal graph and visitor

Page 37: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Collaborations and DJ

• DJ allows us to conveniently express traversal-based collaborations. – The positive part (delete the bypassing) of a

traversal strategy qualifies as participant graph.– The negative part of a traversal strategy can be

considered a part of the connector.

Page 38: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

Non-traversal based collaborations?

• Unless the participant graph consists only of one node, there is always some traversal in a collaboration.

• Otherwise it would not be a collaboration.

• Writing collaboration behavior in DJ simplifies connectors and makes behavior higher level.

Page 39: AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.

More info

• DJ Home Page