svn:keywords correction

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/sandbox/graph2/trunk@155447 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
dirkv 2005-02-26 13:21:52 +00:00
parent e1665833bb
commit 91b8264007
50 changed files with 5096 additions and 5096 deletions

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph; package org.apache.commons.graph;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,35 +14,35 @@ package org.apache.commons.graph;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import java.util.Set; import java.util.Set;
/** /**
* Description of the Interface * Description of the Interface
*/ */
public interface DirectedGraph public interface DirectedGraph
extends Graph extends Graph
{ {
/** /**
* getInbound( Vertex ) Returns the set of edges which are inbound to the * getInbound( Vertex ) Returns the set of edges which are inbound to the
* Vertex. * Vertex.
*/ */
public Set getInbound(Vertex v); public Set getInbound(Vertex v);
/** /**
* getOutbound( Vertex ) Returns the set of edges which lead away from the * getOutbound( Vertex ) Returns the set of edges which lead away from the
* Vertex. * Vertex.
*/ */
public Set getOutbound(Vertex v); public Set getOutbound(Vertex v);
/** /**
* getSource( Edge ) Returns the vertex which originates the edge. * getSource( Edge ) Returns the vertex which originates the edge.
*/ */
public Vertex getSource(Edge e); public Vertex getSource(Edge e);
/** /**
* getTarget( Edge ) Returns the vertex which terminates the edge. * getTarget( Edge ) Returns the vertex which terminates the edge.
*/ */
public Vertex getTarget(Edge e); public Vertex getTarget(Edge e);
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph; package org.apache.commons.graph;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,11 +14,11 @@ package org.apache.commons.graph;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/** /**
* Description of the Interface * Description of the Interface
*/ */
public interface Edge public interface Edge
{ {
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph; package org.apache.commons.graph;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,40 +14,40 @@ package org.apache.commons.graph;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/** /**
* Graph This is the basic interface for a graph. G = { v, e } * Graph This is the basic interface for a graph. G = { v, e }
* getAdjacentVertices and getAdjacentEdges helps to define the behavior of * getAdjacentVertices and getAdjacentEdges helps to define the behavior of
* Edges. * Edges.
*/ */
import java.util.Set; import java.util.Set;
/** /**
* Description of the Interface * Description of the Interface
*/ */
public interface Graph public interface Graph
{ {
/** /**
* getVertices - Returns the total set of Vertices in the graph. * getVertices - Returns the total set of Vertices in the graph.
*/ */
public Set getVertices(); public Set getVertices();
/** /**
* getEdges - Returns the total set of Edges in the graph. * getEdges - Returns the total set of Edges in the graph.
*/ */
public Set getEdges(); public Set getEdges();
/** /**
* getEdges( Vertex ) - This method will return all edges which touch this * getEdges( Vertex ) - This method will return all edges which touch this
* vertex. * vertex.
*/ */
public Set getEdges(Vertex v); public Set getEdges(Vertex v);
/** /**
* getVertices( Edge ) - This method will return the set of Verticies on * getVertices( Edge ) - This method will return the set of Verticies on
* this Edge. (2 for normal edges, > 2 for HyperEdges.) * this Edge. (2 for normal edges, > 2 for HyperEdges.)
*/ */
public Set getVertices(Edge e); public Set getVertices(Edge e);
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph; package org.apache.commons.graph;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,39 +14,39 @@ package org.apache.commons.graph;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import org.apache.commons.graph.exception.*; import org.apache.commons.graph.exception.*;
/** /**
* Description of the Interface * Description of the Interface
*/ */
public interface MutableDirectedGraph public interface MutableDirectedGraph
extends DirectedGraph extends DirectedGraph
{ {
/** /**
* Adds a feature to the Vertex attribute of the MutableDirectedGraph object * Adds a feature to the Vertex attribute of the MutableDirectedGraph object
*/ */
public void addVertex(Vertex v) public void addVertex(Vertex v)
throws GraphException; throws GraphException;
/** /**
* Adds a feature to the Edge attribute of the MutableDirectedGraph object * Adds a feature to the Edge attribute of the MutableDirectedGraph object
*/ */
public void addEdge(Edge e, public void addEdge(Edge e,
Vertex source, Vertex source,
Vertex target) Vertex target)
throws GraphException; throws GraphException;
/** /**
* Description of the Method * Description of the Method
*/ */
public void removeVertex(Vertex v) public void removeVertex(Vertex v)
throws GraphException; throws GraphException;
/** /**
* Description of the Method * Description of the Method
*/ */
public void removeEdge(Edge e) public void removeEdge(Edge e)
throws GraphException; throws GraphException;
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph; package org.apache.commons.graph;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,49 +14,49 @@ package org.apache.commons.graph;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import org.apache.commons.graph.exception.*; import org.apache.commons.graph.exception.*;
/** /**
* Description of the Interface * Description of the Interface
*/ */
public interface MutableGraph public interface MutableGraph
extends Graph extends Graph
{ {
/** /**
* Adds a feature to the Vertex attribute of the MutableGraph object * Adds a feature to the Vertex attribute of the MutableGraph object
*/ */
public void addVertex(Vertex v) public void addVertex(Vertex v)
throws GraphException; throws GraphException;
/** /**
* Description of the Method * Description of the Method
*/ */
public void removeVertex(Vertex v) public void removeVertex(Vertex v)
throws GraphException; throws GraphException;
/** /**
* Adds a feature to the Edge attribute of the MutableGraph object * Adds a feature to the Edge attribute of the MutableGraph object
*/ */
public void addEdge(Edge e) public void addEdge(Edge e)
throws GraphException; throws GraphException;
/** /**
* Description of the Method * Description of the Method
*/ */
public void removeEdge(Edge e) public void removeEdge(Edge e)
throws GraphException; throws GraphException;
/** /**
* Description of the Method * Description of the Method
*/ */
public void connect(Edge e, Vertex v) public void connect(Edge e, Vertex v)
throws GraphException; throws GraphException;
/** /**
* Description of the Method * Description of the Method
*/ */
public void disconnect(Edge e, Vertex v) public void disconnect(Edge e, Vertex v)
throws GraphException; throws GraphException;
} }

View File

@ -1,12 +1,12 @@
package org.apache.commons.graph; package org.apache.commons.graph;
/** /**
* Description of the Interface * Description of the Interface
*/ */
public interface Named public interface Named
{ {
/** /**
* Gets the name attribute of the Named object * Gets the name attribute of the Named object
*/ */
public String getName(); public String getName();
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph; package org.apache.commons.graph;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,42 +14,42 @@ package org.apache.commons.graph;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import java.util.List; import java.util.List;
/** /**
* Description of the Interface * Description of the Interface
*/ */
public interface Path public interface Path
{ {
/** /**
* Returns the start of the path. * Returns the start of the path.
*/ */
public Vertex getStart(); public Vertex getStart();
/** /**
* Returns the end of the path. * Returns the end of the path.
*/ */
public Vertex getEnd(); public Vertex getEnd();
/** /**
* getVertices() - This returns a list of Vertices, in order as they go from * getVertices() - This returns a list of Vertices, in order as they go from
* Start to End. This includes the Start and End vertex, and will have one * Start to End. This includes the Start and End vertex, and will have one
* more entry than the Edges list. * more entry than the Edges list.
*/ */
public List getVertices(); public List getVertices();
/** /**
* getEdges() - This returns a list of Edges which comprise the path. It * getEdges() - This returns a list of Edges which comprise the path. It
* will have one less than the list of Vertices. * will have one less than the list of Vertices.
*/ */
public List getEdges(); public List getEdges();
/** /**
* size() - This returns the size of the path in terms of number of * size() - This returns the size of the path in terms of number of
* verticies it visits. * verticies it visits.
*/ */
public int size(); public int size();
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph; package org.apache.commons.graph;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,12 +14,12 @@ package org.apache.commons.graph;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/** /**
* Description of the Interface * Description of the Interface
*/ */
public interface UndirectedGraph public interface UndirectedGraph
extends Graph extends Graph
{ {
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph; package org.apache.commons.graph;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,11 +14,11 @@ package org.apache.commons.graph;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/** /**
* Description of the Interface * Description of the Interface
*/ */
public interface Vertex public interface Vertex
{ {
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph; package org.apache.commons.graph;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,16 +14,16 @@ package org.apache.commons.graph;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/** /**
* Description of the Interface * Description of the Interface
*/ */
public interface WeightedEdge public interface WeightedEdge
extends Edge extends Edge
{ {
/** /**
* Gets the weight attribute of the WeightedEdge object * Gets the weight attribute of the WeightedEdge object
*/ */
public double getWeight(); public double getWeight();
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph; package org.apache.commons.graph;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,16 +14,16 @@ package org.apache.commons.graph;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/** /**
* Description of the Interface * Description of the Interface
*/ */
public interface WeightedPath public interface WeightedPath
extends Path extends Path
{ {
/** /**
* Gets the weight attribute of the WeightedPath object * Gets the weight attribute of the WeightedPath object
*/ */
public double getWeight(); public double getWeight();
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph.algorithm.path; package org.apache.commons.graph.algorithm.path;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,354 +14,354 @@ package org.apache.commons.graph.algorithm.path;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/** /**
* AllPairs solves the All Points Shortest Path problem for a DirectedGraph. (If * AllPairs solves the All Points Shortest Path problem for a DirectedGraph. (If
* it is weighted, it will do shortest path by weight. Otherwise shortest path * it is weighted, it will do shortest path by weight. Otherwise shortest path
* by jumps.) Uses Floyd's Algorithm. * by jumps.) Uses Floyd's Algorithm.
*/ */
import java.util.Set; import java.util.Set;
import java.util.Map; import java.util.Map;
import java.util.List; import java.util.List;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.AbstractList; import java.util.AbstractList;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
import org.apache.commons.graph.exception.*; import org.apache.commons.graph.exception.*;
/** /**
* Description of the Class * Description of the Class
*/ */
public class AllPairsShortestPath public class AllPairsShortestPath
{ {
private int pred[][]; private int pred[][];
private double cost[][]; private double cost[][];
private Vertex vArray[]; private Vertex vArray[];
private DirectedGraph graph; private DirectedGraph graph;
private Map vertexIndex = new HashMap();// VERTEX X INTEGER private Map vertexIndex = new HashMap();// VERTEX X INTEGER
/** /**
* Description of the Class * Description of the Class
*/ */
public class EdgeList public class EdgeList
extends AbstractList extends AbstractList
{ {
private DirectedGraph graph; private DirectedGraph graph;
private List vertices; private List vertices;
/** /**
* Constructor for the EdgeList object * Constructor for the EdgeList object
* *
* @param graph * @param graph
* @param vertices * @param vertices
*/ */
public EdgeList(DirectedGraph graph, public EdgeList(DirectedGraph graph,
List vertices) List vertices)
{ {
this.graph = graph; this.graph = graph;
this.vertices = vertices; this.vertices = vertices;
} }
/** /**
* Description of the Method * Description of the Method
*/ */
public int size() public int size()
{ {
return vertices.size() - 1; return vertices.size() - 1;
} }
/** /**
* Description of the Method * Description of the Method
*/ */
public Object get(int index) public Object get(int index)
{ {
Edge RC = null; Edge RC = null;
Vertex source = (Vertex) vertices.get(index); Vertex source = (Vertex) vertices.get(index);
Vertex target = (Vertex) vertices.get(index + 1); Vertex target = (Vertex) vertices.get(index + 1);
Set outboundSet = graph.getOutbound(source); Set outboundSet = graph.getOutbound(source);
if (outboundSet == null) if (outboundSet == null)
{ {
return null; return null;
} }
Iterator outbound = outboundSet.iterator(); Iterator outbound = outboundSet.iterator();
while (outbound.hasNext()) while (outbound.hasNext())
{ {
RC = (Edge) outbound.next(); RC = (Edge) outbound.next();
if (graph.getTarget(RC) == target) if (graph.getTarget(RC) == target)
{ {
break; break;
} }
} }
if (graph.getTarget(RC) != target) if (graph.getTarget(RC) != target)
{ {
return null; return null;
} }
return RC; return RC;
} }
} }
/** /**
* Description of the Class * Description of the Class
*/ */
public class WPath public class WPath
implements WeightedPath implements WeightedPath
{ {
private Vertex start; private Vertex start;
private Vertex finish; private Vertex finish;
private List vertexList = new ArrayList(); private List vertexList = new ArrayList();
private DirectedGraph graph; private DirectedGraph graph;
private double cost; private double cost;
/** /**
* Constructor for the WPath object * Constructor for the WPath object
* *
* @param graph * @param graph
* @param vArray * @param vArray
* @param pred * @param pred
* @param start * @param start
* @param finish * @param finish
* @param cost * @param cost
* @exception GraphException * @exception GraphException
*/ */
public WPath(DirectedGraph graph, public WPath(DirectedGraph graph,
Vertex vArray[], int pred[][], Vertex vArray[], int pred[][],
int start, int finish, double cost) int start, int finish, double cost)
throws GraphException throws GraphException
{ {
this.start = vArray[start]; this.start = vArray[start];
this.finish = vArray[finish]; this.finish = vArray[finish];
this.cost = cost; this.cost = cost;
this.graph = graph; this.graph = graph;
vertexList.addAll(segment(vArray, pred, vertexList.addAll(segment(vArray, pred,
start, finish)); start, finish));
vertexList.add(vArray[finish]); vertexList.add(vArray[finish]);
} }
/** /**
* Returns a List of Vectors in order. Includes the start, but not the * Returns a List of Vectors in order. Includes the start, but not the
* finish. * finish.
*/ */
private List segment(Vertex vArray[], int pred[][], private List segment(Vertex vArray[], int pred[][],
int start, int finish) int start, int finish)
throws GraphException throws GraphException
{ {
int mid = pred[start][finish]; int mid = pred[start][finish];
if (mid == -1) if (mid == -1)
{ {
throw new NoPathException("No SubPath Available: " + throw new NoPathException("No SubPath Available: " +
vArray[start] + " -> " + vArray[start] + " -> " +
vArray[finish]); vArray[finish]);
} }
List RC = new ArrayList(); List RC = new ArrayList();
if (start == finish) if (start == finish)
{ {
return RC; return RC;
} }
if (start == mid) if (start == mid)
{ {
RC.add(vArray[start]); RC.add(vArray[start]);
} }
else else
{ {
RC.addAll(segment(vArray, pred, RC.addAll(segment(vArray, pred,
start, mid)); start, mid));
RC.add(vArray[mid]); RC.add(vArray[mid]);
} }
if (mid == pred[mid][finish]) if (mid == pred[mid][finish])
{ {
} }
else else
{ {
RC.addAll(segment(vArray, pred, RC.addAll(segment(vArray, pred,
mid, finish)); mid, finish));
} }
return RC; return RC;
} }
/** /**
* Gets the weight attribute of the WPath object * Gets the weight attribute of the WPath object
*/ */
public double getWeight() public double getWeight()
{ {
return cost; return cost;
} }
/** /**
* Gets the vertices attribute of the WPath object * Gets the vertices attribute of the WPath object
*/ */
public List getVertices() public List getVertices()
{ {
return vertexList; return vertexList;
} }
/** /**
* Gets the edges attribute of the WPath object * Gets the edges attribute of the WPath object
*/ */
public List getEdges() public List getEdges()
{ {
return new EdgeList(graph, vertexList); return new EdgeList(graph, vertexList);
} }
/** /**
* Gets the start attribute of the WPath object * Gets the start attribute of the WPath object
*/ */
public Vertex getStart() public Vertex getStart()
{ {
return start; return start;
} }
/** /**
* Gets the end attribute of the WPath object * Gets the end attribute of the WPath object
*/ */
public Vertex getEnd() public Vertex getEnd()
{ {
return finish; return finish;
} }
public int size() { public int size() {
return vertexList.size(); return vertexList.size();
} }
} }
/** /**
* Constructor for the AllPairsShortestPath object * Constructor for the AllPairsShortestPath object
* *
* @param graph * @param graph
* @exception NegativeCycleException * @exception NegativeCycleException
*/ */
public AllPairsShortestPath(DirectedGraph graph) public AllPairsShortestPath(DirectedGraph graph)
throws NegativeCycleException throws NegativeCycleException
{ {
update(graph); update(graph);
} }
/** /**
* Description of the Method * Description of the Method
*/ */
private void initIndex(Vertex vArray[]) private void initIndex(Vertex vArray[])
{ {
for (int i = 0; i < vArray.length; i++) for (int i = 0; i < vArray.length; i++)
{ {
vertexIndex.put(vArray[i], new Integer(i)); vertexIndex.put(vArray[i], new Integer(i));
} }
} }
/** /**
* Description of the Method * Description of the Method
*/ */
public void update(DirectedGraph graph) public void update(DirectedGraph graph)
{ {
this.graph = graph; this.graph = graph;
Set vertexSet = graph.getVertices(); Set vertexSet = graph.getVertices();
vArray = (Vertex[]) vertexSet.toArray(new Vertex[vertexSet.size()]); vArray = (Vertex[]) vertexSet.toArray(new Vertex[vertexSet.size()]);
initIndex(vArray); initIndex(vArray);
pred = new int[vArray.length][vArray.length]; pred = new int[vArray.length][vArray.length];
cost = new double[vArray.length][vArray.length]; cost = new double[vArray.length][vArray.length];
for (int i = 0; i < vArray.length; i++) for (int i = 0; i < vArray.length; i++)
{ {
for (int j = 0; j < vArray.length; j++) for (int j = 0; j < vArray.length; j++)
{ {
pred[i][j] = -1; pred[i][j] = -1;
cost[i][j] = Double.POSITIVE_INFINITY; cost[i][j] = Double.POSITIVE_INFINITY;
} }
// First round values need to be in the matrix. // First round values need to be in the matrix.
Iterator edgeSet = graph.getOutbound(vArray[i]).iterator(); Iterator edgeSet = graph.getOutbound(vArray[i]).iterator();
while (edgeSet.hasNext()) while (edgeSet.hasNext())
{ {
Edge e = (Edge) edgeSet.next(); Edge e = (Edge) edgeSet.next();
int j = index(graph.getTarget(e)); int j = index(graph.getTarget(e));
pred[i][j] = i; pred[i][j] = i;
if (graph instanceof WeightedGraph) if (graph instanceof WeightedGraph)
{ {
cost[i][j] = ((WeightedGraph) graph).getWeight(e); cost[i][j] = ((WeightedGraph) graph).getWeight(e);
} }
else else
{ {
cost[i][j] = 1.0; cost[i][j] = 1.0;
} }
} }
// Cost from any node to itself is 0.0 // Cost from any node to itself is 0.0
cost[i][i] = 0.0; cost[i][i] = 0.0;
pred[i][i] = i; pred[i][i] = i;
} }
compute(graph, vArray); compute(graph, vArray);
} }
/** /**
* Description of the Method * Description of the Method
*/ */
private int index(Vertex v) private int index(Vertex v)
{ {
return ((Integer) vertexIndex.get(v)).intValue(); return ((Integer) vertexIndex.get(v)).intValue();
} }
/** /**
* Description of the Method * Description of the Method
*/ */
private void compute(DirectedGraph graph, Vertex vArray[]) private void compute(DirectedGraph graph, Vertex vArray[])
throws NegativeCycleException throws NegativeCycleException
{ {
for (int k = 0; k < vArray.length; k++) for (int k = 0; k < vArray.length; k++)
{// Mid Point {// Mid Point
for (int i = 0; i < vArray.length; i++) for (int i = 0; i < vArray.length; i++)
{// Source {// Source
for (int j = 0; j < vArray.length; j++) for (int j = 0; j < vArray.length; j++)
{// Target {// Target
if (cost[i][k] + cost[k][j] < cost[i][j]) if (cost[i][k] + cost[k][j] < cost[i][j])
{ {
if (i == j) if (i == j)
{ {
throw new NegativeCycleException(); throw new NegativeCycleException();
} }
// It is cheaper to go through K. // It is cheaper to go through K.
cost[i][j] = cost[i][k] + cost[k][j]; cost[i][j] = cost[i][k] + cost[k][j];
pred[i][j] = k; pred[i][j] = k;
} }
} }
} }
} }
} }
/** /**
* Gets the shortestPath attribute of the AllPairsShortestPath object * Gets the shortestPath attribute of the AllPairsShortestPath object
*/ */
public WeightedPath getShortestPath(Vertex start, Vertex end) public WeightedPath getShortestPath(Vertex start, Vertex end)
throws GraphException throws GraphException
{ {
return new WPath(graph, vArray, pred, return new WPath(graph, vArray, pred,
index(start), index(end), index(start), index(end),
cost[index(start)][index(end)]); cost[index(start)][index(end)]);
} }
/** /**
* Determines if a path exists or not. * Determines if a path exists or not.
*/ */
public boolean hasPath( Vertex start, Vertex end ) public boolean hasPath( Vertex start, Vertex end )
{ {
return cost[index(start)][index(end)] < Double.POSITIVE_INFINITY; return cost[index(start)][index(end)] < Double.POSITIVE_INFINITY;
} }
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph.algorithm.path; package org.apache.commons.graph.algorithm.path;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,155 +14,155 @@ package org.apache.commons.graph.algorithm.path;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/** /**
* AllPaths finds for each pair of vertices, {i, j} the set of * AllPaths finds for each pair of vertices, {i, j} the set of
* all potential paths between them. (Unrolling loops by a set * all potential paths between them. (Unrolling loops by a set
* amount. * amount.
*/ */
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
import org.apache.commons.graph.algorithm.util.*; import org.apache.commons.graph.algorithm.util.*;
public class AllPaths public class AllPaths
{ {
private Map allPaths = new HashMap(); // VERTEX PAIR X SET( PATHS ) private Map allPaths = new HashMap(); // VERTEX PAIR X SET( PATHS )
private AllPairsShortestPath apsp = null; private AllPairsShortestPath apsp = null;
private DirectedGraph graph = null; private DirectedGraph graph = null;
public AllPaths( DirectedGraph graph ) { public AllPaths( DirectedGraph graph ) {
this.graph = graph; this.graph = graph;
try { try {
apsp = new AllPairsShortestPath( graph ); apsp = new AllPairsShortestPath( graph );
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
public Iterator findPaths( final Vertex i, public Iterator findPaths( final Vertex i,
final Vertex j ) { final Vertex j ) {
final PathIterator RC = new PathIterator(); final PathIterator RC = new PathIterator();
Runnable run = new Runnable() { Runnable run = new Runnable() {
public void run() { public void run() {
findPaths( RC, i, j, Integer.MAX_VALUE ); findPaths( RC, i, j, Integer.MAX_VALUE );
} }
}; };
Thread thread = new Thread( run ); Thread thread = new Thread( run );
RC.setThread( thread ); RC.setThread( thread );
thread.start(); thread.start();
return RC; return RC;
} }
public void findPaths( PathListener listener, public void findPaths( PathListener listener,
Vertex i, Vertex j, Vertex i, Vertex j,
int maxLength ) { int maxLength ) {
Set workingSet = new HashSet(); Set workingSet = new HashSet();
workingSet.add( new PathImpl( i )); workingSet.add( new PathImpl( i ));
for (int k = 0; k < maxLength; k++) { for (int k = 0; k < maxLength; k++) {
Iterator workingPaths = workingSet.iterator(); Iterator workingPaths = workingSet.iterator();
if (!workingPaths.hasNext()) break; if (!workingPaths.hasNext()) break;
Set newWorkingSet = new HashSet(); Set newWorkingSet = new HashSet();
while (workingPaths.hasNext()) { while (workingPaths.hasNext()) {
PathImpl workingPath = (PathImpl) workingPaths.next(); PathImpl workingPath = (PathImpl) workingPaths.next();
Iterator outbound = Iterator outbound =
graph.getOutbound(workingPath.getEnd()).iterator(); graph.getOutbound(workingPath.getEnd()).iterator();
while (outbound.hasNext()) { while (outbound.hasNext()) {
Edge obEdge = (Edge) outbound.next(); Edge obEdge = (Edge) outbound.next();
if (apsp.hasPath( graph.getTarget( obEdge ), j)) { if (apsp.hasPath( graph.getTarget( obEdge ), j)) {
PathImpl path = PathImpl path =
workingPath.append(graph.getTarget(obEdge), workingPath.append(graph.getTarget(obEdge),
obEdge ); obEdge );
newWorkingSet.add( path ); newWorkingSet.add( path );
if (path.getEnd() == j) { if (path.getEnd() == j) {
listener.notifyPath( path ); listener.notifyPath( path );
} }
} }
} }
} }
workingSet = newWorkingSet; workingSet = newWorkingSet;
} }
} }
/** /**
* getAllPaths will return the set of all possible ways of moving * getAllPaths will return the set of all possible ways of moving
* from i to j using the directed graph AllPaths was initialized * from i to j using the directed graph AllPaths was initialized
* with. * with.
* *
* @deprecated Use findPaths instead. Doesn't work, but code * @deprecated Use findPaths instead. Doesn't work, but code
* may be useful in the near future. * may be useful in the near future.
*/ */
public Set getAllPaths( Vertex i, Vertex j ) { public Set getAllPaths( Vertex i, Vertex j ) {
Set RC = new HashSet(); Set RC = new HashSet();
VertexPair key = new VertexPair( i, j ); VertexPair key = new VertexPair( i, j );
// If we have already started this, return what we // If we have already started this, return what we
// were doing. (May still be in progress.) // were doing. (May still be in progress.)
// //
// If we haven't started, go ahead and start. . . // If we haven't started, go ahead and start. . .
if (allPaths.containsKey( key )) { if (allPaths.containsKey( key )) {
return (Set) allPaths.get( key ); return (Set) allPaths.get( key );
} else { } else {
allPaths.put( key, RC ); allPaths.put( key, RC );
} }
Iterator outbounds = graph.getOutbound(i).iterator(); Iterator outbounds = graph.getOutbound(i).iterator();
while (outbounds.hasNext()) { while (outbounds.hasNext()) {
Edge outbound = (Edge) outbounds.next(); Edge outbound = (Edge) outbounds.next();
if (graph.getTarget( outbound ) == j) { if (graph.getTarget( outbound ) == j) {
RC.add( new PathImpl( i, j, outbound )); RC.add( new PathImpl( i, j, outbound ));
} }
} }
Iterator ks = graph.getVertices().iterator(); Iterator ks = graph.getVertices().iterator();
while (ks.hasNext()) { while (ks.hasNext()) {
Vertex k = (Vertex) ks.next(); Vertex k = (Vertex) ks.next();
if (k != i && k != j) { if (k != i && k != j) {
appendPaths( RC, appendPaths( RC,
getAllPaths( i, k ), getAllPaths( i, k ),
getAllPaths( k, j )); getAllPaths( k, j ));
} }
} }
allPaths.put( key, RC ); allPaths.put( key, RC );
return RC; return RC;
} }
public void appendPaths( Set RC, Set iks, Set kjs ) { public void appendPaths( Set RC, Set iks, Set kjs ) {
Iterator ikPaths = iks.iterator(); Iterator ikPaths = iks.iterator();
while (ikPaths.hasNext()) { while (ikPaths.hasNext()) {
PathImpl ik = (PathImpl) ikPaths.next(); PathImpl ik = (PathImpl) ikPaths.next();
Iterator kjPaths = kjs.iterator(); Iterator kjPaths = kjs.iterator();
while (kjPaths.hasNext()) { while (kjPaths.hasNext()) {
PathImpl kj = (PathImpl) kjPaths.next(); PathImpl kj = (PathImpl) kjPaths.next();
RC.add( ik.append( kj )); RC.add( ik.append( kj ));
} }
} }
} }
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph.algorithm.search; package org.apache.commons.graph.algorithm.search;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,42 +14,42 @@ package org.apache.commons.graph.algorithm.search;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
/** /**
* Description of the Interface * Description of the Interface
*/ */
public interface Visitor public interface Visitor
{ {
/** /**
* Description of the Method * Description of the Method
*/ */
public void discoverGraph(Graph graph); public void discoverGraph(Graph graph);
/** /**
* Description of the Method * Description of the Method
*/ */
public void discoverVertex(Vertex vertex); public void discoverVertex(Vertex vertex);
/** /**
* Description of the Method * Description of the Method
*/ */
public void discoverEdge(Edge edge); public void discoverEdge(Edge edge);
/** /**
* Description of the Method * Description of the Method
*/ */
public void finishEdge(Edge edge); public void finishEdge(Edge edge);
/** /**
* Description of the Method * Description of the Method
*/ */
public void finishVertex(Vertex vertex); public void finishVertex(Vertex vertex);
/** /**
* Description of the Method * Description of the Method
*/ */
public void finishGraph(Graph graph); public void finishGraph(Graph graph);
} }

View File

@ -1,151 +1,151 @@
package org.apache.commons.graph.algorithm.spanning; package org.apache.commons.graph.algorithm.spanning;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
import org.apache.commons.graph.exception.*; import org.apache.commons.graph.exception.*;
import org.apache.commons.graph.decorator.*; import org.apache.commons.graph.decorator.*;
import org.apache.commons.graph.domain.basic.*; import org.apache.commons.graph.domain.basic.*;
import org.apache.commons.graph.algorithm.util.*; import org.apache.commons.graph.algorithm.util.*;
import org.apache.commons.collections.PriorityQueue; import org.apache.commons.collections.PriorityQueue;
import org.apache.commons.collections.BinaryHeap; import org.apache.commons.collections.BinaryHeap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.Comparator; import java.util.Comparator;
public class MinimumSpanningForest public class MinimumSpanningForest
extends UndirectedGraphImpl extends UndirectedGraphImpl
implements UndirectedGraph, implements UndirectedGraph,
WeightedGraph WeightedGraph
{ {
private PriorityQueue queue = null; private PriorityQueue queue = null;
private Map labels = new HashMap(); // VERTEX X LABEL private Map labels = new HashMap(); // VERTEX X LABEL
private Set chords = new HashSet(); // Edges not in MSF. private Set chords = new HashSet(); // Edges not in MSF.
private DDirectedGraph ddg = null; private DDirectedGraph ddg = null;
public class WeightedEdgeComparator public class WeightedEdgeComparator
implements Comparator implements Comparator
{ {
private WeightedGraph graph = null; private WeightedGraph graph = null;
public WeightedEdgeComparator( WeightedGraph graph ) { public WeightedEdgeComparator( WeightedGraph graph ) {
this.graph = graph; this.graph = graph;
} }
public int compare( Object o1, Object o2 ) { public int compare( Object o1, Object o2 ) {
if ((o1 instanceof Edge) && if ((o1 instanceof Edge) &&
(o2 instanceof Edge)) { (o2 instanceof Edge)) {
double val = ( graph.getWeight( (Edge) o1 ) - double val = ( graph.getWeight( (Edge) o1 ) -
graph.getWeight( (Edge) o2 )); graph.getWeight( (Edge) o2 ));
if (val > 0) return 1; if (val > 0) return 1;
if (val == 0) return 0; if (val == 0) return 0;
if (val < 0) return -1; if (val < 0) return -1;
} }
return -1; return -1;
} }
} }
public MinimumSpanningForest( WeightedGraph graph ) { public MinimumSpanningForest( WeightedGraph graph ) {
calculateMSF( true, graph ); calculateMSF( true, graph );
} }
public MinimumSpanningForest( boolean isMin, WeightedGraph graph ) { public MinimumSpanningForest( boolean isMin, WeightedGraph graph ) {
calculateMSF( isMin, graph ); calculateMSF( isMin, graph );
} }
protected Label findLabel( Vertex v ) { protected Label findLabel( Vertex v ) {
return (Label) labels.get( v ); return (Label) labels.get( v );
} }
protected boolean connectsLabels( Graph graph, protected boolean connectsLabels( Graph graph,
Edge e ) Edge e )
{ {
Label first = null; Label first = null;
Label second = null; Label second = null;
Iterator vertices = graph.getVertices( e ).iterator(); Iterator vertices = graph.getVertices( e ).iterator();
if (vertices.hasNext()) { if (vertices.hasNext()) {
first = findLabel( (Vertex) vertices.next() ); first = findLabel( (Vertex) vertices.next() );
} else { return false; } } else { return false; }
if (vertices.hasNext()) { if (vertices.hasNext()) {
second = findLabel( (Vertex) vertices.next() ); second = findLabel( (Vertex) vertices.next() );
} else { return false; } } else { return false; }
if (vertices.hasNext()) { if (vertices.hasNext()) {
throw new HyperGraphException("Unable to compute MSF on Hypergraph."); throw new HyperGraphException("Unable to compute MSF on Hypergraph.");
} }
return !first.getRoot().equals( second.getRoot() ); return !first.getRoot().equals( second.getRoot() );
} }
protected void addEdge( WeightedGraph graph, protected void addEdge( WeightedGraph graph,
Edge edge ) { Edge edge ) {
addEdge( edge ); addEdge( edge );
chords.remove( edge ); chords.remove( edge );
Iterator vertices = graph.getVertices( edge ).iterator(); Iterator vertices = graph.getVertices( edge ).iterator();
Label prime = null; Label prime = null;
if (vertices.hasNext()) { if (vertices.hasNext()) {
Vertex p = (Vertex) vertices.next(); Vertex p = (Vertex) vertices.next();
prime = findLabel( p ); prime = findLabel( p );
connect( edge, p ); connect( edge, p );
} }
while (vertices.hasNext()) { while (vertices.hasNext()) {
Vertex v = (Vertex) vertices.next(); Vertex v = (Vertex) vertices.next();
Label l = findLabel( v ); Label l = findLabel( v );
l.setRoot( prime ); l.setRoot( prime );
connect( edge, v ); connect( edge, v );
} }
setWeight( edge, graph.getWeight( edge )); setWeight( edge, graph.getWeight( edge ));
} }
protected void calculateMSF( boolean isMin, protected void calculateMSF( boolean isMin,
WeightedGraph graph ) { WeightedGraph graph ) {
PriorityQueue queue = PriorityQueue queue =
new BinaryHeap( isMin, new WeightedEdgeComparator( graph )); new BinaryHeap( isMin, new WeightedEdgeComparator( graph ));
chords = new HashSet( graph.getEdges() ); chords = new HashSet( graph.getEdges() );
// Fill the Queue with all the edges. // Fill the Queue with all the edges.
Iterator edges = graph.getEdges().iterator(); Iterator edges = graph.getEdges().iterator();
while (edges.hasNext()) { while (edges.hasNext()) {
queue.insert( edges.next() ); queue.insert( edges.next() );
} }
// Fill the graph we have with all the Vertexes. // Fill the graph we have with all the Vertexes.
Iterator vertices = graph.getVertices().iterator(); Iterator vertices = graph.getVertices().iterator();
while (vertices.hasNext()) { while (vertices.hasNext()) {
Vertex v = (Vertex) vertices.next(); Vertex v = (Vertex) vertices.next();
labels.put( v, new Label() ); labels.put( v, new Label() );
addVertex( v ); addVertex( v );
} }
// Bring the edges out in the right order. // Bring the edges out in the right order.
while (!queue.isEmpty()) { while (!queue.isEmpty()) {
Edge e = (Edge) queue.pop(); Edge e = (Edge) queue.pop();
if ( connectsLabels( graph, e )) { if ( connectsLabels( graph, e )) {
addEdge( graph, e ); addEdge( graph, e );
} }
} }
} }
public Set getChords() { public Set getChords() {
return chords; return chords;
} }
} }

View File

@ -1,39 +1,39 @@
package org.apache.commons.graph.algorithm.util; package org.apache.commons.graph.algorithm.util;
public class Label public class Label
{ {
private Label root = null; private Label root = null;
public Label() { public Label() {
} }
public Label( Label root ) { public Label( Label root ) {
this.root = root.getRoot(); this.root = root.getRoot();
} }
public void setRoot( Label root ) { public void setRoot( Label root ) {
if (this.root == null ) { if (this.root == null ) {
this.root = root.getRoot(); this.root = root.getRoot();
} else { } else {
if (root != this.root) { if (root != this.root) {
this.root.setRoot( root.getRoot() ); this.root.setRoot( root.getRoot() );
this.root = root.getRoot(); this.root = root.getRoot();
} }
} }
} }
public Label getRoot() { public Label getRoot() {
if ((root == null) || if ((root == null) ||
(root == this)) { (root == this)) {
return this; return this;
} else { } else {
root = root.getRoot(); root = root.getRoot();
return root; return root;
} }
} }
} }

View File

@ -1,81 +1,81 @@
package org.apache.commons.graph.algorithm.util; package org.apache.commons.graph.algorithm.util;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
public class PathImpl public class PathImpl
implements Path implements Path
{ {
protected List vertexList = new ArrayList(); protected List vertexList = new ArrayList();
protected List edgeList = new ArrayList(); protected List edgeList = new ArrayList();
public PathImpl( List vertexList, public PathImpl( List vertexList,
List edgeList ) { List edgeList ) {
this.vertexList = vertexList; this.vertexList = vertexList;
this.edgeList = edgeList; this.edgeList = edgeList;
} }
public PathImpl( Vertex start ) { public PathImpl( Vertex start ) {
this.vertexList.add( start ); this.vertexList.add( start );
} }
public PathImpl( Vertex start, public PathImpl( Vertex start,
Vertex end, Vertex end,
Edge edge ) { Edge edge ) {
vertexList = new ArrayList(); vertexList = new ArrayList();
vertexList.add( start ); vertexList.add( start );
vertexList.add( end ); vertexList.add( end );
edgeList = new ArrayList(); edgeList = new ArrayList();
edgeList.add( edge ); edgeList.add( edge );
} }
public PathImpl append( PathImpl impl ) { public PathImpl append( PathImpl impl ) {
List newVertices = new ArrayList( vertexList ); List newVertices = new ArrayList( vertexList );
newVertices.remove( newVertices.size() - 1 ); // Last should be duplicated newVertices.remove( newVertices.size() - 1 ); // Last should be duplicated
newVertices.addAll( impl.getVertices() ); newVertices.addAll( impl.getVertices() );
List newEdges = new ArrayList( edgeList ); List newEdges = new ArrayList( edgeList );
newEdges.addAll( impl.getEdges() ); newEdges.addAll( impl.getEdges() );
return new PathImpl( newVertices, newEdges ); return new PathImpl( newVertices, newEdges );
} }
public PathImpl append( Vertex v, Edge e ) { public PathImpl append( Vertex v, Edge e ) {
List newVertices = new ArrayList( vertexList ); List newVertices = new ArrayList( vertexList );
newVertices.add( v ); newVertices.add( v );
List newEdges = new ArrayList( edgeList ); List newEdges = new ArrayList( edgeList );
newEdges.add( e ); newEdges.add( e );
return new PathImpl( newVertices, newEdges ); return new PathImpl( newVertices, newEdges );
} }
public Vertex getStart() { public Vertex getStart() {
return (Vertex) vertexList.get( 0 ); return (Vertex) vertexList.get( 0 );
} }
public Vertex getEnd() { public Vertex getEnd() {
return (Vertex) vertexList.get( vertexList.size() - 1 ); return (Vertex) vertexList.get( vertexList.size() - 1 );
} }
public List getVertices() { public List getVertices() {
return vertexList; return vertexList;
} }
public List getEdges() { public List getEdges() {
return edgeList; return edgeList;
} }
public int size() { public int size() {
return vertexList.size(); return vertexList.size();
} }
public String toString() { public String toString() {
return vertexList.toString(); return vertexList.toString();
} }
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph.algorithm.util; package org.apache.commons.graph.algorithm.util;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,37 +14,37 @@ package org.apache.commons.graph.algorithm.util;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/** /**
* VertexPair * VertexPair
* *
* This is a tuple of two vertices which is capable * This is a tuple of two vertices which is capable
* of storing things in a Hashtable. * of storing things in a Hashtable.
*/ */
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
public class VertexPair { public class VertexPair {
public Vertex i = null; public Vertex i = null;
public Vertex j = null; public Vertex j = null;
public VertexPair( Vertex i, Vertex j ) { public VertexPair( Vertex i, Vertex j ) {
this.i = i; this.i = i;
this.j = j; this.j = j;
} }
public boolean equals( Object o ) { public boolean equals( Object o ) {
VertexPair vp = (VertexPair) o; VertexPair vp = (VertexPair) o;
return ( this.i == vp.i && return ( this.i == vp.i &&
this.j == vp.j ); this.j == vp.j );
} }
public int hashCode() { public int hashCode() {
return (17 * i.hashCode()) + j.hashCode(); return (17 * i.hashCode()) + j.hashCode();
} }
} }

View File

@ -1,8 +1,8 @@
package org.apache.commons.graph.contract; package org.apache.commons.graph.contract;
/** /**
* Description of the Interface * Description of the Interface
*/ */
public interface Acyclic public interface Acyclic
{ {
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph.contract; package org.apache.commons.graph.contract;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,154 +14,154 @@ package org.apache.commons.graph.contract;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import java.util.Iterator; import java.util.Iterator;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
import org.apache.commons.graph.algorithm.search.*; import org.apache.commons.graph.algorithm.search.*;
import org.apache.commons.graph.exception.*; import org.apache.commons.graph.exception.*;
import org.apache.commons.graph.decorator.*; import org.apache.commons.graph.decorator.*;
/** /**
* Description of the Class * Description of the Class
*/ */
public class AcyclicContract public class AcyclicContract
implements Contract implements Contract
{ {
private DDirectedGraph graph = null; private DDirectedGraph graph = null;
/** /**
* Description of the Class * Description of the Class
*/ */
public class CycleDetector public class CycleDetector
implements Visitor implements Visitor
{ {
private DFS dfs = null; private DFS dfs = null;
private boolean isCyclic = false; private boolean isCyclic = false;
private DirectedGraph graph = null; private DirectedGraph graph = null;
/** /**
* Constructor for the CycleDetector object * Constructor for the CycleDetector object
* *
* @param graph * @param graph
*/ */
public CycleDetector(DirectedGraph graph) public CycleDetector(DirectedGraph graph)
{ {
this.dfs = new DFS(); this.dfs = new DFS();
this.graph = graph; this.graph = graph;
Iterator verts = graph.getVertices().iterator(); Iterator verts = graph.getVertices().iterator();
if (verts.hasNext()) if (verts.hasNext())
{ {
dfs.visit(graph, (Vertex) verts.next(), this); dfs.visit(graph, (Vertex) verts.next(), this);
} }
} }
/** /**
* Description of the Method * Description of the Method
*/ */
public void discoverGraph(Graph graph) { } public void discoverGraph(Graph graph) { }
/** /**
* Description of the Method * Description of the Method
*/ */
public void discoverVertex(Vertex v) { } public void discoverVertex(Vertex v) { }
/** /**
* Description of the Method * Description of the Method
*/ */
public void discoverEdge(Edge e) public void discoverEdge(Edge e)
{ {
if (dfs.getColor(graph.getTarget(e)) == DFS.GRAY) if (dfs.getColor(graph.getTarget(e)) == DFS.GRAY)
{ {
this.isCyclic = true; this.isCyclic = true;
} }
} }
/** /**
* Description of the Method * Description of the Method
*/ */
public void finishEdge(Edge e) { } public void finishEdge(Edge e) { }
/** /**
* Description of the Method * Description of the Method
*/ */
public void finishVertex(Vertex v) { } public void finishVertex(Vertex v) { }
/** /**
* Description of the Method * Description of the Method
*/ */
public void finishGraph(Graph graph) { } public void finishGraph(Graph graph) { }
/** /**
* Description of the Method * Description of the Method
*/ */
public boolean hasCycle() public boolean hasCycle()
{ {
return isCyclic; return isCyclic;
} }
} }
/** /**
* Constructor for the AcyclicContract object * Constructor for the AcyclicContract object
*/ */
public AcyclicContract() { } public AcyclicContract() { }
/** /**
* Sets the impl attribute of the AcyclicContract object * Sets the impl attribute of the AcyclicContract object
*/ */
public void setImpl(DirectedGraph graph) public void setImpl(DirectedGraph graph)
{ {
this.graph = DDirectedGraph.decorateGraph(graph); this.graph = DDirectedGraph.decorateGraph(graph);
} }
/** /**
* Gets the interface attribute of the AcyclicContract object * Gets the interface attribute of the AcyclicContract object
*/ */
public Class getInterface() public Class getInterface()
{ {
return org.apache.commons.graph.contract.Acyclic.class; return org.apache.commons.graph.contract.Acyclic.class;
} }
/** /**
* Description of the Method * Description of the Method
*/ */
public void verify() public void verify()
throws CycleException throws CycleException
{ {
CycleDetector cd = new CycleDetector(graph); CycleDetector cd = new CycleDetector(graph);
if (cd.hasCycle()) if (cd.hasCycle())
{ {
throw new CycleException("Cycle detected in Graph."); throw new CycleException("Cycle detected in Graph.");
} }
} }
/** /**
* Adds a feature to the Vertex attribute of the AcyclicContract object * Adds a feature to the Vertex attribute of the AcyclicContract object
*/ */
public void addVertex(Vertex v) { } public void addVertex(Vertex v) { }
/** /**
* Adds a feature to the Edge attribute of the AcyclicContract object * Adds a feature to the Edge attribute of the AcyclicContract object
*/ */
public void addEdge(Edge e, public void addEdge(Edge e,
Vertex start, Vertex start,
Vertex end) Vertex end)
throws GraphException throws GraphException
{ {
if (graph.hasConnection(end, start)) if (graph.hasConnection(end, start))
{ {
throw new CycleException("Introducing edge will cause a Cycle."); throw new CycleException("Introducing edge will cause a Cycle.");
} }
} }
/** /**
* Description of the Method * Description of the Method
*/ */
public void removeVertex(Vertex v) { } public void removeVertex(Vertex v) { }
/** /**
* Description of the Method * Description of the Method
*/ */
public void removeEdge(Edge e) { } public void removeEdge(Edge e) { }
} }

View File

@ -1,57 +1,57 @@
package org.apache.commons.graph.contract; package org.apache.commons.graph.contract;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
import org.apache.commons.graph.exception.*; import org.apache.commons.graph.exception.*;
/** /**
* Description of the Interface * Description of the Interface
*/ */
public interface Contract public interface Contract
{ {
/** /**
* The impl that gets passed in is read-only. This is the representation of * The impl that gets passed in is read-only. This is the representation of
* the graph you should work off of. If an edge or vertex addition is * the graph you should work off of. If an edge or vertex addition is
* illegal to the contract, raise a GraphException with and explanation. * illegal to the contract, raise a GraphException with and explanation.
*/ */
public void setImpl(DirectedGraph impl); public void setImpl(DirectedGraph impl);
/** /**
* getInterface This returns the marker interface which is associated with * getInterface This returns the marker interface which is associated with
* the Contract. For instance, AcyclicContract will return AcyclicGraph * the Contract. For instance, AcyclicContract will return AcyclicGraph
* here. * here.
*/ */
public Class getInterface(); public Class getInterface();
/** /**
* verify - This verifies that the graph it is working on complies. * verify - This verifies that the graph it is working on complies.
*/ */
public void verify() public void verify()
throws GraphException; throws GraphException;
/** /**
* Adds a feature to the Edge attribute of the Contract object * Adds a feature to the Edge attribute of the Contract object
*/ */
public void addEdge(Edge e, public void addEdge(Edge e,
Vertex start, Vertex start,
Vertex end) Vertex end)
throws GraphException; throws GraphException;
/** /**
* Adds a feature to the Vertex attribute of the Contract object * Adds a feature to the Vertex attribute of the Contract object
*/ */
public void addVertex(Vertex v) public void addVertex(Vertex v)
throws GraphException; throws GraphException;
/** /**
* Description of the Method * Description of the Method
*/ */
public void removeEdge(Edge e) public void removeEdge(Edge e)
throws GraphException; throws GraphException;
/** /**
* Description of the Method * Description of the Method
*/ */
public void removeVertex(Vertex v) public void removeVertex(Vertex v)
throws GraphException; throws GraphException;
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph.domain.basic; package org.apache.commons.graph.domain.basic;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,410 +14,410 @@ package org.apache.commons.graph.domain.basic;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import java.lang.reflect.*; import java.lang.reflect.*;
import java.util.Set; import java.util.Set;
import java.util.Map; import java.util.Map;
import java.util.List; import java.util.List;
import java.util.HashSet; import java.util.HashSet;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.ArrayList; import java.util.ArrayList;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
import org.apache.commons.graph.contract.*; import org.apache.commons.graph.contract.*;
import org.apache.commons.graph.exception.*; import org.apache.commons.graph.exception.*;
/** /**
* Description of the Class * Description of the Class
*/ */
public class DirectedGraphImpl public class DirectedGraphImpl
implements DirectedGraph, implements DirectedGraph,
WeightedGraph, WeightedGraph,
MutableDirectedGraph, MutableDirectedGraph,
InvocationHandler InvocationHandler
{ {
private Vertex root = null; private Vertex root = null;
private Set vertices = new HashSet(); private Set vertices = new HashSet();
private Set edges = new HashSet(); private Set edges = new HashSet();
private List contracts = new ArrayList(); private List contracts = new ArrayList();
private Map inbound = new HashMap();// VERTEX X SET( EDGE ) private Map inbound = new HashMap();// VERTEX X SET( EDGE )
private Map outbound = new HashMap();// - " " - private Map outbound = new HashMap();// - " " -
private Map edgeSource = new HashMap();// EDGE X VERTEX private Map edgeSource = new HashMap();// EDGE X VERTEX
private Map edgeTarget = new HashMap();// EDGE X TARGET private Map edgeTarget = new HashMap();// EDGE X TARGET
private Map edgeWeights = new HashMap();// EDGE X WEIGHT private Map edgeWeights = new HashMap();// EDGE X WEIGHT
/** /**
* Constructor for the DirectedGraphImpl object * Constructor for the DirectedGraphImpl object
*/ */
public DirectedGraphImpl() { } public DirectedGraphImpl() { }
/** /**
* Constructor for the DirectedGraphImpl object * Constructor for the DirectedGraphImpl object
* *
* @param dg * @param dg
*/ */
public DirectedGraphImpl(DirectedGraph dg) public DirectedGraphImpl(DirectedGraph dg)
{ {
Iterator v = dg.getVertices().iterator(); Iterator v = dg.getVertices().iterator();
while (v.hasNext()) while (v.hasNext())
{ {
addVertexI((Vertex) v.next()); addVertexI((Vertex) v.next());
} }
Iterator e = dg.getEdges().iterator(); Iterator e = dg.getEdges().iterator();
while (e.hasNext()) while (e.hasNext())
{ {
Edge edge = (Edge) e.next(); Edge edge = (Edge) e.next();
addEdgeI(edge, addEdgeI(edge,
dg.getSource(edge), dg.getSource(edge),
dg.getTarget(edge)); dg.getTarget(edge));
if (dg instanceof WeightedGraph) if (dg instanceof WeightedGraph)
{ {
setWeight(edge, ((WeightedGraph) dg).getWeight(edge)); setWeight(edge, ((WeightedGraph) dg).getWeight(edge));
} }
} }
} }
/** /**
* Adds a feature to the Contract attribute of the DirectedGraphImpl object * Adds a feature to the Contract attribute of the DirectedGraphImpl object
*/ */
public void addContract(Contract c) public void addContract(Contract c)
throws GraphException throws GraphException
{ {
c.setImpl(this); c.setImpl(this);
c.verify(); c.verify();
contracts.add(c); contracts.add(c);
} }
/** /**
* Description of the Method * Description of the Method
*/ */
public void removeContract(Contract c) public void removeContract(Contract c)
{ {
contracts.remove(c); contracts.remove(c);
} }
/** /**
* Sets the weight attribute of the DirectedGraphImpl object * Sets the weight attribute of the DirectedGraphImpl object
*/ */
public void setWeight(Edge e, double value) public void setWeight(Edge e, double value)
{ {
if (edgeWeights.containsKey(e)) if (edgeWeights.containsKey(e))
{ {
edgeWeights.remove(e); edgeWeights.remove(e);
} }
edgeWeights.put(e, new Double(value)); edgeWeights.put(e, new Double(value));
} }
// Interface Methods // Interface Methods
// Graph // Graph
/** /**
* Gets the vertices attribute of the DirectedGraphImpl object * Gets the vertices attribute of the DirectedGraphImpl object
*/ */
public Set getVertices() public Set getVertices()
{ {
return new HashSet(vertices); return new HashSet(vertices);
} }
/** /**
* Gets the vertices attribute of the DirectedGraphImpl object * Gets the vertices attribute of the DirectedGraphImpl object
*/ */
public Set getVertices(Edge e) public Set getVertices(Edge e)
{ {
Set RC = new HashSet(); Set RC = new HashSet();
if (edgeSource.containsKey(e)) if (edgeSource.containsKey(e))
{ {
RC.add(edgeSource.get(e)); RC.add(edgeSource.get(e));
} }
if (edgeTarget.containsKey(e)) if (edgeTarget.containsKey(e))
{ {
RC.add(edgeTarget.get(e)); RC.add(edgeTarget.get(e));
} }
return RC; return RC;
} }
/** /**
* Gets the edges attribute of the DirectedGraphImpl object * Gets the edges attribute of the DirectedGraphImpl object
*/ */
public Set getEdges() public Set getEdges()
{ {
return new HashSet(edges); return new HashSet(edges);
} }
/** /**
* Gets the edges attribute of the DirectedGraphImpl object * Gets the edges attribute of the DirectedGraphImpl object
*/ */
public Set getEdges(Vertex v) public Set getEdges(Vertex v)
{ {
Set RC = new HashSet(); Set RC = new HashSet();
if (inbound.containsKey(v)) if (inbound.containsKey(v))
{ {
RC.addAll((Set) inbound.get(v)); RC.addAll((Set) inbound.get(v));
} }
if (outbound.containsKey(v)) if (outbound.containsKey(v))
{ {
RC.addAll((Set) outbound.get(v)); RC.addAll((Set) outbound.get(v));
} }
return RC; return RC;
} }
// Directed Graph // Directed Graph
/** /**
* Gets the source attribute of the DirectedGraphImpl object * Gets the source attribute of the DirectedGraphImpl object
*/ */
public Vertex getSource(Edge e) public Vertex getSource(Edge e)
{ {
return (Vertex) edgeSource.get(e); return (Vertex) edgeSource.get(e);
} }
/** /**
* Gets the target attribute of the DirectedGraphImpl object * Gets the target attribute of the DirectedGraphImpl object
*/ */
public Vertex getTarget(Edge e) public Vertex getTarget(Edge e)
{ {
return (Vertex) edgeTarget.get(e); return (Vertex) edgeTarget.get(e);
} }
/** /**
* Gets the inbound attribute of the DirectedGraphImpl object * Gets the inbound attribute of the DirectedGraphImpl object
*/ */
public Set getInbound(Vertex v) public Set getInbound(Vertex v)
{ {
if (inbound.containsKey(v)) if (inbound.containsKey(v))
{ {
return new HashSet((Set) inbound.get(v)); return new HashSet((Set) inbound.get(v));
} }
else else
{ {
return new HashSet(); return new HashSet();
} }
} }
/** /**
* Gets the outbound attribute of the DirectedGraphImpl object * Gets the outbound attribute of the DirectedGraphImpl object
*/ */
public Set getOutbound(Vertex v) public Set getOutbound(Vertex v)
{ {
if (outbound.containsKey(v)) if (outbound.containsKey(v))
{ {
return new HashSet((Set) outbound.get(v)); return new HashSet((Set) outbound.get(v));
} }
else else
{ {
return new HashSet(); return new HashSet();
} }
} }
// MutableDirectedGraph // MutableDirectedGraph
/** /**
* Adds a feature to the VertexI attribute of the DirectedGraphImpl object * Adds a feature to the VertexI attribute of the DirectedGraphImpl object
*/ */
private void addVertexI(Vertex v) private void addVertexI(Vertex v)
throws GraphException throws GraphException
{ {
if (root == null) root = v; if (root == null) root = v;
vertices.add(v); vertices.add(v);
} }
/** /**
* Adds a feature to the Vertex attribute of the DirectedGraphImpl object * Adds a feature to the Vertex attribute of the DirectedGraphImpl object
*/ */
public void addVertex(Vertex v) public void addVertex(Vertex v)
throws GraphException throws GraphException
{ {
Iterator conts = contracts.iterator(); Iterator conts = contracts.iterator();
while (conts.hasNext()) while (conts.hasNext())
{ {
((Contract) conts.next()).addVertex(v); ((Contract) conts.next()).addVertex(v);
} }
addVertexI(v); addVertexI(v);
} }
/** /**
* Description of the Method * Description of the Method
*/ */
private void removeVertexI(Vertex v) private void removeVertexI(Vertex v)
throws GraphException throws GraphException
{ {
try try
{ {
vertices.remove(v); vertices.remove(v);
} }
catch (Exception ex) catch (Exception ex)
{ {
throw new GraphException(ex); throw new GraphException(ex);
} }
} }
/** /**
* Description of the Method * Description of the Method
*/ */
public void removeVertex(Vertex v) public void removeVertex(Vertex v)
throws GraphException throws GraphException
{ {
Iterator conts = contracts.iterator(); Iterator conts = contracts.iterator();
while (conts.hasNext()) while (conts.hasNext())
{ {
((Contract) conts.next()).removeVertex(v); ((Contract) conts.next()).removeVertex(v);
} }
removeVertexI(v); removeVertexI(v);
} }
/** /**
* Adds a feature to the EdgeI attribute of the DirectedGraphImpl object * Adds a feature to the EdgeI attribute of the DirectedGraphImpl object
*/ */
private void addEdgeI(Edge e, Vertex start, Vertex end) private void addEdgeI(Edge e, Vertex start, Vertex end)
throws GraphException throws GraphException
{ {
edges.add(e); edges.add(e);
if (e instanceof WeightedEdge) if (e instanceof WeightedEdge)
{ {
edgeWeights.put(e, new Double(((WeightedEdge) e).getWeight())); edgeWeights.put(e, new Double(((WeightedEdge) e).getWeight()));
} }
else else
{ {
edgeWeights.put(e, new Double(1.0)); edgeWeights.put(e, new Double(1.0));
} }
edgeSource.put(e, start); edgeSource.put(e, start);
edgeTarget.put(e, end); edgeTarget.put(e, end);
if (!outbound.containsKey(start)) if (!outbound.containsKey(start))
{ {
Set edgeSet = new HashSet(); Set edgeSet = new HashSet();
edgeSet.add(e); edgeSet.add(e);
outbound.put(start, edgeSet); outbound.put(start, edgeSet);
} }
else else
{ {
((Set) outbound.get(start)).add(e); ((Set) outbound.get(start)).add(e);
} }
if (!inbound.containsKey(end)) if (!inbound.containsKey(end))
{ {
Set edgeSet = new HashSet(); Set edgeSet = new HashSet();
edgeSet.add(e); edgeSet.add(e);
inbound.put(end, edgeSet); inbound.put(end, edgeSet);
} }
else else
{ {
((Set) inbound.get(end)).add(e); ((Set) inbound.get(end)).add(e);
} }
} }
/** /**
* Adds a feature to the Edge attribute of the DirectedGraphImpl object * Adds a feature to the Edge attribute of the DirectedGraphImpl object
*/ */
public void addEdge(Edge e, public void addEdge(Edge e,
Vertex start, Vertex start,
Vertex end) Vertex end)
throws GraphException throws GraphException
{ {
Iterator conts = contracts.iterator(); Iterator conts = contracts.iterator();
while (conts.hasNext()) while (conts.hasNext())
{ {
Contract cont = (Contract) conts.next(); Contract cont = (Contract) conts.next();
cont.addEdge(e, start, end); cont.addEdge(e, start, end);
} }
addEdgeI(e, start, end); addEdgeI(e, start, end);
} }
/** /**
* Description of the Method * Description of the Method
*/ */
private void removeEdgeI(Edge e) private void removeEdgeI(Edge e)
throws GraphException throws GraphException
{ {
try try
{ {
Set edgeSet = null; Set edgeSet = null;
Vertex source = (Vertex) edgeSource.get(e); Vertex source = (Vertex) edgeSource.get(e);
edgeSource.remove(e); edgeSource.remove(e);
edgeSet = (Set) outbound.get(source); edgeSet = (Set) outbound.get(source);
edgeSet.remove(e); edgeSet.remove(e);
Vertex target = (Vertex) edgeTarget.get(e); Vertex target = (Vertex) edgeTarget.get(e);
edgeTarget.remove(e); edgeTarget.remove(e);
edgeSet = (Set) inbound.get(target); edgeSet = (Set) inbound.get(target);
edgeSet.remove(e); edgeSet.remove(e);
if (edgeWeights.containsKey(e)) if (edgeWeights.containsKey(e))
{ {
edgeWeights.remove(e); edgeWeights.remove(e);
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
throw new GraphException(ex); throw new GraphException(ex);
} }
} }
/** /**
* Description of the Method * Description of the Method
*/ */
public void removeEdge(Edge e) public void removeEdge(Edge e)
throws GraphException throws GraphException
{ {
Iterator conts = contracts.iterator(); Iterator conts = contracts.iterator();
while (conts.hasNext()) while (conts.hasNext())
{ {
((Contract) conts.next()).removeEdge(e); ((Contract) conts.next()).removeEdge(e);
} }
removeEdgeI(e); removeEdgeI(e);
} }
// WeightedGraph // WeightedGraph
/** /**
* Gets the weight attribute of the DirectedGraphImpl object * Gets the weight attribute of the DirectedGraphImpl object
*/ */
public double getWeight(Edge e) public double getWeight(Edge e)
{ {
if (edgeWeights.containsKey(e)) if (edgeWeights.containsKey(e))
{ {
return ((Double) edgeWeights.get(e)).doubleValue(); return ((Double) edgeWeights.get(e)).doubleValue();
} }
else else
{ {
return 1.0; return 1.0;
} }
} }
/** /**
* Description of the Method * Description of the Method
*/ */
public Object invoke(Object proxy, public Object invoke(Object proxy,
Method method, Method method,
Object args[]) Object args[])
throws Throwable throws Throwable
{ {
try { try {
return method.invoke(this, args); return method.invoke(this, args);
} catch (InvocationTargetException ex) { } catch (InvocationTargetException ex) {
throw ex.getTargetException(); throw ex.getTargetException();
} }
} }
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph.domain.basic; package org.apache.commons.graph.domain.basic;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,91 +14,91 @@ package org.apache.commons.graph.domain.basic;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/** /**
* DirectedGraphWrapper This is a superclass to all wrappers that work over * DirectedGraphWrapper This is a superclass to all wrappers that work over
* DirectedGraphs. * DirectedGraphs.
*/ */
import java.util.Set; import java.util.Set;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
/** /**
* Description of the Class * Description of the Class
*/ */
public class DirectedGraphWrapper public class DirectedGraphWrapper
extends GraphWrapper extends GraphWrapper
implements DirectedGraph implements DirectedGraph
{ {
private DirectedGraph impl = null; private DirectedGraph impl = null;
/** /**
* Constructor for the DirectedGraphWrapper object * Constructor for the DirectedGraphWrapper object
* *
* @param graph * @param graph
*/ */
public DirectedGraphWrapper(DirectedGraph graph) public DirectedGraphWrapper(DirectedGraph graph)
{ {
super(graph); super(graph);
impl = graph; impl = graph;
} }
/** /**
* Constructor for the DirectedGraphWrapper object * Constructor for the DirectedGraphWrapper object
*/ */
public DirectedGraphWrapper() public DirectedGraphWrapper()
{ {
super(); super();
} }
/** /**
* Sets the dirGraph attribute of the DirectedGraphWrapper object * Sets the dirGraph attribute of the DirectedGraphWrapper object
*/ */
public void setDirGraph(DirectedGraph graph) public void setDirGraph(DirectedGraph graph)
{ {
impl = graph; impl = graph;
setGraph(graph); setGraph(graph);
} }
/** /**
* Gets the inbound attribute of the DirectedGraphWrapper object * Gets the inbound attribute of the DirectedGraphWrapper object
*/ */
public Set getInbound(Vertex v) public Set getInbound(Vertex v)
{ {
return impl.getInbound(v); return impl.getInbound(v);
} }
/** /**
* Gets the outbound attribute of the DirectedGraphWrapper object * Gets the outbound attribute of the DirectedGraphWrapper object
*/ */
public Set getOutbound(Vertex v) public Set getOutbound(Vertex v)
{ {
return impl.getOutbound(v); return impl.getOutbound(v);
} }
/** /**
* Gets the source attribute of the DirectedGraphWrapper object * Gets the source attribute of the DirectedGraphWrapper object
*/ */
public Vertex getSource(Edge e) public Vertex getSource(Edge e)
{ {
return impl.getSource(e); return impl.getSource(e);
} }
/** /**
* Gets the target attribute of the DirectedGraphWrapper object * Gets the target attribute of the DirectedGraphWrapper object
*/ */
public Vertex getTarget(Edge e) public Vertex getTarget(Edge e)
{ {
return impl.getTarget(e); return impl.getTarget(e);
} }
} }

View File

@ -1,74 +1,74 @@
package org.apache.commons.graph.domain.basic; package org.apache.commons.graph.domain.basic;
/** /**
* GraphWrapper This is a superclass of all Wrapper implementations. It * GraphWrapper This is a superclass of all Wrapper implementations. It
* basically does a redirection to the graph. * basically does a redirection to the graph.
*/ */
import java.util.Set; import java.util.Set;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
/** /**
* Description of the Class * Description of the Class
*/ */
public class GraphWrapper public class GraphWrapper
{ {
private Graph impl = null; private Graph impl = null;
/** /**
* Constructor for the GraphWrapper object * Constructor for the GraphWrapper object
* *
* @param impl * @param impl
*/ */
public GraphWrapper(Graph impl) public GraphWrapper(Graph impl)
{ {
this.impl = impl; this.impl = impl;
} }
/** /**
* Constructor for the GraphWrapper object * Constructor for the GraphWrapper object
*/ */
public GraphWrapper() { } public GraphWrapper() { }
/** /**
* Sets the graph attribute of the GraphWrapper object * Sets the graph attribute of the GraphWrapper object
*/ */
public void setGraph(Graph impl) public void setGraph(Graph impl)
{ {
this.impl = impl; this.impl = impl;
} }
// Graph Implementation. . . // Graph Implementation. . .
/** /**
* Gets the vertices attribute of the GraphWrapper object * Gets the vertices attribute of the GraphWrapper object
*/ */
public Set getVertices() public Set getVertices()
{ {
return impl.getVertices(); return impl.getVertices();
} }
/** /**
* Gets the edges attribute of the GraphWrapper object * Gets the edges attribute of the GraphWrapper object
*/ */
public Set getEdges() public Set getEdges()
{ {
return impl.getEdges(); return impl.getEdges();
} }
/** /**
* Gets the vertices attribute of the GraphWrapper object * Gets the vertices attribute of the GraphWrapper object
*/ */
public Set getVertices(Edge e) public Set getVertices(Edge e)
{ {
return impl.getVertices(e); return impl.getVertices(e);
} }
/** /**
* Gets the edges attribute of the GraphWrapper object * Gets the edges attribute of the GraphWrapper object
*/ */
public Set getEdges(Vertex v) public Set getEdges(Vertex v)
{ {
return impl.getEdges(v); return impl.getEdges(v);
} }
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph.domain.basic; package org.apache.commons.graph.domain.basic;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,201 +14,201 @@ package org.apache.commons.graph.domain.basic;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import java.util.Set; import java.util.Set;
import java.util.Map; import java.util.Map;
import java.util.HashSet; import java.util.HashSet;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.lang.reflect.*; import java.lang.reflect.*;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
import org.apache.commons.graph.exception.*; import org.apache.commons.graph.exception.*;
/** /**
* Description of the Class * Description of the Class
*/ */
public class UndirectedGraphImpl public class UndirectedGraphImpl
implements UndirectedGraph, implements UndirectedGraph,
WeightedGraph, WeightedGraph,
MutableGraph, MutableGraph,
InvocationHandler InvocationHandler
{ {
private Set vertices = new HashSet(); private Set vertices = new HashSet();
private Set edges = new HashSet(); private Set edges = new HashSet();
private Map edgeVerts = new HashMap();// EDGE X SET( VERTS ) private Map edgeVerts = new HashMap();// EDGE X SET( VERTS )
private Map vertEdges = new HashMap();// VERTEX X SET( EDGE ) private Map vertEdges = new HashMap();// VERTEX X SET( EDGE )
private Map edgeWeights = new HashMap(); // EDGE X WEIGHT private Map edgeWeights = new HashMap(); // EDGE X WEIGHT
/** /**
* Constructor for the UndirectedGraphImpl object * Constructor for the UndirectedGraphImpl object
*/ */
public UndirectedGraphImpl() { } public UndirectedGraphImpl() { }
/** /**
* Adds a feature to the Vertex attribute of the UndirectedGraphImpl object * Adds a feature to the Vertex attribute of the UndirectedGraphImpl object
*/ */
public void addVertex(Vertex v) public void addVertex(Vertex v)
throws GraphException throws GraphException
{ {
vertices.add(v); vertices.add(v);
} }
public void removeVertex( Vertex v ) public void removeVertex( Vertex v )
throws GraphException throws GraphException
{ {
vertices.remove( v ); vertices.remove( v );
} }
public void removeEdge( Edge e ) public void removeEdge( Edge e )
throws GraphException throws GraphException
{ {
edges.remove( e ); edges.remove( e );
} }
public void addEdge(Edge e) public void addEdge(Edge e)
throws GraphException throws GraphException
{ {
edges.add( e ); edges.add( e );
} }
public void disconnect(Edge e, Vertex v) { public void disconnect(Edge e, Vertex v) {
if (edgeVerts.containsKey( e )) { if (edgeVerts.containsKey( e )) {
((Set) edgeVerts.get( e )).remove( v ); ((Set) edgeVerts.get( e )).remove( v );
} }
if (vertEdges.containsKey( v )) { if (vertEdges.containsKey( v )) {
((Set) vertEdges.get( v )).remove( e ); ((Set) vertEdges.get( v )).remove( e );
} }
} }
public void connect( Edge e, Vertex v ) { public void connect( Edge e, Vertex v ) {
Set verts = null; Set verts = null;
if (!edgeVerts.containsKey( e )) { if (!edgeVerts.containsKey( e )) {
verts = new HashSet(); verts = new HashSet();
edgeVerts.put( e, verts ); edgeVerts.put( e, verts );
} else { } else {
verts = (Set) edgeVerts.get( e ); verts = (Set) edgeVerts.get( e );
} }
verts.add( v ); verts.add( v );
Set edges = null; Set edges = null;
if (!vertEdges.containsKey( v )) { if (!vertEdges.containsKey( v )) {
edges = new HashSet(); edges = new HashSet();
vertEdges.put( v, edges ); vertEdges.put( v, edges );
} else { } else {
edges = (Set) vertEdges.get( v ); edges = (Set) vertEdges.get( v );
} }
edges.add( e ); edges.add( e );
} }
/** /**
* Adds a feature to the Edge attribute of the UndirectedGraphImpl object * Adds a feature to the Edge attribute of the UndirectedGraphImpl object
*/ */
public void addEdge(Edge e, public void addEdge(Edge e,
Set vertices) Set vertices)
throws GraphException throws GraphException
{ {
addEdge( e ); addEdge( e );
Iterator verts = vertices.iterator(); Iterator verts = vertices.iterator();
while (verts.hasNext()) { while (verts.hasNext()) {
connect( e, (Vertex) verts.next() ); connect( e, (Vertex) verts.next() );
} }
} }
// Interface Methods // Interface Methods
/** /**
* Gets the vertices attribute of the UndirectedGraphImpl object * Gets the vertices attribute of the UndirectedGraphImpl object
*/ */
public Set getVertices() public Set getVertices()
{ {
return new HashSet(vertices); return new HashSet(vertices);
} }
/** /**
* Gets the vertices attribute of the UndirectedGraphImpl object * Gets the vertices attribute of the UndirectedGraphImpl object
*/ */
public Set getVertices(Edge e) public Set getVertices(Edge e)
{ {
if (edgeVerts.containsKey(e)) if (edgeVerts.containsKey(e))
{ {
return new HashSet((Set) edgeVerts.get(e)); return new HashSet((Set) edgeVerts.get(e));
} }
else else
{ {
return new HashSet(); return new HashSet();
} }
} }
/** /**
* Gets the edges attribute of the UndirectedGraphImpl object * Gets the edges attribute of the UndirectedGraphImpl object
*/ */
public Set getEdges() public Set getEdges()
{ {
return new HashSet(edges); return new HashSet(edges);
} }
/** /**
* Gets the edges attribute of the UndirectedGraphImpl object * Gets the edges attribute of the UndirectedGraphImpl object
*/ */
public Set getEdges(Vertex v) public Set getEdges(Vertex v)
{ {
if (vertEdges.containsKey(v)) if (vertEdges.containsKey(v))
{ {
return new HashSet((Set) vertEdges.get(v)); return new HashSet((Set) vertEdges.get(v));
} }
else else
{ {
return new HashSet(); return new HashSet();
} }
} }
public void setWeight( Edge e, double w ) { public void setWeight( Edge e, double w ) {
if (edgeWeights.containsKey( e )) { if (edgeWeights.containsKey( e )) {
edgeWeights.remove( e ); edgeWeights.remove( e );
} }
edgeWeights.put( e, new Double( w ) ); edgeWeights.put( e, new Double( w ) );
} }
public double getWeight( Edge e ) { public double getWeight( Edge e ) {
if (edgeWeights.containsKey( e )) { if (edgeWeights.containsKey( e )) {
return ((Double) edgeWeights.get( e ) ).doubleValue(); return ((Double) edgeWeights.get( e ) ).doubleValue();
} else { } else {
return 1.0; return 1.0;
} }
} }
/** /**
* Description of the Method * Description of the Method
*/ */
public Object invoke(Object proxy, public Object invoke(Object proxy,
Method method, Method method,
Object args[]) Object args[])
throws Throwable throws Throwable
{ {
try { try {
return method.invoke(this, args); return method.invoke(this, args);
} catch (InvocationTargetException ex) { } catch (InvocationTargetException ex) {
throw ex.getTargetException(); throw ex.getTargetException();
} }
} }
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph.domain.basic; package org.apache.commons.graph.domain.basic;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,57 +14,57 @@ package org.apache.commons.graph.domain.basic;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/** /**
* This is a simple wrapper to wrap around a graph, and create a weighted graph. * This is a simple wrapper to wrap around a graph, and create a weighted graph.
*/ */
import java.util.Map; import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
/** /**
* Description of the Class * Description of the Class
*/ */
public class WeightedGraphWrapper public class WeightedGraphWrapper
extends GraphWrapper extends GraphWrapper
implements WeightedGraph implements WeightedGraph
{ {
private Map weights = new HashMap();// EDGE X WEIGHT private Map weights = new HashMap();// EDGE X WEIGHT
/** /**
* Constructor for the WeightedGraphWrapper object * Constructor for the WeightedGraphWrapper object
* *
* @param graph * @param graph
*/ */
public WeightedGraphWrapper(Graph graph) public WeightedGraphWrapper(Graph graph)
{ {
super(graph); super(graph);
} }
/** /**
* Gets the weight attribute of the WeightedGraphWrapper object * Gets the weight attribute of the WeightedGraphWrapper object
*/ */
public double getWeight(Edge e) public double getWeight(Edge e)
{ {
if (weights.containsKey(e)) if (weights.containsKey(e))
{ {
return ((Double) weights.get(e)).doubleValue(); return ((Double) weights.get(e)).doubleValue();
} }
else else
{ {
return 1.0; return 1.0;
} }
} }
/** /**
* Sets the weight attribute of the WeightedGraphWrapper object * Sets the weight attribute of the WeightedGraphWrapper object
*/ */
public void setWeight(Edge e, double weight) public void setWeight(Edge e, double weight)
{ {
weights.put(e, new Double(weight)); weights.put(e, new Double(weight));
} }
} }

View File

@ -1,33 +1,33 @@
package org.apache.commons.graph.domain.dependency; package org.apache.commons.graph.domain.dependency;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
/** /**
* Description of the Class * Description of the Class
*/ */
public class Dependency implements Edge public class Dependency implements Edge
{ {
private Object head = null; private Object head = null;
private Object dep = null; private Object dep = null;
/** /**
* Constructor for the Dependency object * Constructor for the Dependency object
* *
* @param head * @param head
* @param dep * @param dep
*/ */
public Dependency(Object head, public Dependency(Object head,
Object dep) Object dep)
{ {
this.head = head; this.head = head;
this.dep = dep; this.dep = dep;
} }
/** /**
* Description of the Method * Description of the Method
*/ */
public String description() public String description()
{ {
return head + " depends on " + dep; return head + " depends on " + dep;
} }
} }

View File

@ -1,30 +1,30 @@
package org.apache.commons.graph.domain.dependency; package org.apache.commons.graph.domain.dependency;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
/** /**
* Description of the Class * Description of the Class
*/ */
public class DependencyVertex public class DependencyVertex
implements Vertex implements Vertex
{ {
private Object value; private Object value;
/** /**
* Constructor for the DependencyVertex object * Constructor for the DependencyVertex object
* *
* @param value * @param value
*/ */
public DependencyVertex(Object value) public DependencyVertex(Object value)
{ {
this.value = value; this.value = value;
} }
/** /**
* Gets the value attribute of the DependencyVertex object * Gets the value attribute of the DependencyVertex object
*/ */
public Object getValue() public Object getValue()
{ {
return value; return value;
} }
} }

View File

@ -1,84 +1,84 @@
package org.apache.commons.graph.domain.dependency; package org.apache.commons.graph.domain.dependency;
import java.util.List; import java.util.List;
import java.util.LinkedList; import java.util.LinkedList;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
import org.apache.commons.graph.algorithm.search.*; import org.apache.commons.graph.algorithm.search.*;
/** /**
* Description of the Class * Description of the Class
*/ */
public class DependencyVisitor public class DependencyVisitor
implements Visitor implements Visitor
{ {
private List deps = null; private List deps = null;
private DFS dfs = new DFS(); private DFS dfs = new DFS();
/** /**
* Constructor for the DependencyVisitor object * Constructor for the DependencyVisitor object
*/ */
public DependencyVisitor() { } public DependencyVisitor() { }
/** /**
* Description of the Method * Description of the Method
*/ */
public void discoverGraph(Graph g) { } public void discoverGraph(Graph g) { }
/** /**
* Description of the Method * Description of the Method
*/ */
public void discoverVertex(Vertex v) { } public void discoverVertex(Vertex v) { }
/** /**
* Description of the Method * Description of the Method
*/ */
public void discoverEdge(Edge e) { } public void discoverEdge(Edge e) { }
/** /**
* Description of the Method * Description of the Method
*/ */
public void finishGraph(Graph g) { } public void finishGraph(Graph g) { }
/** /**
* Description of the Method * Description of the Method
*/ */
public void finishVertex(Vertex v) public void finishVertex(Vertex v)
{ {
if (v instanceof DependencyVertex) if (v instanceof DependencyVertex)
{ {
deps.add(((DependencyVertex) v).getValue()); deps.add(((DependencyVertex) v).getValue());
} }
else else
{ {
deps.add(v); deps.add(v);
} }
} }
/** /**
* Description of the Method * Description of the Method
*/ */
public void finishEdge(Edge e) { } public void finishEdge(Edge e) { }
/** /**
* Gets the sortedDependencies attribute of the DependencyVisitor object * Gets the sortedDependencies attribute of the DependencyVisitor object
*/ */
public synchronized List public synchronized List
getSortedDependencies(DependencyGraph dg, getSortedDependencies(DependencyGraph dg,
Vertex root) Vertex root)
{ {
deps = new LinkedList(); deps = new LinkedList();
dfs.visit(dg, root, this); dfs.visit(dg, root, this);
return deps; return deps;
} }
public synchronized List public synchronized List
getSortedDependencies(DependencyGraph dg ) getSortedDependencies(DependencyGraph dg )
{ {
deps = new LinkedList(); deps = new LinkedList();
dfs.visit( dg, this ); dfs.visit( dg, this );
return deps; return deps;
} }
} }

View File

@ -1,41 +1,41 @@
package org.apache.commons.graph.domain.jdepend; package org.apache.commons.graph.domain.jdepend;
import java.awt.Color; import java.awt.Color;
import jdepend.framework.*; import jdepend.framework.*;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
import org.apache.commons.graph.visualize.*; import org.apache.commons.graph.visualize.*;
public class ClassVertex public class ClassVertex
implements Vertex, Named, Colored implements Vertex, Named, Colored
{ {
private JavaClass clazz = null; private JavaClass clazz = null;
public ClassVertex( JavaClass clazz ) { public ClassVertex( JavaClass clazz ) {
this.clazz = clazz; this.clazz = clazz;
} }
public JavaClass getJavaClass() { public JavaClass getJavaClass() {
return clazz; return clazz;
} }
public String getName() { public String getName() {
return clazz.getName(); return clazz.getName();
} }
public String toString() { public String toString() {
return getName(); return getName();
} }
public Color getBackgroundColor() { public Color getBackgroundColor() {
return Color.blue; return Color.blue;
} }
public Color getTextColor() { public Color getTextColor() {
return Color.white; return Color.white;
} }
} }

View File

@ -1,35 +1,35 @@
package org.apache.commons.graph.domain.jdepend; package org.apache.commons.graph.domain.jdepend;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
import jdepend.framework.*; import jdepend.framework.*;
public class ImportEdge public class ImportEdge
implements Edge, Named implements Edge, Named
{ {
private JavaPackage pkg = null; private JavaPackage pkg = null;
private JavaClass clz = null; private JavaClass clz = null;
public ImportEdge( JavaClass clz, public ImportEdge( JavaClass clz,
JavaPackage pkg) { JavaPackage pkg) {
this.pkg = pkg; this.pkg = pkg;
this.clz = clz; this.clz = clz;
} }
public JavaPackage getJavaPackage() { public JavaPackage getJavaPackage() {
return pkg; return pkg;
} }
public JavaClass getJavaClass() { public JavaClass getJavaClass() {
return clz; return clz;
} }
public String getName() { public String getName() {
return clz.getName() + " imports " + pkg.getName(); return clz.getName() + " imports " + pkg.getName();
} }
public String toString() { public String toString() {
return getName(); return getName();
} }
} }

View File

@ -1,92 +1,92 @@
package org.apache.commons.graph.domain.jdepend; package org.apache.commons.graph.domain.jdepend;
import java.util.Map; import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.io.IOException; import java.io.IOException;
import jdepend.framework.*; import jdepend.framework.*;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
import org.apache.commons.graph.domain.basic.*; import org.apache.commons.graph.domain.basic.*;
public class JDependGraph public class JDependGraph
extends DirectedGraphImpl extends DirectedGraphImpl
implements DirectedGraph implements DirectedGraph
{ {
private JDepend jdep = new JDepend(); private JDepend jdep = new JDepend();
private Map pkgMap = new HashMap(); // JP X JPV private Map pkgMap = new HashMap(); // JP X JPV
private Map clazzMap = new HashMap(); // JC X JCV private Map clazzMap = new HashMap(); // JC X JCV
public JDependGraph( ) { } public JDependGraph( ) { }
public void addDirectory( String directory ) public void addDirectory( String directory )
throws IOException throws IOException
{ {
jdep.addDirectory( directory ); jdep.addDirectory( directory );
} }
/** /**
* This not only finds the PackageVertex, but also * This not only finds the PackageVertex, but also
* adds it to the graph, if it isn't already there. * adds it to the graph, if it isn't already there.
*/ */
private PackageVertex findPackageVertex( JavaPackage pkg ) { private PackageVertex findPackageVertex( JavaPackage pkg ) {
if (pkgMap.containsKey( pkg.getName() )) { if (pkgMap.containsKey( pkg.getName() )) {
return (PackageVertex) pkgMap.get( pkg.getName() ); return (PackageVertex) pkgMap.get( pkg.getName() );
} else { } else {
PackageVertex RC = new PackageVertex( pkg ); PackageVertex RC = new PackageVertex( pkg );
pkgMap.put( pkg.getName(), RC ); pkgMap.put( pkg.getName(), RC );
addVertex( RC ); addVertex( RC );
return RC; return RC;
} }
} }
/** /**
* This not only finds the PackageVertex, but also * This not only finds the PackageVertex, but also
* adds it to the graph, if it isn't already there. * adds it to the graph, if it isn't already there.
*/ */
private ClassVertex findClassVertex( JavaClass clz ) { private ClassVertex findClassVertex( JavaClass clz ) {
if (clazzMap.containsKey( clz )) { if (clazzMap.containsKey( clz )) {
return (ClassVertex) clazzMap.get( clz ); return (ClassVertex) clazzMap.get( clz );
} else { } else {
ClassVertex RC = new ClassVertex( clz ); ClassVertex RC = new ClassVertex( clz );
clazzMap.put( clz, RC ); clazzMap.put( clz, RC );
addVertex( RC ); addVertex( RC );
return RC; return RC;
} }
} }
public void analyze() { public void analyze() {
Iterator pkgs = jdep.analyze().iterator(); Iterator pkgs = jdep.analyze().iterator();
while (pkgs.hasNext()) { while (pkgs.hasNext()) {
JavaPackage pkg = (JavaPackage) pkgs.next(); JavaPackage pkg = (JavaPackage) pkgs.next();
PackageVertex pv = findPackageVertex( pkg ); PackageVertex pv = findPackageVertex( pkg );
Iterator clzs = pkg.getClasses().iterator(); Iterator clzs = pkg.getClasses().iterator();
while (clzs.hasNext()) { while (clzs.hasNext()) {
JavaClass clz = (JavaClass) clzs.next(); JavaClass clz = (JavaClass) clzs.next();
ClassVertex cv = findClassVertex( clz ); ClassVertex cv = findClassVertex( clz );
OwnershipEdge oe = new OwnershipEdge( pkg, clz ); OwnershipEdge oe = new OwnershipEdge( pkg, clz );
addEdge( oe, pv, cv ); addEdge( oe, pv, cv );
setWeight( oe, 5.0 ); setWeight( oe, 5.0 );
Iterator ipkgs = clz.getImportedPackages().iterator(); Iterator ipkgs = clz.getImportedPackages().iterator();
while (ipkgs.hasNext()) { while (ipkgs.hasNext()) {
JavaPackage ipkg = (JavaPackage) ipkgs.next(); JavaPackage ipkg = (JavaPackage) ipkgs.next();
PackageVertex ipv = findPackageVertex( ipkg ); PackageVertex ipv = findPackageVertex( ipkg );
ImportEdge ie = new ImportEdge( clz, pkg ); ImportEdge ie = new ImportEdge( clz, pkg );
addEdge( ie, cv, ipv ); addEdge( ie, cv, ipv );
setWeight( ie, 200.0 * ipkg.afferentCoupling() + 100.0 ); setWeight( ie, 200.0 * ipkg.afferentCoupling() + 100.0 );
} }
} }
} }
} }
} }

View File

@ -1,39 +1,39 @@
package org.apache.commons.graph.domain.jdepend; package org.apache.commons.graph.domain.jdepend;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
import jdepend.framework.*; import jdepend.framework.*;
public class OwnershipEdge public class OwnershipEdge
implements Edge, Named implements Edge, Named
{ {
private JavaPackage pkg = null; private JavaPackage pkg = null;
private JavaClass clz = null; private JavaClass clz = null;
public OwnershipEdge( JavaPackage pkg, public OwnershipEdge( JavaPackage pkg,
JavaClass clz) { JavaClass clz) {
this.pkg = pkg; this.pkg = pkg;
this.clz = clz; this.clz = clz;
} }
public JavaPackage getJavaPackage() { public JavaPackage getJavaPackage() {
return pkg; return pkg;
} }
public JavaClass getJavaClass() { public JavaClass getJavaClass() {
return clz; return clz;
} }
public String getName() { public String getName() {
return pkg.getName() + " owns " + clz.getName(); return pkg.getName() + " owns " + clz.getName();
} }
public String toString() { public String toString() {
return getName(); return getName();
} }
} }

View File

@ -1,38 +1,38 @@
package org.apache.commons.graph.domain.jdepend; package org.apache.commons.graph.domain.jdepend;
import java.awt.Color; import java.awt.Color;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
import org.apache.commons.graph.visualize.*; import org.apache.commons.graph.visualize.*;
import jdepend.framework.*; import jdepend.framework.*;
public class PackageVertex public class PackageVertex
implements Vertex, Named, Colored implements Vertex, Named, Colored
{ {
private JavaPackage pkg = null; private JavaPackage pkg = null;
public PackageVertex( JavaPackage pkg ) { public PackageVertex( JavaPackage pkg ) {
this.pkg = pkg; this.pkg = pkg;
} }
public JavaPackage getJavaPackage() { public JavaPackage getJavaPackage() {
return pkg; return pkg;
} }
public Color getBackgroundColor() { public Color getBackgroundColor() {
return Color.red; return Color.red;
} }
public Color getTextColor() { public Color getTextColor() {
return Color.white; return Color.white;
} }
public String getName() { public String getName() {
return pkg.getName(); return pkg.getName();
} }
public String toString() { public String toString() {
return getName(); return getName();
} }
} }

View File

@ -1,48 +1,48 @@
package org.apache.commons.graph.domain.statemachine; package org.apache.commons.graph.domain.statemachine;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
/** /**
* Description of the Class * Description of the Class
*/ */
public class State public class State
implements Vertex, Named implements Vertex, Named
{ {
private String name; private String name;
private StateMachine subMachine = null; private StateMachine subMachine = null;
/** /**
* Constructor for the State object * Constructor for the State object
* *
* @param name * @param name
*/ */
public State(String name) public State(String name)
{ {
this.name = name; this.name = name;
} }
/** /**
* Gets the name attribute of the State object * Gets the name attribute of the State object
*/ */
public String getName() public String getName()
{ {
return name; return name;
} }
/** /**
* Sets the submachine attribute of the State object * Sets the submachine attribute of the State object
*/ */
public void setSubmachine(StateMachine subMachine) public void setSubmachine(StateMachine subMachine)
{ {
this.subMachine = subMachine; this.subMachine = subMachine;
} }
/** /**
* Gets the submachine attribute of the State object * Gets the submachine attribute of the State object
*/ */
public StateMachine getSubmachine() public StateMachine getSubmachine()
{ {
return this.subMachine; return this.subMachine;
} }
} }

View File

@ -1,121 +1,121 @@
package org.apache.commons.graph.domain.statemachine; package org.apache.commons.graph.domain.statemachine;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
/** /**
* Description of the Class * Description of the Class
*/ */
public class Transition public class Transition
implements Edge, Named implements Edge, Named
{ {
private String name; private String name;
private State source; private State source;
private State target; private State target;
private String action = null; private String action = null;
private String guard = null; private String guard = null;
private String output = null; private String output = null;
private String trigger = null; private String trigger = null;
/** /**
* Description of the Field * Description of the Field
*/ */
public final static String EPSILON = "\u03B5"; public final static String EPSILON = "\u03B5";
/** /**
* Constructor for the Transition object * Constructor for the Transition object
* *
* @param name * @param name
* @param source * @param source
* @param target * @param target
*/ */
public Transition(String name, public Transition(String name,
State source, State source,
State target) State target)
{ {
this.name = name; this.name = name;
this.source = source; this.source = source;
this.target = target; this.target = target;
} }
/** /**
* Gets the name attribute of the Transition object * Gets the name attribute of the Transition object
*/ */
public String getName() public String getName()
{ {
return name; return name;
} }
/** /**
* Gets the source attribute of the Transition object * Gets the source attribute of the Transition object
*/ */
public State getSource() public State getSource()
{ {
return source; return source;
} }
/** /**
* Gets the target attribute of the Transition object * Gets the target attribute of the Transition object
*/ */
public State getTarget() public State getTarget()
{ {
return target; return target;
} }
/** /**
* Sets the action attribute of the Transition object * Sets the action attribute of the Transition object
*/ */
public void setTrigger( String trigger ) public void setTrigger( String trigger )
{ {
this.trigger = trigger; this.trigger = trigger;
} }
/** /**
* Sets the action attribute of the Transition object * Sets the action attribute of the Transition object
*/ */
public void setAction( String action ) public void setAction( String action )
{ {
this.action = action; this.action = action;
} }
/** /**
* Gets the action attribute of the Transition object * Gets the action attribute of the Transition object
*/ */
public String getAction() public String getAction()
{ {
return action; return action;
} }
/** /**
* Sets the guard attribute of the Transition object * Sets the guard attribute of the Transition object
*/ */
public void setGuard(String guard) public void setGuard(String guard)
{ {
this.guard = guard; this.guard = guard;
} }
/** /**
* Gets the guard attribute of the Transition object * Gets the guard attribute of the Transition object
*/ */
public String getGuard() public String getGuard()
{ {
return guard; return guard;
} }
/** /**
* Gets the output attribute of the Transition object * Gets the output attribute of the Transition object
*/ */
public String getOutput() public String getOutput()
{ {
return output; return output;
} }
/** /**
* Sets the output attribute of the Transition object * Sets the output attribute of the Transition object
*/ */
public void setOutput(String output) public void setOutput(String output)
{ {
this.output = output; this.output = output;
} }
} }

View File

@ -1,35 +1,35 @@
package org.apache.commons.graph.exception; package org.apache.commons.graph.exception;
/** /**
* Description of the Class * Description of the Class
*/ */
public class ContractVerificationException extends GraphException public class ContractVerificationException extends GraphException
{ {
/** /**
* Constructor for the ContractVerificationException object * Constructor for the ContractVerificationException object
*/ */
public ContractVerificationException() public ContractVerificationException()
{ {
super(); super();
} }
/** /**
* Constructor for the ContractVerificationException object * Constructor for the ContractVerificationException object
* *
* @param msg * @param msg
*/ */
public ContractVerificationException(String msg) public ContractVerificationException(String msg)
{ {
super(msg); super(msg);
} }
/** /**
* Constructor for the ContractVerificationException object * Constructor for the ContractVerificationException object
* *
* @param cause * @param cause
*/ */
public ContractVerificationException(Throwable cause) public ContractVerificationException(Throwable cause)
{ {
super(cause); super(cause);
} }
} }

View File

@ -1,4 +1,4 @@
package org.apache.commons.graph.exception; package org.apache.commons.graph.exception;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -13,38 +13,38 @@ package org.apache.commons.graph.exception;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/** /**
* Description of the Class * Description of the Class
*/ */
public class CycleException extends GraphException public class CycleException extends GraphException
{ {
/** /**
* Constructor for the CycleException object * Constructor for the CycleException object
*/ */
public CycleException() public CycleException()
{ {
super(); super();
} }
/** /**
* Constructor for the CycleException object * Constructor for the CycleException object
* *
* @param msg * @param msg
*/ */
public CycleException(String msg) public CycleException(String msg)
{ {
super(msg); super(msg);
} }
/** /**
* Constructor for the CycleException object * Constructor for the CycleException object
* *
* @param cause * @param cause
*/ */
public CycleException(Throwable cause) public CycleException(Throwable cause)
{ {
super(cause); super(cause);
} }
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph.exception; package org.apache.commons.graph.exception;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,62 +14,62 @@ package org.apache.commons.graph.exception;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/** /**
* GraphException This is the superclass of all exceptions that can be thrown. * GraphException This is the superclass of all exceptions that can be thrown.
*/ */
import java.io.PrintStream; import java.io.PrintStream;
import java.io.PrintWriter; import java.io.PrintWriter;
public class GraphException extends RuntimeException public class GraphException extends RuntimeException
{ {
private Throwable cause = null; private Throwable cause = null;
/** /**
* Constructor for the GraphException object * Constructor for the GraphException object
*/ */
public GraphException() public GraphException()
{ {
super(); super();
} }
/** /**
* Constructor for the GraphException object * Constructor for the GraphException object
* *
* @param msg * @param msg
*/ */
public GraphException(String msg) public GraphException(String msg)
{ {
super(msg); super(msg);
} }
/** /**
* Constructor for the GraphException object * Constructor for the GraphException object
* *
* @param cause * @param cause
*/ */
public GraphException(Throwable cause) public GraphException(Throwable cause)
{ {
super(cause.getMessage()); super(cause.getMessage());
this.cause = cause; this.cause = cause;
} }
public Throwable getCause() { public Throwable getCause() {
return cause; return cause;
} }
public void printStackTrace() { public void printStackTrace() {
cause.printStackTrace(); cause.printStackTrace();
} }
public void printStackTrace( PrintStream s ) { public void printStackTrace( PrintStream s ) {
cause.printStackTrace( s ); cause.printStackTrace( s );
} }
public void printStackTrace( PrintWriter w ) { public void printStackTrace( PrintWriter w ) {
cause.printStackTrace( w ); cause.printStackTrace( w );
} }
} }

View File

@ -1,4 +1,4 @@
package org.apache.commons.graph.exception; package org.apache.commons.graph.exception;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -13,38 +13,38 @@ package org.apache.commons.graph.exception;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/** /**
* Description of the Class * Description of the Class
*/ */
public class HyperGraphException extends GraphException public class HyperGraphException extends GraphException
{ {
/** /**
* Constructor for the HyperGraphException object * Constructor for the HyperGraphException object
*/ */
public HyperGraphException() public HyperGraphException()
{ {
super(); super();
} }
/** /**
* Constructor for the HyperGraphException object * Constructor for the HyperGraphException object
* *
* @param msg * @param msg
*/ */
public HyperGraphException(String msg) public HyperGraphException(String msg)
{ {
super(msg); super(msg);
} }
/** /**
* Constructor for the HyperGraphException object * Constructor for the HyperGraphException object
* *
* @param cause * @param cause
*/ */
public HyperGraphException(Throwable cause) public HyperGraphException(Throwable cause)
{ {
super(cause); super(cause);
} }
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph.exception; package org.apache.commons.graph.exception;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,38 +14,38 @@ package org.apache.commons.graph.exception;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/** /**
* Description of the Class * Description of the Class
*/ */
public class NegativeCycleException extends GraphException public class NegativeCycleException extends GraphException
{ {
/** /**
* Constructor for the NegativeCycleException object * Constructor for the NegativeCycleException object
*/ */
public NegativeCycleException() public NegativeCycleException()
{ {
super(); super();
} }
/** /**
* Constructor for the NegativeCycleException object * Constructor for the NegativeCycleException object
* *
* @param msg * @param msg
*/ */
public NegativeCycleException(String msg) public NegativeCycleException(String msg)
{ {
super(msg); super(msg);
} }
/** /**
* Constructor for the NegativeCycleException object * Constructor for the NegativeCycleException object
* *
* @param cause * @param cause
*/ */
public NegativeCycleException(Throwable cause) public NegativeCycleException(Throwable cause)
{ {
super(cause); super(cause);
} }
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph.exception; package org.apache.commons.graph.exception;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,39 +14,39 @@ package org.apache.commons.graph.exception;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/** /**
* Description of the Class * Description of the Class
*/ */
public class NoPathException public class NoPathException
extends GraphException extends GraphException
{ {
/** /**
* Constructor for the NoPathException object * Constructor for the NoPathException object
*/ */
public NoPathException() public NoPathException()
{ {
super(); super();
} }
/** /**
* Constructor for the NoPathException object * Constructor for the NoPathException object
* *
* @param msg * @param msg
*/ */
public NoPathException(String msg) public NoPathException(String msg)
{ {
super(msg); super(msg);
} }
/** /**
* Constructor for the NoPathException object * Constructor for the NoPathException object
* *
* @param t * @param t
*/ */
public NoPathException(Throwable t) public NoPathException(Throwable t)
{ {
super(t); super(t);
} }
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph.factory; package org.apache.commons.graph.factory;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,140 +14,140 @@ package org.apache.commons.graph.factory;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import java.lang.reflect.*; import java.lang.reflect.*;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
import org.apache.commons.graph.contract.*; import org.apache.commons.graph.contract.*;
import org.apache.commons.graph.exception.*; import org.apache.commons.graph.exception.*;
import org.apache.commons.graph.domain.basic.*; import org.apache.commons.graph.domain.basic.*;
/** /**
* Description of the Class * Description of the Class
*/ */
public class GraphFactory public class GraphFactory
{ {
/** /**
* Constructor for the GraphFactory object * Constructor for the GraphFactory object
*/ */
public GraphFactory() { } public GraphFactory() { }
/** /**
* makeGraph * makeGraph
* *
* @param contracts Which contracts to enforce. * @param contracts Which contracts to enforce.
* @param baseGraph Is the actual *GraphImpl which will be at the core of * @param baseGraph Is the actual *GraphImpl which will be at the core of
* the Proxy. * the Proxy.
* @param baseGraphType Interface which is returned. * @param baseGraphType Interface which is returned.
* @param isWeighted Does the graph handle Weights? * @param isWeighted Does the graph handle Weights?
* @param init Initialization Graph. * @param init Initialization Graph.
*/ */
private Object makeGraph(Contract contracts[], private Object makeGraph(Contract contracts[],
InvocationHandler baseGraph, InvocationHandler baseGraph,
Class baseGraphType, Class baseGraphType,
boolean isWeighted) boolean isWeighted)
throws GraphException throws GraphException
{ {
int interfaceCount = contracts.length; int interfaceCount = contracts.length;
interfaceCount++;// BaseGraph Type interfaceCount++;// BaseGraph Type
if (isWeighted) if (isWeighted)
{ {
interfaceCount++; interfaceCount++;
}// WeightedGraph Type }// WeightedGraph Type
Class inter[] = new Class[interfaceCount]; Class inter[] = new Class[interfaceCount];
int pos = 0; int pos = 0;
for (pos = 0; pos < contracts.length; pos++) for (pos = 0; pos < contracts.length; pos++)
{ {
inter[pos] = contracts[pos].getInterface(); inter[pos] = contracts[pos].getInterface();
} }
if (isWeighted) if (isWeighted)
{ {
inter[pos] = org.apache.commons.graph.WeightedGraph.class; inter[pos] = org.apache.commons.graph.WeightedGraph.class;
pos++; pos++;
} }
inter[pos] = baseGraphType; inter[pos] = baseGraphType;
return Proxy.newProxyInstance(baseGraph.getClass().getClassLoader(), return Proxy.newProxyInstance(baseGraph.getClass().getClassLoader(),
inter, baseGraph); inter, baseGraph);
} }
/** /**
* makeDirectedGraph * makeDirectedGraph
* *
* @param contracts - Array of Contracts this Graph should meet. * @param contracts - Array of Contracts this Graph should meet.
* @param isWeighted - If true, the Graph will implement the WeightedGraph * @param isWeighted - If true, the Graph will implement the WeightedGraph
* interface. * interface.
* @param graph - If it is provided, the graph will initially be equal to * @param graph - If it is provided, the graph will initially be equal to
* the graph. * the graph.
*/ */
public DirectedGraph makeDirectedGraph(Contract contracts[], public DirectedGraph makeDirectedGraph(Contract contracts[],
boolean isWeighted, boolean isWeighted,
DirectedGraph graph) DirectedGraph graph)
throws GraphException throws GraphException
{ {
DirectedGraphImpl dgi = null; DirectedGraphImpl dgi = null;
if (graph != null) if (graph != null)
{ {
dgi = new DirectedGraphImpl(graph); dgi = new DirectedGraphImpl(graph);
} }
else else
{ {
dgi = new DirectedGraphImpl(); dgi = new DirectedGraphImpl();
} }
for (int i = 0; i < contracts.length; i++) for (int i = 0; i < contracts.length; i++)
{ {
dgi.addContract(contracts[i]); dgi.addContract(contracts[i]);
} }
return (DirectedGraph) return (DirectedGraph)
makeGraph(contracts, makeGraph(contracts,
dgi, org.apache.commons.graph.DirectedGraph.class, dgi, org.apache.commons.graph.DirectedGraph.class,
isWeighted); isWeighted);
} }
/** /**
* makeMutableDirectedGraph * makeMutableDirectedGraph
* *
* @param contracts - Array of Contracts this Graph should meet. * @param contracts - Array of Contracts this Graph should meet.
* @param isWeighted - If true, the Graph will implement the WeightedGraph * @param isWeighted - If true, the Graph will implement the WeightedGraph
* interface. * interface.
* @param graph - If it is provided, the graph will initially be equal to * @param graph - If it is provided, the graph will initially be equal to
* the graph. * the graph.
*/ */
public MutableDirectedGraph public MutableDirectedGraph
makeMutableDirectedGraph(Contract contracts[], makeMutableDirectedGraph(Contract contracts[],
boolean isWeighted, boolean isWeighted,
DirectedGraph graph) DirectedGraph graph)
throws GraphException throws GraphException
{ {
DirectedGraphImpl dgi = null; DirectedGraphImpl dgi = null;
if (graph != null) if (graph != null)
{ {
dgi = new DirectedGraphImpl(graph); dgi = new DirectedGraphImpl(graph);
} }
else else
{ {
dgi = new DirectedGraphImpl(); dgi = new DirectedGraphImpl();
} }
for (int i = 0; i < contracts.length; i++) for (int i = 0; i < contracts.length; i++)
{ {
dgi.addContract(contracts[i]); dgi.addContract(contracts[i]);
} }
return (MutableDirectedGraph) return (MutableDirectedGraph)
makeGraph(contracts, makeGraph(contracts,
dgi, dgi,
org.apache.commons.graph.MutableDirectedGraph.class, org.apache.commons.graph.MutableDirectedGraph.class,
isWeighted); isWeighted);
} }
} }

View File

@ -1,8 +1,8 @@
package org.apache.commons.graph.visualize; package org.apache.commons.graph.visualize;
import java.awt.Color; import java.awt.Color;
public interface Colored { public interface Colored {
public Color getBackgroundColor(); public Color getBackgroundColor();
public Color getTextColor(); public Color getTextColor();
} }

View File

@ -1,248 +1,248 @@
package org.apache.commons.graph.visualize; package org.apache.commons.graph.visualize;
import java.util.Random; import java.util.Random;
import java.util.Iterator; import java.util.Iterator;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.awt.Color; import java.awt.Color;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
/** /**
* Description of the Class * Description of the Class
*/ */
public class TouchGraph public class TouchGraph
{ {
private Color vertexColor = Color.yellow; private Color vertexColor = Color.yellow;
private Color textColor = Color.black; private Color textColor = Color.black;
private Color edgeColor = Color.red; private Color edgeColor = Color.red;
private double lengthFactor = 500.0; private double lengthFactor = 500.0;
private double defaultLength = 1.0; private double defaultLength = 1.0;
private int fontSize = 18; private int fontSize = 18;
private Random random = new Random(); private Random random = new Random();
/** /**
* Constructor for the TouchGraph object * Constructor for the TouchGraph object
*/ */
public TouchGraph() { } public TouchGraph() { }
/** /**
* Gets the colorText attribute of the TouchGraph object * Gets the colorText attribute of the TouchGraph object
*/ */
private String getColorText(Color color) private String getColorText(Color color)
{ {
String RC = Integer.toHexString( color.getRGB()).toUpperCase(); String RC = Integer.toHexString( color.getRGB()).toUpperCase();
return RC.substring( 2, 8 ); return RC.substring( 2, 8 );
} }
/** /**
* Sets the vertexColor attribute of the TouchGraph object * Sets the vertexColor attribute of the TouchGraph object
*/ */
public void setVertexColor(Color vertexColor) public void setVertexColor(Color vertexColor)
{ {
this.vertexColor = vertexColor; this.vertexColor = vertexColor;
} }
/** /**
* Gets the vertexColor attribute of the TouchGraph object * Gets the vertexColor attribute of the TouchGraph object
*/ */
public Color getVertexColor() public Color getVertexColor()
{ {
return this.vertexColor; return this.vertexColor;
} }
/** /**
* Gets the vertexColorText attribute of the TouchGraph object * Gets the vertexColorText attribute of the TouchGraph object
*/ */
private String getVertexColorText() private String getVertexColorText()
{ {
return getColorText(getVertexColor()); return getColorText(getVertexColor());
} }
/** /**
* Sets the textColor attribute of the TouchGraph object * Sets the textColor attribute of the TouchGraph object
*/ */
public void setTextColor(Color vertexColor) public void setTextColor(Color vertexColor)
{ {
this.vertexColor = vertexColor; this.vertexColor = vertexColor;
} }
/** /**
* Gets the textColor attribute of the TouchGraph object * Gets the textColor attribute of the TouchGraph object
*/ */
public Color getTextColor() public Color getTextColor()
{ {
return this.vertexColor; return this.vertexColor;
} }
/** /**
* Gets the textColorText attribute of the TouchGraph object * Gets the textColorText attribute of the TouchGraph object
*/ */
private String getTextColorText() private String getTextColorText()
{ {
return getColorText(getTextColor()); return getColorText(getTextColor());
} }
/** /**
* Sets the edgeColor attribute of the TouchGraph object * Sets the edgeColor attribute of the TouchGraph object
*/ */
public void setEdgeColor(Color edgeColor) public void setEdgeColor(Color edgeColor)
{ {
this.edgeColor = edgeColor; this.edgeColor = edgeColor;
} }
/** /**
* Gets the edgeColor attribute of the TouchGraph object * Gets the edgeColor attribute of the TouchGraph object
*/ */
public Color getEdgeColor() public Color getEdgeColor()
{ {
return this.edgeColor; return this.edgeColor;
} }
/** /**
* Gets the edgeColorText attribute of the TouchGraph object * Gets the edgeColorText attribute of the TouchGraph object
*/ */
private String getEdgeColorText() private String getEdgeColorText()
{ {
return getColorText(getEdgeColor()); return getColorText(getEdgeColor());
} }
/** /**
* Sets the fontSize attribute of the TouchGraph object * Sets the fontSize attribute of the TouchGraph object
*/ */
public void setFontSize(int size) public void setFontSize(int size)
{ {
this.fontSize = size; this.fontSize = size;
} }
/** /**
* Gets the fontSize attribute of the TouchGraph object * Gets the fontSize attribute of the TouchGraph object
*/ */
public int getFontSize() public int getFontSize()
{ {
return fontSize; return fontSize;
} }
/** /**
* Sets the defaultEdgeLength attribute of the TouchGraph object * Sets the defaultEdgeLength attribute of the TouchGraph object
*/ */
public void setDefaultEdgeLength(int length) public void setDefaultEdgeLength(int length)
{ {
this.defaultLength = length; this.defaultLength = length;
} }
/** /**
* Gets the defaultEdgeLength attribute of the TouchGraph object * Gets the defaultEdgeLength attribute of the TouchGraph object
*/ */
public double getDefaultEdgeLength() public double getDefaultEdgeLength()
{ {
return defaultLength; return defaultLength;
} }
/** /**
* Description of the Method * Description of the Method
*/ */
protected void writeNodeset(PrintWriter pw, protected void writeNodeset(PrintWriter pw,
DirectedGraph graph) DirectedGraph graph)
{ {
pw.println("<NODESET>"); pw.println("<NODESET>");
Iterator vertices = Iterator vertices =
graph.getVertices().iterator(); graph.getVertices().iterator();
while (vertices.hasNext()) while (vertices.hasNext())
{ {
Vertex v = (Vertex) vertices.next(); Vertex v = (Vertex) vertices.next();
pw.println("<NODE nodeID=\"" + v.toString() + "\">"); pw.println("<NODE nodeID=\"" + v.toString() + "\">");
pw.println("<NODE_LOCATION x=\"" + random.nextInt(200) + pw.println("<NODE_LOCATION x=\"" + random.nextInt(200) +
"\" y = \"" + random.nextInt(200) + "\" y = \"" + random.nextInt(200) +
"\" visible=\"true\" />"); "\" visible=\"true\" />");
String label; String label;
if (v instanceof Named) if (v instanceof Named)
{ {
label = ((Named) v).getName(); label = ((Named) v).getName();
} }
else else
{ {
label = v.toString(); label = v.toString();
} }
String backColor = null; String backColor = null;
String textColor = null; String textColor = null;
if (v instanceof Colored) { if (v instanceof Colored) {
backColor = getColorText(((Colored) v).getBackgroundColor()); backColor = getColorText(((Colored) v).getBackgroundColor());
textColor = getColorText(((Colored) v).getTextColor() ); textColor = getColorText(((Colored) v).getTextColor() );
} else { } else {
backColor = getVertexColorText(); backColor = getVertexColorText();
textColor = getTextColorText(); textColor = getTextColorText();
} }
pw.println("<NODE_LABEL label=\"" + label + "\" " + pw.println("<NODE_LABEL label=\"" + label + "\" " +
"shape=\"2\" " + "shape=\"2\" " +
"backColor=\"" + backColor + "\" " + "backColor=\"" + backColor + "\" " +
"textColor=\"" + textColor + "\" " + "textColor=\"" + textColor + "\" " +
"fontSize=\"" + fontSize + "\" />"); "fontSize=\"" + fontSize + "\" />");
pw.println("</NODE>"); pw.println("</NODE>");
} }
pw.println("</NODESET>"); pw.println("</NODESET>");
} }
/** /**
* Description of the Method * Description of the Method
*/ */
protected void writeEdgeset(PrintWriter pw, DirectedGraph graph) protected void writeEdgeset(PrintWriter pw, DirectedGraph graph)
{ {
pw.println("<EDGESET>"); pw.println("<EDGESET>");
Iterator edges = graph.getEdges().iterator(); Iterator edges = graph.getEdges().iterator();
while (edges.hasNext()) while (edges.hasNext())
{ {
Edge next = (Edge) edges.next(); Edge next = (Edge) edges.next();
int length = new Double(lengthFactor * int length = new Double(lengthFactor *
defaultLength).intValue(); defaultLength).intValue();
if (graph instanceof WeightedGraph) if (graph instanceof WeightedGraph)
{ {
length = length =
new Double(lengthFactor * new Double(lengthFactor *
((WeightedGraph) graph) ((WeightedGraph) graph)
.getWeight(next)).intValue(); .getWeight(next)).intValue();
} }
pw.println("<EDGE fromID=\"" + graph.getSource(next) + "\" " + pw.println("<EDGE fromID=\"" + graph.getSource(next) + "\" " +
"toID=\"" + graph.getTarget(next) + "\" " + "toID=\"" + graph.getTarget(next) + "\" " +
"type=\"2\" " + "type=\"2\" " +
"visible=\"true\" " + "visible=\"true\" " +
"length=\"" + length + "\" />"); "length=\"" + length + "\" />");
} }
pw.println("</EDGESET>"); pw.println("</EDGESET>");
} }
/** /**
* Description of the Method * Description of the Method
*/ */
public void toXML(DirectedGraph graph, public void toXML(DirectedGraph graph,
OutputStream os) OutputStream os)
{ {
PrintWriter pw = new PrintWriter(os); PrintWriter pw = new PrintWriter(os);
pw.println("<?xml version=\"1.0\"?>"); pw.println("<?xml version=\"1.0\"?>");
pw.println("<TOUCHGRAPH_LB version=\"1.20\">"); pw.println("<TOUCHGRAPH_LB version=\"1.20\">");
writeNodeset(pw, graph); writeNodeset(pw, graph);
writeEdgeset(pw, graph); writeEdgeset(pw, graph);
pw.println("</TOUCHGRAPH_LB>"); pw.println("</TOUCHGRAPH_LB>");
pw.flush(); pw.flush();
return; return;
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,367 +1,367 @@
package org.apache.commons.graph.algorithm.spanning; package org.apache.commons.graph.algorithm.spanning;
import java.util.Set; import java.util.Set;
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
import org.apache.commons.graph.exception.*; import org.apache.commons.graph.exception.*;
public class MinimumSpanningForestTest public class MinimumSpanningForestTest
extends WeightedGraphTest extends WeightedGraphTest
{ {
private String name = null; private String name = null;
public MinimumSpanningForestTest( String name ) { public MinimumSpanningForestTest( String name ) {
super( name ); super( name );
this.name = name; this.name = name;
} }
public void testNullGraph() throws Throwable { public void testNullGraph() throws Throwable {
WeightedGraph graph = (WeightedGraph) makeNullGraph(); WeightedGraph graph = (WeightedGraph) makeNullGraph();
MinimumSpanningForest msf = MinimumSpanningForest msf =
new MinimumSpanningForest( graph ); new MinimumSpanningForest( graph );
verifyGraph( msf, 0, 0 ); verifyGraph( msf, 0, 0 );
verifyChords( msf, makeSet() ); verifyChords( msf, makeSet() );
} }
public void testDirNullGraph() throws Throwable { public void testDirNullGraph() throws Throwable {
WeightedGraph graph = (WeightedGraph) makeDirNullGraph(); WeightedGraph graph = (WeightedGraph) makeDirNullGraph();
MinimumSpanningForest msf = MinimumSpanningForest msf =
new MinimumSpanningForest( graph ); new MinimumSpanningForest( graph );
verifyGraph( msf, 0, 0 ); verifyGraph( msf, 0, 0 );
verifyChords( msf, makeSet() ); verifyChords( msf, makeSet() );
} }
public void testSingleVertex() throws Throwable { public void testSingleVertex() throws Throwable {
WeightedGraph graph = (WeightedGraph) makeSingleVertex(); WeightedGraph graph = (WeightedGraph) makeSingleVertex();
MinimumSpanningForest msf = MinimumSpanningForest msf =
new MinimumSpanningForest( graph ); new MinimumSpanningForest( graph );
verifyGraph( msf, 1, 0 ); verifyGraph( msf, 1, 0 );
verifyChords( msf, makeSet() ); verifyChords( msf, makeSet() );
} }
public void testDirSingleVertex() throws Throwable { public void testDirSingleVertex() throws Throwable {
WeightedGraph graph = (WeightedGraph) makeDirSingleVertex(); WeightedGraph graph = (WeightedGraph) makeDirSingleVertex();
MinimumSpanningForest msf = MinimumSpanningForest msf =
new MinimumSpanningForest( graph ); new MinimumSpanningForest( graph );
verifyGraph( msf, 1, 0 ); verifyGraph( msf, 1, 0 );
verifyChords( msf, makeSet() ); verifyChords( msf, makeSet() );
} }
public void testSelfLoop() throws Throwable { public void testSelfLoop() throws Throwable {
WeightedGraph graph = (WeightedGraph) makeSelfLoop(); WeightedGraph graph = (WeightedGraph) makeSelfLoop();
MinimumSpanningForest msf = MinimumSpanningForest msf =
new MinimumSpanningForest( graph ); new MinimumSpanningForest( graph );
verifyGraph( msf, 1, 0 ); verifyGraph( msf, 1, 0 );
verifyChords(msf, makeSet( V1_V1 )); verifyChords(msf, makeSet( V1_V1 ));
} }
public void testDoubleVertex() throws Throwable { public void testDoubleVertex() throws Throwable {
WeightedGraph graph = (WeightedGraph) makeDoubleVertex(); WeightedGraph graph = (WeightedGraph) makeDoubleVertex();
MinimumSpanningForest msf = MinimumSpanningForest msf =
new MinimumSpanningForest( graph ); new MinimumSpanningForest( graph );
verifyGraph( msf, 2, 0 ); verifyGraph( msf, 2, 0 );
verifyChords(msf, makeSet() ); verifyChords(msf, makeSet() );
} }
public void testDirDoubleVertex() throws Throwable { public void testDirDoubleVertex() throws Throwable {
WeightedGraph graph = (WeightedGraph) makeDirDoubleVertex(); WeightedGraph graph = (WeightedGraph) makeDirDoubleVertex();
MinimumSpanningForest msf = MinimumSpanningForest msf =
new MinimumSpanningForest( graph ); new MinimumSpanningForest( graph );
verifyGraph( msf, 2, 0 ); verifyGraph( msf, 2, 0 );
verifyChords( msf, makeSet() ); verifyChords( msf, makeSet() );
} }
public void testSingleEdge() throws Throwable { public void testSingleEdge() throws Throwable {
WeightedGraph graph = (WeightedGraph) makeSingleEdge(); WeightedGraph graph = (WeightedGraph) makeSingleEdge();
MinimumSpanningForest msf = MinimumSpanningForest msf =
new MinimumSpanningForest( graph ); new MinimumSpanningForest( graph );
verifyGraph( msf, 2, 1 ); verifyGraph( msf, 2, 1 );
verifyChords( msf, makeSet()); verifyChords( msf, makeSet());
} }
public void testDirectedEdge() throws Throwable { public void testDirectedEdge() throws Throwable {
WeightedGraph graph = (WeightedGraph) makeDirectedEdge(); WeightedGraph graph = (WeightedGraph) makeDirectedEdge();
MinimumSpanningForest msf = MinimumSpanningForest msf =
new MinimumSpanningForest( graph ); new MinimumSpanningForest( graph );
verifyGraph( msf, 2, 1 ); verifyGraph( msf, 2, 1 );
verifyChords( msf, makeSet()); verifyChords( msf, makeSet());
} }
public void testParallelEdges() throws Throwable { public void testParallelEdges() throws Throwable {
WeightedGraph graph = (WeightedGraph) makeParallelEdges(); WeightedGraph graph = (WeightedGraph) makeParallelEdges();
MinimumSpanningForest msf = MinimumSpanningForest msf =
new MinimumSpanningForest( graph ); new MinimumSpanningForest( graph );
verifyGraph( msf, 2, 1 ); verifyGraph( msf, 2, 1 );
} }
public void testDirParallelEdges() throws Throwable { public void testDirParallelEdges() throws Throwable {
WeightedGraph graph = (WeightedGraph) makeDirParallelEdges(); WeightedGraph graph = (WeightedGraph) makeDirParallelEdges();
MinimumSpanningForest msf = MinimumSpanningForest msf =
new MinimumSpanningForest( graph ); new MinimumSpanningForest( graph );
verifyGraph( msf, 2, 1 ); verifyGraph( msf, 2, 1 );
} }
public void testCycle() throws Throwable { public void testCycle() throws Throwable {
WeightedGraph graph = (WeightedGraph) makeCycle(); WeightedGraph graph = (WeightedGraph) makeCycle();
MinimumSpanningForest msf = MinimumSpanningForest msf =
new MinimumSpanningForest( graph ); new MinimumSpanningForest( graph );
verifyGraph( msf, 3, 2 ); verifyGraph( msf, 3, 2 );
} }
public void testTwoCycle() throws Throwable { public void testTwoCycle() throws Throwable {
WeightedGraph graph = (WeightedGraph) makeTwoCycle(); WeightedGraph graph = (WeightedGraph) makeTwoCycle();
MinimumSpanningForest msf = MinimumSpanningForest msf =
new MinimumSpanningForest( graph ); new MinimumSpanningForest( graph );
verifyGraph( msf, 2, 1 ); verifyGraph( msf, 2, 1 );
} }
public void testDirectedCycle() throws Throwable { public void testDirectedCycle() throws Throwable {
WeightedGraph graph = (WeightedGraph) makeDirectedCycle(); WeightedGraph graph = (WeightedGraph) makeDirectedCycle();
MinimumSpanningForest msf = MinimumSpanningForest msf =
new MinimumSpanningForest( graph ); new MinimumSpanningForest( graph );
verifyGraph( msf, 3, 2 ); verifyGraph( msf, 3, 2 );
} }
public void testNoCycle() throws Throwable { public void testNoCycle() throws Throwable {
WeightedGraph graph = (WeightedGraph) makeNoCycle(); WeightedGraph graph = (WeightedGraph) makeNoCycle();
MinimumSpanningForest msf = MinimumSpanningForest msf =
new MinimumSpanningForest( graph ); new MinimumSpanningForest( graph );
verifyGraph( msf, 3, 2 ); verifyGraph( msf, 3, 2 );
} }
public void testPipe() throws Throwable { public void testPipe() throws Throwable {
WeightedGraph graph = (WeightedGraph) makePipe(); WeightedGraph graph = (WeightedGraph) makePipe();
MinimumSpanningForest msf = MinimumSpanningForest msf =
new MinimumSpanningForest( graph ); new MinimumSpanningForest( graph );
verifyGraph( msf, 3, 2 ); verifyGraph( msf, 3, 2 );
} }
public void testDiamond() throws Throwable { public void testDiamond() throws Throwable {
WeightedGraph graph = (WeightedGraph) makeDiamond(); WeightedGraph graph = (WeightedGraph) makeDiamond();
MinimumSpanningForest msf = MinimumSpanningForest msf =
new MinimumSpanningForest( graph ); new MinimumSpanningForest( graph );
verifyGraph( msf, 4, 3 ); verifyGraph( msf, 4, 3 );
} }
public void testPipelessCycle() throws Throwable { public void testPipelessCycle() throws Throwable {
WeightedGraph graph = (WeightedGraph) makePipelessCycle(); WeightedGraph graph = (WeightedGraph) makePipelessCycle();
MinimumSpanningForest msf = MinimumSpanningForest msf =
new MinimumSpanningForest( graph ); new MinimumSpanningForest( graph );
verifyGraph( msf, 4, 3 ); verifyGraph( msf, 4, 3 );
} }
public void testHyperGraph() throws Throwable { public void testHyperGraph() throws Throwable {
WeightedGraph graph = (WeightedGraph) makeHyperGraph(); WeightedGraph graph = (WeightedGraph) makeHyperGraph();
try { try {
MinimumSpanningForest msf = MinimumSpanningForest msf =
new MinimumSpanningForest( graph ); new MinimumSpanningForest( graph );
fail("HyperGraph Exception not thrown."); fail("HyperGraph Exception not thrown.");
} catch (HyperGraphException hge) { } } catch (HyperGraphException hge) { }
} }
public void testWDirectedEdge() public void testWDirectedEdge()
throws Throwable throws Throwable
{ {
WeightedGraph graph = makeWDirectedEdge(); WeightedGraph graph = makeWDirectedEdge();
MinimumSpanningForest msf = MinimumSpanningForest msf =
new MinimumSpanningForest( graph ); new MinimumSpanningForest( graph );
verifyGraph( msf, 2, 1 ); verifyGraph( msf, 2, 1 );
verifyEdges( msf, makeSet( V1_V2 ) ); verifyEdges( msf, makeSet( V1_V2 ) );
assertEquals("Edge Weight not the same.", assertEquals("Edge Weight not the same.",
5.0, msf.getWeight(V1_V2), 0.00001 ); 5.0, msf.getWeight(V1_V2), 0.00001 );
verifyChords( msf, makeSet() ); verifyChords( msf, makeSet() );
} }
public void testWSelfLoop() public void testWSelfLoop()
throws Throwable throws Throwable
{ {
MinimumSpanningForest IUT = MinimumSpanningForest IUT =
new MinimumSpanningForest( makeWSelfLoop() ); new MinimumSpanningForest( makeWSelfLoop() );
verifyGraph(IUT, 1, 0); verifyGraph(IUT, 1, 0);
verifyEdges(IUT, makeSet() ); verifyEdges(IUT, makeSet() );
verifyChords(IUT, makeSet( V1_V1 )); verifyChords(IUT, makeSet( V1_V1 ));
} }
public void testPositiveCycle() public void testPositiveCycle()
throws Throwable throws Throwable
{ {
MinimumSpanningForest IUT = MinimumSpanningForest IUT =
new MinimumSpanningForest( makePositiveCycle() ); new MinimumSpanningForest( makePositiveCycle() );
verifyGraph( IUT, 3, 2 ); verifyGraph( IUT, 3, 2 );
verifyEdges( IUT, makeSet( V3_V1, V1_V2 )); verifyEdges( IUT, makeSet( V3_V1, V1_V2 ));
verifyChords( IUT, makeSet( V2_V3 )); verifyChords( IUT, makeSet( V2_V3 ));
} }
public void testPositivePartNegCycle() public void testPositivePartNegCycle()
throws Throwable throws Throwable
{ {
MinimumSpanningForest IUT = MinimumSpanningForest IUT =
new MinimumSpanningForest( makePositivePartNegCycle() ); new MinimumSpanningForest( makePositivePartNegCycle() );
verifyGraph( IUT, 3, 2 ); verifyGraph( IUT, 3, 2 );
verifyEdges( IUT, makeSet( V3_V1, V1_V2 ) ); verifyEdges( IUT, makeSet( V3_V1, V1_V2 ) );
verifyChords( IUT, makeSet( V2_V3 )); verifyChords( IUT, makeSet( V2_V3 ));
} }
public void testNegativeCycle() public void testNegativeCycle()
throws Throwable throws Throwable
{ {
MinimumSpanningForest IUT = MinimumSpanningForest IUT =
new MinimumSpanningForest( makeNegativeCycle() ); new MinimumSpanningForest( makeNegativeCycle() );
verifyGraph( IUT, 3, 2 ); verifyGraph( IUT, 3, 2 );
verifyEdges( IUT, makeSet( V2_V3, V1_V2 )); verifyEdges( IUT, makeSet( V2_V3, V1_V2 ));
verifyChords( IUT, makeSet( V3_V1 )); verifyChords( IUT, makeSet( V3_V1 ));
} }
public void testNegativePartPosCycle() public void testNegativePartPosCycle()
throws Throwable throws Throwable
{ {
MinimumSpanningForest IUT = MinimumSpanningForest IUT =
new MinimumSpanningForest( makeNegativePartPosCycle() ); new MinimumSpanningForest( makeNegativePartPosCycle() );
verifyGraph( IUT, 3, 2 ); verifyGraph( IUT, 3, 2 );
verifyEdges( IUT, makeSet( V2_V3, V1_V2 )); verifyEdges( IUT, makeSet( V2_V3, V1_V2 ));
verifyChords( IUT, makeSet( V3_V1 )); verifyChords( IUT, makeSet( V3_V1 ));
} }
public void testPositivePipe() public void testPositivePipe()
throws Throwable throws Throwable
{ {
MinimumSpanningForest IUT = MinimumSpanningForest IUT =
new MinimumSpanningForest( makePositivePipe() ); new MinimumSpanningForest( makePositivePipe() );
verifyGraph( IUT, 3, 2 ); verifyGraph( IUT, 3, 2 );
verifyEdges( IUT, makeSet( V1_V2, V2_V3 )); verifyEdges( IUT, makeSet( V1_V2, V2_V3 ));
verifyChords(IUT, makeSet()); verifyChords(IUT, makeSet());
} }
public void testPositivePartNegPipe() public void testPositivePartNegPipe()
throws Throwable throws Throwable
{ {
MinimumSpanningForest IUT = MinimumSpanningForest IUT =
new MinimumSpanningForest( makePositivePartNegPipe() ); new MinimumSpanningForest( makePositivePartNegPipe() );
verifyGraph( IUT, 3, 2 ); verifyGraph( IUT, 3, 2 );
verifyEdges( IUT, makeSet( V1_V2, V2_V3 )); verifyEdges( IUT, makeSet( V1_V2, V2_V3 ));
verifyChords(IUT, makeSet()); verifyChords(IUT, makeSet());
} }
public void testNegativePipe() public void testNegativePipe()
throws Throwable throws Throwable
{ {
MinimumSpanningForest IUT = MinimumSpanningForest IUT =
new MinimumSpanningForest( makeNegativePipe() ); new MinimumSpanningForest( makeNegativePipe() );
verifyGraph( IUT, 3, 2 ); verifyGraph( IUT, 3, 2 );
verifyEdges( IUT, makeSet( V1_V2, V2_V3 )); verifyEdges( IUT, makeSet( V1_V2, V2_V3 ));
verifyChords(IUT, makeSet()); verifyChords(IUT, makeSet());
} }
public void testNegativePartPosPipe() public void testNegativePartPosPipe()
throws Throwable throws Throwable
{ {
MinimumSpanningForest IUT = MinimumSpanningForest IUT =
new MinimumSpanningForest( makeNegativePartPosPipe() ); new MinimumSpanningForest( makeNegativePartPosPipe() );
verifyGraph( IUT, 3, 2 ); verifyGraph( IUT, 3, 2 );
verifyEdges( IUT, makeSet( V1_V2, V2_V3 )); verifyEdges( IUT, makeSet( V1_V2, V2_V3 ));
verifyChords(IUT, makeSet()); verifyChords(IUT, makeSet());
} }
public void testMultiplePathL() public void testMultiplePathL()
throws Throwable throws Throwable
{ {
MinimumSpanningForest IUT = MinimumSpanningForest IUT =
new MinimumSpanningForest( makeMultiplePathL() ); new MinimumSpanningForest( makeMultiplePathL() );
verifyGraph( IUT, 4, 3 ); verifyGraph( IUT, 4, 3 );
verifyEdges( IUT, makeSet( V1_V2, V2_V4, V1_V3 )); verifyEdges( IUT, makeSet( V1_V2, V2_V4, V1_V3 ));
verifyChords(IUT, makeSet( V3_V4 )); verifyChords(IUT, makeSet( V3_V4 ));
} }
public void testMultiplePathR() public void testMultiplePathR()
throws Throwable throws Throwable
{ {
MinimumSpanningForest IUT = MinimumSpanningForest IUT =
new MinimumSpanningForest( makeMultiplePathR() ); new MinimumSpanningForest( makeMultiplePathR() );
verifyGraph( IUT, 4, 3 ); verifyGraph( IUT, 4, 3 );
verifyEdges( IUT, makeSet( V1_V3, V3_V4, V2_V4 )); verifyEdges( IUT, makeSet( V1_V3, V3_V4, V2_V4 ));
verifyChords(IUT, makeSet( V1_V2 )); verifyChords(IUT, makeSet( V1_V2 ));
} }
public void testMultiplePathEarlyLow() public void testMultiplePathEarlyLow()
throws Throwable throws Throwable
{ {
MinimumSpanningForest IUT = MinimumSpanningForest IUT =
new MinimumSpanningForest( makeMultiplePathEarlyLow() ); new MinimumSpanningForest( makeMultiplePathEarlyLow() );
verifyGraph( IUT, 4, 3 ); verifyGraph( IUT, 4, 3 );
verifyEdges( IUT, makeSet( V1_V2, V2_V4, V1_V3 )); verifyEdges( IUT, makeSet( V1_V2, V2_V4, V1_V3 ));
verifyChords(IUT, makeSet( V3_V4 )); verifyChords(IUT, makeSet( V3_V4 ));
} }
public void testMaxMultiplePathEarlyLow() public void testMaxMultiplePathEarlyLow()
throws Throwable throws Throwable
{ {
MinimumSpanningForest IUT = MinimumSpanningForest IUT =
new MinimumSpanningForest( false, new MinimumSpanningForest( false,
makeMultiplePathEarlyLow() ); makeMultiplePathEarlyLow() );
verifyGraph( IUT, 4, 3 ); verifyGraph( IUT, 4, 3 );
verifyEdges( IUT, makeSet( V1_V2, V2_V4, V3_V4 )); verifyEdges( IUT, makeSet( V1_V2, V2_V4, V3_V4 ));
verifyChords(IUT, makeSet( V1_V3 )); verifyChords(IUT, makeSet( V1_V3 ));
} }
public void testMultiplePathEarlyHigh() public void testMultiplePathEarlyHigh()
throws Throwable throws Throwable
{ {
MinimumSpanningForest IUT = MinimumSpanningForest IUT =
new MinimumSpanningForest( makeMultiplePathEarlyHigh() ); new MinimumSpanningForest( makeMultiplePathEarlyHigh() );
verifyGraph( IUT, 4, 3 ); verifyGraph( IUT, 4, 3 );
verifyEdges( IUT, makeSet( V1_V2, V2_V4, V3_V4 ) ); verifyEdges( IUT, makeSet( V1_V2, V2_V4, V3_V4 ) );
verifyChords(IUT, makeSet( V1_V3 )); verifyChords(IUT, makeSet( V1_V3 ));
} }
public void testMaxMultiplePathEarlyHigh() public void testMaxMultiplePathEarlyHigh()
throws Throwable throws Throwable
{ {
MinimumSpanningForest IUT = MinimumSpanningForest IUT =
new MinimumSpanningForest( false, new MinimumSpanningForest( false,
makeMultiplePathEarlyHigh()); makeMultiplePathEarlyHigh());
verifyGraph( IUT, 4, 3 ); verifyGraph( IUT, 4, 3 );
verifyEdges( IUT, makeSet( V1_V2, V2_V4, V1_V3 )); verifyEdges( IUT, makeSet( V1_V2, V2_V4, V1_V3 ));
verifyChords(IUT, makeSet( V3_V4 )); verifyChords(IUT, makeSet( V3_V4 ));
} }
// Helpers. . . // Helpers. . .
public void verifyEdges(Graph g, public void verifyEdges(Graph g,
Set edges) throws Throwable { Set edges) throws Throwable {
Set gEdges = g.getEdges(); Set gEdges = g.getEdges();
assertTrue( "Graph has edges not in expected set.", assertTrue( "Graph has edges not in expected set.",
gEdges.containsAll( edges )); gEdges.containsAll( edges ));
assertTrue( "Graph does not have expected edges.", assertTrue( "Graph does not have expected edges.",
edges.containsAll( edges )); edges.containsAll( edges ));
} }
public void verifyChords(MinimumSpanningForest msf, public void verifyChords(MinimumSpanningForest msf,
Set chords) throws Throwable { Set chords) throws Throwable {
Set gChords = msf.getChords(); Set gChords = msf.getChords();
assertTrue( "Graph has chords not in expected set.", assertTrue( "Graph has chords not in expected set.",
gChords.containsAll( chords )); gChords.containsAll( chords ));
assertTrue( "Graph does not have expected edges.", assertTrue( "Graph does not have expected edges.",
chords.containsAll( gChords )); chords.containsAll( gChords ));
} }
} }

View File

@ -1,150 +1,150 @@
package org.apache.commons.graph.algorithm.util; package org.apache.commons.graph.algorithm.util;
import junit.framework.*; import junit.framework.*;
public class LabelTest public class LabelTest
extends TestCase extends TestCase
{ {
private String testName = null; private String testName = null;
public LabelTest( String testName ) { public LabelTest( String testName ) {
super( testName ); super( testName );
this.testName = testName; this.testName = testName;
} }
private Label L1; private Label L1;
private Label L1_; private Label L1_;
private Label L1__; private Label L1__;
private Label L2; private Label L2;
private Label L3; private Label L3;
private Label L4; private Label L4;
private Label L5; private Label L5;
private Label L6; private Label L6;
public void setUp() { public void setUp() {
L1 = new Label(); L1 = new Label();
L2 = new Label(); L2 = new Label();
L3 = new Label(); L3 = new Label();
L4 = new Label(); L4 = new Label();
L5 = new Label(); L5 = new Label();
L6 = new Label(); L6 = new Label();
L1_ = new Label( L1 ); L1_ = new Label( L1 );
L1__ = new Label( L1_ ); L1__ = new Label( L1_ );
} }
public void testDefaultConfig() public void testDefaultConfig()
throws Throwable throws Throwable
{ {
assertEquals( L1.getRoot(), L1 ); assertEquals( L1.getRoot(), L1 );
assertEquals( L1_.getRoot(), L1 ); assertEquals( L1_.getRoot(), L1 );
assertEquals( L1__.getRoot(), L1 ); assertEquals( L1__.getRoot(), L1 );
} }
public void testSetRoot1() public void testSetRoot1()
throws Throwable throws Throwable
{ {
assertEquals( L1.getRoot(), L1 ); assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L2 ); assertEquals( L2.getRoot(), L2 );
L2.setRoot( L1 ); L2.setRoot( L1 );
assertEquals( L2.getRoot(), L1 ); assertEquals( L2.getRoot(), L1 );
} }
public void testSetRoot2() public void testSetRoot2()
throws Throwable throws Throwable
{ {
assertEquals( L1.getRoot(), L1 ); assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L2 ); assertEquals( L2.getRoot(), L2 );
assertEquals( L3.getRoot(), L3 ); assertEquals( L3.getRoot(), L3 );
L3.setRoot( L2 ); L3.setRoot( L2 );
L2.setRoot( L1 ); L2.setRoot( L1 );
assertEquals( L3.getRoot(), L1 ); assertEquals( L3.getRoot(), L1 );
assertEquals( L2.getRoot(), L1 ); assertEquals( L2.getRoot(), L1 );
assertEquals( L1.getRoot(), L1 ); assertEquals( L1.getRoot(), L1 );
} }
public void testSetRoot3() public void testSetRoot3()
throws Throwable throws Throwable
{ {
assertEquals( L1.getRoot(), L1 ); assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L2 ); assertEquals( L2.getRoot(), L2 );
assertEquals( L3.getRoot(), L3 ); assertEquals( L3.getRoot(), L3 );
L2.setRoot( L1 ); L2.setRoot( L1 );
L3.setRoot( L2 ); L3.setRoot( L2 );
assertEquals( L3.getRoot(), L1 ); assertEquals( L3.getRoot(), L1 );
assertEquals( L2.getRoot(), L1 ); assertEquals( L2.getRoot(), L1 );
assertEquals( L1.getRoot(), L1 ); assertEquals( L1.getRoot(), L1 );
} }
public void testSetRoot4() public void testSetRoot4()
throws Throwable throws Throwable
{ {
assertEquals( L1.getRoot(), L1 ); assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L2 ); assertEquals( L2.getRoot(), L2 );
assertEquals( L3.getRoot(), L3 ); assertEquals( L3.getRoot(), L3 );
assertEquals( L4.getRoot(), L4 ); assertEquals( L4.getRoot(), L4 );
L4.setRoot( L3 ); L4.setRoot( L3 );
L2.setRoot( L1 ); L2.setRoot( L1 );
L3.setRoot( L1 ); L3.setRoot( L1 );
assertEquals( L4.getRoot(), L1 ); assertEquals( L4.getRoot(), L1 );
assertEquals( L3.getRoot(), L1 ); assertEquals( L3.getRoot(), L1 );
assertEquals( L2.getRoot(), L1 ); assertEquals( L2.getRoot(), L1 );
assertEquals( L1.getRoot(), L1 ); assertEquals( L1.getRoot(), L1 );
} }
public void testSetRootCycle() public void testSetRootCycle()
throws Throwable throws Throwable
{ {
assertEquals( L1.getRoot(), L1 ); assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L2 ); assertEquals( L2.getRoot(), L2 );
L1.setRoot( L2 ); L1.setRoot( L2 );
L2.setRoot( L1 ); L2.setRoot( L1 );
assertEquals( L2.getRoot(), L2 ); assertEquals( L2.getRoot(), L2 );
assertEquals( L1.getRoot(), L2 ); assertEquals( L1.getRoot(), L2 );
} }
public void setDiamond1() throws Throwable { public void setDiamond1() throws Throwable {
assertEquals( L1.getRoot(), L1 ); assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L2 ); assertEquals( L2.getRoot(), L2 );
assertEquals( L3.getRoot(), L3 ); assertEquals( L3.getRoot(), L3 );
assertEquals( L4.getRoot(), L4 ); assertEquals( L4.getRoot(), L4 );
L2.setRoot( L1 ); L2.setRoot( L1 );
L3.setRoot( L1 ); L3.setRoot( L1 );
L4.setRoot( L3 ); L4.setRoot( L3 );
assertEquals( L1.getRoot(), L1 ); assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L1 ); assertEquals( L2.getRoot(), L1 );
assertEquals( L3.getRoot(), L1 ); assertEquals( L3.getRoot(), L1 );
assertEquals( L4.getRoot(), L1 ); assertEquals( L4.getRoot(), L1 );
} }
public void setDiamond2() throws Throwable { public void setDiamond2() throws Throwable {
assertEquals( L1.getRoot(), L1 ); assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L2 ); assertEquals( L2.getRoot(), L2 );
assertEquals( L3.getRoot(), L3 ); assertEquals( L3.getRoot(), L3 );
assertEquals( L4.getRoot(), L4 ); assertEquals( L4.getRoot(), L4 );
L4.setRoot( L3 ); L4.setRoot( L3 );
L2.setRoot( L1 ); L2.setRoot( L1 );
L3.setRoot( L1 ); L3.setRoot( L1 );
assertEquals( L1.getRoot(), L1 ); assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L1 ); assertEquals( L2.getRoot(), L1 );
assertEquals( L3.getRoot(), L1 ); assertEquals( L3.getRoot(), L1 );
assertEquals( L4.getRoot(), L1 ); assertEquals( L4.getRoot(), L1 );
} }
} }

View File

@ -1,5 +1,5 @@
package org.apache.commons.graph.contract; package org.apache.commons.graph.contract;
/* /*
* Copyright 2001,2004 The Apache Software Foundation. * Copyright 2001,2004 The Apache Software Foundation.
* *
@ -14,410 +14,410 @@ package org.apache.commons.graph.contract;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
import org.apache.commons.graph.exception.*; import org.apache.commons.graph.exception.*;
/** /**
* Description of the Class * Description of the Class
*/ */
public class AcyclicContractTest public class AcyclicContractTest
extends GraphTest extends GraphTest
{ {
/** /**
* Constructor for the AcyclicContractTest object * Constructor for the AcyclicContractTest object
* *
* @param name * @param name
*/ */
public AcyclicContractTest(String name) public AcyclicContractTest(String name)
{ {
super(name); super(name);
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testDirNullGraph() public void testDirNullGraph()
throws Throwable throws Throwable
{ {
AcyclicContract IUT = AcyclicContract IUT =
new AcyclicContract(); new AcyclicContract();
IUT.setImpl(makeDirNullGraph()); IUT.setImpl(makeDirNullGraph());
IUT.verify(); IUT.verify();
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testDirSingleVertex() public void testDirSingleVertex()
throws Throwable throws Throwable
{ {
AcyclicContract IUT = AcyclicContract IUT =
new AcyclicContract(); new AcyclicContract();
IUT.setImpl(makeDirSingleVertex()); IUT.setImpl(makeDirSingleVertex());
IUT.verify(); IUT.verify();
try try
{ {
IUT.addEdge(V1_V1, V1, V1); IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced."); fail("No GraphException thrown when Self-Cycle introduced.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testSelfLoop() public void testSelfLoop()
throws Throwable throws Throwable
{ {
AcyclicContract IUT = AcyclicContract IUT =
new AcyclicContract(); new AcyclicContract();
IUT.setImpl(makeSelfLoop()); IUT.setImpl(makeSelfLoop());
try try
{ {
IUT.verify(); IUT.verify();
fail("No CycleException thrown on Verification."); fail("No CycleException thrown on Verification.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testDirDoubleVertex() public void testDirDoubleVertex()
throws Throwable throws Throwable
{ {
AcyclicContract IUT = AcyclicContract IUT =
new AcyclicContract(); new AcyclicContract();
IUT.setImpl(makeDirDoubleVertex()); IUT.setImpl(makeDirDoubleVertex());
IUT.verify(); IUT.verify();
try try
{ {
IUT.addEdge(V1_V1, V1, V1); IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced."); fail("No GraphException thrown when Self-Cycle introduced.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
try try
{ {
IUT.addEdge(V1_V2, V1, V2); IUT.addEdge(V1_V2, V1, V2);
} }
catch (GraphException ex) catch (GraphException ex)
{ {
fail("Contract prevented adding of valid edge. V1->V2"); fail("Contract prevented adding of valid edge. V1->V2");
} }
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testDirectedEdge() public void testDirectedEdge()
throws Throwable throws Throwable
{ {
AcyclicContract IUT = AcyclicContract IUT =
new AcyclicContract(); new AcyclicContract();
IUT.setImpl(makeDirectedEdge()); IUT.setImpl(makeDirectedEdge());
IUT.verify(); IUT.verify();
try try
{ {
IUT.addEdge(V1_V1, V1, V1); IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced."); fail("No GraphException thrown when Self-Cycle introduced.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
try try
{ {
IUT.addEdge(V1_V2_, V1, V2); IUT.addEdge(V1_V2_, V1, V2);
} }
catch (GraphException ex) catch (GraphException ex)
{ {
fail("Contract prevented adding of valid edge. V1->V2'"); fail("Contract prevented adding of valid edge. V1->V2'");
} }
try try
{ {
IUT.addEdge(V2_V1, V2, V1); IUT.addEdge(V2_V1, V2, V1);
fail("Contract allowed cycle to be introduced."); fail("Contract allowed cycle to be introduced.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testDirParallelEdges() public void testDirParallelEdges()
throws Throwable throws Throwable
{ {
AcyclicContract IUT = AcyclicContract IUT =
new AcyclicContract(); new AcyclicContract();
IUT.setImpl(makeDirParallelEdges()); IUT.setImpl(makeDirParallelEdges());
IUT.verify(); IUT.verify();
try try
{ {
IUT.addEdge(V1_V1, V1, V1); IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced."); fail("No GraphException thrown when Self-Cycle introduced.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
try try
{ {
IUT.addEdge(V1_V2__, V1, V2); IUT.addEdge(V1_V2__, V1, V2);
} }
catch (GraphException ex) catch (GraphException ex)
{ {
fail("Contract prevented adding of valid edge. V1->V2'"); fail("Contract prevented adding of valid edge. V1->V2'");
} }
try try
{ {
IUT.addEdge(V2_V1, V2, V1); IUT.addEdge(V2_V1, V2, V1);
fail("Contract allowed cycle to be introduced."); fail("Contract allowed cycle to be introduced.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testTwoCycle() public void testTwoCycle()
throws Throwable throws Throwable
{ {
AcyclicContract IUT = AcyclicContract IUT =
new AcyclicContract(); new AcyclicContract();
IUT.setImpl(makeTwoCycle()); IUT.setImpl(makeTwoCycle());
try try
{ {
IUT.verify(); IUT.verify();
fail("No CycleException thrown on Verification."); fail("No CycleException thrown on Verification.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testDirectedCycle() public void testDirectedCycle()
throws Throwable throws Throwable
{ {
AcyclicContract IUT = AcyclicContract IUT =
new AcyclicContract(); new AcyclicContract();
IUT.setImpl(makeDirectedCycle()); IUT.setImpl(makeDirectedCycle());
try try
{ {
IUT.verify(); IUT.verify();
fail("No CycleException thrown on Verification."); fail("No CycleException thrown on Verification.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testPipe() public void testPipe()
throws Throwable throws Throwable
{ {
AcyclicContract IUT = AcyclicContract IUT =
new AcyclicContract(); new AcyclicContract();
IUT.setImpl(makePipe()); IUT.setImpl(makePipe());
IUT.verify(); IUT.verify();
try try
{ {
IUT.addEdge(V1_V1, V1, V1); IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced."); fail("No GraphException thrown when Self-Cycle introduced.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
try try
{ {
IUT.addEdge(V1_V2_, V1, V2); IUT.addEdge(V1_V2_, V1, V2);
} }
catch (GraphException ex) catch (GraphException ex)
{ {
fail("Contract prevented adding of valid edge. V1->V2'"); fail("Contract prevented adding of valid edge. V1->V2'");
} }
try try
{ {
IUT.addEdge(V3_V1, V3, V1); IUT.addEdge(V3_V1, V3, V1);
fail("Contract allowed cycle to be introduced."); fail("Contract allowed cycle to be introduced.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testDiamond() public void testDiamond()
throws Throwable throws Throwable
{ {
AcyclicContract IUT = AcyclicContract IUT =
new AcyclicContract(); new AcyclicContract();
IUT.setImpl(makeDiamond()); IUT.setImpl(makeDiamond());
IUT.verify(); IUT.verify();
try try
{ {
IUT.addEdge(V1_V1, V1, V1); IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced."); fail("No GraphException thrown when Self-Cycle introduced.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
try try
{ {
IUT.addEdge(V2_V3, V2, V3); IUT.addEdge(V2_V3, V2, V3);
} }
catch (GraphException ex) catch (GraphException ex)
{ {
fail("Contract prevented adding of valid edge. V2->V3"); fail("Contract prevented adding of valid edge. V2->V3");
} }
try try
{ {
IUT.addEdge(V4_V1, V4, V1); IUT.addEdge(V4_V1, V4, V1);
fail("Contract allowed cycle to be introduced."); fail("Contract allowed cycle to be introduced.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testPipelessCycle() public void testPipelessCycle()
throws Throwable throws Throwable
{ {
AcyclicContract IUT = AcyclicContract IUT =
new AcyclicContract(); new AcyclicContract();
IUT.setImpl(makePipelessCycle()); IUT.setImpl(makePipelessCycle());
IUT.verify(); IUT.verify();
try try
{ {
IUT.addEdge(V1_V1, V1, V1); IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced."); fail("No GraphException thrown when Self-Cycle introduced.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
try try
{ {
IUT.addEdge(V2_V3, V2, V3); IUT.addEdge(V2_V3, V2, V3);
} }
catch (GraphException ex) catch (GraphException ex)
{ {
fail("Contract prevented adding of valid edge. V2->V3"); fail("Contract prevented adding of valid edge. V2->V3");
} }
try try
{ {
IUT.addEdge(V3_V4, V3, V4); IUT.addEdge(V3_V4, V3, V4);
fail("Contract allowed cycle to be introduced."); fail("Contract allowed cycle to be introduced.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testParentTree() public void testParentTree()
throws Throwable throws Throwable
{ {
System.err.println("---- PARENT TREE ----"); System.err.println("---- PARENT TREE ----");
AcyclicContract IUT = AcyclicContract IUT =
new AcyclicContract(); new AcyclicContract();
IUT.setImpl(makeParentTree()); IUT.setImpl(makeParentTree());
IUT.verify(); IUT.verify();
try try
{ {
IUT.addEdge(V1_V1, V1, V1); IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced."); fail("No GraphException thrown when Self-Cycle introduced.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
try try
{ {
IUT.addEdge(V2_V3, V2, V3); IUT.addEdge(V2_V3, V2, V3);
} }
catch (GraphException ex) catch (GraphException ex)
{ {
fail("Contract prevented adding of valid edge. V2->V3"); fail("Contract prevented adding of valid edge. V2->V3");
} }
try try
{ {
IUT.addEdge(V1_V5, V1, V5); IUT.addEdge(V1_V5, V1, V5);
fail("Contract allowed cycle to be introduced."); fail("Contract allowed cycle to be introduced.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testChildTree() public void testChildTree()
throws Throwable throws Throwable
{ {
AcyclicContract IUT = AcyclicContract IUT =
new AcyclicContract(); new AcyclicContract();
IUT.setImpl(makeChildTree()); IUT.setImpl(makeChildTree());
IUT.verify(); IUT.verify();
try try
{ {
IUT.addEdge(V1_V1, V1, V1); IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced."); fail("No GraphException thrown when Self-Cycle introduced.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
try try
{ {
IUT.addEdge(V2_V3, V2, V3); IUT.addEdge(V2_V3, V2, V3);
} }
catch (GraphException ex) catch (GraphException ex)
{ {
fail("Contract prevented adding of valid edge. V2->V3"); fail("Contract prevented adding of valid edge. V2->V3");
} }
try try
{ {
IUT.addEdge(V5_V1, V5, V1); IUT.addEdge(V5_V1, V5, V1);
fail("Contract allowed cycle to be introduced."); fail("Contract allowed cycle to be introduced.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
} }
} }

View File

@ -1,275 +1,275 @@
package org.apache.commons.graph.contract; package org.apache.commons.graph.contract;
/** /**
* DAGTest This test looks to verify that yes indeed contracts are being called * DAGTest This test looks to verify that yes indeed contracts are being called
* when created through the GraphFactory. * when created through the GraphFactory.
*/ */
import org.apache.commons.graph.*; import org.apache.commons.graph.*;
import org.apache.commons.graph.contract.*; import org.apache.commons.graph.contract.*;
import org.apache.commons.graph.exception.*; import org.apache.commons.graph.exception.*;
import org.apache.commons.graph.factory.*; import org.apache.commons.graph.factory.*;
/** /**
* Description of the Class * Description of the Class
*/ */
public class DAGTest public class DAGTest
extends GraphTest extends GraphTest
{ {
private Contract[] dagContracts = new Contract[1]; private Contract[] dagContracts = new Contract[1];
private GraphFactory factory = new GraphFactory(); private GraphFactory factory = new GraphFactory();
private String testName = null; private String testName = null;
/** /**
* Constructor for the DAGTest object * Constructor for the DAGTest object
* *
* @param name * @param name
*/ */
public DAGTest(String name) public DAGTest(String name)
{ {
super(name); super(name);
this.testName = name; this.testName = name;
} }
/** /**
* The JUnit setup method * The JUnit setup method
*/ */
public void setUp() public void setUp()
{ {
dagContracts[0] = new AcyclicContract(); dagContracts[0] = new AcyclicContract();
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testDAGSelfLoop() public void testDAGSelfLoop()
throws Throwable throws Throwable
{ {
DirectedGraph dg = null; DirectedGraph dg = null;
try try
{ {
try try
{ {
dg = factory.makeDirectedGraph(dagContracts, dg = factory.makeDirectedGraph(dagContracts,
true, true,
makeSelfLoop()); makeSelfLoop());
fail("Should not have created DAG."); fail("Should not have created DAG.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
} }
catch (Throwable ex) catch (Throwable ex)
{ {
printGraph(ex, dg); printGraph(ex, dg);
throw ex; throw ex;
} }
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testDAGTwoLoop() public void testDAGTwoLoop()
throws Throwable throws Throwable
{ {
DirectedGraph dg = null; DirectedGraph dg = null;
try try
{ {
try try
{ {
dg = factory.makeDirectedGraph(dagContracts, dg = factory.makeDirectedGraph(dagContracts,
true, true,
makeTwoCycle()); makeTwoCycle());
fail("Should not have created DAG."); fail("Should not have created DAG.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
} }
catch (Throwable ex) catch (Throwable ex)
{ {
printGraph(ex, dg); printGraph(ex, dg);
throw ex; throw ex;
} }
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testMakeDAGDirCycle() public void testMakeDAGDirCycle()
throws Throwable throws Throwable
{ {
MutableDirectedGraph mdg = null; MutableDirectedGraph mdg = null;
try try
{ {
mdg = factory.makeMutableDirectedGraph(dagContracts, mdg = factory.makeMutableDirectedGraph(dagContracts,
true, true,
null); null);
mdg.addVertex(V1); mdg.addVertex(V1);
mdg.addVertex(V2); mdg.addVertex(V2);
mdg.addEdge(V1_V2, V1, V2); mdg.addEdge(V1_V2, V1, V2);
mdg.addVertex(V3); mdg.addVertex(V3);
mdg.addEdge(V2_V3, V2, V3); mdg.addEdge(V2_V3, V2, V3);
try try
{ {
mdg.addEdge(V3_V1, V3, V1); mdg.addEdge(V3_V1, V3, V1);
fail("No CycleException thrown on introduction of cycle."); fail("No CycleException thrown on introduction of cycle.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
} }
catch (Throwable t) catch (Throwable t)
{ {
printGraph(t, mdg); printGraph(t, mdg);
} }
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testDAGDirCycle() public void testDAGDirCycle()
throws Throwable throws Throwable
{ {
DirectedGraph dg = null; DirectedGraph dg = null;
try try
{ {
try try
{ {
dg = factory.makeDirectedGraph(dagContracts, dg = factory.makeDirectedGraph(dagContracts,
true, true,
makeDirectedCycle()); makeDirectedCycle());
fail("Should not have created DAG."); fail("Should not have created DAG.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
} }
catch (Throwable ex) catch (Throwable ex)
{ {
printGraph(ex, dg); printGraph(ex, dg);
throw ex; throw ex;
} }
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testDAGAddCycleToPipe() public void testDAGAddCycleToPipe()
throws Throwable throws Throwable
{ {
MutableDirectedGraph mdg = null; MutableDirectedGraph mdg = null;
try try
{ {
try try
{ {
mdg = factory.makeMutableDirectedGraph(dagContracts, mdg = factory.makeMutableDirectedGraph(dagContracts,
true, true,
makePipe()); makePipe());
mdg.addEdge(V3_V1, V3, V1); mdg.addEdge(V3_V1, V3, V1);
fail("No Exception thrown on adding of invalid edge."); fail("No Exception thrown on adding of invalid edge.");
} }
catch (CycleException e) catch (CycleException e)
{} {}
} }
catch (Throwable ex) catch (Throwable ex)
{ {
printGraph(ex, mdg); printGraph(ex, mdg);
throw ex; throw ex;
} }
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testDAGAddCycleToDirEdge() public void testDAGAddCycleToDirEdge()
throws Throwable throws Throwable
{ {
MutableDirectedGraph mdg = null; MutableDirectedGraph mdg = null;
try try
{ {
try try
{ {
mdg = factory.makeMutableDirectedGraph(dagContracts, mdg = factory.makeMutableDirectedGraph(dagContracts,
true, true,
makeDirectedEdge()); makeDirectedEdge());
mdg.addEdge(V2_V1, V2, V1); mdg.addEdge(V2_V1, V2, V1);
fail("Failed to throw exception on introducing Cycle."); fail("Failed to throw exception on introducing Cycle.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
} }
catch (Throwable ex) catch (Throwable ex)
{ {
printGraph(ex, mdg); printGraph(ex, mdg);
throw ex; throw ex;
} }
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testDAGAddSelfLoop() public void testDAGAddSelfLoop()
throws Throwable throws Throwable
{ {
MutableDirectedGraph mdg = null; MutableDirectedGraph mdg = null;
try try
{ {
try try
{ {
mdg = factory.makeMutableDirectedGraph(dagContracts, mdg = factory.makeMutableDirectedGraph(dagContracts,
true, true,
makeDirSingleVertex()); makeDirSingleVertex());
mdg.addEdge(V1_V1, V1, V1); mdg.addEdge(V1_V1, V1, V1);
fail("Failed to throw exception on introducing Self Loop."); fail("Failed to throw exception on introducing Self Loop.");
} }
catch (CycleException ex) catch (CycleException ex)
{} {}
} }
catch (Throwable ex) catch (Throwable ex)
{ {
printGraph(ex, mdg); printGraph(ex, mdg);
throw ex; throw ex;
} }
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testDAGValidEdge() public void testDAGValidEdge()
throws Throwable throws Throwable
{ {
MutableDirectedGraph mdg = null; MutableDirectedGraph mdg = null;
try try
{ {
mdg = factory.makeMutableDirectedGraph(dagContracts, mdg = factory.makeMutableDirectedGraph(dagContracts,
true, true,
makeParentTree()); makeParentTree());
mdg.addEdge(V2_V3, V2, V3); mdg.addEdge(V2_V3, V2, V3);
} }
catch (Throwable ex) catch (Throwable ex)
{ {
printGraph(ex, mdg); printGraph(ex, mdg);
throw ex; throw ex;
} }
} }
/** /**
* A unit test for JUnit * A unit test for JUnit
*/ */
public void testDAGValidEdge2() public void testDAGValidEdge2()
throws Throwable throws Throwable
{ {
MutableDirectedGraph mdg = null; MutableDirectedGraph mdg = null;
try try
{ {
mdg = factory.makeMutableDirectedGraph(dagContracts, mdg = factory.makeMutableDirectedGraph(dagContracts,
true, true,
makeDirDoubleVertex()); makeDirDoubleVertex());
mdg.addEdge(V1_V2, V1, V2); mdg.addEdge(V1_V2, V1, V2);
} }
catch (Throwable ex) catch (Throwable ex)
{ {
printGraph(ex, mdg); printGraph(ex, mdg);
throw ex; throw ex;
} }
} }
} }