o adding a routine that gives something like an overall build order.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/sandbox/graph2/trunk@144626 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
jvanzyl 2002-07-26 12:29:39 +00:00
parent 1c638f36d8
commit 1ac5a48e03
3 changed files with 108 additions and 10 deletions

3
maven.xml Normal file
View File

@ -0,0 +1,3 @@
<project default="java:jar">
</project>

View File

@ -2,9 +2,9 @@
<project>
<pomVersion>3</pomVersion>
<name>commons-graph</name>
<id>Graph</id>
<currentVersion>0.8</currentVersion>
<id>commons-graph</id>
<name>Graph</name>
<currentVersion>0.8.1</currentVersion>
<organization>
<name>Apache Software Foundation</name>
@ -23,7 +23,7 @@
<url>http://jakarta.apache.org/commons/graph/</url>
<repository>
<connection>scm:cvs:anoncvs@cvs.apache.org:/home/cvspublic:jakarta-commons/graph2</connection>
<connection>scm:cvs:anoncvs@cvs.apache.org:/home/cvspublic:jakarta-commons-sandbox/graph2</connection>
<url>http://cvs.apache.org/viewcvs/jakarta-commons-sandbox/graph2</url>
</repository>
@ -110,7 +110,7 @@
<unitTest>
<includes>
<include>**/*Test.class</include>
<include>**/*Test.java</include>
</includes>
</unitTest>

View File

@ -8,11 +8,14 @@ import org.apache.commons.graph.factory.GraphFactory;
import org.apache.commons.graph.decorator.DDirectedGraph;
import org.apache.commons.graph.dependency.exception.*;
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.Iterator;
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
@ -49,7 +52,7 @@ public class DependencyGraph
private void init()
{
}
/**
* Adds a feature to the Dependencies attribute of the DependencyGraph
* object
@ -114,6 +117,98 @@ public class DependencyGraph
{
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();
}
*/
}