From 61dc018cc8339aea21e73c533a1b9e67ff9938ee Mon Sep 17 00:00:00 2001 From: ddp Date: Fri, 26 Jul 2002 12:36:05 +0000 Subject: [PATCH] Adding SortedDependencies git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/sandbox/graph2/trunk@144627 13f79535-47bb-0310-9956-ffa450edef68 --- build.xml | 216 ++++++++--- project.xml | 3 +- .../commons/graph/algorithm/search/DFS.java | 22 ++ .../graph/domain/basic/DirectedGraphImpl.java | 10 +- .../domain/dependency/DependencyGraph.java | 347 +++++++----------- .../domain/dependency/DependencyVisitor.java | 8 + 6 files changed, 343 insertions(+), 263 deletions(-) diff --git a/build.xml b/build.xml index 67718ba..7feeb26 100644 --- a/build.xml +++ b/build.xml @@ -12,68 +12,194 @@ + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + - - - + + + + - - - + + + + - - - + + + + - + + + + + + + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/project.xml b/project.xml index efa996a..ffd961e 100644 --- a/project.xml +++ b/project.xml @@ -89,12 +89,13 @@ commons-collections 1.0 - + junit diff --git a/src/java/org/apache/commons/graph/algorithm/search/DFS.java b/src/java/org/apache/commons/graph/algorithm/search/DFS.java index 7525db9..f52cc25 100644 --- a/src/java/org/apache/commons/graph/algorithm/search/DFS.java +++ b/src/java/org/apache/commons/graph/algorithm/search/DFS.java @@ -164,5 +164,27 @@ public class DFS visitor.finishGraph(graph); } + /** + * visit - Visits all nodes in the graph. + */ + public void visit( DirectedGraph graph, Visitor visitor ) { + Iterator vertices = graph.getVertices().iterator(); + while (vertices.hasNext()) { + colors.put( vertices.next(), WHITE ); + } + + visitor.discoverGraph( graph ); + + vertices = graph.getVertices().iterator(); + while (vertices.hasNext()) { + Vertex v = (Vertex) vertices.next(); + + if (colors.get( v ) == WHITE) { + visitVertex( graph, v, visitor ); + } + } + + visitor.finishGraph( graph ); + } } diff --git a/src/java/org/apache/commons/graph/domain/basic/DirectedGraphImpl.java b/src/java/org/apache/commons/graph/domain/basic/DirectedGraphImpl.java index 167480a..baf398c 100644 --- a/src/java/org/apache/commons/graph/domain/basic/DirectedGraphImpl.java +++ b/src/java/org/apache/commons/graph/domain/basic/DirectedGraphImpl.java @@ -72,10 +72,12 @@ import org.apache.commons.graph.exception.*; */ public class DirectedGraphImpl implements DirectedGraph, - WeightedGraph, - MutableDirectedGraph, - InvocationHandler + WeightedGraph, + MutableDirectedGraph, + InvocationHandler { + private Vertex root = null; + private Set vertices = new HashSet(); private Set edges = new HashSet(); private List contracts = new ArrayList(); @@ -264,6 +266,8 @@ public class DirectedGraphImpl private void addVertexI(Vertex v) throws GraphException { + if (root == null) root = v; + vertices.add(v); } diff --git a/src/java/org/apache/commons/graph/domain/dependency/DependencyGraph.java b/src/java/org/apache/commons/graph/domain/dependency/DependencyGraph.java index d8f2fd9..e22f61b 100644 --- a/src/java/org/apache/commons/graph/domain/dependency/DependencyGraph.java +++ b/src/java/org/apache/commons/graph/domain/dependency/DependencyGraph.java @@ -1,214 +1,133 @@ -package org.apache.commons.graph.domain.dependency; - -import org.apache.commons.graph.*; -import org.apache.commons.graph.contract.*; -import org.apache.commons.graph.exception.*; -import org.apache.commons.graph.domain.basic.*; -import org.apache.commons.graph.factory.GraphFactory; -import org.apache.commons.graph.decorator.DDirectedGraph; -import org.apache.commons.graph.dependency.exception.*; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * Description of the Class - */ -public class DependencyGraph - extends DDirectedGraph - implements Acyclic -{ - private GraphFactory factory = new GraphFactory(); - private AcyclicContract acyclic = new AcyclicContract(); - private DependencyVisitor visitor = new DependencyVisitor(); - - private Map vertices = new HashMap(); - - private MutableDirectedGraph DAG = null; - - /** - * Constructor for the DependencyGraph object - */ - public DependencyGraph() - { - super(); - Contract[] dagContracts = new Contract[1]; - dagContracts[0] = acyclic; - DAG = factory.makeMutableDirectedGraph(dagContracts, - false, - null); - setDirGraph(DAG); - } - - /** - * Description of the Method - */ - private void init() - { - } - - /** - * Adds a feature to the Dependencies attribute of the DependencyGraph - * object - */ - public void addDependencies(Object head, - Collection deps) - throws GraphException, - CircularDependencyException - { - DependencyVertex vHead = findVertex(head); - - if (!getVertices().contains(vHead)) - { - DAG.addVertex(vHead); - } - - try - { - Iterator v = deps.iterator(); - - while (v.hasNext()) - { - DependencyVertex vDep = findVertex(v.next()); - - if (!getVertices().contains(vDep)) - { - DAG.addVertex(vDep); - } - - DAG.addEdge(new Dependency(vHead.getValue(), - vDep.getValue()), - vHead, vDep); - } - } - catch (CycleException ex) - { - throw new CircularDependencyException(ex); - } - } - - /** - * Description of the Method - */ - public DependencyVertex findVertex(Object o) - { - if (vertices.containsKey(o)) - { - return (DependencyVertex) vertices.get(o); - } - else - { - DependencyVertex RC = new DependencyVertex(o); - vertices.put(o, RC); - return RC; - } - } - - /** - * Gets the sortedDependencies attribute of the DependencyGraph object - */ - public List getSortedDependencies(Object head) - { - return visitor.getSortedDependencies(this, findVertex(head)); - } - - /** Retrieve the vertices in a flattened, sorted, valid order. - * - * @return The list of vertices in a valid order. - */ - public List getSortedVertices() - { - Set vertices = new HashSet( getVertices() ); - List sortedDeps = new ArrayList( vertices.size() ); - - Vertex vertex = null; - List deps = null; - - DependencyVisitor visitor = new DependencyVisitor(); - - // While we still have unaccounted-for vertices - - while ( ! vertices.isEmpty() ) - { - // System.err.println( "--------------------------" ); - - // System.err.println( "## vertices = " + dumpVertices( vertices ) ); - // pick a vertex - vertex = (Vertex) vertices.iterator().next(); - - // System.err.println( "## vertex = " + ((Project)((DependencyVertex)vertex).getValue()).getId() ); - - // finds its sorted dependencies (including itself) - deps = visitor.getSortedDependencies( this, vertex ); - - // System.err.println( "## deps = " + dumpVertices( deps ) ); - - DependencyVertex eachVertex = null; - - // for each dependency... - for (Iterator i = deps.iterator(); i.hasNext();) - { - eachVertex = findVertex( i.next() ); - - // if we haven't accounted for the dependency - if ( vertices.contains( eachVertex ) ) - { - // account for it. - vertices.remove( eachVertex ); - - // tag it to the tail end of the sorted list. - sortedDeps.add( eachVertex.getValue() ); - } - } - } - - return sortedDeps; - } - - /* - protected String dumpVertices(Collection vertices) - { - StringBuffer buf = new StringBuffer(); - - buf.append( "{" ); - - Iterator vertIter = vertices.iterator(); - - Object eachVertex = null; - DependencyVertex vertex = null; - - while ( vertIter.hasNext() ) - { - eachVertex = (Object) vertIter.next(); - - if ( eachVertex instanceof DependencyVertex) - { - vertex = (DependencyVertex) eachVertex; - } - else - { - vertex = findVertex( eachVertex ); - } - - buf.append( ((Project)vertex.getValue()).getId() ); - - if ( vertIter.hasNext() ) - { - buf.append( ", " ); - } - } - - buf.append( "}" ); - - return buf.toString(); - } - */ -} - - +package org.apache.commons.graph.domain.dependency; + +import org.apache.commons.graph.*; +import org.apache.commons.graph.contract.*; +import org.apache.commons.graph.exception.*; +import org.apache.commons.graph.domain.basic.*; +import org.apache.commons.graph.factory.GraphFactory; +import org.apache.commons.graph.decorator.DDirectedGraph; +import org.apache.commons.graph.dependency.exception.*; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Description of the Class + */ +public class DependencyGraph + extends DDirectedGraph + implements Acyclic +{ + private GraphFactory factory = new GraphFactory(); + private AcyclicContract acyclic = new AcyclicContract(); + private DependencyVisitor visitor = new DependencyVisitor(); + + private Map vertices = new HashMap(); + + private MutableDirectedGraph DAG = null; + + /** + * Constructor for the DependencyGraph object + */ + public DependencyGraph() + { + super(); + Contract[] dagContracts = new Contract[1]; + dagContracts[0] = acyclic; + DAG = factory.makeMutableDirectedGraph(dagContracts, + false, + null); + setDirGraph(DAG); + } + + /** + * Description of the Method + */ + private void init() + { + } + + /** + * Adds a feature to the Dependencies attribute of the DependencyGraph + * object + */ + public void addDependencies(Object head, + Collection deps) + throws GraphException, + CircularDependencyException + { + DependencyVertex vHead = findVertex(head); + + if (!getVertices().contains(vHead)) + { + DAG.addVertex(vHead); + } + + try + { + Iterator v = deps.iterator(); + + while (v.hasNext()) + { + DependencyVertex vDep = findVertex(v.next()); + + if (!getVertices().contains(vDep)) + { + DAG.addVertex(vDep); + } + + DAG.addEdge(new Dependency(vHead.getValue(), + vDep.getValue()), + vHead, vDep); + } + } + catch (CycleException ex) + { + throw new CircularDependencyException(ex); + } + } + + /** + * Description of the Method + */ + public DependencyVertex findVertex(Object o) + { + if (vertices.containsKey(o)) + { + return (DependencyVertex) vertices.get(o); + } + else + { + DependencyVertex RC = new DependencyVertex(o); + vertices.put(o, RC); + return RC; + } + } + + /** + * Gets the sortedDependencies attribute of the DependencyGraph object + */ + public List getSortedDependencies(Object head) + { + return visitor.getSortedDependencies(this, findVertex(head)); + } + + /** + * Retrieve the vertices in a flattened, sorted, valid order. + * + * @return The list of vertices in a valid order. + */ + public List getSortedDependencies() + { + return visitor.getSortedDependencies( this ); + } +} + + + diff --git a/src/java/org/apache/commons/graph/domain/dependency/DependencyVisitor.java b/src/java/org/apache/commons/graph/domain/dependency/DependencyVisitor.java index 0b13523..b9e1f03 100644 --- a/src/java/org/apache/commons/graph/domain/dependency/DependencyVisitor.java +++ b/src/java/org/apache/commons/graph/domain/dependency/DependencyVisitor.java @@ -72,5 +72,13 @@ public class DependencyVisitor dfs.visit(dg, root, this); return deps; } + + public synchronized List + getSortedDependencies(DependencyGraph dg ) + { + deps = new LinkedList(); + dfs.visit( dg, this ); + return deps; + } }