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.
*
@ -14,35 +14,35 @@ package org.apache.commons.graph;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.Set;
/**
* Description of the Interface
*/
public interface DirectedGraph
extends Graph
{
/**
* getInbound( Vertex ) Returns the set of edges which are inbound to the
* Vertex.
*/
public Set getInbound(Vertex v);
/**
* getOutbound( Vertex ) Returns the set of edges which lead away from the
* Vertex.
*/
public Set getOutbound(Vertex v);
/**
* getSource( Edge ) Returns the vertex which originates the edge.
*/
public Vertex getSource(Edge e);
/**
* getTarget( Edge ) Returns the vertex which terminates the edge.
*/
public Vertex getTarget(Edge e);
}
*/
import java.util.Set;
/**
* Description of the Interface
*/
public interface DirectedGraph
extends Graph
{
/**
* getInbound( Vertex ) Returns the set of edges which are inbound to the
* Vertex.
*/
public Set getInbound(Vertex v);
/**
* getOutbound( Vertex ) Returns the set of edges which lead away from the
* Vertex.
*/
public Set getOutbound(Vertex v);
/**
* getSource( Edge ) Returns the vertex which originates the edge.
*/
public Vertex getSource(Edge e);
/**
* getTarget( Edge ) Returns the vertex which terminates the edge.
*/
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.
*
@ -14,11 +14,11 @@ package org.apache.commons.graph;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Description of the Interface
*/
public interface Edge
{
}
*/
/**
* Description of the Interface
*/
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.
*
@ -14,40 +14,40 @@ package org.apache.commons.graph;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Graph This is the basic interface for a graph. G = { v, e }
* getAdjacentVertices and getAdjacentEdges helps to define the behavior of
* Edges.
*/
import java.util.Set;
/**
* Description of the Interface
*/
public interface Graph
{
/**
* getVertices - Returns the total set of Vertices in the graph.
*/
public Set getVertices();
/**
* getEdges - Returns the total set of Edges in the graph.
*/
public Set getEdges();
/**
* getEdges( Vertex ) - This method will return all edges which touch this
* vertex.
*/
public Set getEdges(Vertex v);
/**
* getVertices( Edge ) - This method will return the set of Verticies on
* this Edge. (2 for normal edges, > 2 for HyperEdges.)
*/
public Set getVertices(Edge e);
}
*/
/**
* Graph This is the basic interface for a graph. G = { v, e }
* getAdjacentVertices and getAdjacentEdges helps to define the behavior of
* Edges.
*/
import java.util.Set;
/**
* Description of the Interface
*/
public interface Graph
{
/**
* getVertices - Returns the total set of Vertices in the graph.
*/
public Set getVertices();
/**
* getEdges - Returns the total set of Edges in the graph.
*/
public Set getEdges();
/**
* getEdges( Vertex ) - This method will return all edges which touch this
* vertex.
*/
public Set getEdges(Vertex v);
/**
* getVertices( Edge ) - This method will return the set of Verticies on
* this Edge. (2 for normal edges, > 2 for HyperEdges.)
*/
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.
*
@ -14,39 +14,39 @@ package org.apache.commons.graph;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.commons.graph.exception.*;
/**
* Description of the Interface
*/
public interface MutableDirectedGraph
extends DirectedGraph
{
/**
* Adds a feature to the Vertex attribute of the MutableDirectedGraph object
*/
public void addVertex(Vertex v)
throws GraphException;
/**
* Adds a feature to the Edge attribute of the MutableDirectedGraph object
*/
public void addEdge(Edge e,
Vertex source,
Vertex target)
throws GraphException;
/**
* Description of the Method
*/
public void removeVertex(Vertex v)
throws GraphException;
/**
* Description of the Method
*/
public void removeEdge(Edge e)
throws GraphException;
}
*/
import org.apache.commons.graph.exception.*;
/**
* Description of the Interface
*/
public interface MutableDirectedGraph
extends DirectedGraph
{
/**
* Adds a feature to the Vertex attribute of the MutableDirectedGraph object
*/
public void addVertex(Vertex v)
throws GraphException;
/**
* Adds a feature to the Edge attribute of the MutableDirectedGraph object
*/
public void addEdge(Edge e,
Vertex source,
Vertex target)
throws GraphException;
/**
* Description of the Method
*/
public void removeVertex(Vertex v)
throws GraphException;
/**
* Description of the Method
*/
public void removeEdge(Edge e)
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.
*
@ -14,49 +14,49 @@ package org.apache.commons.graph;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.commons.graph.exception.*;
/**
* Description of the Interface
*/
public interface MutableGraph
extends Graph
{
/**
* Adds a feature to the Vertex attribute of the MutableGraph object
*/
public void addVertex(Vertex v)
throws GraphException;
/**
* Description of the Method
*/
public void removeVertex(Vertex v)
throws GraphException;
/**
* Adds a feature to the Edge attribute of the MutableGraph object
*/
public void addEdge(Edge e)
throws GraphException;
/**
* Description of the Method
*/
public void removeEdge(Edge e)
throws GraphException;
/**
* Description of the Method
*/
public void connect(Edge e, Vertex v)
throws GraphException;
/**
* Description of the Method
*/
public void disconnect(Edge e, Vertex v)
throws GraphException;
}
*/
import org.apache.commons.graph.exception.*;
/**
* Description of the Interface
*/
public interface MutableGraph
extends Graph
{
/**
* Adds a feature to the Vertex attribute of the MutableGraph object
*/
public void addVertex(Vertex v)
throws GraphException;
/**
* Description of the Method
*/
public void removeVertex(Vertex v)
throws GraphException;
/**
* Adds a feature to the Edge attribute of the MutableGraph object
*/
public void addEdge(Edge e)
throws GraphException;
/**
* Description of the Method
*/
public void removeEdge(Edge e)
throws GraphException;
/**
* Description of the Method
*/
public void connect(Edge e, Vertex v)
throws GraphException;
/**
* Description of the Method
*/
public void disconnect(Edge e, Vertex v)
throws GraphException;
}

View File

@ -1,12 +1,12 @@
package org.apache.commons.graph;
/**
* Description of the Interface
*/
public interface Named
{
/**
* Gets the name attribute of the Named object
*/
public String getName();
}
package org.apache.commons.graph;
/**
* Description of the Interface
*/
public interface Named
{
/**
* Gets the name attribute of the Named object
*/
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.
*
@ -14,42 +14,42 @@ package org.apache.commons.graph;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.List;
/**
* Description of the Interface
*/
public interface Path
{
/**
* Returns the start of the path.
*/
public Vertex getStart();
/**
* Returns the end of the path.
*/
public Vertex getEnd();
/**
* 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
* more entry than the Edges list.
*/
public List getVertices();
/**
* getEdges() - This returns a list of Edges which comprise the path. It
* will have one less than the list of Vertices.
*/
public List getEdges();
/**
* size() - This returns the size of the path in terms of number of
* verticies it visits.
*/
public int size();
}
*/
import java.util.List;
/**
* Description of the Interface
*/
public interface Path
{
/**
* Returns the start of the path.
*/
public Vertex getStart();
/**
* Returns the end of the path.
*/
public Vertex getEnd();
/**
* 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
* more entry than the Edges list.
*/
public List getVertices();
/**
* getEdges() - This returns a list of Edges which comprise the path. It
* will have one less than the list of Vertices.
*/
public List getEdges();
/**
* size() - This returns the size of the path in terms of number of
* verticies it visits.
*/
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.
*
@ -14,12 +14,12 @@ package org.apache.commons.graph;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Description of the Interface
*/
public interface UndirectedGraph
extends Graph
{
}
*/
/**
* Description of the Interface
*/
public interface UndirectedGraph
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.
*
@ -14,11 +14,11 @@ package org.apache.commons.graph;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Description of the Interface
*/
public interface Vertex
{
}
*/
/**
* Description of the Interface
*/
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.
*
@ -14,16 +14,16 @@ package org.apache.commons.graph;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Description of the Interface
*/
public interface WeightedEdge
extends Edge
{
/**
* Gets the weight attribute of the WeightedEdge object
*/
public double getWeight();
}
*/
/**
* Description of the Interface
*/
public interface WeightedEdge
extends Edge
{
/**
* Gets the weight attribute of the WeightedEdge object
*/
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.
*
@ -14,16 +14,16 @@ package org.apache.commons.graph;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Description of the Interface
*/
public interface WeightedPath
extends Path
{
/**
* Gets the weight attribute of the WeightedPath object
*/
public double getWeight();
}
*/
/**
* Description of the Interface
*/
public interface WeightedPath
extends Path
{
/**
* Gets the weight attribute of the WeightedPath object
*/
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.
*
@ -14,354 +14,354 @@ package org.apache.commons.graph.algorithm.path;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* 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
* by jumps.) Uses Floyd's Algorithm.
*/
import java.util.Set;
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.AbstractList;
import org.apache.commons.graph.*;
import org.apache.commons.graph.exception.*;
/**
* Description of the Class
*/
public class AllPairsShortestPath
{
private int pred[][];
private double cost[][];
private Vertex vArray[];
private DirectedGraph graph;
private Map vertexIndex = new HashMap();// VERTEX X INTEGER
/**
* Description of the Class
*/
public class EdgeList
extends AbstractList
{
private DirectedGraph graph;
private List vertices;
/**
* Constructor for the EdgeList object
*
* @param graph
* @param vertices
*/
public EdgeList(DirectedGraph graph,
List vertices)
{
this.graph = graph;
this.vertices = vertices;
}
/**
* Description of the Method
*/
public int size()
{
return vertices.size() - 1;
}
/**
* Description of the Method
*/
public Object get(int index)
{
Edge RC = null;
Vertex source = (Vertex) vertices.get(index);
Vertex target = (Vertex) vertices.get(index + 1);
Set outboundSet = graph.getOutbound(source);
if (outboundSet == null)
{
return null;
}
Iterator outbound = outboundSet.iterator();
while (outbound.hasNext())
{
RC = (Edge) outbound.next();
if (graph.getTarget(RC) == target)
{
break;
}
}
if (graph.getTarget(RC) != target)
{
return null;
}
return RC;
}
}
/**
* Description of the Class
*/
public class WPath
implements WeightedPath
{
private Vertex start;
private Vertex finish;
private List vertexList = new ArrayList();
private DirectedGraph graph;
private double cost;
/**
* Constructor for the WPath object
*
* @param graph
* @param vArray
* @param pred
* @param start
* @param finish
* @param cost
* @exception GraphException
*/
public WPath(DirectedGraph graph,
Vertex vArray[], int pred[][],
int start, int finish, double cost)
throws GraphException
{
this.start = vArray[start];
this.finish = vArray[finish];
this.cost = cost;
this.graph = graph;
vertexList.addAll(segment(vArray, pred,
start, finish));
vertexList.add(vArray[finish]);
}
/**
* Returns a List of Vectors in order. Includes the start, but not the
* finish.
*/
private List segment(Vertex vArray[], int pred[][],
int start, int finish)
throws GraphException
{
int mid = pred[start][finish];
if (mid == -1)
{
throw new NoPathException("No SubPath Available: " +
vArray[start] + " -> " +
vArray[finish]);
}
List RC = new ArrayList();
if (start == finish)
{
return RC;
}
if (start == mid)
{
RC.add(vArray[start]);
}
else
{
RC.addAll(segment(vArray, pred,
start, mid));
RC.add(vArray[mid]);
}
if (mid == pred[mid][finish])
{
}
else
{
RC.addAll(segment(vArray, pred,
mid, finish));
}
return RC;
}
/**
* Gets the weight attribute of the WPath object
*/
public double getWeight()
{
return cost;
}
/**
* Gets the vertices attribute of the WPath object
*/
public List getVertices()
{
return vertexList;
}
/**
* Gets the edges attribute of the WPath object
*/
public List getEdges()
{
return new EdgeList(graph, vertexList);
}
/**
* Gets the start attribute of the WPath object
*/
public Vertex getStart()
{
return start;
}
/**
* Gets the end attribute of the WPath object
*/
public Vertex getEnd()
{
return finish;
}
public int size() {
return vertexList.size();
}
}
/**
* Constructor for the AllPairsShortestPath object
*
* @param graph
* @exception NegativeCycleException
*/
public AllPairsShortestPath(DirectedGraph graph)
throws NegativeCycleException
{
update(graph);
}
/**
* Description of the Method
*/
private void initIndex(Vertex vArray[])
{
for (int i = 0; i < vArray.length; i++)
{
vertexIndex.put(vArray[i], new Integer(i));
}
}
/**
* Description of the Method
*/
public void update(DirectedGraph graph)
{
this.graph = graph;
Set vertexSet = graph.getVertices();
vArray = (Vertex[]) vertexSet.toArray(new Vertex[vertexSet.size()]);
initIndex(vArray);
pred = new int[vArray.length][vArray.length];
cost = new double[vArray.length][vArray.length];
for (int i = 0; i < vArray.length; i++)
{
for (int j = 0; j < vArray.length; j++)
{
pred[i][j] = -1;
cost[i][j] = Double.POSITIVE_INFINITY;
}
// First round values need to be in the matrix.
Iterator edgeSet = graph.getOutbound(vArray[i]).iterator();
while (edgeSet.hasNext())
{
Edge e = (Edge) edgeSet.next();
int j = index(graph.getTarget(e));
pred[i][j] = i;
if (graph instanceof WeightedGraph)
{
cost[i][j] = ((WeightedGraph) graph).getWeight(e);
}
else
{
cost[i][j] = 1.0;
}
}
// Cost from any node to itself is 0.0
cost[i][i] = 0.0;
pred[i][i] = i;
}
compute(graph, vArray);
}
/**
* Description of the Method
*/
private int index(Vertex v)
{
return ((Integer) vertexIndex.get(v)).intValue();
}
/**
* Description of the Method
*/
private void compute(DirectedGraph graph, Vertex vArray[])
throws NegativeCycleException
{
for (int k = 0; k < vArray.length; k++)
{// Mid Point
for (int i = 0; i < vArray.length; i++)
{// Source
for (int j = 0; j < vArray.length; j++)
{// Target
if (cost[i][k] + cost[k][j] < cost[i][j])
{
if (i == j)
{
throw new NegativeCycleException();
}
// It is cheaper to go through K.
cost[i][j] = cost[i][k] + cost[k][j];
pred[i][j] = k;
}
}
}
}
}
/**
* Gets the shortestPath attribute of the AllPairsShortestPath object
*/
public WeightedPath getShortestPath(Vertex start, Vertex end)
throws GraphException
{
return new WPath(graph, vArray, pred,
index(start), index(end),
cost[index(start)][index(end)]);
}
/**
* Determines if a path exists or not.
*/
public boolean hasPath( Vertex start, Vertex end )
{
return cost[index(start)][index(end)] < Double.POSITIVE_INFINITY;
}
}
*/
/**
* 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
* by jumps.) Uses Floyd's Algorithm.
*/
import java.util.Set;
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.AbstractList;
import org.apache.commons.graph.*;
import org.apache.commons.graph.exception.*;
/**
* Description of the Class
*/
public class AllPairsShortestPath
{
private int pred[][];
private double cost[][];
private Vertex vArray[];
private DirectedGraph graph;
private Map vertexIndex = new HashMap();// VERTEX X INTEGER
/**
* Description of the Class
*/
public class EdgeList
extends AbstractList
{
private DirectedGraph graph;
private List vertices;
/**
* Constructor for the EdgeList object
*
* @param graph
* @param vertices
*/
public EdgeList(DirectedGraph graph,
List vertices)
{
this.graph = graph;
this.vertices = vertices;
}
/**
* Description of the Method
*/
public int size()
{
return vertices.size() - 1;
}
/**
* Description of the Method
*/
public Object get(int index)
{
Edge RC = null;
Vertex source = (Vertex) vertices.get(index);
Vertex target = (Vertex) vertices.get(index + 1);
Set outboundSet = graph.getOutbound(source);
if (outboundSet == null)
{
return null;
}
Iterator outbound = outboundSet.iterator();
while (outbound.hasNext())
{
RC = (Edge) outbound.next();
if (graph.getTarget(RC) == target)
{
break;
}
}
if (graph.getTarget(RC) != target)
{
return null;
}
return RC;
}
}
/**
* Description of the Class
*/
public class WPath
implements WeightedPath
{
private Vertex start;
private Vertex finish;
private List vertexList = new ArrayList();
private DirectedGraph graph;
private double cost;
/**
* Constructor for the WPath object
*
* @param graph
* @param vArray
* @param pred
* @param start
* @param finish
* @param cost
* @exception GraphException
*/
public WPath(DirectedGraph graph,
Vertex vArray[], int pred[][],
int start, int finish, double cost)
throws GraphException
{
this.start = vArray[start];
this.finish = vArray[finish];
this.cost = cost;
this.graph = graph;
vertexList.addAll(segment(vArray, pred,
start, finish));
vertexList.add(vArray[finish]);
}
/**
* Returns a List of Vectors in order. Includes the start, but not the
* finish.
*/
private List segment(Vertex vArray[], int pred[][],
int start, int finish)
throws GraphException
{
int mid = pred[start][finish];
if (mid == -1)
{
throw new NoPathException("No SubPath Available: " +
vArray[start] + " -> " +
vArray[finish]);
}
List RC = new ArrayList();
if (start == finish)
{
return RC;
}
if (start == mid)
{
RC.add(vArray[start]);
}
else
{
RC.addAll(segment(vArray, pred,
start, mid));
RC.add(vArray[mid]);
}
if (mid == pred[mid][finish])
{
}
else
{
RC.addAll(segment(vArray, pred,
mid, finish));
}
return RC;
}
/**
* Gets the weight attribute of the WPath object
*/
public double getWeight()
{
return cost;
}
/**
* Gets the vertices attribute of the WPath object
*/
public List getVertices()
{
return vertexList;
}
/**
* Gets the edges attribute of the WPath object
*/
public List getEdges()
{
return new EdgeList(graph, vertexList);
}
/**
* Gets the start attribute of the WPath object
*/
public Vertex getStart()
{
return start;
}
/**
* Gets the end attribute of the WPath object
*/
public Vertex getEnd()
{
return finish;
}
public int size() {
return vertexList.size();
}
}
/**
* Constructor for the AllPairsShortestPath object
*
* @param graph
* @exception NegativeCycleException
*/
public AllPairsShortestPath(DirectedGraph graph)
throws NegativeCycleException
{
update(graph);
}
/**
* Description of the Method
*/
private void initIndex(Vertex vArray[])
{
for (int i = 0; i < vArray.length; i++)
{
vertexIndex.put(vArray[i], new Integer(i));
}
}
/**
* Description of the Method
*/
public void update(DirectedGraph graph)
{
this.graph = graph;
Set vertexSet = graph.getVertices();
vArray = (Vertex[]) vertexSet.toArray(new Vertex[vertexSet.size()]);
initIndex(vArray);
pred = new int[vArray.length][vArray.length];
cost = new double[vArray.length][vArray.length];
for (int i = 0; i < vArray.length; i++)
{
for (int j = 0; j < vArray.length; j++)
{
pred[i][j] = -1;
cost[i][j] = Double.POSITIVE_INFINITY;
}
// First round values need to be in the matrix.
Iterator edgeSet = graph.getOutbound(vArray[i]).iterator();
while (edgeSet.hasNext())
{
Edge e = (Edge) edgeSet.next();
int j = index(graph.getTarget(e));
pred[i][j] = i;
if (graph instanceof WeightedGraph)
{
cost[i][j] = ((WeightedGraph) graph).getWeight(e);
}
else
{
cost[i][j] = 1.0;
}
}
// Cost from any node to itself is 0.0
cost[i][i] = 0.0;
pred[i][i] = i;
}
compute(graph, vArray);
}
/**
* Description of the Method
*/
private int index(Vertex v)
{
return ((Integer) vertexIndex.get(v)).intValue();
}
/**
* Description of the Method
*/
private void compute(DirectedGraph graph, Vertex vArray[])
throws NegativeCycleException
{
for (int k = 0; k < vArray.length; k++)
{// Mid Point
for (int i = 0; i < vArray.length; i++)
{// Source
for (int j = 0; j < vArray.length; j++)
{// Target
if (cost[i][k] + cost[k][j] < cost[i][j])
{
if (i == j)
{
throw new NegativeCycleException();
}
// It is cheaper to go through K.
cost[i][j] = cost[i][k] + cost[k][j];
pred[i][j] = k;
}
}
}
}
}
/**
* Gets the shortestPath attribute of the AllPairsShortestPath object
*/
public WeightedPath getShortestPath(Vertex start, Vertex end)
throws GraphException
{
return new WPath(graph, vArray, pred,
index(start), index(end),
cost[index(start)][index(end)]);
}
/**
* Determines if a path exists or not.
*/
public boolean hasPath( Vertex start, Vertex end )
{
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.
*
@ -14,155 +14,155 @@ package org.apache.commons.graph.algorithm.path;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* AllPaths finds for each pair of vertices, {i, j} the set of
* all potential paths between them. (Unrolling loops by a set
* amount.
*/
import java.util.Map;
import java.util.Set;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import org.apache.commons.graph.*;
import org.apache.commons.graph.algorithm.util.*;
public class AllPaths
{
private Map allPaths = new HashMap(); // VERTEX PAIR X SET( PATHS )
private AllPairsShortestPath apsp = null;
private DirectedGraph graph = null;
public AllPaths( DirectedGraph graph ) {
this.graph = graph;
try {
apsp = new AllPairsShortestPath( graph );
} catch (Exception ex) {
ex.printStackTrace();
}
}
public Iterator findPaths( final Vertex i,
final Vertex j ) {
final PathIterator RC = new PathIterator();
Runnable run = new Runnable() {
public void run() {
findPaths( RC, i, j, Integer.MAX_VALUE );
}
};
Thread thread = new Thread( run );
RC.setThread( thread );
thread.start();
return RC;
}
public void findPaths( PathListener listener,
Vertex i, Vertex j,
int maxLength ) {
Set workingSet = new HashSet();
workingSet.add( new PathImpl( i ));
for (int k = 0; k < maxLength; k++) {
Iterator workingPaths = workingSet.iterator();
if (!workingPaths.hasNext()) break;
Set newWorkingSet = new HashSet();
while (workingPaths.hasNext()) {
PathImpl workingPath = (PathImpl) workingPaths.next();
Iterator outbound =
graph.getOutbound(workingPath.getEnd()).iterator();
while (outbound.hasNext()) {
Edge obEdge = (Edge) outbound.next();
if (apsp.hasPath( graph.getTarget( obEdge ), j)) {
PathImpl path =
workingPath.append(graph.getTarget(obEdge),
obEdge );
newWorkingSet.add( path );
if (path.getEnd() == j) {
listener.notifyPath( path );
}
}
}
}
workingSet = newWorkingSet;
}
}
/**
* getAllPaths will return the set of all possible ways of moving
* from i to j using the directed graph AllPaths was initialized
* with.
*
* @deprecated Use findPaths instead. Doesn't work, but code
* may be useful in the near future.
*/
public Set getAllPaths( Vertex i, Vertex j ) {
Set RC = new HashSet();
VertexPair key = new VertexPair( i, j );
// If we have already started this, return what we
// were doing. (May still be in progress.)
//
// If we haven't started, go ahead and start. . .
if (allPaths.containsKey( key )) {
return (Set) allPaths.get( key );
} else {
allPaths.put( key, RC );
}
Iterator outbounds = graph.getOutbound(i).iterator();
while (outbounds.hasNext()) {
Edge outbound = (Edge) outbounds.next();
if (graph.getTarget( outbound ) == j) {
RC.add( new PathImpl( i, j, outbound ));
}
}
Iterator ks = graph.getVertices().iterator();
while (ks.hasNext()) {
Vertex k = (Vertex) ks.next();
if (k != i && k != j) {
appendPaths( RC,
getAllPaths( i, k ),
getAllPaths( k, j ));
}
}
allPaths.put( key, RC );
return RC;
}
public void appendPaths( Set RC, Set iks, Set kjs ) {
Iterator ikPaths = iks.iterator();
while (ikPaths.hasNext()) {
PathImpl ik = (PathImpl) ikPaths.next();
Iterator kjPaths = kjs.iterator();
while (kjPaths.hasNext()) {
PathImpl kj = (PathImpl) kjPaths.next();
RC.add( ik.append( kj ));
}
}
}
}
*/
/**
* AllPaths finds for each pair of vertices, {i, j} the set of
* all potential paths between them. (Unrolling loops by a set
* amount.
*/
import java.util.Map;
import java.util.Set;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import org.apache.commons.graph.*;
import org.apache.commons.graph.algorithm.util.*;
public class AllPaths
{
private Map allPaths = new HashMap(); // VERTEX PAIR X SET( PATHS )
private AllPairsShortestPath apsp = null;
private DirectedGraph graph = null;
public AllPaths( DirectedGraph graph ) {
this.graph = graph;
try {
apsp = new AllPairsShortestPath( graph );
} catch (Exception ex) {
ex.printStackTrace();
}
}
public Iterator findPaths( final Vertex i,
final Vertex j ) {
final PathIterator RC = new PathIterator();
Runnable run = new Runnable() {
public void run() {
findPaths( RC, i, j, Integer.MAX_VALUE );
}
};
Thread thread = new Thread( run );
RC.setThread( thread );
thread.start();
return RC;
}
public void findPaths( PathListener listener,
Vertex i, Vertex j,
int maxLength ) {
Set workingSet = new HashSet();
workingSet.add( new PathImpl( i ));
for (int k = 0; k < maxLength; k++) {
Iterator workingPaths = workingSet.iterator();
if (!workingPaths.hasNext()) break;
Set newWorkingSet = new HashSet();
while (workingPaths.hasNext()) {
PathImpl workingPath = (PathImpl) workingPaths.next();
Iterator outbound =
graph.getOutbound(workingPath.getEnd()).iterator();
while (outbound.hasNext()) {
Edge obEdge = (Edge) outbound.next();
if (apsp.hasPath( graph.getTarget( obEdge ), j)) {
PathImpl path =
workingPath.append(graph.getTarget(obEdge),
obEdge );
newWorkingSet.add( path );
if (path.getEnd() == j) {
listener.notifyPath( path );
}
}
}
}
workingSet = newWorkingSet;
}
}
/**
* getAllPaths will return the set of all possible ways of moving
* from i to j using the directed graph AllPaths was initialized
* with.
*
* @deprecated Use findPaths instead. Doesn't work, but code
* may be useful in the near future.
*/
public Set getAllPaths( Vertex i, Vertex j ) {
Set RC = new HashSet();
VertexPair key = new VertexPair( i, j );
// If we have already started this, return what we
// were doing. (May still be in progress.)
//
// If we haven't started, go ahead and start. . .
if (allPaths.containsKey( key )) {
return (Set) allPaths.get( key );
} else {
allPaths.put( key, RC );
}
Iterator outbounds = graph.getOutbound(i).iterator();
while (outbounds.hasNext()) {
Edge outbound = (Edge) outbounds.next();
if (graph.getTarget( outbound ) == j) {
RC.add( new PathImpl( i, j, outbound ));
}
}
Iterator ks = graph.getVertices().iterator();
while (ks.hasNext()) {
Vertex k = (Vertex) ks.next();
if (k != i && k != j) {
appendPaths( RC,
getAllPaths( i, k ),
getAllPaths( k, j ));
}
}
allPaths.put( key, RC );
return RC;
}
public void appendPaths( Set RC, Set iks, Set kjs ) {
Iterator ikPaths = iks.iterator();
while (ikPaths.hasNext()) {
PathImpl ik = (PathImpl) ikPaths.next();
Iterator kjPaths = kjs.iterator();
while (kjPaths.hasNext()) {
PathImpl kj = (PathImpl) kjPaths.next();
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.
*
@ -14,42 +14,42 @@ package org.apache.commons.graph.algorithm.search;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.commons.graph.*;
/**
* Description of the Interface
*/
public interface Visitor
{
/**
* Description of the Method
*/
public void discoverGraph(Graph graph);
/**
* Description of the Method
*/
public void discoverVertex(Vertex vertex);
/**
* Description of the Method
*/
public void discoverEdge(Edge edge);
/**
* Description of the Method
*/
public void finishEdge(Edge edge);
/**
* Description of the Method
*/
public void finishVertex(Vertex vertex);
/**
* Description of the Method
*/
public void finishGraph(Graph graph);
}
*/
import org.apache.commons.graph.*;
/**
* Description of the Interface
*/
public interface Visitor
{
/**
* Description of the Method
*/
public void discoverGraph(Graph graph);
/**
* Description of the Method
*/
public void discoverVertex(Vertex vertex);
/**
* Description of the Method
*/
public void discoverEdge(Edge edge);
/**
* Description of the Method
*/
public void finishEdge(Edge edge);
/**
* Description of the Method
*/
public void finishVertex(Vertex vertex);
/**
* Description of the Method
*/
public void finishGraph(Graph graph);
}

View File

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

View File

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

View File

@ -1,81 +1,81 @@
package org.apache.commons.graph.algorithm.util;
import java.util.List;
import java.util.ArrayList;
import org.apache.commons.graph.*;
public class PathImpl
implements Path
{
protected List vertexList = new ArrayList();
protected List edgeList = new ArrayList();
public PathImpl( List vertexList,
List edgeList ) {
this.vertexList = vertexList;
this.edgeList = edgeList;
}
public PathImpl( Vertex start ) {
this.vertexList.add( start );
}
public PathImpl( Vertex start,
Vertex end,
Edge edge ) {
vertexList = new ArrayList();
vertexList.add( start );
vertexList.add( end );
edgeList = new ArrayList();
edgeList.add( edge );
}
public PathImpl append( PathImpl impl ) {
List newVertices = new ArrayList( vertexList );
newVertices.remove( newVertices.size() - 1 ); // Last should be duplicated
newVertices.addAll( impl.getVertices() );
List newEdges = new ArrayList( edgeList );
newEdges.addAll( impl.getEdges() );
return new PathImpl( newVertices, newEdges );
}
public PathImpl append( Vertex v, Edge e ) {
List newVertices = new ArrayList( vertexList );
newVertices.add( v );
List newEdges = new ArrayList( edgeList );
newEdges.add( e );
return new PathImpl( newVertices, newEdges );
}
public Vertex getStart() {
return (Vertex) vertexList.get( 0 );
}
public Vertex getEnd() {
return (Vertex) vertexList.get( vertexList.size() - 1 );
}
public List getVertices() {
return vertexList;
}
public List getEdges() {
return edgeList;
}
public int size() {
return vertexList.size();
}
public String toString() {
return vertexList.toString();
}
}
package org.apache.commons.graph.algorithm.util;
import java.util.List;
import java.util.ArrayList;
import org.apache.commons.graph.*;
public class PathImpl
implements Path
{
protected List vertexList = new ArrayList();
protected List edgeList = new ArrayList();
public PathImpl( List vertexList,
List edgeList ) {
this.vertexList = vertexList;
this.edgeList = edgeList;
}
public PathImpl( Vertex start ) {
this.vertexList.add( start );
}
public PathImpl( Vertex start,
Vertex end,
Edge edge ) {
vertexList = new ArrayList();
vertexList.add( start );
vertexList.add( end );
edgeList = new ArrayList();
edgeList.add( edge );
}
public PathImpl append( PathImpl impl ) {
List newVertices = new ArrayList( vertexList );
newVertices.remove( newVertices.size() - 1 ); // Last should be duplicated
newVertices.addAll( impl.getVertices() );
List newEdges = new ArrayList( edgeList );
newEdges.addAll( impl.getEdges() );
return new PathImpl( newVertices, newEdges );
}
public PathImpl append( Vertex v, Edge e ) {
List newVertices = new ArrayList( vertexList );
newVertices.add( v );
List newEdges = new ArrayList( edgeList );
newEdges.add( e );
return new PathImpl( newVertices, newEdges );
}
public Vertex getStart() {
return (Vertex) vertexList.get( 0 );
}
public Vertex getEnd() {
return (Vertex) vertexList.get( vertexList.size() - 1 );
}
public List getVertices() {
return vertexList;
}
public List getEdges() {
return edgeList;
}
public int size() {
return vertexList.size();
}
public String 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.
*
@ -14,37 +14,37 @@ package org.apache.commons.graph.algorithm.util;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* VertexPair
*
* This is a tuple of two vertices which is capable
* of storing things in a Hashtable.
*/
import org.apache.commons.graph.*;
public class VertexPair {
public Vertex i = null;
public Vertex j = null;
public VertexPair( Vertex i, Vertex j ) {
this.i = i;
this.j = j;
}
public boolean equals( Object o ) {
VertexPair vp = (VertexPair) o;
return ( this.i == vp.i &&
this.j == vp.j );
}
public int hashCode() {
return (17 * i.hashCode()) + j.hashCode();
}
}
*/
/**
* VertexPair
*
* This is a tuple of two vertices which is capable
* of storing things in a Hashtable.
*/
import org.apache.commons.graph.*;
public class VertexPair {
public Vertex i = null;
public Vertex j = null;
public VertexPair( Vertex i, Vertex j ) {
this.i = i;
this.j = j;
}
public boolean equals( Object o ) {
VertexPair vp = (VertexPair) o;
return ( this.i == vp.i &&
this.j == vp.j );
}
public int hashCode() {
return (17 * i.hashCode()) + j.hashCode();
}
}

View File

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

View File

@ -1,57 +1,57 @@
package org.apache.commons.graph.contract;
import org.apache.commons.graph.*;
import org.apache.commons.graph.exception.*;
/**
* Description of the Interface
*/
public interface Contract
{
/**
* 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
* illegal to the contract, raise a GraphException with and explanation.
*/
public void setImpl(DirectedGraph impl);
/**
* getInterface This returns the marker interface which is associated with
* the Contract. For instance, AcyclicContract will return AcyclicGraph
* here.
*/
public Class getInterface();
/**
* verify - This verifies that the graph it is working on complies.
*/
public void verify()
throws GraphException;
/**
* Adds a feature to the Edge attribute of the Contract object
*/
public void addEdge(Edge e,
Vertex start,
Vertex end)
throws GraphException;
/**
* Adds a feature to the Vertex attribute of the Contract object
*/
public void addVertex(Vertex v)
throws GraphException;
/**
* Description of the Method
*/
public void removeEdge(Edge e)
throws GraphException;
/**
* Description of the Method
*/
public void removeVertex(Vertex v)
throws GraphException;
}
package org.apache.commons.graph.contract;
import org.apache.commons.graph.*;
import org.apache.commons.graph.exception.*;
/**
* Description of the Interface
*/
public interface Contract
{
/**
* 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
* illegal to the contract, raise a GraphException with and explanation.
*/
public void setImpl(DirectedGraph impl);
/**
* getInterface This returns the marker interface which is associated with
* the Contract. For instance, AcyclicContract will return AcyclicGraph
* here.
*/
public Class getInterface();
/**
* verify - This verifies that the graph it is working on complies.
*/
public void verify()
throws GraphException;
/**
* Adds a feature to the Edge attribute of the Contract object
*/
public void addEdge(Edge e,
Vertex start,
Vertex end)
throws GraphException;
/**
* Adds a feature to the Vertex attribute of the Contract object
*/
public void addVertex(Vertex v)
throws GraphException;
/**
* Description of the Method
*/
public void removeEdge(Edge e)
throws GraphException;
/**
* Description of the Method
*/
public void removeVertex(Vertex v)
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.
*
@ -14,410 +14,410 @@ package org.apache.commons.graph.domain.basic;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.lang.reflect.*;
import java.util.Set;
import java.util.Map;
import java.util.List;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ArrayList;
import org.apache.commons.graph.*;
import org.apache.commons.graph.contract.*;
import org.apache.commons.graph.exception.*;
/**
* Description of the Class
*/
public class DirectedGraphImpl
implements DirectedGraph,
WeightedGraph,
MutableDirectedGraph,
InvocationHandler
{
private Vertex root = null;
private Set vertices = new HashSet();
private Set edges = new HashSet();
private List contracts = new ArrayList();
private Map inbound = new HashMap();// VERTEX X SET( EDGE )
private Map outbound = new HashMap();// - " " -
private Map edgeSource = new HashMap();// EDGE X VERTEX
private Map edgeTarget = new HashMap();// EDGE X TARGET
private Map edgeWeights = new HashMap();// EDGE X WEIGHT
/**
* Constructor for the DirectedGraphImpl object
*/
public DirectedGraphImpl() { }
/**
* Constructor for the DirectedGraphImpl object
*
* @param dg
*/
public DirectedGraphImpl(DirectedGraph dg)
{
Iterator v = dg.getVertices().iterator();
while (v.hasNext())
{
addVertexI((Vertex) v.next());
}
Iterator e = dg.getEdges().iterator();
while (e.hasNext())
{
Edge edge = (Edge) e.next();
addEdgeI(edge,
dg.getSource(edge),
dg.getTarget(edge));
if (dg instanceof WeightedGraph)
{
setWeight(edge, ((WeightedGraph) dg).getWeight(edge));
}
}
}
/**
* Adds a feature to the Contract attribute of the DirectedGraphImpl object
*/
public void addContract(Contract c)
throws GraphException
{
c.setImpl(this);
c.verify();
contracts.add(c);
}
/**
* Description of the Method
*/
public void removeContract(Contract c)
{
contracts.remove(c);
}
/**
* Sets the weight attribute of the DirectedGraphImpl object
*/
public void setWeight(Edge e, double value)
{
if (edgeWeights.containsKey(e))
{
edgeWeights.remove(e);
}
edgeWeights.put(e, new Double(value));
}
// Interface Methods
// Graph
/**
* Gets the vertices attribute of the DirectedGraphImpl object
*/
public Set getVertices()
{
return new HashSet(vertices);
}
/**
* Gets the vertices attribute of the DirectedGraphImpl object
*/
public Set getVertices(Edge e)
{
Set RC = new HashSet();
if (edgeSource.containsKey(e))
{
RC.add(edgeSource.get(e));
}
if (edgeTarget.containsKey(e))
{
RC.add(edgeTarget.get(e));
}
return RC;
}
/**
* Gets the edges attribute of the DirectedGraphImpl object
*/
public Set getEdges()
{
return new HashSet(edges);
}
/**
* Gets the edges attribute of the DirectedGraphImpl object
*/
public Set getEdges(Vertex v)
{
Set RC = new HashSet();
if (inbound.containsKey(v))
{
RC.addAll((Set) inbound.get(v));
}
if (outbound.containsKey(v))
{
RC.addAll((Set) outbound.get(v));
}
return RC;
}
// Directed Graph
/**
* Gets the source attribute of the DirectedGraphImpl object
*/
public Vertex getSource(Edge e)
{
return (Vertex) edgeSource.get(e);
}
/**
* Gets the target attribute of the DirectedGraphImpl object
*/
public Vertex getTarget(Edge e)
{
return (Vertex) edgeTarget.get(e);
}
/**
* Gets the inbound attribute of the DirectedGraphImpl object
*/
public Set getInbound(Vertex v)
{
if (inbound.containsKey(v))
{
return new HashSet((Set) inbound.get(v));
}
else
{
return new HashSet();
}
}
/**
* Gets the outbound attribute of the DirectedGraphImpl object
*/
public Set getOutbound(Vertex v)
{
if (outbound.containsKey(v))
{
return new HashSet((Set) outbound.get(v));
}
else
{
return new HashSet();
}
}
// MutableDirectedGraph
/**
* Adds a feature to the VertexI attribute of the DirectedGraphImpl object
*/
private void addVertexI(Vertex v)
throws GraphException
{
if (root == null) root = v;
vertices.add(v);
}
/**
* Adds a feature to the Vertex attribute of the DirectedGraphImpl object
*/
public void addVertex(Vertex v)
throws GraphException
{
Iterator conts = contracts.iterator();
while (conts.hasNext())
{
((Contract) conts.next()).addVertex(v);
}
addVertexI(v);
}
/**
* Description of the Method
*/
private void removeVertexI(Vertex v)
throws GraphException
{
try
{
vertices.remove(v);
}
catch (Exception ex)
{
throw new GraphException(ex);
}
}
/**
* Description of the Method
*/
public void removeVertex(Vertex v)
throws GraphException
{
Iterator conts = contracts.iterator();
while (conts.hasNext())
{
((Contract) conts.next()).removeVertex(v);
}
removeVertexI(v);
}
/**
* Adds a feature to the EdgeI attribute of the DirectedGraphImpl object
*/
private void addEdgeI(Edge e, Vertex start, Vertex end)
throws GraphException
{
edges.add(e);
if (e instanceof WeightedEdge)
{
edgeWeights.put(e, new Double(((WeightedEdge) e).getWeight()));
}
else
{
edgeWeights.put(e, new Double(1.0));
}
edgeSource.put(e, start);
edgeTarget.put(e, end);
if (!outbound.containsKey(start))
{
Set edgeSet = new HashSet();
edgeSet.add(e);
outbound.put(start, edgeSet);
}
else
{
((Set) outbound.get(start)).add(e);
}
if (!inbound.containsKey(end))
{
Set edgeSet = new HashSet();
edgeSet.add(e);
inbound.put(end, edgeSet);
}
else
{
((Set) inbound.get(end)).add(e);
}
}
/**
* Adds a feature to the Edge attribute of the DirectedGraphImpl object
*/
public void addEdge(Edge e,
Vertex start,
Vertex end)
throws GraphException
{
Iterator conts = contracts.iterator();
while (conts.hasNext())
{
Contract cont = (Contract) conts.next();
cont.addEdge(e, start, end);
}
addEdgeI(e, start, end);
}
/**
* Description of the Method
*/
private void removeEdgeI(Edge e)
throws GraphException
{
try
{
Set edgeSet = null;
Vertex source = (Vertex) edgeSource.get(e);
edgeSource.remove(e);
edgeSet = (Set) outbound.get(source);
edgeSet.remove(e);
Vertex target = (Vertex) edgeTarget.get(e);
edgeTarget.remove(e);
edgeSet = (Set) inbound.get(target);
edgeSet.remove(e);
if (edgeWeights.containsKey(e))
{
edgeWeights.remove(e);
}
}
catch (Exception ex)
{
throw new GraphException(ex);
}
}
/**
* Description of the Method
*/
public void removeEdge(Edge e)
throws GraphException
{
Iterator conts = contracts.iterator();
while (conts.hasNext())
{
((Contract) conts.next()).removeEdge(e);
}
removeEdgeI(e);
}
// WeightedGraph
/**
* Gets the weight attribute of the DirectedGraphImpl object
*/
public double getWeight(Edge e)
{
if (edgeWeights.containsKey(e))
{
return ((Double) edgeWeights.get(e)).doubleValue();
}
else
{
return 1.0;
}
}
/**
* Description of the Method
*/
public Object invoke(Object proxy,
Method method,
Object args[])
throws Throwable
{
try {
return method.invoke(this, args);
} catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
}
*/
import java.lang.reflect.*;
import java.util.Set;
import java.util.Map;
import java.util.List;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ArrayList;
import org.apache.commons.graph.*;
import org.apache.commons.graph.contract.*;
import org.apache.commons.graph.exception.*;
/**
* Description of the Class
*/
public class DirectedGraphImpl
implements DirectedGraph,
WeightedGraph,
MutableDirectedGraph,
InvocationHandler
{
private Vertex root = null;
private Set vertices = new HashSet();
private Set edges = new HashSet();
private List contracts = new ArrayList();
private Map inbound = new HashMap();// VERTEX X SET( EDGE )
private Map outbound = new HashMap();// - " " -
private Map edgeSource = new HashMap();// EDGE X VERTEX
private Map edgeTarget = new HashMap();// EDGE X TARGET
private Map edgeWeights = new HashMap();// EDGE X WEIGHT
/**
* Constructor for the DirectedGraphImpl object
*/
public DirectedGraphImpl() { }
/**
* Constructor for the DirectedGraphImpl object
*
* @param dg
*/
public DirectedGraphImpl(DirectedGraph dg)
{
Iterator v = dg.getVertices().iterator();
while (v.hasNext())
{
addVertexI((Vertex) v.next());
}
Iterator e = dg.getEdges().iterator();
while (e.hasNext())
{
Edge edge = (Edge) e.next();
addEdgeI(edge,
dg.getSource(edge),
dg.getTarget(edge));
if (dg instanceof WeightedGraph)
{
setWeight(edge, ((WeightedGraph) dg).getWeight(edge));
}
}
}
/**
* Adds a feature to the Contract attribute of the DirectedGraphImpl object
*/
public void addContract(Contract c)
throws GraphException
{
c.setImpl(this);
c.verify();
contracts.add(c);
}
/**
* Description of the Method
*/
public void removeContract(Contract c)
{
contracts.remove(c);
}
/**
* Sets the weight attribute of the DirectedGraphImpl object
*/
public void setWeight(Edge e, double value)
{
if (edgeWeights.containsKey(e))
{
edgeWeights.remove(e);
}
edgeWeights.put(e, new Double(value));
}
// Interface Methods
// Graph
/**
* Gets the vertices attribute of the DirectedGraphImpl object
*/
public Set getVertices()
{
return new HashSet(vertices);
}
/**
* Gets the vertices attribute of the DirectedGraphImpl object
*/
public Set getVertices(Edge e)
{
Set RC = new HashSet();
if (edgeSource.containsKey(e))
{
RC.add(edgeSource.get(e));
}
if (edgeTarget.containsKey(e))
{
RC.add(edgeTarget.get(e));
}
return RC;
}
/**
* Gets the edges attribute of the DirectedGraphImpl object
*/
public Set getEdges()
{
return new HashSet(edges);
}
/**
* Gets the edges attribute of the DirectedGraphImpl object
*/
public Set getEdges(Vertex v)
{
Set RC = new HashSet();
if (inbound.containsKey(v))
{
RC.addAll((Set) inbound.get(v));
}
if (outbound.containsKey(v))
{
RC.addAll((Set) outbound.get(v));
}
return RC;
}
// Directed Graph
/**
* Gets the source attribute of the DirectedGraphImpl object
*/
public Vertex getSource(Edge e)
{
return (Vertex) edgeSource.get(e);
}
/**
* Gets the target attribute of the DirectedGraphImpl object
*/
public Vertex getTarget(Edge e)
{
return (Vertex) edgeTarget.get(e);
}
/**
* Gets the inbound attribute of the DirectedGraphImpl object
*/
public Set getInbound(Vertex v)
{
if (inbound.containsKey(v))
{
return new HashSet((Set) inbound.get(v));
}
else
{
return new HashSet();
}
}
/**
* Gets the outbound attribute of the DirectedGraphImpl object
*/
public Set getOutbound(Vertex v)
{
if (outbound.containsKey(v))
{
return new HashSet((Set) outbound.get(v));
}
else
{
return new HashSet();
}
}
// MutableDirectedGraph
/**
* Adds a feature to the VertexI attribute of the DirectedGraphImpl object
*/
private void addVertexI(Vertex v)
throws GraphException
{
if (root == null) root = v;
vertices.add(v);
}
/**
* Adds a feature to the Vertex attribute of the DirectedGraphImpl object
*/
public void addVertex(Vertex v)
throws GraphException
{
Iterator conts = contracts.iterator();
while (conts.hasNext())
{
((Contract) conts.next()).addVertex(v);
}
addVertexI(v);
}
/**
* Description of the Method
*/
private void removeVertexI(Vertex v)
throws GraphException
{
try
{
vertices.remove(v);
}
catch (Exception ex)
{
throw new GraphException(ex);
}
}
/**
* Description of the Method
*/
public void removeVertex(Vertex v)
throws GraphException
{
Iterator conts = contracts.iterator();
while (conts.hasNext())
{
((Contract) conts.next()).removeVertex(v);
}
removeVertexI(v);
}
/**
* Adds a feature to the EdgeI attribute of the DirectedGraphImpl object
*/
private void addEdgeI(Edge e, Vertex start, Vertex end)
throws GraphException
{
edges.add(e);
if (e instanceof WeightedEdge)
{
edgeWeights.put(e, new Double(((WeightedEdge) e).getWeight()));
}
else
{
edgeWeights.put(e, new Double(1.0));
}
edgeSource.put(e, start);
edgeTarget.put(e, end);
if (!outbound.containsKey(start))
{
Set edgeSet = new HashSet();
edgeSet.add(e);
outbound.put(start, edgeSet);
}
else
{
((Set) outbound.get(start)).add(e);
}
if (!inbound.containsKey(end))
{
Set edgeSet = new HashSet();
edgeSet.add(e);
inbound.put(end, edgeSet);
}
else
{
((Set) inbound.get(end)).add(e);
}
}
/**
* Adds a feature to the Edge attribute of the DirectedGraphImpl object
*/
public void addEdge(Edge e,
Vertex start,
Vertex end)
throws GraphException
{
Iterator conts = contracts.iterator();
while (conts.hasNext())
{
Contract cont = (Contract) conts.next();
cont.addEdge(e, start, end);
}
addEdgeI(e, start, end);
}
/**
* Description of the Method
*/
private void removeEdgeI(Edge e)
throws GraphException
{
try
{
Set edgeSet = null;
Vertex source = (Vertex) edgeSource.get(e);
edgeSource.remove(e);
edgeSet = (Set) outbound.get(source);
edgeSet.remove(e);
Vertex target = (Vertex) edgeTarget.get(e);
edgeTarget.remove(e);
edgeSet = (Set) inbound.get(target);
edgeSet.remove(e);
if (edgeWeights.containsKey(e))
{
edgeWeights.remove(e);
}
}
catch (Exception ex)
{
throw new GraphException(ex);
}
}
/**
* Description of the Method
*/
public void removeEdge(Edge e)
throws GraphException
{
Iterator conts = contracts.iterator();
while (conts.hasNext())
{
((Contract) conts.next()).removeEdge(e);
}
removeEdgeI(e);
}
// WeightedGraph
/**
* Gets the weight attribute of the DirectedGraphImpl object
*/
public double getWeight(Edge e)
{
if (edgeWeights.containsKey(e))
{
return ((Double) edgeWeights.get(e)).doubleValue();
}
else
{
return 1.0;
}
}
/**
* Description of the Method
*/
public Object invoke(Object proxy,
Method method,
Object args[])
throws Throwable
{
try {
return method.invoke(this, args);
} catch (InvocationTargetException ex) {
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.
*
@ -14,91 +14,91 @@ package org.apache.commons.graph.domain.basic;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* DirectedGraphWrapper This is a superclass to all wrappers that work over
* DirectedGraphs.
*/
import java.util.Set;
import org.apache.commons.graph.*;
/**
* Description of the Class
*/
public class DirectedGraphWrapper
extends GraphWrapper
implements DirectedGraph
{
private DirectedGraph impl = null;
/**
* Constructor for the DirectedGraphWrapper object
*
* @param graph
*/
public DirectedGraphWrapper(DirectedGraph graph)
{
super(graph);
impl = graph;
}
/**
* Constructor for the DirectedGraphWrapper object
*/
public DirectedGraphWrapper()
{
super();
}
/**
* Sets the dirGraph attribute of the DirectedGraphWrapper object
*/
public void setDirGraph(DirectedGraph graph)
{
impl = graph;
setGraph(graph);
}
/**
* Gets the inbound attribute of the DirectedGraphWrapper object
*/
public Set getInbound(Vertex v)
{
return impl.getInbound(v);
}
/**
* Gets the outbound attribute of the DirectedGraphWrapper object
*/
public Set getOutbound(Vertex v)
{
return impl.getOutbound(v);
}
/**
* Gets the source attribute of the DirectedGraphWrapper object
*/
public Vertex getSource(Edge e)
{
return impl.getSource(e);
}
/**
* Gets the target attribute of the DirectedGraphWrapper object
*/
public Vertex getTarget(Edge e)
{
return impl.getTarget(e);
}
}
*/
/**
* DirectedGraphWrapper This is a superclass to all wrappers that work over
* DirectedGraphs.
*/
import java.util.Set;
import org.apache.commons.graph.*;
/**
* Description of the Class
*/
public class DirectedGraphWrapper
extends GraphWrapper
implements DirectedGraph
{
private DirectedGraph impl = null;
/**
* Constructor for the DirectedGraphWrapper object
*
* @param graph
*/
public DirectedGraphWrapper(DirectedGraph graph)
{
super(graph);
impl = graph;
}
/**
* Constructor for the DirectedGraphWrapper object
*/
public DirectedGraphWrapper()
{
super();
}
/**
* Sets the dirGraph attribute of the DirectedGraphWrapper object
*/
public void setDirGraph(DirectedGraph graph)
{
impl = graph;
setGraph(graph);
}
/**
* Gets the inbound attribute of the DirectedGraphWrapper object
*/
public Set getInbound(Vertex v)
{
return impl.getInbound(v);
}
/**
* Gets the outbound attribute of the DirectedGraphWrapper object
*/
public Set getOutbound(Vertex v)
{
return impl.getOutbound(v);
}
/**
* Gets the source attribute of the DirectedGraphWrapper object
*/
public Vertex getSource(Edge e)
{
return impl.getSource(e);
}
/**
* Gets the target attribute of the DirectedGraphWrapper object
*/
public Vertex getTarget(Edge e)
{
return impl.getTarget(e);
}
}

View File

@ -1,74 +1,74 @@
package org.apache.commons.graph.domain.basic;
/**
* GraphWrapper This is a superclass of all Wrapper implementations. It
* basically does a redirection to the graph.
*/
import java.util.Set;
import org.apache.commons.graph.*;
/**
* Description of the Class
*/
public class GraphWrapper
{
private Graph impl = null;
/**
* Constructor for the GraphWrapper object
*
* @param impl
*/
public GraphWrapper(Graph impl)
{
this.impl = impl;
}
/**
* Constructor for the GraphWrapper object
*/
public GraphWrapper() { }
/**
* Sets the graph attribute of the GraphWrapper object
*/
public void setGraph(Graph impl)
{
this.impl = impl;
}
// Graph Implementation. . .
/**
* Gets the vertices attribute of the GraphWrapper object
*/
public Set getVertices()
{
return impl.getVertices();
}
/**
* Gets the edges attribute of the GraphWrapper object
*/
public Set getEdges()
{
return impl.getEdges();
}
/**
* Gets the vertices attribute of the GraphWrapper object
*/
public Set getVertices(Edge e)
{
return impl.getVertices(e);
}
/**
* Gets the edges attribute of the GraphWrapper object
*/
public Set getEdges(Vertex v)
{
return impl.getEdges(v);
}
}
package org.apache.commons.graph.domain.basic;
/**
* GraphWrapper This is a superclass of all Wrapper implementations. It
* basically does a redirection to the graph.
*/
import java.util.Set;
import org.apache.commons.graph.*;
/**
* Description of the Class
*/
public class GraphWrapper
{
private Graph impl = null;
/**
* Constructor for the GraphWrapper object
*
* @param impl
*/
public GraphWrapper(Graph impl)
{
this.impl = impl;
}
/**
* Constructor for the GraphWrapper object
*/
public GraphWrapper() { }
/**
* Sets the graph attribute of the GraphWrapper object
*/
public void setGraph(Graph impl)
{
this.impl = impl;
}
// Graph Implementation. . .
/**
* Gets the vertices attribute of the GraphWrapper object
*/
public Set getVertices()
{
return impl.getVertices();
}
/**
* Gets the edges attribute of the GraphWrapper object
*/
public Set getEdges()
{
return impl.getEdges();
}
/**
* Gets the vertices attribute of the GraphWrapper object
*/
public Set getVertices(Edge e)
{
return impl.getVertices(e);
}
/**
* Gets the edges attribute of the GraphWrapper object
*/
public Set getEdges(Vertex 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.
*
@ -14,201 +14,201 @@ package org.apache.commons.graph.domain.basic;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.Set;
import java.util.Map;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Iterator;
import java.lang.reflect.*;
import org.apache.commons.graph.*;
import org.apache.commons.graph.exception.*;
/**
* Description of the Class
*/
public class UndirectedGraphImpl
implements UndirectedGraph,
WeightedGraph,
MutableGraph,
InvocationHandler
{
private Set vertices = new HashSet();
private Set edges = new HashSet();
private Map edgeVerts = new HashMap();// EDGE X SET( VERTS )
private Map vertEdges = new HashMap();// VERTEX X SET( EDGE )
private Map edgeWeights = new HashMap(); // EDGE X WEIGHT
/**
* Constructor for the UndirectedGraphImpl object
*/
public UndirectedGraphImpl() { }
/**
* Adds a feature to the Vertex attribute of the UndirectedGraphImpl object
*/
public void addVertex(Vertex v)
throws GraphException
{
vertices.add(v);
}
public void removeVertex( Vertex v )
throws GraphException
{
vertices.remove( v );
}
public void removeEdge( Edge e )
throws GraphException
{
edges.remove( e );
}
public void addEdge(Edge e)
throws GraphException
{
edges.add( e );
}
public void disconnect(Edge e, Vertex v) {
if (edgeVerts.containsKey( e )) {
((Set) edgeVerts.get( e )).remove( v );
}
if (vertEdges.containsKey( v )) {
((Set) vertEdges.get( v )).remove( e );
}
}
public void connect( Edge e, Vertex v ) {
Set verts = null;
if (!edgeVerts.containsKey( e )) {
verts = new HashSet();
edgeVerts.put( e, verts );
} else {
verts = (Set) edgeVerts.get( e );
}
verts.add( v );
Set edges = null;
if (!vertEdges.containsKey( v )) {
edges = new HashSet();
vertEdges.put( v, edges );
} else {
edges = (Set) vertEdges.get( v );
}
edges.add( e );
}
/**
* Adds a feature to the Edge attribute of the UndirectedGraphImpl object
*/
public void addEdge(Edge e,
Set vertices)
throws GraphException
{
addEdge( e );
Iterator verts = vertices.iterator();
while (verts.hasNext()) {
connect( e, (Vertex) verts.next() );
}
}
// Interface Methods
/**
* Gets the vertices attribute of the UndirectedGraphImpl object
*/
public Set getVertices()
{
return new HashSet(vertices);
}
/**
* Gets the vertices attribute of the UndirectedGraphImpl object
*/
public Set getVertices(Edge e)
{
if (edgeVerts.containsKey(e))
{
return new HashSet((Set) edgeVerts.get(e));
}
else
{
return new HashSet();
}
}
/**
* Gets the edges attribute of the UndirectedGraphImpl object
*/
public Set getEdges()
{
return new HashSet(edges);
}
/**
* Gets the edges attribute of the UndirectedGraphImpl object
*/
public Set getEdges(Vertex v)
{
if (vertEdges.containsKey(v))
{
return new HashSet((Set) vertEdges.get(v));
}
else
{
return new HashSet();
}
}
public void setWeight( Edge e, double w ) {
if (edgeWeights.containsKey( e )) {
edgeWeights.remove( e );
}
edgeWeights.put( e, new Double( w ) );
}
public double getWeight( Edge e ) {
if (edgeWeights.containsKey( e )) {
return ((Double) edgeWeights.get( e ) ).doubleValue();
} else {
return 1.0;
}
}
/**
* Description of the Method
*/
public Object invoke(Object proxy,
Method method,
Object args[])
throws Throwable
{
try {
return method.invoke(this, args);
} catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
}
*/
import java.util.Set;
import java.util.Map;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Iterator;
import java.lang.reflect.*;
import org.apache.commons.graph.*;
import org.apache.commons.graph.exception.*;
/**
* Description of the Class
*/
public class UndirectedGraphImpl
implements UndirectedGraph,
WeightedGraph,
MutableGraph,
InvocationHandler
{
private Set vertices = new HashSet();
private Set edges = new HashSet();
private Map edgeVerts = new HashMap();// EDGE X SET( VERTS )
private Map vertEdges = new HashMap();// VERTEX X SET( EDGE )
private Map edgeWeights = new HashMap(); // EDGE X WEIGHT
/**
* Constructor for the UndirectedGraphImpl object
*/
public UndirectedGraphImpl() { }
/**
* Adds a feature to the Vertex attribute of the UndirectedGraphImpl object
*/
public void addVertex(Vertex v)
throws GraphException
{
vertices.add(v);
}
public void removeVertex( Vertex v )
throws GraphException
{
vertices.remove( v );
}
public void removeEdge( Edge e )
throws GraphException
{
edges.remove( e );
}
public void addEdge(Edge e)
throws GraphException
{
edges.add( e );
}
public void disconnect(Edge e, Vertex v) {
if (edgeVerts.containsKey( e )) {
((Set) edgeVerts.get( e )).remove( v );
}
if (vertEdges.containsKey( v )) {
((Set) vertEdges.get( v )).remove( e );
}
}
public void connect( Edge e, Vertex v ) {
Set verts = null;
if (!edgeVerts.containsKey( e )) {
verts = new HashSet();
edgeVerts.put( e, verts );
} else {
verts = (Set) edgeVerts.get( e );
}
verts.add( v );
Set edges = null;
if (!vertEdges.containsKey( v )) {
edges = new HashSet();
vertEdges.put( v, edges );
} else {
edges = (Set) vertEdges.get( v );
}
edges.add( e );
}
/**
* Adds a feature to the Edge attribute of the UndirectedGraphImpl object
*/
public void addEdge(Edge e,
Set vertices)
throws GraphException
{
addEdge( e );
Iterator verts = vertices.iterator();
while (verts.hasNext()) {
connect( e, (Vertex) verts.next() );
}
}
// Interface Methods
/**
* Gets the vertices attribute of the UndirectedGraphImpl object
*/
public Set getVertices()
{
return new HashSet(vertices);
}
/**
* Gets the vertices attribute of the UndirectedGraphImpl object
*/
public Set getVertices(Edge e)
{
if (edgeVerts.containsKey(e))
{
return new HashSet((Set) edgeVerts.get(e));
}
else
{
return new HashSet();
}
}
/**
* Gets the edges attribute of the UndirectedGraphImpl object
*/
public Set getEdges()
{
return new HashSet(edges);
}
/**
* Gets the edges attribute of the UndirectedGraphImpl object
*/
public Set getEdges(Vertex v)
{
if (vertEdges.containsKey(v))
{
return new HashSet((Set) vertEdges.get(v));
}
else
{
return new HashSet();
}
}
public void setWeight( Edge e, double w ) {
if (edgeWeights.containsKey( e )) {
edgeWeights.remove( e );
}
edgeWeights.put( e, new Double( w ) );
}
public double getWeight( Edge e ) {
if (edgeWeights.containsKey( e )) {
return ((Double) edgeWeights.get( e ) ).doubleValue();
} else {
return 1.0;
}
}
/**
* Description of the Method
*/
public Object invoke(Object proxy,
Method method,
Object args[])
throws Throwable
{
try {
return method.invoke(this, args);
} catch (InvocationTargetException ex) {
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.
*
@ -14,57 +14,57 @@ package org.apache.commons.graph.domain.basic;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This is a simple wrapper to wrap around a graph, and create a weighted graph.
*/
import java.util.Map;
import java.util.HashMap;
import org.apache.commons.graph.*;
/**
* Description of the Class
*/
public class WeightedGraphWrapper
extends GraphWrapper
implements WeightedGraph
{
private Map weights = new HashMap();// EDGE X WEIGHT
/**
* Constructor for the WeightedGraphWrapper object
*
* @param graph
*/
public WeightedGraphWrapper(Graph graph)
{
super(graph);
}
/**
* Gets the weight attribute of the WeightedGraphWrapper object
*/
public double getWeight(Edge e)
{
if (weights.containsKey(e))
{
return ((Double) weights.get(e)).doubleValue();
}
else
{
return 1.0;
}
}
/**
* Sets the weight attribute of the WeightedGraphWrapper object
*/
public void setWeight(Edge e, double weight)
{
weights.put(e, new Double(weight));
}
}
*/
/**
* This is a simple wrapper to wrap around a graph, and create a weighted graph.
*/
import java.util.Map;
import java.util.HashMap;
import org.apache.commons.graph.*;
/**
* Description of the Class
*/
public class WeightedGraphWrapper
extends GraphWrapper
implements WeightedGraph
{
private Map weights = new HashMap();// EDGE X WEIGHT
/**
* Constructor for the WeightedGraphWrapper object
*
* @param graph
*/
public WeightedGraphWrapper(Graph graph)
{
super(graph);
}
/**
* Gets the weight attribute of the WeightedGraphWrapper object
*/
public double getWeight(Edge e)
{
if (weights.containsKey(e))
{
return ((Double) weights.get(e)).doubleValue();
}
else
{
return 1.0;
}
}
/**
* Sets the weight attribute of the WeightedGraphWrapper object
*/
public void setWeight(Edge e, double weight)
{
weights.put(e, new Double(weight));
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,35 +1,35 @@
package org.apache.commons.graph.exception;
/**
* Description of the Class
*/
public class ContractVerificationException extends GraphException
{
/**
* Constructor for the ContractVerificationException object
*/
public ContractVerificationException()
{
super();
}
/**
* Constructor for the ContractVerificationException object
*
* @param msg
*/
public ContractVerificationException(String msg)
{
super(msg);
}
/**
* Constructor for the ContractVerificationException object
*
* @param cause
*/
public ContractVerificationException(Throwable cause)
{
super(cause);
}
}
package org.apache.commons.graph.exception;
/**
* Description of the Class
*/
public class ContractVerificationException extends GraphException
{
/**
* Constructor for the ContractVerificationException object
*/
public ContractVerificationException()
{
super();
}
/**
* Constructor for the ContractVerificationException object
*
* @param msg
*/
public ContractVerificationException(String msg)
{
super(msg);
}
/**
* Constructor for the ContractVerificationException object
*
* @param cause
*/
public ContractVerificationException(Throwable 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.
*
@ -13,38 +13,38 @@ package org.apache.commons.graph.exception;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Description of the Class
*/
public class CycleException extends GraphException
{
/**
* Constructor for the CycleException object
*/
public CycleException()
{
super();
}
/**
* Constructor for the CycleException object
*
* @param msg
*/
public CycleException(String msg)
{
super(msg);
}
/**
* Constructor for the CycleException object
*
* @param cause
*/
public CycleException(Throwable cause)
{
super(cause);
}
}
*/
/**
* Description of the Class
*/
public class CycleException extends GraphException
{
/**
* Constructor for the CycleException object
*/
public CycleException()
{
super();
}
/**
* Constructor for the CycleException object
*
* @param msg
*/
public CycleException(String msg)
{
super(msg);
}
/**
* Constructor for the CycleException object
*
* @param cause
*/
public CycleException(Throwable 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.
*
@ -14,62 +14,62 @@ package org.apache.commons.graph.exception;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* GraphException This is the superclass of all exceptions that can be thrown.
*/
import java.io.PrintStream;
import java.io.PrintWriter;
public class GraphException extends RuntimeException
{
private Throwable cause = null;
/**
* Constructor for the GraphException object
*/
public GraphException()
{
super();
}
/**
* Constructor for the GraphException object
*
* @param msg
*/
public GraphException(String msg)
{
super(msg);
}
/**
* Constructor for the GraphException object
*
* @param cause
*/
public GraphException(Throwable cause)
{
super(cause.getMessage());
this.cause = cause;
}
public Throwable getCause() {
return cause;
}
public void printStackTrace() {
cause.printStackTrace();
}
public void printStackTrace( PrintStream s ) {
cause.printStackTrace( s );
}
public void printStackTrace( PrintWriter w ) {
cause.printStackTrace( w );
}
}
*/
/**
* GraphException This is the superclass of all exceptions that can be thrown.
*/
import java.io.PrintStream;
import java.io.PrintWriter;
public class GraphException extends RuntimeException
{
private Throwable cause = null;
/**
* Constructor for the GraphException object
*/
public GraphException()
{
super();
}
/**
* Constructor for the GraphException object
*
* @param msg
*/
public GraphException(String msg)
{
super(msg);
}
/**
* Constructor for the GraphException object
*
* @param cause
*/
public GraphException(Throwable cause)
{
super(cause.getMessage());
this.cause = cause;
}
public Throwable getCause() {
return cause;
}
public void printStackTrace() {
cause.printStackTrace();
}
public void printStackTrace( PrintStream s ) {
cause.printStackTrace( s );
}
public void printStackTrace( PrintWriter 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.
*
@ -13,38 +13,38 @@ package org.apache.commons.graph.exception;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Description of the Class
*/
public class HyperGraphException extends GraphException
{
/**
* Constructor for the HyperGraphException object
*/
public HyperGraphException()
{
super();
}
/**
* Constructor for the HyperGraphException object
*
* @param msg
*/
public HyperGraphException(String msg)
{
super(msg);
}
/**
* Constructor for the HyperGraphException object
*
* @param cause
*/
public HyperGraphException(Throwable cause)
{
super(cause);
}
}
*/
/**
* Description of the Class
*/
public class HyperGraphException extends GraphException
{
/**
* Constructor for the HyperGraphException object
*/
public HyperGraphException()
{
super();
}
/**
* Constructor for the HyperGraphException object
*
* @param msg
*/
public HyperGraphException(String msg)
{
super(msg);
}
/**
* Constructor for the HyperGraphException object
*
* @param cause
*/
public HyperGraphException(Throwable 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.
*
@ -14,38 +14,38 @@ package org.apache.commons.graph.exception;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Description of the Class
*/
public class NegativeCycleException extends GraphException
{
/**
* Constructor for the NegativeCycleException object
*/
public NegativeCycleException()
{
super();
}
/**
* Constructor for the NegativeCycleException object
*
* @param msg
*/
public NegativeCycleException(String msg)
{
super(msg);
}
/**
* Constructor for the NegativeCycleException object
*
* @param cause
*/
public NegativeCycleException(Throwable cause)
{
super(cause);
}
}
*/
/**
* Description of the Class
*/
public class NegativeCycleException extends GraphException
{
/**
* Constructor for the NegativeCycleException object
*/
public NegativeCycleException()
{
super();
}
/**
* Constructor for the NegativeCycleException object
*
* @param msg
*/
public NegativeCycleException(String msg)
{
super(msg);
}
/**
* Constructor for the NegativeCycleException object
*
* @param cause
*/
public NegativeCycleException(Throwable 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.
*
@ -14,39 +14,39 @@ package org.apache.commons.graph.exception;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Description of the Class
*/
public class NoPathException
extends GraphException
{
/**
* Constructor for the NoPathException object
*/
public NoPathException()
{
super();
}
/**
* Constructor for the NoPathException object
*
* @param msg
*/
public NoPathException(String msg)
{
super(msg);
}
/**
* Constructor for the NoPathException object
*
* @param t
*/
public NoPathException(Throwable t)
{
super(t);
}
}
*/
/**
* Description of the Class
*/
public class NoPathException
extends GraphException
{
/**
* Constructor for the NoPathException object
*/
public NoPathException()
{
super();
}
/**
* Constructor for the NoPathException object
*
* @param msg
*/
public NoPathException(String msg)
{
super(msg);
}
/**
* Constructor for the NoPathException object
*
* @param t
*/
public NoPathException(Throwable 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.
*
@ -14,140 +14,140 @@ package org.apache.commons.graph.factory;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.lang.reflect.*;
import org.apache.commons.graph.*;
import org.apache.commons.graph.contract.*;
import org.apache.commons.graph.exception.*;
import org.apache.commons.graph.domain.basic.*;
/**
* Description of the Class
*/
public class GraphFactory
{
/**
* Constructor for the GraphFactory object
*/
public GraphFactory() { }
/**
* makeGraph
*
* @param contracts Which contracts to enforce.
* @param baseGraph Is the actual *GraphImpl which will be at the core of
* the Proxy.
* @param baseGraphType Interface which is returned.
* @param isWeighted Does the graph handle Weights?
* @param init Initialization Graph.
*/
private Object makeGraph(Contract contracts[],
InvocationHandler baseGraph,
Class baseGraphType,
boolean isWeighted)
throws GraphException
{
int interfaceCount = contracts.length;
interfaceCount++;// BaseGraph Type
if (isWeighted)
{
interfaceCount++;
}// WeightedGraph Type
Class inter[] = new Class[interfaceCount];
int pos = 0;
for (pos = 0; pos < contracts.length; pos++)
{
inter[pos] = contracts[pos].getInterface();
}
if (isWeighted)
{
inter[pos] = org.apache.commons.graph.WeightedGraph.class;
pos++;
}
inter[pos] = baseGraphType;
return Proxy.newProxyInstance(baseGraph.getClass().getClassLoader(),
inter, baseGraph);
}
/**
* makeDirectedGraph
*
* @param contracts - Array of Contracts this Graph should meet.
* @param isWeighted - If true, the Graph will implement the WeightedGraph
* interface.
* @param graph - If it is provided, the graph will initially be equal to
* the graph.
*/
public DirectedGraph makeDirectedGraph(Contract contracts[],
boolean isWeighted,
DirectedGraph graph)
throws GraphException
{
DirectedGraphImpl dgi = null;
if (graph != null)
{
dgi = new DirectedGraphImpl(graph);
}
else
{
dgi = new DirectedGraphImpl();
}
for (int i = 0; i < contracts.length; i++)
{
dgi.addContract(contracts[i]);
}
return (DirectedGraph)
makeGraph(contracts,
dgi, org.apache.commons.graph.DirectedGraph.class,
isWeighted);
}
/**
* makeMutableDirectedGraph
*
* @param contracts - Array of Contracts this Graph should meet.
* @param isWeighted - If true, the Graph will implement the WeightedGraph
* interface.
* @param graph - If it is provided, the graph will initially be equal to
* the graph.
*/
public MutableDirectedGraph
makeMutableDirectedGraph(Contract contracts[],
boolean isWeighted,
DirectedGraph graph)
throws GraphException
{
DirectedGraphImpl dgi = null;
if (graph != null)
{
dgi = new DirectedGraphImpl(graph);
}
else
{
dgi = new DirectedGraphImpl();
}
for (int i = 0; i < contracts.length; i++)
{
dgi.addContract(contracts[i]);
}
return (MutableDirectedGraph)
makeGraph(contracts,
dgi,
org.apache.commons.graph.MutableDirectedGraph.class,
isWeighted);
}
}
*/
import java.lang.reflect.*;
import org.apache.commons.graph.*;
import org.apache.commons.graph.contract.*;
import org.apache.commons.graph.exception.*;
import org.apache.commons.graph.domain.basic.*;
/**
* Description of the Class
*/
public class GraphFactory
{
/**
* Constructor for the GraphFactory object
*/
public GraphFactory() { }
/**
* makeGraph
*
* @param contracts Which contracts to enforce.
* @param baseGraph Is the actual *GraphImpl which will be at the core of
* the Proxy.
* @param baseGraphType Interface which is returned.
* @param isWeighted Does the graph handle Weights?
* @param init Initialization Graph.
*/
private Object makeGraph(Contract contracts[],
InvocationHandler baseGraph,
Class baseGraphType,
boolean isWeighted)
throws GraphException
{
int interfaceCount = contracts.length;
interfaceCount++;// BaseGraph Type
if (isWeighted)
{
interfaceCount++;
}// WeightedGraph Type
Class inter[] = new Class[interfaceCount];
int pos = 0;
for (pos = 0; pos < contracts.length; pos++)
{
inter[pos] = contracts[pos].getInterface();
}
if (isWeighted)
{
inter[pos] = org.apache.commons.graph.WeightedGraph.class;
pos++;
}
inter[pos] = baseGraphType;
return Proxy.newProxyInstance(baseGraph.getClass().getClassLoader(),
inter, baseGraph);
}
/**
* makeDirectedGraph
*
* @param contracts - Array of Contracts this Graph should meet.
* @param isWeighted - If true, the Graph will implement the WeightedGraph
* interface.
* @param graph - If it is provided, the graph will initially be equal to
* the graph.
*/
public DirectedGraph makeDirectedGraph(Contract contracts[],
boolean isWeighted,
DirectedGraph graph)
throws GraphException
{
DirectedGraphImpl dgi = null;
if (graph != null)
{
dgi = new DirectedGraphImpl(graph);
}
else
{
dgi = new DirectedGraphImpl();
}
for (int i = 0; i < contracts.length; i++)
{
dgi.addContract(contracts[i]);
}
return (DirectedGraph)
makeGraph(contracts,
dgi, org.apache.commons.graph.DirectedGraph.class,
isWeighted);
}
/**
* makeMutableDirectedGraph
*
* @param contracts - Array of Contracts this Graph should meet.
* @param isWeighted - If true, the Graph will implement the WeightedGraph
* interface.
* @param graph - If it is provided, the graph will initially be equal to
* the graph.
*/
public MutableDirectedGraph
makeMutableDirectedGraph(Contract contracts[],
boolean isWeighted,
DirectedGraph graph)
throws GraphException
{
DirectedGraphImpl dgi = null;
if (graph != null)
{
dgi = new DirectedGraphImpl(graph);
}
else
{
dgi = new DirectedGraphImpl();
}
for (int i = 0; i < contracts.length; i++)
{
dgi.addContract(contracts[i]);
}
return (MutableDirectedGraph)
makeGraph(contracts,
dgi,
org.apache.commons.graph.MutableDirectedGraph.class,
isWeighted);
}
}

View File

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

View File

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

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -1,150 +1,150 @@
package org.apache.commons.graph.algorithm.util;
import junit.framework.*;
public class LabelTest
extends TestCase
{
private String testName = null;
public LabelTest( String testName ) {
super( testName );
this.testName = testName;
}
private Label L1;
private Label L1_;
private Label L1__;
private Label L2;
private Label L3;
private Label L4;
private Label L5;
private Label L6;
public void setUp() {
L1 = new Label();
L2 = new Label();
L3 = new Label();
L4 = new Label();
L5 = new Label();
L6 = new Label();
L1_ = new Label( L1 );
L1__ = new Label( L1_ );
}
public void testDefaultConfig()
throws Throwable
{
assertEquals( L1.getRoot(), L1 );
assertEquals( L1_.getRoot(), L1 );
assertEquals( L1__.getRoot(), L1 );
}
public void testSetRoot1()
throws Throwable
{
assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L2 );
L2.setRoot( L1 );
assertEquals( L2.getRoot(), L1 );
}
public void testSetRoot2()
throws Throwable
{
assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L2 );
assertEquals( L3.getRoot(), L3 );
L3.setRoot( L2 );
L2.setRoot( L1 );
assertEquals( L3.getRoot(), L1 );
assertEquals( L2.getRoot(), L1 );
assertEquals( L1.getRoot(), L1 );
}
public void testSetRoot3()
throws Throwable
{
assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L2 );
assertEquals( L3.getRoot(), L3 );
L2.setRoot( L1 );
L3.setRoot( L2 );
assertEquals( L3.getRoot(), L1 );
assertEquals( L2.getRoot(), L1 );
assertEquals( L1.getRoot(), L1 );
}
public void testSetRoot4()
throws Throwable
{
assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L2 );
assertEquals( L3.getRoot(), L3 );
assertEquals( L4.getRoot(), L4 );
L4.setRoot( L3 );
L2.setRoot( L1 );
L3.setRoot( L1 );
assertEquals( L4.getRoot(), L1 );
assertEquals( L3.getRoot(), L1 );
assertEquals( L2.getRoot(), L1 );
assertEquals( L1.getRoot(), L1 );
}
public void testSetRootCycle()
throws Throwable
{
assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L2 );
L1.setRoot( L2 );
L2.setRoot( L1 );
assertEquals( L2.getRoot(), L2 );
assertEquals( L1.getRoot(), L2 );
}
public void setDiamond1() throws Throwable {
assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L2 );
assertEquals( L3.getRoot(), L3 );
assertEquals( L4.getRoot(), L4 );
L2.setRoot( L1 );
L3.setRoot( L1 );
L4.setRoot( L3 );
assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L1 );
assertEquals( L3.getRoot(), L1 );
assertEquals( L4.getRoot(), L1 );
}
public void setDiamond2() throws Throwable {
assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L2 );
assertEquals( L3.getRoot(), L3 );
assertEquals( L4.getRoot(), L4 );
L4.setRoot( L3 );
L2.setRoot( L1 );
L3.setRoot( L1 );
assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L1 );
assertEquals( L3.getRoot(), L1 );
assertEquals( L4.getRoot(), L1 );
}
}
package org.apache.commons.graph.algorithm.util;
import junit.framework.*;
public class LabelTest
extends TestCase
{
private String testName = null;
public LabelTest( String testName ) {
super( testName );
this.testName = testName;
}
private Label L1;
private Label L1_;
private Label L1__;
private Label L2;
private Label L3;
private Label L4;
private Label L5;
private Label L6;
public void setUp() {
L1 = new Label();
L2 = new Label();
L3 = new Label();
L4 = new Label();
L5 = new Label();
L6 = new Label();
L1_ = new Label( L1 );
L1__ = new Label( L1_ );
}
public void testDefaultConfig()
throws Throwable
{
assertEquals( L1.getRoot(), L1 );
assertEquals( L1_.getRoot(), L1 );
assertEquals( L1__.getRoot(), L1 );
}
public void testSetRoot1()
throws Throwable
{
assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L2 );
L2.setRoot( L1 );
assertEquals( L2.getRoot(), L1 );
}
public void testSetRoot2()
throws Throwable
{
assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L2 );
assertEquals( L3.getRoot(), L3 );
L3.setRoot( L2 );
L2.setRoot( L1 );
assertEquals( L3.getRoot(), L1 );
assertEquals( L2.getRoot(), L1 );
assertEquals( L1.getRoot(), L1 );
}
public void testSetRoot3()
throws Throwable
{
assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L2 );
assertEquals( L3.getRoot(), L3 );
L2.setRoot( L1 );
L3.setRoot( L2 );
assertEquals( L3.getRoot(), L1 );
assertEquals( L2.getRoot(), L1 );
assertEquals( L1.getRoot(), L1 );
}
public void testSetRoot4()
throws Throwable
{
assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L2 );
assertEquals( L3.getRoot(), L3 );
assertEquals( L4.getRoot(), L4 );
L4.setRoot( L3 );
L2.setRoot( L1 );
L3.setRoot( L1 );
assertEquals( L4.getRoot(), L1 );
assertEquals( L3.getRoot(), L1 );
assertEquals( L2.getRoot(), L1 );
assertEquals( L1.getRoot(), L1 );
}
public void testSetRootCycle()
throws Throwable
{
assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L2 );
L1.setRoot( L2 );
L2.setRoot( L1 );
assertEquals( L2.getRoot(), L2 );
assertEquals( L1.getRoot(), L2 );
}
public void setDiamond1() throws Throwable {
assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L2 );
assertEquals( L3.getRoot(), L3 );
assertEquals( L4.getRoot(), L4 );
L2.setRoot( L1 );
L3.setRoot( L1 );
L4.setRoot( L3 );
assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L1 );
assertEquals( L3.getRoot(), L1 );
assertEquals( L4.getRoot(), L1 );
}
public void setDiamond2() throws Throwable {
assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L2 );
assertEquals( L3.getRoot(), L3 );
assertEquals( L4.getRoot(), L4 );
L4.setRoot( L3 );
L2.setRoot( L1 );
L3.setRoot( L1 );
assertEquals( L1.getRoot(), L1 );
assertEquals( L2.getRoot(), L1 );
assertEquals( L3.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.
*
@ -14,410 +14,410 @@ package org.apache.commons.graph.contract;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.commons.graph.*;
import org.apache.commons.graph.exception.*;
/**
* Description of the Class
*/
public class AcyclicContractTest
extends GraphTest
{
/**
* Constructor for the AcyclicContractTest object
*
* @param name
*/
public AcyclicContractTest(String name)
{
super(name);
}
/**
* A unit test for JUnit
*/
public void testDirNullGraph()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeDirNullGraph());
IUT.verify();
}
/**
* A unit test for JUnit
*/
public void testDirSingleVertex()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeDirSingleVertex());
IUT.verify();
try
{
IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced.");
}
catch (CycleException ex)
{}
}
/**
* A unit test for JUnit
*/
public void testSelfLoop()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeSelfLoop());
try
{
IUT.verify();
fail("No CycleException thrown on Verification.");
}
catch (CycleException ex)
{}
}
/**
* A unit test for JUnit
*/
public void testDirDoubleVertex()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeDirDoubleVertex());
IUT.verify();
try
{
IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced.");
}
catch (CycleException ex)
{}
try
{
IUT.addEdge(V1_V2, V1, V2);
}
catch (GraphException ex)
{
fail("Contract prevented adding of valid edge. V1->V2");
}
}
/**
* A unit test for JUnit
*/
public void testDirectedEdge()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeDirectedEdge());
IUT.verify();
try
{
IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced.");
}
catch (CycleException ex)
{}
try
{
IUT.addEdge(V1_V2_, V1, V2);
}
catch (GraphException ex)
{
fail("Contract prevented adding of valid edge. V1->V2'");
}
try
{
IUT.addEdge(V2_V1, V2, V1);
fail("Contract allowed cycle to be introduced.");
}
catch (CycleException ex)
{}
}
/**
* A unit test for JUnit
*/
public void testDirParallelEdges()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeDirParallelEdges());
IUT.verify();
try
{
IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced.");
}
catch (CycleException ex)
{}
try
{
IUT.addEdge(V1_V2__, V1, V2);
}
catch (GraphException ex)
{
fail("Contract prevented adding of valid edge. V1->V2'");
}
try
{
IUT.addEdge(V2_V1, V2, V1);
fail("Contract allowed cycle to be introduced.");
}
catch (CycleException ex)
{}
}
/**
* A unit test for JUnit
*/
public void testTwoCycle()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeTwoCycle());
try
{
IUT.verify();
fail("No CycleException thrown on Verification.");
}
catch (CycleException ex)
{}
}
/**
* A unit test for JUnit
*/
public void testDirectedCycle()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeDirectedCycle());
try
{
IUT.verify();
fail("No CycleException thrown on Verification.");
}
catch (CycleException ex)
{}
}
/**
* A unit test for JUnit
*/
public void testPipe()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makePipe());
IUT.verify();
try
{
IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced.");
}
catch (CycleException ex)
{}
try
{
IUT.addEdge(V1_V2_, V1, V2);
}
catch (GraphException ex)
{
fail("Contract prevented adding of valid edge. V1->V2'");
}
try
{
IUT.addEdge(V3_V1, V3, V1);
fail("Contract allowed cycle to be introduced.");
}
catch (CycleException ex)
{}
}
/**
* A unit test for JUnit
*/
public void testDiamond()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeDiamond());
IUT.verify();
try
{
IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced.");
}
catch (CycleException ex)
{}
try
{
IUT.addEdge(V2_V3, V2, V3);
}
catch (GraphException ex)
{
fail("Contract prevented adding of valid edge. V2->V3");
}
try
{
IUT.addEdge(V4_V1, V4, V1);
fail("Contract allowed cycle to be introduced.");
}
catch (CycleException ex)
{}
}
/**
* A unit test for JUnit
*/
public void testPipelessCycle()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makePipelessCycle());
IUT.verify();
try
{
IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced.");
}
catch (CycleException ex)
{}
try
{
IUT.addEdge(V2_V3, V2, V3);
}
catch (GraphException ex)
{
fail("Contract prevented adding of valid edge. V2->V3");
}
try
{
IUT.addEdge(V3_V4, V3, V4);
fail("Contract allowed cycle to be introduced.");
}
catch (CycleException ex)
{}
}
/**
* A unit test for JUnit
*/
public void testParentTree()
throws Throwable
{
System.err.println("---- PARENT TREE ----");
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeParentTree());
IUT.verify();
try
{
IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced.");
}
catch (CycleException ex)
{}
try
{
IUT.addEdge(V2_V3, V2, V3);
}
catch (GraphException ex)
{
fail("Contract prevented adding of valid edge. V2->V3");
}
try
{
IUT.addEdge(V1_V5, V1, V5);
fail("Contract allowed cycle to be introduced.");
}
catch (CycleException ex)
{}
}
/**
* A unit test for JUnit
*/
public void testChildTree()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeChildTree());
IUT.verify();
try
{
IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced.");
}
catch (CycleException ex)
{}
try
{
IUT.addEdge(V2_V3, V2, V3);
}
catch (GraphException ex)
{
fail("Contract prevented adding of valid edge. V2->V3");
}
try
{
IUT.addEdge(V5_V1, V5, V1);
fail("Contract allowed cycle to be introduced.");
}
catch (CycleException ex)
{}
}
}
*/
import org.apache.commons.graph.*;
import org.apache.commons.graph.exception.*;
/**
* Description of the Class
*/
public class AcyclicContractTest
extends GraphTest
{
/**
* Constructor for the AcyclicContractTest object
*
* @param name
*/
public AcyclicContractTest(String name)
{
super(name);
}
/**
* A unit test for JUnit
*/
public void testDirNullGraph()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeDirNullGraph());
IUT.verify();
}
/**
* A unit test for JUnit
*/
public void testDirSingleVertex()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeDirSingleVertex());
IUT.verify();
try
{
IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced.");
}
catch (CycleException ex)
{}
}
/**
* A unit test for JUnit
*/
public void testSelfLoop()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeSelfLoop());
try
{
IUT.verify();
fail("No CycleException thrown on Verification.");
}
catch (CycleException ex)
{}
}
/**
* A unit test for JUnit
*/
public void testDirDoubleVertex()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeDirDoubleVertex());
IUT.verify();
try
{
IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced.");
}
catch (CycleException ex)
{}
try
{
IUT.addEdge(V1_V2, V1, V2);
}
catch (GraphException ex)
{
fail("Contract prevented adding of valid edge. V1->V2");
}
}
/**
* A unit test for JUnit
*/
public void testDirectedEdge()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeDirectedEdge());
IUT.verify();
try
{
IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced.");
}
catch (CycleException ex)
{}
try
{
IUT.addEdge(V1_V2_, V1, V2);
}
catch (GraphException ex)
{
fail("Contract prevented adding of valid edge. V1->V2'");
}
try
{
IUT.addEdge(V2_V1, V2, V1);
fail("Contract allowed cycle to be introduced.");
}
catch (CycleException ex)
{}
}
/**
* A unit test for JUnit
*/
public void testDirParallelEdges()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeDirParallelEdges());
IUT.verify();
try
{
IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced.");
}
catch (CycleException ex)
{}
try
{
IUT.addEdge(V1_V2__, V1, V2);
}
catch (GraphException ex)
{
fail("Contract prevented adding of valid edge. V1->V2'");
}
try
{
IUT.addEdge(V2_V1, V2, V1);
fail("Contract allowed cycle to be introduced.");
}
catch (CycleException ex)
{}
}
/**
* A unit test for JUnit
*/
public void testTwoCycle()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeTwoCycle());
try
{
IUT.verify();
fail("No CycleException thrown on Verification.");
}
catch (CycleException ex)
{}
}
/**
* A unit test for JUnit
*/
public void testDirectedCycle()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeDirectedCycle());
try
{
IUT.verify();
fail("No CycleException thrown on Verification.");
}
catch (CycleException ex)
{}
}
/**
* A unit test for JUnit
*/
public void testPipe()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makePipe());
IUT.verify();
try
{
IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced.");
}
catch (CycleException ex)
{}
try
{
IUT.addEdge(V1_V2_, V1, V2);
}
catch (GraphException ex)
{
fail("Contract prevented adding of valid edge. V1->V2'");
}
try
{
IUT.addEdge(V3_V1, V3, V1);
fail("Contract allowed cycle to be introduced.");
}
catch (CycleException ex)
{}
}
/**
* A unit test for JUnit
*/
public void testDiamond()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeDiamond());
IUT.verify();
try
{
IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced.");
}
catch (CycleException ex)
{}
try
{
IUT.addEdge(V2_V3, V2, V3);
}
catch (GraphException ex)
{
fail("Contract prevented adding of valid edge. V2->V3");
}
try
{
IUT.addEdge(V4_V1, V4, V1);
fail("Contract allowed cycle to be introduced.");
}
catch (CycleException ex)
{}
}
/**
* A unit test for JUnit
*/
public void testPipelessCycle()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makePipelessCycle());
IUT.verify();
try
{
IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced.");
}
catch (CycleException ex)
{}
try
{
IUT.addEdge(V2_V3, V2, V3);
}
catch (GraphException ex)
{
fail("Contract prevented adding of valid edge. V2->V3");
}
try
{
IUT.addEdge(V3_V4, V3, V4);
fail("Contract allowed cycle to be introduced.");
}
catch (CycleException ex)
{}
}
/**
* A unit test for JUnit
*/
public void testParentTree()
throws Throwable
{
System.err.println("---- PARENT TREE ----");
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeParentTree());
IUT.verify();
try
{
IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced.");
}
catch (CycleException ex)
{}
try
{
IUT.addEdge(V2_V3, V2, V3);
}
catch (GraphException ex)
{
fail("Contract prevented adding of valid edge. V2->V3");
}
try
{
IUT.addEdge(V1_V5, V1, V5);
fail("Contract allowed cycle to be introduced.");
}
catch (CycleException ex)
{}
}
/**
* A unit test for JUnit
*/
public void testChildTree()
throws Throwable
{
AcyclicContract IUT =
new AcyclicContract();
IUT.setImpl(makeChildTree());
IUT.verify();
try
{
IUT.addEdge(V1_V1, V1, V1);
fail("No GraphException thrown when Self-Cycle introduced.");
}
catch (CycleException ex)
{}
try
{
IUT.addEdge(V2_V3, V2, V3);
}
catch (GraphException ex)
{
fail("Contract prevented adding of valid edge. V2->V3");
}
try
{
IUT.addEdge(V5_V1, V5, V1);
fail("Contract allowed cycle to be introduced.");
}
catch (CycleException ex)
{}
}
}

View File

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