Clean up code/fix compile errors
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/sandbox/graph2/trunk@144633 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
1 parent
0bab695631
commit
a1fd257727
21 files changed
+4402
-4438
No files matched your search
@@ -1,68 +1,66 @@
|
||||
package org.apache.commons.graph;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Apache" and "Apache Software Foundation" and
|
||||
* "Commons" must not be used to endorse or promote products
|
||||
* derived from this software without prior written permission. For
|
||||
* written permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache",
|
||||
* "Commons", nor may "Apache" appear in their name, without
|
||||
* prior written permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* Description of the Interface
|
||||
*/
|
||||
public interface WeightedGraph extends Graph
|
||||
{
|
||||
/**
|
||||
* Gets the weight attribute of the WeightedGraph object
|
||||
*/
|
||||
public double getWeight(Edge e);
|
||||
}
|
||||
package org.apache.commons.graph;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Apache" and "Apache Software Foundation" and
|
||||
* "Commons" must not be used to endorse or promote products
|
||||
* derived from this software without prior written permission. For
|
||||
* written permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache",
|
||||
* "Commons", nor may "Apache" appear in their name, without
|
||||
* prior written permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Description of the Interface
|
||||
*/
|
||||
public interface WeightedGraph extends Graph
|
||||
{
|
||||
/**
|
||||
* Gets the weight attribute of the WeightedGraph object
|
||||
*/
|
||||
public double getWeight(Edge e);
|
||||
}
|
||||
@@ -6,9 +6,6 @@ import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
import org.apache.commons.graph.exception.*;
|
||||
import org.apache.commons.graph.domain.basic.*;
|
||||
|
||||
|
||||
public class DataFlowSolutions
|
||||
{
|
||||
|
||||
@@ -1,291 +1,291 @@
|
||||
package org.apache.commons.graph.algorithm.search;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Apache" and "Apache Software Foundation" and
|
||||
* "Commons" must not be used to endorse or promote products
|
||||
* derived from this software without prior written permission. For
|
||||
* written permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache",
|
||||
* "Commons", nor may "Apache" appear in their name, without
|
||||
* prior written permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* This is a Cost searching algorithm. It will basically follow edges/paths with
|
||||
* minimal cost first, and then go to the later costs.
|
||||
*/
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
|
||||
import org.apache.commons.collections.*;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class CostSearch
|
||||
{
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class ComparableEdge
|
||||
implements Edge, Comparable
|
||||
{
|
||||
private Edge e;
|
||||
private double cost;
|
||||
|
||||
/**
|
||||
* Constructor for the ComparableEdge object
|
||||
*
|
||||
* @param e
|
||||
* @param cost
|
||||
*/
|
||||
public ComparableEdge(Edge e, double cost)
|
||||
{
|
||||
this.e = e;
|
||||
this.cost = cost;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the edge attribute of the ComparableEdge object
|
||||
*/
|
||||
public Edge getEdge()
|
||||
{
|
||||
return e;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the cost attribute of the ComparableEdge object
|
||||
*/
|
||||
public double getCost()
|
||||
{
|
||||
return cost;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public int compareTo(Object o)
|
||||
{
|
||||
ComparableVertex cv = (ComparableVertex) o;
|
||||
if (cv.cost > cost)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (cv.cost == cost)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (cv.cost < cost)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class ComparableVertex
|
||||
implements Vertex, Comparable
|
||||
{
|
||||
private Vertex v;
|
||||
private double cost;
|
||||
|
||||
/**
|
||||
* Constructor for the ComparableVertex object
|
||||
*
|
||||
* @param v
|
||||
* @param cost
|
||||
*/
|
||||
public ComparableVertex(Vertex v, double cost)
|
||||
{
|
||||
this.v = v;
|
||||
this.cost = cost;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public int compareTo(Object o)
|
||||
{
|
||||
ComparableVertex cv = (ComparableVertex) o;
|
||||
if (cv.cost > cost)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (cv.cost == cost)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (cv.cost < cost)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the vertex attribute of the ComparableVertex object
|
||||
*/
|
||||
public Vertex getVertex()
|
||||
{
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
private Map colors = new HashMap();// VERTEX X COLOR
|
||||
private PriorityQueue tasks = null;
|
||||
|
||||
private String WHITE = "white";
|
||||
private String BLACK = "black";
|
||||
private String GRAY = "gray";
|
||||
|
||||
/**
|
||||
* Constructor for the CostSearch object
|
||||
*/
|
||||
public CostSearch()
|
||||
{
|
||||
tasks = new BinaryHeap(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the CostSearch object
|
||||
*
|
||||
* @param isMin
|
||||
*/
|
||||
public CostSearch(boolean isMin)
|
||||
{
|
||||
tasks = new BinaryHeap(isMin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void visitVertex(WeightedGraph graph,
|
||||
Vertex vertex,
|
||||
double cost,
|
||||
Visitor visitor)
|
||||
{
|
||||
colors.remove(vertex);
|
||||
colors.put(vertex, GRAY);
|
||||
|
||||
visitor.discoverVertex(vertex);
|
||||
Iterator edges = graph.getEdges(vertex).iterator();
|
||||
while (edges.hasNext())
|
||||
{
|
||||
Edge edge = (Edge) edges.next();
|
||||
|
||||
double edgeCost = graph.getWeight(edge);
|
||||
ComparableEdge wEdge =
|
||||
new ComparableEdge(edge, edgeCost + cost);
|
||||
tasks.insert(wEdge);
|
||||
}
|
||||
|
||||
visitor.finishVertex(vertex);
|
||||
colors.remove(vertex);
|
||||
colors.put(vertex, BLACK);
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void visitEdge(WeightedGraph graph,
|
||||
Edge e,
|
||||
double cost,
|
||||
Visitor visitor)
|
||||
{
|
||||
visitor.discoverEdge(e);
|
||||
|
||||
Iterator vertices = graph.getVertices(e).iterator();
|
||||
while (vertices.hasNext())
|
||||
{
|
||||
Vertex v = (Vertex) vertices.next();
|
||||
if (colors.get(v) == WHITE)
|
||||
{
|
||||
visitVertex(graph, v, cost, visitor);
|
||||
}
|
||||
}
|
||||
|
||||
visitor.finishEdge(e);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void visit(WeightedGraph graph,
|
||||
Vertex root,
|
||||
Visitor visitor)
|
||||
{
|
||||
Iterator vertices = graph.getVertices().iterator();
|
||||
while (vertices.hasNext())
|
||||
{
|
||||
colors.put(vertices.next(), WHITE);
|
||||
}
|
||||
|
||||
visitor.discoverGraph(graph);
|
||||
|
||||
visitVertex(graph, root, 0.0, visitor);
|
||||
|
||||
while (!tasks.isEmpty())
|
||||
{
|
||||
ComparableEdge wEdge = (ComparableEdge) tasks.pop();
|
||||
visitEdge(graph, wEdge.getEdge(), wEdge.getCost(), visitor);
|
||||
}
|
||||
|
||||
visitor.finishGraph(graph);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
package org.apache.commons.graph.algorithm.search;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Apache" and "Apache Software Foundation" and
|
||||
* "Commons" must not be used to endorse or promote products
|
||||
* derived from this software without prior written permission. For
|
||||
* written permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache",
|
||||
* "Commons", nor may "Apache" appear in their name, without
|
||||
* prior written permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* This is a Cost searching algorithm. It will basically follow edges/paths with
|
||||
* minimal cost first, and then go to the later costs.
|
||||
*/
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.collections.BinaryHeap;
|
||||
import org.apache.commons.collections.PriorityQueue;
|
||||
import org.apache.commons.graph.Edge;
|
||||
import org.apache.commons.graph.Vertex;
|
||||
import org.apache.commons.graph.WeightedGraph;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class CostSearch
|
||||
{
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class ComparableEdge
|
||||
implements Edge, Comparable
|
||||
{
|
||||
private Edge e;
|
||||
private double cost;
|
||||
|
||||
/**
|
||||
* Constructor for the ComparableEdge object
|
||||
*
|
||||
* @param e
|
||||
* @param cost
|
||||
*/
|
||||
public ComparableEdge(Edge e, double cost)
|
||||
{
|
||||
this.e = e;
|
||||
this.cost = cost;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the edge attribute of the ComparableEdge object
|
||||
*/
|
||||
public Edge getEdge()
|
||||
{
|
||||
return e;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the cost attribute of the ComparableEdge object
|
||||
*/
|
||||
public double getCost()
|
||||
{
|
||||
return cost;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public int compareTo(Object o)
|
||||
{
|
||||
ComparableVertex cv = (ComparableVertex) o;
|
||||
if (cv.cost > cost)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (cv.cost == cost)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (cv.cost < cost)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class ComparableVertex
|
||||
implements Vertex, Comparable
|
||||
{
|
||||
private Vertex v;
|
||||
private double cost;
|
||||
|
||||
/**
|
||||
* Constructor for the ComparableVertex object
|
||||
*
|
||||
* @param v
|
||||
* @param cost
|
||||
*/
|
||||
public ComparableVertex(Vertex v, double cost)
|
||||
{
|
||||
this.v = v;
|
||||
this.cost = cost;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public int compareTo(Object o)
|
||||
{
|
||||
ComparableVertex cv = (ComparableVertex) o;
|
||||
if (cv.cost > cost)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (cv.cost == cost)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (cv.cost < cost)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the vertex attribute of the ComparableVertex object
|
||||
*/
|
||||
public Vertex getVertex()
|
||||
{
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
private Map colors = new HashMap();// VERTEX X COLOR
|
||||
private PriorityQueue tasks = null;
|
||||
|
||||
private String WHITE = "white";
|
||||
private String BLACK = "black";
|
||||
private String GRAY = "gray";
|
||||
|
||||
/**
|
||||
* Constructor for the CostSearch object
|
||||
*/
|
||||
public CostSearch()
|
||||
{
|
||||
tasks = new BinaryHeap(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the CostSearch object
|
||||
*
|
||||
* @param isMin
|
||||
*/
|
||||
public CostSearch(boolean isMin)
|
||||
{
|
||||
tasks = new BinaryHeap(isMin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void visitVertex(WeightedGraph graph,
|
||||
Vertex vertex,
|
||||
double cost,
|
||||
Visitor visitor)
|
||||
{
|
||||
colors.remove(vertex);
|
||||
colors.put(vertex, GRAY);
|
||||
|
||||
visitor.discoverVertex(vertex);
|
||||
Iterator edges = graph.getEdges(vertex).iterator();
|
||||
while (edges.hasNext())
|
||||
{
|
||||
Edge edge = (Edge) edges.next();
|
||||
|
||||
double edgeCost = graph.getWeight(edge);
|
||||
ComparableEdge wEdge =
|
||||
new ComparableEdge(edge, edgeCost + cost);
|
||||
tasks.insert(wEdge);
|
||||
}
|
||||
|
||||
visitor.finishVertex(vertex);
|
||||
colors.remove(vertex);
|
||||
colors.put(vertex, BLACK);
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void visitEdge(WeightedGraph graph,
|
||||
Edge e,
|
||||
double cost,
|
||||
Visitor visitor)
|
||||
{
|
||||
visitor.discoverEdge(e);
|
||||
|
||||
Iterator vertices = graph.getVertices(e).iterator();
|
||||
while (vertices.hasNext())
|
||||
{
|
||||
Vertex v = (Vertex) vertices.next();
|
||||
if (colors.get(v) == WHITE)
|
||||
{
|
||||
visitVertex(graph, v, cost, visitor);
|
||||
}
|
||||
}
|
||||
|
||||
visitor.finishEdge(e);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void visit(WeightedGraph graph,
|
||||
Vertex root,
|
||||
Visitor visitor)
|
||||
{
|
||||
Iterator vertices = graph.getVertices().iterator();
|
||||
while (vertices.hasNext())
|
||||
{
|
||||
colors.put(vertices.next(), WHITE);
|
||||
}
|
||||
|
||||
visitor.discoverGraph(graph);
|
||||
|
||||
visitVertex(graph, root, 0.0, visitor);
|
||||
|
||||
while (!tasks.isEmpty())
|
||||
{
|
||||
ComparableEdge wEdge = (ComparableEdge) tasks.pop();
|
||||
visitEdge(graph, wEdge.getEdge(), wEdge.getCost(), visitor);
|
||||
}
|
||||
|
||||
visitor.finishGraph(graph);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,190 +1,188 @@
|
||||
package org.apache.commons.graph.algorithm.search;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Apache" and "Apache Software Foundation" and
|
||||
* "Commons" must not be used to endorse or promote products
|
||||
* derived from this software without prior written permission. For
|
||||
* written permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache",
|
||||
* "Commons", nor may "Apache" appear in their name, without
|
||||
* prior written permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* This class does a Depth First Search. It visits all of the children nodes
|
||||
* before moving to the siblling nodes.
|
||||
*/
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.Map;
|
||||
import java.util.HashSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class DFS
|
||||
{
|
||||
private Map colors = new HashMap();// VERTEX X COLOR
|
||||
/**
|
||||
* Description of the Field
|
||||
*/
|
||||
public final static String WHITE = "white";
|
||||
/**
|
||||
* Description of the Field
|
||||
*/
|
||||
public final static String BLACK = "black";
|
||||
/**
|
||||
* Description of the Field
|
||||
*/
|
||||
public final static String GRAY = "gray";
|
||||
|
||||
/**
|
||||
* Constructor for the DFS object
|
||||
*/
|
||||
public DFS() { }
|
||||
|
||||
/**
|
||||
* Gets the color attribute of the DFS object
|
||||
*/
|
||||
public String getColor(Vertex v)
|
||||
{
|
||||
return (String) colors.get(v);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
private void visitEdge(DirectedGraph graph,
|
||||
Edge e,
|
||||
Visitor visitor)
|
||||
{
|
||||
visitor.discoverEdge(e);
|
||||
|
||||
Vertex v = graph.getTarget(e);
|
||||
|
||||
if (colors.get(v) == WHITE)
|
||||
{
|
||||
visitVertex(graph, v, visitor);
|
||||
}
|
||||
|
||||
visitor.finishEdge(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
private void visitVertex(DirectedGraph graph,
|
||||
Vertex v,
|
||||
Visitor visitor)
|
||||
{
|
||||
colors.remove(v);
|
||||
colors.put(v, GRAY);
|
||||
|
||||
visitor.discoverVertex(v);
|
||||
|
||||
Iterator edges = graph.getOutbound(v).iterator();
|
||||
while (edges.hasNext())
|
||||
{
|
||||
Edge e = (Edge) edges.next();
|
||||
visitEdge(graph, e, visitor);
|
||||
}
|
||||
|
||||
visitor.finishVertex(v);
|
||||
|
||||
colors.remove(v);
|
||||
colors.put(v, BLACK);
|
||||
}
|
||||
|
||||
/**
|
||||
* visit - Visits the graph
|
||||
*/
|
||||
public void visit(DirectedGraph graph,
|
||||
Vertex root,
|
||||
Visitor visitor)
|
||||
{
|
||||
Iterator vertices = graph.getVertices().iterator();
|
||||
while (vertices.hasNext())
|
||||
{
|
||||
colors.put(vertices.next(), WHITE);
|
||||
}
|
||||
|
||||
visitor.discoverGraph(graph);
|
||||
|
||||
visitVertex(graph, root, visitor);
|
||||
|
||||
visitor.finishGraph(graph);
|
||||
}
|
||||
|
||||
/**
|
||||
* visit - Visits all nodes in the graph.
|
||||
*/
|
||||
public void visit( DirectedGraph graph, Visitor visitor ) {
|
||||
Iterator vertices = graph.getVertices().iterator();
|
||||
while (vertices.hasNext()) {
|
||||
colors.put( vertices.next(), WHITE );
|
||||
}
|
||||
|
||||
visitor.discoverGraph( graph );
|
||||
|
||||
vertices = graph.getVertices().iterator();
|
||||
while (vertices.hasNext()) {
|
||||
Vertex v = (Vertex) vertices.next();
|
||||
|
||||
if (colors.get( v ) == WHITE) {
|
||||
visitVertex( graph, v, visitor );
|
||||
}
|
||||
}
|
||||
|
||||
visitor.finishGraph( graph );
|
||||
}
|
||||
}
|
||||
|
||||
package org.apache.commons.graph.algorithm.search;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Apache" and "Apache Software Foundation" and
|
||||
* "Commons" must not be used to endorse or promote products
|
||||
* derived from this software without prior written permission. For
|
||||
* written permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache",
|
||||
* "Commons", nor may "Apache" appear in their name, without
|
||||
* prior written permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* This class does a Depth First Search. It visits all of the children nodes
|
||||
* before moving to the siblling nodes.
|
||||
*/
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class DFS
|
||||
{
|
||||
private Map colors = new HashMap();// VERTEX X COLOR
|
||||
/**
|
||||
* Description of the Field
|
||||
*/
|
||||
public final static String WHITE = "white";
|
||||
/**
|
||||
* Description of the Field
|
||||
*/
|
||||
public final static String BLACK = "black";
|
||||
/**
|
||||
* Description of the Field
|
||||
*/
|
||||
public final static String GRAY = "gray";
|
||||
|
||||
/**
|
||||
* Constructor for the DFS object
|
||||
*/
|
||||
public DFS() { }
|
||||
|
||||
/**
|
||||
* Gets the color attribute of the DFS object
|
||||
*/
|
||||
public String getColor(Vertex v)
|
||||
{
|
||||
return (String) colors.get(v);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
private void visitEdge(DirectedGraph graph,
|
||||
Edge e,
|
||||
Visitor visitor)
|
||||
{
|
||||
visitor.discoverEdge(e);
|
||||
|
||||
Vertex v = graph.getTarget(e);
|
||||
|
||||
if (colors.get(v) == WHITE)
|
||||
{
|
||||
visitVertex(graph, v, visitor);
|
||||
}
|
||||
|
||||
visitor.finishEdge(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
private void visitVertex(DirectedGraph graph,
|
||||
Vertex v,
|
||||
Visitor visitor)
|
||||
{
|
||||
colors.remove(v);
|
||||
colors.put(v, GRAY);
|
||||
|
||||
visitor.discoverVertex(v);
|
||||
|
||||
Iterator edges = graph.getOutbound(v).iterator();
|
||||
while (edges.hasNext())
|
||||
{
|
||||
Edge e = (Edge) edges.next();
|
||||
visitEdge(graph, e, visitor);
|
||||
}
|
||||
|
||||
visitor.finishVertex(v);
|
||||
|
||||
colors.remove(v);
|
||||
colors.put(v, BLACK);
|
||||
}
|
||||
|
||||
/**
|
||||
* visit - Visits the graph
|
||||
*/
|
||||
public void visit(DirectedGraph graph,
|
||||
Vertex root,
|
||||
Visitor visitor)
|
||||
{
|
||||
Iterator vertices = graph.getVertices().iterator();
|
||||
while (vertices.hasNext())
|
||||
{
|
||||
colors.put(vertices.next(), WHITE);
|
||||
}
|
||||
|
||||
visitor.discoverGraph(graph);
|
||||
|
||||
visitVertex(graph, root, visitor);
|
||||
|
||||
visitor.finishGraph(graph);
|
||||
}
|
||||
|
||||
/**
|
||||
* visit - Visits all nodes in the graph.
|
||||
*/
|
||||
public void visit( DirectedGraph graph, Visitor visitor ) {
|
||||
Iterator vertices = graph.getVertices().iterator();
|
||||
while (vertices.hasNext()) {
|
||||
colors.put( vertices.next(), WHITE );
|
||||
}
|
||||
|
||||
visitor.discoverGraph( graph );
|
||||
|
||||
vertices = graph.getVertices().iterator();
|
||||
while (vertices.hasNext()) {
|
||||
Vertex v = (Vertex) vertices.next();
|
||||
|
||||
if (colors.get( v ) == WHITE) {
|
||||
visitVertex( graph, v, visitor );
|
||||
}
|
||||
}
|
||||
|
||||
visitor.finishGraph( graph );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,242 +1,246 @@
|
||||
package org.apache.commons.graph.decorator;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Apache" and "Apache Software Foundation" and
|
||||
* "Commons" must not be used to endorse or promote products
|
||||
* derived from this software without prior written permission. For
|
||||
* written permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache",
|
||||
* "Commons", nor may "Apache" appear in their name, without
|
||||
* prior written permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
import java.util.Set;
|
||||
import java.util.Map;
|
||||
import java.util.HashSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
import org.apache.commons.graph.exception.*;
|
||||
import org.apache.commons.graph.domain.basic.*;
|
||||
import org.apache.commons.graph.algorithm.path.*;
|
||||
import org.apache.commons.graph.algorithm.spanning.*;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class DDirectedGraph
|
||||
extends DirectedGraphWrapper
|
||||
implements DirectedGraph,
|
||||
WeightedGraph
|
||||
{
|
||||
private WeightedGraph weighted;
|
||||
private Map weights = new HashMap();// EDGE X DOUBLE
|
||||
private static Map decoratedGraphs = new HashMap();// DGRAPH X DDGRAPH
|
||||
private AllPairsShortestPath allPaths = null;
|
||||
|
||||
protected DDirectedGraph() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the DDirectedGraph object
|
||||
*
|
||||
* @param impl
|
||||
*/
|
||||
protected DDirectedGraph(DirectedGraph impl)
|
||||
{
|
||||
super(impl);
|
||||
|
||||
if (impl instanceof WeightedGraph)
|
||||
{
|
||||
weighted = (WeightedGraph) impl;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public static DDirectedGraph decorateGraph(DirectedGraph graph)
|
||||
{
|
||||
if (graph instanceof DDirectedGraph)
|
||||
{
|
||||
return (DDirectedGraph) graph;
|
||||
}
|
||||
|
||||
if (decoratedGraphs.containsKey(graph))
|
||||
{
|
||||
return (DDirectedGraph) decoratedGraphs.get(graph);
|
||||
}
|
||||
|
||||
DDirectedGraph RC = new DDirectedGraph(graph);
|
||||
decoratedGraphs.put(graph, RC);
|
||||
return RC;
|
||||
}
|
||||
|
||||
// WeightedGraph Implementation
|
||||
/**
|
||||
* Gets the weight attribute of the DDirectedGraph object
|
||||
*/
|
||||
public double getWeight(Edge e)
|
||||
{
|
||||
if (weighted != null)
|
||||
{
|
||||
return weighted.getWeight(e);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (weights.containsKey(e))
|
||||
{
|
||||
return ((Double) weights.get(e)).doubleValue();
|
||||
}
|
||||
else
|
||||
{
|
||||
return 1.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the weight attribute of the DDirectedGraph object
|
||||
*/
|
||||
public void setWeight(Edge e, double value)
|
||||
throws GraphException
|
||||
{
|
||||
if (weighted != null)
|
||||
{
|
||||
throw new GraphException("Unable to set weight.");
|
||||
}
|
||||
|
||||
weights.put(e, new Double(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public DirectedGraph transpose()
|
||||
throws GraphException
|
||||
{
|
||||
try
|
||||
{
|
||||
DirectedGraphImpl RC = new DirectedGraphImpl();
|
||||
Set vertexSet = getVertices();
|
||||
Set edgeSet = getEdges();
|
||||
|
||||
Iterator vertices = vertexSet.iterator();
|
||||
while (vertices.hasNext())
|
||||
{
|
||||
RC.addVertex((Vertex) vertices.next());
|
||||
}
|
||||
|
||||
Iterator edges = edgeSet.iterator();
|
||||
while (edges.hasNext())
|
||||
{
|
||||
Edge edge = (Edge) edges.next();
|
||||
|
||||
RC.addEdge(edge,
|
||||
getTarget(edge),
|
||||
getSource(edge));
|
||||
}
|
||||
|
||||
return RC;
|
||||
}
|
||||
catch (GraphException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new GraphException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public boolean hasConnection(Vertex start, Vertex end)
|
||||
throws GraphException
|
||||
{
|
||||
if (start == end)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (allPaths == null)
|
||||
{
|
||||
allPaths = new AllPairsShortestPath(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
allPaths.update(this);
|
||||
}
|
||||
|
||||
WeightedPath path =
|
||||
allPaths.getShortestPath(start, end);
|
||||
}
|
||||
catch (GraphException ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public MinimumSpanningForest minimumSpanningForest() {
|
||||
return new MinimumSpanningForest( this );
|
||||
}
|
||||
|
||||
public MinimumSpanningForest maximumSpanningForest() {
|
||||
return new MinimumSpanningForest( false, this );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
package org.apache.commons.graph.decorator;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Apache" and "Apache Software Foundation" and
|
||||
* "Commons" must not be used to endorse or promote products
|
||||
* derived from this software without prior written permission. For
|
||||
* written permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache",
|
||||
* "Commons", nor may "Apache" appear in their name, without
|
||||
* prior written permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.graph.DirectedGraph;
|
||||
import org.apache.commons.graph.Edge;
|
||||
import org.apache.commons.graph.Vertex;
|
||||
import org.apache.commons.graph.WeightedGraph;
|
||||
import org.apache.commons.graph.WeightedPath;
|
||||
import org.apache.commons.graph.algorithm.path.AllPairsShortestPath;
|
||||
import org.apache.commons.graph.algorithm.spanning.MinimumSpanningForest;
|
||||
import org.apache.commons.graph.domain.basic.DirectedGraphImpl;
|
||||
import org.apache.commons.graph.domain.basic.DirectedGraphWrapper;
|
||||
import org.apache.commons.graph.exception.GraphException;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class DDirectedGraph
|
||||
extends DirectedGraphWrapper
|
||||
implements DirectedGraph,
|
||||
WeightedGraph
|
||||
{
|
||||
private WeightedGraph weighted;
|
||||
private Map weights = new HashMap();// EDGE X DOUBLE
|
||||
private static Map decoratedGraphs = new HashMap();// DGRAPH X DDGRAPH
|
||||
private AllPairsShortestPath allPaths = null;
|
||||
|
||||
protected DDirectedGraph() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the DDirectedGraph object
|
||||
*
|
||||
* @param impl
|
||||
*/
|
||||
protected DDirectedGraph(DirectedGraph impl)
|
||||
{
|
||||
super(impl);
|
||||
|
||||
if (impl instanceof WeightedGraph)
|
||||
{
|
||||
weighted = (WeightedGraph) impl;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public static DDirectedGraph decorateGraph(DirectedGraph graph)
|
||||
{
|
||||
if (graph instanceof DDirectedGraph)
|
||||
{
|
||||
return (DDirectedGraph) graph;
|
||||
}
|
||||
|
||||
if (decoratedGraphs.containsKey(graph))
|
||||
{
|
||||
return (DDirectedGraph) decoratedGraphs.get(graph);
|
||||
}
|
||||
|
||||
DDirectedGraph RC = new DDirectedGraph(graph);
|
||||
decoratedGraphs.put(graph, RC);
|
||||
return RC;
|
||||
}
|
||||
|
||||
// WeightedGraph Implementation
|
||||
/**
|
||||
* Gets the weight attribute of the DDirectedGraph object
|
||||
*/
|
||||
public double getWeight(Edge e)
|
||||
{
|
||||
if (weighted != null)
|
||||
{
|
||||
return weighted.getWeight(e);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (weights.containsKey(e))
|
||||
{
|
||||
return ((Double) weights.get(e)).doubleValue();
|
||||
}
|
||||
else
|
||||
{
|
||||
return 1.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the weight attribute of the DDirectedGraph object
|
||||
*/
|
||||
public void setWeight(Edge e, double value)
|
||||
throws GraphException
|
||||
{
|
||||
if (weighted != null)
|
||||
{
|
||||
throw new GraphException("Unable to set weight.");
|
||||
}
|
||||
|
||||
weights.put(e, new Double(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public DirectedGraph transpose()
|
||||
throws GraphException
|
||||
{
|
||||
try
|
||||
{
|
||||
DirectedGraphImpl RC = new DirectedGraphImpl();
|
||||
Set vertexSet = getVertices();
|
||||
Set edgeSet = getEdges();
|
||||
|
||||
Iterator vertices = vertexSet.iterator();
|
||||
while (vertices.hasNext())
|
||||
{
|
||||
RC.addVertex((Vertex) vertices.next());
|
||||
}
|
||||
|
||||
Iterator edges = edgeSet.iterator();
|
||||
while (edges.hasNext())
|
||||
{
|
||||
Edge edge = (Edge) edges.next();
|
||||
|
||||
RC.addEdge(edge,
|
||||
getTarget(edge),
|
||||
getSource(edge));
|
||||
}
|
||||
|
||||
return RC;
|
||||
}
|
||||
catch (GraphException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new GraphException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public boolean hasConnection(Vertex start, Vertex end)
|
||||
throws GraphException
|
||||
{
|
||||
if (start == end)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (allPaths == null)
|
||||
{
|
||||
allPaths = new AllPairsShortestPath(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
allPaths.update(this);
|
||||
}
|
||||
|
||||
WeightedPath path =
|
||||
allPaths.getShortestPath(start, end);
|
||||
}
|
||||
catch (GraphException ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public MinimumSpanningForest minimumSpanningForest() {
|
||||
return new MinimumSpanningForest( this );
|
||||
}
|
||||
|
||||
public MinimumSpanningForest maximumSpanningForest() {
|
||||
return new MinimumSpanningForest( false, this );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -3,19 +3,15 @@ package org.apache.commons.graph.domain.dependency;
|
||||
import org.apache.commons.graph.*;
|
||||
import org.apache.commons.graph.contract.*;
|
||||
import org.apache.commons.graph.exception.*;
|
||||
import org.apache.commons.graph.domain.basic.*;
|
||||
import org.apache.commons.graph.factory.GraphFactory;
|
||||
import org.apache.commons.graph.decorator.DDirectedGraph;
|
||||
import org.apache.commons.graph.dependency.exception.*;
|
||||
import org.apache.commons.graph.domain.dependency.exception.CircularDependencyException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
|
||||
+38
-38
@@ -1,38 +1,38 @@
|
||||
package org.apache.commons.graph.dependency.exception;
|
||||
|
||||
import org.apache.commons.graph.exception.*;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class CircularDependencyException
|
||||
extends CycleException
|
||||
{
|
||||
/**
|
||||
* Constructor for the CircularDependencyException object
|
||||
*/
|
||||
public CircularDependencyException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the CircularDependencyException object
|
||||
*
|
||||
* @param msg
|
||||
*/
|
||||
public CircularDependencyException(String msg)
|
||||
{
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the CircularDependencyException object
|
||||
*
|
||||
* @param cause
|
||||
*/
|
||||
public CircularDependencyException(Throwable cause)
|
||||
{
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
package org.apache.commons.graph.domain.dependency.exception;
|
||||
|
||||
import org.apache.commons.graph.exception.*;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class CircularDependencyException
|
||||
extends CycleException
|
||||
{
|
||||
/**
|
||||
* Constructor for the CircularDependencyException object
|
||||
*/
|
||||
public CircularDependencyException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the CircularDependencyException object
|
||||
*
|
||||
* @param msg
|
||||
*/
|
||||
public CircularDependencyException(String msg)
|
||||
{
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the CircularDependencyException object
|
||||
*
|
||||
* @param cause
|
||||
*/
|
||||
public CircularDependencyException(Throwable cause)
|
||||
{
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,15 @@
|
||||
package org.apache.commons.graph.domain.statemachine;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
import org.apache.commons.graph.exception.*;
|
||||
import org.apache.commons.graph.domain.basic.*;
|
||||
import org.apache.commons.graph.MutableDirectedGraph;
|
||||
import org.apache.commons.graph.contract.Contract;
|
||||
import org.apache.commons.graph.factory.GraphFactory;
|
||||
import org.apache.commons.graph.decorator.DDirectedGraph;
|
||||
import org.apache.commons.graph.domain.statemachine.exception.*;
|
||||
import org.apache.commons.graph.exception.GraphException;
|
||||
import org.apache.commons.graph.factory.GraphFactory;
|
||||
|
||||
/**
|
||||
* StateMachine -
|
||||
|
||||
+38
-38
@@ -1,38 +1,38 @@
|
||||
package org.apache.commons.graph.statemachine.exception;
|
||||
|
||||
import org.apache.commons.graph.exception.*;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class StateMachineException
|
||||
extends GraphException
|
||||
{
|
||||
/**
|
||||
* Constructor for the StateMachineException object
|
||||
*/
|
||||
public StateMachineException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the StateMachineException object
|
||||
*
|
||||
* @param msg
|
||||
*/
|
||||
public StateMachineException(String msg)
|
||||
{
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the StateMachineException object
|
||||
*
|
||||
* @param t
|
||||
*/
|
||||
public StateMachineException(Throwable t)
|
||||
{
|
||||
super(t);
|
||||
}
|
||||
}
|
||||
package org.apache.commons.graph.domain.statemachine.exception;
|
||||
|
||||
import org.apache.commons.graph.exception.GraphException;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class StateMachineException
|
||||
extends GraphException
|
||||
{
|
||||
/**
|
||||
* Constructor for the StateMachineException object
|
||||
*/
|
||||
public StateMachineException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the StateMachineException object
|
||||
*
|
||||
* @param msg
|
||||
*/
|
||||
public StateMachineException(String msg)
|
||||
{
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the StateMachineException object
|
||||
*
|
||||
* @param t
|
||||
*/
|
||||
public StateMachineException(Throwable t)
|
||||
{
|
||||
super(t);
|
||||
}
|
||||
}
|
||||
@@ -1,96 +1,92 @@
|
||||
package org.apache.commons.graph.domain.uml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
import java.util.Enumeration;
|
||||
|
||||
import java.util.zip.*;
|
||||
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import org.apache.commons.graph.domain.uml.exception.*;
|
||||
|
||||
import org.apache.log4j.Category;
|
||||
|
||||
import ru.novosoft.uml.xmi.XMIReader;
|
||||
import ru.novosoft.uml.model_management.MModel;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class ModelFactory
|
||||
{
|
||||
private XMIReader xmiReader = null;
|
||||
private final static String xmiVersion = "1.1";
|
||||
private static Category log =
|
||||
Category.getInstance(org.apache.commons.graph.domain.uml.ModelFactory.class);
|
||||
|
||||
/**
|
||||
* Constructor for the ModelFactory object
|
||||
*
|
||||
* @exception Exception
|
||||
*/
|
||||
public ModelFactory()
|
||||
throws Exception
|
||||
{
|
||||
log.debug("ModelFactory.__init__()");
|
||||
try
|
||||
{
|
||||
xmiReader = new XMIReader();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error(e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the fromStream attribute of the ModelFactory object
|
||||
*/
|
||||
public MModel getFromStream(InputStream stream)
|
||||
{
|
||||
log.debug("getFromStream");
|
||||
return xmiReader.parse(new InputSource(stream));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the fromXMI attribute of the ModelFactory object
|
||||
*/
|
||||
public MModel getFromXMI(File xmiFile)
|
||||
throws IOException
|
||||
{
|
||||
log.debug("getFromXMI(" + xmiFile.getName() + ")");
|
||||
return getFromStream(new FileInputStream(xmiFile));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the fromZargo attribute of the ModelFactory object
|
||||
*/
|
||||
public MModel getFromZargo(File zargoFile)
|
||||
throws IOException
|
||||
{
|
||||
log.debug("getFromZargo(" + zargoFile.getName() + ")");
|
||||
MModel RC = null;
|
||||
|
||||
ZipFile zargoZip = new ZipFile(zargoFile);
|
||||
|
||||
Enumeration entries = zargoZip.entries();
|
||||
while (entries.hasMoreElements())
|
||||
{
|
||||
ZipEntry entry = (ZipEntry) entries.nextElement();
|
||||
|
||||
if (entry.getName().endsWith(".xmi"))
|
||||
{
|
||||
log.debug("Zargo Entry: " + entry.getName());
|
||||
return getFromStream(zargoZip.getInputStream(entry));
|
||||
}
|
||||
}
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
|
||||
}
|
||||
package org.apache.commons.graph.domain.uml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Enumeration;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
import org.apache.log4j.Category;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import ru.novosoft.uml.model_management.MModel;
|
||||
import ru.novosoft.uml.xmi.XMIReader;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class ModelFactory
|
||||
{
|
||||
private XMIReader xmiReader = null;
|
||||
private final static String xmiVersion = "1.1";
|
||||
private static Category log =
|
||||
Category.getInstance(org.apache.commons.graph.domain.uml.ModelFactory.class);
|
||||
|
||||
/**
|
||||
* Constructor for the ModelFactory object
|
||||
*
|
||||
* @exception Exception
|
||||
*/
|
||||
public ModelFactory()
|
||||
throws Exception
|
||||
{
|
||||
log.debug("ModelFactory.__init__()");
|
||||
try
|
||||
{
|
||||
xmiReader = new XMIReader();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error(e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the fromStream attribute of the ModelFactory object
|
||||
*/
|
||||
public MModel getFromStream(InputStream stream)
|
||||
{
|
||||
log.debug("getFromStream");
|
||||
return xmiReader.parse(new InputSource(stream));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the fromXMI attribute of the ModelFactory object
|
||||
*/
|
||||
public MModel getFromXMI(File xmiFile)
|
||||
throws IOException
|
||||
{
|
||||
log.debug("getFromXMI(" + xmiFile.getName() + ")");
|
||||
return getFromStream(new FileInputStream(xmiFile));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the fromZargo attribute of the ModelFactory object
|
||||
*/
|
||||
public MModel getFromZargo(File zargoFile)
|
||||
throws IOException
|
||||
{
|
||||
log.debug("getFromZargo(" + zargoFile.getName() + ")");
|
||||
MModel RC = null;
|
||||
|
||||
ZipFile zargoZip = new ZipFile(zargoFile);
|
||||
|
||||
Enumeration entries = zargoZip.entries();
|
||||
while (entries.hasMoreElements())
|
||||
{
|
||||
ZipEntry entry = (ZipEntry) entries.nextElement();
|
||||
|
||||
if (entry.getName().endsWith(".xmi"))
|
||||
{
|
||||
log.debug("Zargo Entry: " + entry.getName());
|
||||
return getFromStream(zargoZip.getInputStream(entry));
|
||||
}
|
||||
}
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,232 +1,237 @@
|
||||
package org.apache.commons.graph.domain.uml;
|
||||
|
||||
/**
|
||||
* StateMachineFactory This class will build a State Machine from an NSUML Model
|
||||
* which can be used for testing.
|
||||
*/
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.Map;
|
||||
import java.util.HashSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.apache.commons.graph.domain.uml.exception.*;
|
||||
|
||||
import org.apache.log4j.Category;
|
||||
|
||||
import org.apache.commons.graph.exception.*;
|
||||
import org.apache.commons.graph.domain.statemachine.*;
|
||||
|
||||
import ru.novosoft.uml.foundation.data_types.*;
|
||||
import ru.novosoft.uml.model_management.MModel;
|
||||
import ru.novosoft.uml.foundation.core.*;
|
||||
import ru.novosoft.uml.behavior.state_machines.*;
|
||||
import ru.novosoft.uml.behavior.common_behavior.*;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class StateMachineFactory
|
||||
{
|
||||
private static Category log =
|
||||
Category.getInstance(org.apache.commons.graph.domain.uml.StateMachineFactory.class);
|
||||
|
||||
private MModel extent = null;
|
||||
|
||||
private Map stateNames = new HashMap();// MSTATEVERTEX X NAME
|
||||
|
||||
/**
|
||||
* Constructor for the StateMachineFactory object
|
||||
*
|
||||
* @param extent
|
||||
*/
|
||||
public StateMachineFactory(MModel extent)
|
||||
{
|
||||
this.extent = extent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name attribute of the StateMachineFactory object
|
||||
*/
|
||||
private String getName(String namespace, MStateVertex msv)
|
||||
{
|
||||
if (msv.getName() != null)
|
||||
{
|
||||
return namespace + "/" + msv.getName();
|
||||
}
|
||||
|
||||
if (msv instanceof MPseudostate)
|
||||
{
|
||||
return namespace + "/_" +
|
||||
((MPseudostate) msv).getKind().getName() + "_";
|
||||
}
|
||||
|
||||
if (msv instanceof MFinalState)
|
||||
{
|
||||
return namespace + "/_final_";
|
||||
}
|
||||
|
||||
return namespace + "/_unknown_";
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
private StateMachine makeStateMachine(String namespace,
|
||||
MCompositeState mcs)
|
||||
throws GraphException
|
||||
{
|
||||
log.debug("makeStateMachine(" + getName(namespace, mcs) + "): Entry");
|
||||
StateMachine RC = new StateMachine(namespace);
|
||||
|
||||
// Step 1 - Add States to the State Machine
|
||||
Iterator states =
|
||||
mcs.getSubvertices().iterator();
|
||||
|
||||
while (states.hasNext())
|
||||
{
|
||||
MStateVertex msv =
|
||||
(MStateVertex) states.next();
|
||||
|
||||
RC.addState(getName(namespace, msv));
|
||||
|
||||
stateNames.put(msv, getName(namespace, msv));
|
||||
|
||||
if (msv instanceof MPseudostate)
|
||||
{
|
||||
if (((MPseudostate) msv).getKind() ==
|
||||
MPseudostateKind.INITIAL)
|
||||
{
|
||||
RC.setStartState(RC.getState(getName(namespace, msv)));
|
||||
}
|
||||
}
|
||||
|
||||
if (msv instanceof MCompositeState)
|
||||
{
|
||||
StateMachine ssm = makeStateMachine(getName(namespace, msv),
|
||||
(MCompositeState) msv);
|
||||
RC.getState(getName(namespace, msv)).setSubmachine(ssm);
|
||||
}
|
||||
|
||||
if (msv instanceof MFinalState)
|
||||
{
|
||||
RC.addFinalState(RC.getState(getName(namespace, msv)));
|
||||
}
|
||||
}
|
||||
|
||||
// Step 2 - Add Transitions to State Machine
|
||||
states = mcs.getSubvertices().iterator();
|
||||
while (states.hasNext())
|
||||
{
|
||||
MStateVertex msv = (MStateVertex) states.next();
|
||||
String msvName = getName(namespace, msv);
|
||||
|
||||
Iterator transes =
|
||||
msv.getIncomings().iterator();
|
||||
while (transes.hasNext())
|
||||
{
|
||||
MTransition trans =
|
||||
(MTransition) transes.next();
|
||||
MEvent trigger = trans.getTrigger();
|
||||
MGuard guard = trans.getGuard();
|
||||
|
||||
String trigStr = null;
|
||||
String guardStr = null;
|
||||
|
||||
if (guard != null) {
|
||||
guardStr = guard.getName();
|
||||
}
|
||||
|
||||
if (trigger != null)
|
||||
{
|
||||
trigStr = trigger.getName();
|
||||
}
|
||||
else
|
||||
{
|
||||
trigStr = Transition.EPSILON;
|
||||
}
|
||||
|
||||
String sourceName =
|
||||
(String) stateNames.get(trans.getSource());
|
||||
String targetName =
|
||||
(String) stateNames.get(trans.getTarget());
|
||||
|
||||
State source = RC.getState(sourceName);
|
||||
State target = RC.getState(targetName);
|
||||
|
||||
String transName = source + "-" + target + "/" + trigStr;
|
||||
if (guardStr != null) {
|
||||
transName = transName + "[" + guardStr + "]";
|
||||
}
|
||||
|
||||
Transition tranx =
|
||||
new Transition(transName,
|
||||
source, target);
|
||||
tranx.setTrigger( trigStr );
|
||||
tranx.setGuard( guardStr );
|
||||
|
||||
RC.addTransition(tranx);
|
||||
}
|
||||
}
|
||||
|
||||
log.debug("makeStateMachine(" + getName(namespace, mcs) + "): Exit");
|
||||
return RC;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public StateMachine makeStateMachine(String selector)
|
||||
throws GraphException, ModelNotFoundException
|
||||
{
|
||||
log.debug("makeStateMachine(" + selector + "):Enter");
|
||||
MStateMachine model = null;
|
||||
StateMachine RC = null;
|
||||
|
||||
Iterator owned =
|
||||
extent.getOwnedElements().iterator();
|
||||
|
||||
while (owned.hasNext())
|
||||
{
|
||||
Object next = owned.next();
|
||||
if (next instanceof
|
||||
ru.novosoft.uml.foundation.core.MClass)
|
||||
{
|
||||
MClass mClass = (MClass) next;
|
||||
|
||||
if (selector.equals(mClass.getName()))
|
||||
{
|
||||
Iterator machines = mClass.getBehaviors().iterator();
|
||||
if (machines.hasNext())
|
||||
{
|
||||
model = (MStateMachine) machines.next();
|
||||
log.info("StateMachine Found: " + model);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (model == null)
|
||||
{
|
||||
throw new ModelNotFoundException("Cannot find StateMachine for " +
|
||||
selector);
|
||||
}
|
||||
|
||||
MState top = model.getTop();
|
||||
if (top instanceof MCompositeState)
|
||||
{
|
||||
RC = makeStateMachine(selector,
|
||||
(MCompositeState) top);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ModelNotFoundException("Expecting CompositeState at top.");
|
||||
}
|
||||
|
||||
log.debug("makeStateMachine(" + selector + "):Exit");
|
||||
return RC;
|
||||
}
|
||||
}
|
||||
|
||||
package org.apache.commons.graph.domain.uml;
|
||||
|
||||
/**
|
||||
* StateMachineFactory This class will build a State Machine from an NSUML Model
|
||||
* which can be used for testing.
|
||||
*/
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.graph.domain.statemachine.State;
|
||||
import org.apache.commons.graph.domain.statemachine.StateMachine;
|
||||
import org.apache.commons.graph.domain.statemachine.Transition;
|
||||
import org.apache.commons.graph.domain.uml.exception.ModelNotFoundException;
|
||||
import org.apache.commons.graph.exception.GraphException;
|
||||
import org.apache.log4j.Category;
|
||||
|
||||
import ru.novosoft.uml.behavior.state_machines.MCompositeState;
|
||||
import ru.novosoft.uml.behavior.state_machines.MEvent;
|
||||
import ru.novosoft.uml.behavior.state_machines.MFinalState;
|
||||
import ru.novosoft.uml.behavior.state_machines.MGuard;
|
||||
import ru.novosoft.uml.behavior.state_machines.MPseudostate;
|
||||
import ru.novosoft.uml.behavior.state_machines.MState;
|
||||
import ru.novosoft.uml.behavior.state_machines.MStateMachine;
|
||||
import ru.novosoft.uml.behavior.state_machines.MStateVertex;
|
||||
import ru.novosoft.uml.behavior.state_machines.MTransition;
|
||||
import ru.novosoft.uml.foundation.core.MClass;
|
||||
import ru.novosoft.uml.foundation.data_types.MPseudostateKind;
|
||||
import ru.novosoft.uml.model_management.MModel;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class StateMachineFactory
|
||||
{
|
||||
private static Category log =
|
||||
Category.getInstance(org.apache.commons.graph.domain.uml.StateMachineFactory.class);
|
||||
|
||||
private MModel extent = null;
|
||||
|
||||
private Map stateNames = new HashMap();// MSTATEVERTEX X NAME
|
||||
|
||||
/**
|
||||
* Constructor for the StateMachineFactory object
|
||||
*
|
||||
* @param extent
|
||||
*/
|
||||
public StateMachineFactory(MModel extent)
|
||||
{
|
||||
this.extent = extent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name attribute of the StateMachineFactory object
|
||||
*/
|
||||
private String getName(String namespace, MStateVertex msv)
|
||||
{
|
||||
if (msv.getName() != null)
|
||||
{
|
||||
return namespace + "/" + msv.getName();
|
||||
}
|
||||
|
||||
if (msv instanceof MPseudostate)
|
||||
{
|
||||
return namespace + "/_" +
|
||||
((MPseudostate) msv).getKind().getName() + "_";
|
||||
}
|
||||
|
||||
if (msv instanceof MFinalState)
|
||||
{
|
||||
return namespace + "/_final_";
|
||||
}
|
||||
|
||||
return namespace + "/_unknown_";
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
private StateMachine makeStateMachine(String namespace,
|
||||
MCompositeState mcs)
|
||||
throws GraphException
|
||||
{
|
||||
log.debug("makeStateMachine(" + getName(namespace, mcs) + "): Entry");
|
||||
StateMachine RC = new StateMachine(namespace);
|
||||
|
||||
// Step 1 - Add States to the State Machine
|
||||
Iterator states =
|
||||
mcs.getSubvertices().iterator();
|
||||
|
||||
while (states.hasNext())
|
||||
{
|
||||
MStateVertex msv =
|
||||
(MStateVertex) states.next();
|
||||
|
||||
RC.addState(getName(namespace, msv));
|
||||
|
||||
stateNames.put(msv, getName(namespace, msv));
|
||||
|
||||
if (msv instanceof MPseudostate)
|
||||
{
|
||||
if (((MPseudostate) msv).getKind() ==
|
||||
MPseudostateKind.INITIAL)
|
||||
{
|
||||
RC.setStartState(RC.getState(getName(namespace, msv)));
|
||||
}
|
||||
}
|
||||
|
||||
if (msv instanceof MCompositeState)
|
||||
{
|
||||
StateMachine ssm = makeStateMachine(getName(namespace, msv),
|
||||
(MCompositeState) msv);
|
||||
RC.getState(getName(namespace, msv)).setSubmachine(ssm);
|
||||
}
|
||||
|
||||
if (msv instanceof MFinalState)
|
||||
{
|
||||
RC.addFinalState(RC.getState(getName(namespace, msv)));
|
||||
}
|
||||
}
|
||||
|
||||
// Step 2 - Add Transitions to State Machine
|
||||
states = mcs.getSubvertices().iterator();
|
||||
while (states.hasNext())
|
||||
{
|
||||
MStateVertex msv = (MStateVertex) states.next();
|
||||
String msvName = getName(namespace, msv);
|
||||
|
||||
Iterator transes =
|
||||
msv.getIncomings().iterator();
|
||||
while (transes.hasNext())
|
||||
{
|
||||
MTransition trans =
|
||||
(MTransition) transes.next();
|
||||
MEvent trigger = trans.getTrigger();
|
||||
MGuard guard = trans.getGuard();
|
||||
|
||||
String trigStr = null;
|
||||
String guardStr = null;
|
||||
|
||||
if (guard != null) {
|
||||
guardStr = guard.getName();
|
||||
}
|
||||
|
||||
if (trigger != null)
|
||||
{
|
||||
trigStr = trigger.getName();
|
||||
}
|
||||
else
|
||||
{
|
||||
trigStr = Transition.EPSILON;
|
||||
}
|
||||
|
||||
String sourceName =
|
||||
(String) stateNames.get(trans.getSource());
|
||||
String targetName =
|
||||
(String) stateNames.get(trans.getTarget());
|
||||
|
||||
State source = RC.getState(sourceName);
|
||||
State target = RC.getState(targetName);
|
||||
|
||||
String transName = source + "-" + target + "/" + trigStr;
|
||||
if (guardStr != null) {
|
||||
transName = transName + "[" + guardStr + "]";
|
||||
}
|
||||
|
||||
Transition tranx =
|
||||
new Transition(transName,
|
||||
source, target);
|
||||
tranx.setTrigger( trigStr );
|
||||
tranx.setGuard( guardStr );
|
||||
|
||||
RC.addTransition(tranx);
|
||||
}
|
||||
}
|
||||
|
||||
log.debug("makeStateMachine(" + getName(namespace, mcs) + "): Exit");
|
||||
return RC;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public StateMachine makeStateMachine(String selector)
|
||||
throws GraphException, ModelNotFoundException
|
||||
{
|
||||
log.debug("makeStateMachine(" + selector + "):Enter");
|
||||
MStateMachine model = null;
|
||||
StateMachine RC = null;
|
||||
|
||||
Iterator owned =
|
||||
extent.getOwnedElements().iterator();
|
||||
|
||||
while (owned.hasNext())
|
||||
{
|
||||
Object next = owned.next();
|
||||
if (next instanceof
|
||||
ru.novosoft.uml.foundation.core.MClass)
|
||||
{
|
||||
MClass mClass = (MClass) next;
|
||||
|
||||
if (selector.equals(mClass.getName()))
|
||||
{
|
||||
Iterator machines = mClass.getBehaviors().iterator();
|
||||
if (machines.hasNext())
|
||||
{
|
||||
model = (MStateMachine) machines.next();
|
||||
log.info("StateMachine Found: " + model);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (model == null)
|
||||
{
|
||||
throw new ModelNotFoundException("Cannot find StateMachine for " +
|
||||
selector);
|
||||
}
|
||||
|
||||
MState top = model.getTop();
|
||||
if (top instanceof MCompositeState)
|
||||
{
|
||||
RC = makeStateMachine(selector,
|
||||
(MCompositeState) top);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ModelNotFoundException("Expecting CompositeState at top.");
|
||||
}
|
||||
|
||||
log.debug("makeStateMachine(" + selector + "):Exit");
|
||||
return RC;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,15 +59,15 @@ package org.apache.commons.graph.search;
|
||||
* minimal cost first, and then go to the later costs.
|
||||
*/
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
|
||||
import org.apache.commons.collections.*;
|
||||
import org.apache.commons.collections.BinaryHeap;
|
||||
import org.apache.commons.collections.PriorityQueue;
|
||||
import org.apache.commons.graph.Edge;
|
||||
import org.apache.commons.graph.Vertex;
|
||||
import org.apache.commons.graph.WeightedGraph;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
|
||||
@@ -59,9 +59,7 @@ package org.apache.commons.graph.search;
|
||||
* before moving to the siblling nodes.
|
||||
*/
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.Map;
|
||||
import java.util.HashSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
|
||||
|
||||
@@ -1,478 +1,474 @@
|
||||
package org.apache.commons.graph;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Apache" and "Apache Software Foundation" and
|
||||
* "Commons" must not be used to endorse or promote products
|
||||
* derived from this software without prior written permission. For
|
||||
* written permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache",
|
||||
* "Commons", nor may "Apache" appear in their name, without
|
||||
* prior written permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* DirGraphTest This test will ensure that we can represent a Directed Graph.
|
||||
*/
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class DirGraphTest
|
||||
extends GraphTest
|
||||
{
|
||||
private String testName = null;
|
||||
|
||||
/**
|
||||
* Constructor for the DirGraphTest object
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public DirGraphTest(String name)
|
||||
{
|
||||
super(name);
|
||||
this.testName = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testDirNull()
|
||||
throws Throwable
|
||||
{
|
||||
verifyGraph(makeDirNullGraph(), 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testSingleVertex()
|
||||
throws Throwable
|
||||
{
|
||||
verifyGraph(makeDirSingleVertex(), 1, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testDoubleVertex()
|
||||
throws Throwable
|
||||
{
|
||||
verifyGraph(makeDirDoubleVertex(), 2, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testSelfLoop()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph IUT = makeSelfLoop();
|
||||
try
|
||||
{
|
||||
|
||||
verifyGraph(IUT, 1, 1);
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1));
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1),
|
||||
makeSet(V1));
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
printGraph(t, IUT);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testDirectedEdge()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph IUT = makeDirectedEdge();
|
||||
try
|
||||
{
|
||||
|
||||
verifyGraph(IUT, 2, 1);
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1, V2));
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1, V2));
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(),
|
||||
makeSet(V2));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1),
|
||||
makeSet());
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
printGraph(t, IUT);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testDirParallelEdges()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph IUT = makeDirParallelEdges();
|
||||
try
|
||||
{
|
||||
|
||||
verifyGraph(IUT, 2, 2);
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1, V2));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1, V2));
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(),
|
||||
makeSet(V2));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1),
|
||||
makeSet());
|
||||
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
printGraph(t, IUT);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testTwoCycle()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph IUT = makeTwoCycle();
|
||||
try
|
||||
{
|
||||
|
||||
verifyGraph(IUT, 2, 2);
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1, V2));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1, V2));
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V2),
|
||||
makeSet(V2));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1),
|
||||
makeSet(V1));
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
printGraph(t, IUT);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testDirectedCycle()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph IUT = makeDirectedCycle();
|
||||
try
|
||||
{
|
||||
|
||||
verifyGraph(IUT, 3, 3);
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1, V2, V3));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1, V2, V3));
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V1, V2, V3));
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V3),
|
||||
makeSet(V2));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1),
|
||||
makeSet(V3));
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V2),
|
||||
makeSet(V1));
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
printGraph(t, IUT);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testPipe()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph IUT = makePipe();
|
||||
try
|
||||
{
|
||||
|
||||
verifyGraph(IUT, 3, 2);
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1, V2));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1, V2, V3));
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V2, V3));
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(),
|
||||
makeSet(V2));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1),
|
||||
makeSet(V3));
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V2),
|
||||
makeSet());
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
printGraph(t, IUT);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testDiamond()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph IUT = makeDiamond();
|
||||
try
|
||||
{
|
||||
|
||||
verifyGraph(IUT, 4, 4);
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1, V2, V3));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1, V2, V4));
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V1, V3, V4));
|
||||
verifyAdjVertices(IUT, V4,
|
||||
makeSet(V2, V3, V4));
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(),
|
||||
makeSet(V2, V3));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1),
|
||||
makeSet(V4));
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V1),
|
||||
makeSet(V4));
|
||||
verifyAdjVertices(IUT, V4,
|
||||
makeSet(V2, V3),
|
||||
makeSet());
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
printGraph(t, IUT);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testPipelessCycle()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph IUT = makePipelessCycle();
|
||||
try
|
||||
{
|
||||
|
||||
verifyGraph(IUT, 4, 4);
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1, V2, V3));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1, V2, V4));
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V1, V3, V4));
|
||||
verifyAdjVertices(IUT, V4,
|
||||
makeSet(V2, V3, V4));
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(),
|
||||
makeSet(V2, V3));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1, V4),
|
||||
makeSet());
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V1, V4),
|
||||
makeSet());
|
||||
verifyAdjVertices(IUT, V4,
|
||||
makeSet(),
|
||||
makeSet(V2, V3));
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
printGraph(t, IUT);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testParentTree()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph IUT = makeParentTree();
|
||||
try
|
||||
{
|
||||
|
||||
verifyGraph(IUT, 5, 4);
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1, V2, V3));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1, V2));
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V1, V3, V4, V5));
|
||||
verifyAdjVertices(IUT, V4,
|
||||
makeSet(V3, V4));
|
||||
verifyAdjVertices(IUT, V5,
|
||||
makeSet(V3, V5));
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V2, V3),
|
||||
makeSet());
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(),
|
||||
makeSet(V1));
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V4, V5),
|
||||
makeSet(V1));
|
||||
verifyAdjVertices(IUT, V4,
|
||||
makeSet(),
|
||||
makeSet(V3));
|
||||
verifyAdjVertices(IUT, V5,
|
||||
makeSet(),
|
||||
makeSet(V3));
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
printGraph(t, IUT);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testChildTree()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph IUT = makeChildTree();
|
||||
try
|
||||
{
|
||||
|
||||
verifyGraph(IUT, 5, 4);
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1, V2, V3));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1, V2));
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V1, V3, V4, V5));
|
||||
verifyAdjVertices(IUT, V4,
|
||||
makeSet(V3, V4));
|
||||
verifyAdjVertices(IUT, V5,
|
||||
makeSet(V3, V5));
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(),
|
||||
makeSet(V2, V3));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1),
|
||||
makeSet());
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V1),
|
||||
makeSet(V4, V5));
|
||||
verifyAdjVertices(IUT, V4,
|
||||
makeSet(V3),
|
||||
makeSet());
|
||||
verifyAdjVertices(IUT, V5,
|
||||
makeSet(V3),
|
||||
makeSet());
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
printGraph(t, IUT);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
package org.apache.commons.graph;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Apache" and "Apache Software Foundation" and
|
||||
* "Commons" must not be used to endorse or promote products
|
||||
* derived from this software without prior written permission. For
|
||||
* written permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache",
|
||||
* "Commons", nor may "Apache" appear in their name, without
|
||||
* prior written permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* DirGraphTest This test will ensure that we can represent a Directed Graph.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class DirGraphTest
|
||||
extends GraphTest
|
||||
{
|
||||
private String testName = null;
|
||||
|
||||
/**
|
||||
* Constructor for the DirGraphTest object
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public DirGraphTest(String name)
|
||||
{
|
||||
super(name);
|
||||
this.testName = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testDirNull()
|
||||
throws Throwable
|
||||
{
|
||||
verifyGraph(makeDirNullGraph(), 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testSingleVertex()
|
||||
throws Throwable
|
||||
{
|
||||
verifyGraph(makeDirSingleVertex(), 1, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testDoubleVertex()
|
||||
throws Throwable
|
||||
{
|
||||
verifyGraph(makeDirDoubleVertex(), 2, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testSelfLoop()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph IUT = makeSelfLoop();
|
||||
try
|
||||
{
|
||||
|
||||
verifyGraph(IUT, 1, 1);
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1));
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1),
|
||||
makeSet(V1));
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
printGraph(t, IUT);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testDirectedEdge()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph IUT = makeDirectedEdge();
|
||||
try
|
||||
{
|
||||
|
||||
verifyGraph(IUT, 2, 1);
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1, V2));
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1, V2));
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(),
|
||||
makeSet(V2));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1),
|
||||
makeSet());
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
printGraph(t, IUT);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testDirParallelEdges()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph IUT = makeDirParallelEdges();
|
||||
try
|
||||
{
|
||||
|
||||
verifyGraph(IUT, 2, 2);
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1, V2));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1, V2));
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(),
|
||||
makeSet(V2));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1),
|
||||
makeSet());
|
||||
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
printGraph(t, IUT);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testTwoCycle()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph IUT = makeTwoCycle();
|
||||
try
|
||||
{
|
||||
|
||||
verifyGraph(IUT, 2, 2);
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1, V2));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1, V2));
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V2),
|
||||
makeSet(V2));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1),
|
||||
makeSet(V1));
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
printGraph(t, IUT);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testDirectedCycle()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph IUT = makeDirectedCycle();
|
||||
try
|
||||
{
|
||||
|
||||
verifyGraph(IUT, 3, 3);
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1, V2, V3));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1, V2, V3));
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V1, V2, V3));
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V3),
|
||||
makeSet(V2));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1),
|
||||
makeSet(V3));
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V2),
|
||||
makeSet(V1));
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
printGraph(t, IUT);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testPipe()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph IUT = makePipe();
|
||||
try
|
||||
{
|
||||
|
||||
verifyGraph(IUT, 3, 2);
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1, V2));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1, V2, V3));
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V2, V3));
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(),
|
||||
makeSet(V2));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1),
|
||||
makeSet(V3));
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V2),
|
||||
makeSet());
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
printGraph(t, IUT);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testDiamond()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph IUT = makeDiamond();
|
||||
try
|
||||
{
|
||||
|
||||
verifyGraph(IUT, 4, 4);
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1, V2, V3));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1, V2, V4));
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V1, V3, V4));
|
||||
verifyAdjVertices(IUT, V4,
|
||||
makeSet(V2, V3, V4));
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(),
|
||||
makeSet(V2, V3));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1),
|
||||
makeSet(V4));
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V1),
|
||||
makeSet(V4));
|
||||
verifyAdjVertices(IUT, V4,
|
||||
makeSet(V2, V3),
|
||||
makeSet());
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
printGraph(t, IUT);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testPipelessCycle()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph IUT = makePipelessCycle();
|
||||
try
|
||||
{
|
||||
|
||||
verifyGraph(IUT, 4, 4);
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1, V2, V3));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1, V2, V4));
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V1, V3, V4));
|
||||
verifyAdjVertices(IUT, V4,
|
||||
makeSet(V2, V3, V4));
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(),
|
||||
makeSet(V2, V3));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1, V4),
|
||||
makeSet());
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V1, V4),
|
||||
makeSet());
|
||||
verifyAdjVertices(IUT, V4,
|
||||
makeSet(),
|
||||
makeSet(V2, V3));
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
printGraph(t, IUT);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testParentTree()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph IUT = makeParentTree();
|
||||
try
|
||||
{
|
||||
|
||||
verifyGraph(IUT, 5, 4);
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1, V2, V3));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1, V2));
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V1, V3, V4, V5));
|
||||
verifyAdjVertices(IUT, V4,
|
||||
makeSet(V3, V4));
|
||||
verifyAdjVertices(IUT, V5,
|
||||
makeSet(V3, V5));
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V2, V3),
|
||||
makeSet());
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(),
|
||||
makeSet(V1));
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V4, V5),
|
||||
makeSet(V1));
|
||||
verifyAdjVertices(IUT, V4,
|
||||
makeSet(),
|
||||
makeSet(V3));
|
||||
verifyAdjVertices(IUT, V5,
|
||||
makeSet(),
|
||||
makeSet(V3));
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
printGraph(t, IUT);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testChildTree()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph IUT = makeChildTree();
|
||||
try
|
||||
{
|
||||
|
||||
verifyGraph(IUT, 5, 4);
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(V1, V2, V3));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1, V2));
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V1, V3, V4, V5));
|
||||
verifyAdjVertices(IUT, V4,
|
||||
makeSet(V3, V4));
|
||||
verifyAdjVertices(IUT, V5,
|
||||
makeSet(V3, V5));
|
||||
|
||||
verifyAdjVertices(IUT, V1,
|
||||
makeSet(),
|
||||
makeSet(V2, V3));
|
||||
verifyAdjVertices(IUT, V2,
|
||||
makeSet(V1),
|
||||
makeSet());
|
||||
verifyAdjVertices(IUT, V3,
|
||||
makeSet(V1),
|
||||
makeSet(V4, V5));
|
||||
verifyAdjVertices(IUT, V4,
|
||||
makeSet(V3),
|
||||
makeSet());
|
||||
verifyAdjVertices(IUT, V5,
|
||||
makeSet(V3),
|
||||
makeSet());
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
printGraph(t, IUT);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,379 +1,375 @@
|
||||
package org.apache.commons.graph;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Apache" and "Apache Software Foundation" and
|
||||
* "Commons" must not be used to endorse or promote products
|
||||
* derived from this software without prior written permission. For
|
||||
* written permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache",
|
||||
* "Commons", nor may "Apache" appear in their name, without
|
||||
* prior written permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
/**
|
||||
* GraphTest This test will ensure that we can indeed represent a graph. We will
|
||||
* implement the following graphs. () No Vertex, No Edges @ One Vertex, No Edges
|
||||
* @ @ Two Vertices, No Edges @-@ Two Vertices, One edge /-\ @ @ Two Vertices,
|
||||
* Two Edges (Parallel Edges) \-/ @ / \ Three Vertices, Three Edges (Cycle)
|
||||
* @---@ @--@--@ Three Vertices, Two Edges (No Cycle) @ / \ @ @ 5 Vertices, 4
|
||||
* Edges (Tree) / \ @ @ @-@ @-@ 4 Vertices, 2 Edges (Disconnected)
|
||||
*/
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.Map;
|
||||
import java.util.HashSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
|
||||
import junit.framework.*;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class UndirGraphTest extends GraphTest
|
||||
{
|
||||
/**
|
||||
* Constructor for the UndirGraphTest object
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public UndirGraphTest(String name)
|
||||
{
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the NULL Graph.
|
||||
*/
|
||||
public void testNullGraph()
|
||||
throws Throwable
|
||||
{
|
||||
verifyGraph(makeNullGraph(), 0, 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test the Single Vertex.
|
||||
*/
|
||||
public void testSingleVertex()
|
||||
throws Throwable
|
||||
{
|
||||
verifyGraph(makeSingleVertex(), 1, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests Double Vertex graph.
|
||||
*/
|
||||
public void testDoubleVertex()
|
||||
throws Throwable
|
||||
{
|
||||
verifyGraph(makeDoubleVertex(), 2, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Single Edge Graph.
|
||||
*/
|
||||
public void testSingleEdge()
|
||||
throws Throwable
|
||||
{
|
||||
Graph IUT = makeSingleEdge();
|
||||
|
||||
verifyGraph(IUT, 2, 1);
|
||||
|
||||
// Verify Edge from Vertex Count
|
||||
verifyVertexEdgeCount(IUT, V1, 1);
|
||||
verifyVertexEdgeCount(IUT, V2, 1);
|
||||
|
||||
Set expectV1 = new HashSet();
|
||||
expectV1.add(V2);
|
||||
|
||||
verifyAdjVertices(IUT, V1, expectV1);
|
||||
|
||||
Set expectV2 = new HashSet();
|
||||
expectV2.add(V1);
|
||||
|
||||
verifyAdjVertices(IUT, V2, expectV2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Parallel Edges, make sure the representation is right.
|
||||
*/
|
||||
public void testParallelEdges()
|
||||
throws Throwable
|
||||
{
|
||||
Graph IUT = makeParallelEdges();
|
||||
|
||||
verifyGraph(IUT, 2, 2);
|
||||
|
||||
// Verify Edge from Vertex Count
|
||||
verifyVertexEdgeCount(IUT, V1, 2);
|
||||
verifyVertexEdgeCount(IUT, V2, 2);
|
||||
|
||||
// Verify Adjacent Vertex
|
||||
Set expectV1 = new HashSet();
|
||||
Set expectV2 = new HashSet();
|
||||
|
||||
expectV1.add(V2);
|
||||
expectV2.add(V1);
|
||||
|
||||
verifyAdjVertices(IUT, V1, expectV1);
|
||||
verifyAdjVertices(IUT, V2, expectV2);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the Cycle Graph.
|
||||
*/
|
||||
public void testCycle()
|
||||
throws Throwable
|
||||
{
|
||||
Graph IUT = makeCycle();
|
||||
|
||||
verifyGraph(IUT, 3, 3);
|
||||
|
||||
// Verify Edge from Vertex Count
|
||||
verifyVertexEdgeCount(IUT, V1, 2);
|
||||
verifyVertexEdgeCount(IUT, V2, 2);
|
||||
verifyVertexEdgeCount(IUT, V3, 2);
|
||||
|
||||
// Verify Adjacent Vertex
|
||||
Set expectV1 = new HashSet();
|
||||
Set expectV2 = new HashSet();
|
||||
Set expectV3 = new HashSet();
|
||||
|
||||
expectV1.add(V2);
|
||||
expectV1.add(V3);
|
||||
expectV2.add(V1);
|
||||
expectV2.add(V3);
|
||||
expectV3.add(V1);
|
||||
expectV3.add(V2);
|
||||
|
||||
verifyAdjVertices(IUT, V1, expectV1);
|
||||
verifyAdjVertices(IUT, V2, expectV2);
|
||||
verifyAdjVertices(IUT, V3, expectV3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the No Cycle Graph.
|
||||
*/
|
||||
public void testNoCycle()
|
||||
throws Throwable
|
||||
{
|
||||
Graph IUT = makeNoCycle();
|
||||
|
||||
verifyGraph(IUT, 3, 2);
|
||||
|
||||
// Verify Edge from Vertex Count
|
||||
verifyVertexEdgeCount(IUT, V1, 1);
|
||||
verifyVertexEdgeCount(IUT, V2, 2);
|
||||
verifyVertexEdgeCount(IUT, V3, 1);
|
||||
|
||||
// Verify Adjacent Vertex
|
||||
Set expectV1 = new HashSet();
|
||||
Set expectV2 = new HashSet();
|
||||
Set expectV3 = new HashSet();
|
||||
|
||||
expectV1.add(V2);
|
||||
expectV2.add(V1);
|
||||
expectV2.add(V3);
|
||||
expectV3.add(V2);
|
||||
|
||||
verifyAdjVertices(IUT, V1, expectV1);
|
||||
verifyAdjVertices(IUT, V2, expectV2);
|
||||
verifyAdjVertices(IUT, V3, expectV3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the Tree Graph.
|
||||
*/
|
||||
public void testTree()
|
||||
throws Throwable
|
||||
{
|
||||
Graph IUT = makeTree();
|
||||
|
||||
verifyGraph(IUT, 5, 4);
|
||||
|
||||
// Verify Edge from Vertex Count
|
||||
verifyVertexEdgeCount(IUT, V1, 2);
|
||||
verifyVertexEdgeCount(IUT, V2, 1);
|
||||
verifyVertexEdgeCount(IUT, V3, 3);
|
||||
verifyVertexEdgeCount(IUT, V4, 1);
|
||||
verifyVertexEdgeCount(IUT, V5, 1);
|
||||
|
||||
// Verify Adjacent Vertex
|
||||
Set expectV1 = new HashSet();
|
||||
Set expectV2 = new HashSet();
|
||||
Set expectV3 = new HashSet();
|
||||
Set expectV4 = new HashSet();
|
||||
Set expectV5 = new HashSet();
|
||||
|
||||
expectV1.add(V2);
|
||||
expectV1.add(V3);
|
||||
expectV2.add(V1);
|
||||
expectV3.add(V1);
|
||||
expectV3.add(V4);
|
||||
expectV3.add(V5);
|
||||
expectV4.add(V3);
|
||||
expectV5.add(V3);
|
||||
|
||||
verifyAdjVertices(IUT, V1, expectV1);
|
||||
verifyAdjVertices(IUT, V2, expectV2);
|
||||
verifyAdjVertices(IUT, V3, expectV3);
|
||||
verifyAdjVertices(IUT, V4, expectV4);
|
||||
verifyAdjVertices(IUT, V5, expectV5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the Disconnected Graph.
|
||||
*/
|
||||
public void testDisconnected()
|
||||
throws Throwable
|
||||
{
|
||||
Graph IUT = makeDisconnected();
|
||||
|
||||
verifyGraph(IUT, 4, 2);
|
||||
|
||||
// Verify Edge from Vertex Count
|
||||
verifyVertexEdgeCount(IUT, V1, 1);
|
||||
verifyVertexEdgeCount(IUT, V2, 1);
|
||||
verifyVertexEdgeCount(IUT, V3, 1);
|
||||
verifyVertexEdgeCount(IUT, V4, 1);
|
||||
|
||||
// Verify Adjacent Vertex
|
||||
Set expectV1 = new HashSet();
|
||||
Set expectV2 = new HashSet();
|
||||
Set expectV3 = new HashSet();
|
||||
Set expectV4 = new HashSet();
|
||||
|
||||
expectV1.add(V2);
|
||||
expectV2.add(V1);
|
||||
expectV3.add(V4);
|
||||
expectV4.add(V3);
|
||||
|
||||
verifyAdjVertices(IUT, V1, expectV1);
|
||||
verifyAdjVertices(IUT, V2, expectV2);
|
||||
verifyAdjVertices(IUT, V3, expectV3);
|
||||
verifyAdjVertices(IUT, V4, expectV4);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void verifyVertexEdgeCount(Graph IUT,
|
||||
Vertex v,
|
||||
int numEdge)
|
||||
throws Throwable
|
||||
{
|
||||
assertEquals(v.toString() + " has wrong number of adjacent edges.",
|
||||
IUT.getEdges(v).size(), numEdge);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the adjVertices attribute of the UndirGraphTest object
|
||||
*/
|
||||
private Set getAdjVertices(Graph IUT,
|
||||
Vertex v)
|
||||
{
|
||||
Set RC = new HashSet();
|
||||
Iterator edges = IUT.getEdges(v).iterator();
|
||||
|
||||
while (edges.hasNext())
|
||||
{
|
||||
Edge e = (Edge) edges.next();
|
||||
|
||||
Set verts = IUT.getVertices(e);
|
||||
verts.remove(v);
|
||||
RC.addAll(verts);
|
||||
}
|
||||
|
||||
return RC;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void verifyAdjVertices(Graph IUT,
|
||||
Vertex v,
|
||||
Set expected)
|
||||
throws Throwable
|
||||
{
|
||||
Set adjacent = getAdjVertices(IUT, v);
|
||||
Iterator adjV = adjacent.iterator();
|
||||
|
||||
while (adjV.hasNext())
|
||||
{
|
||||
Vertex curr = (Vertex) adjV.next();
|
||||
|
||||
assertTrue(curr.toString() + " is not supposed to be " +
|
||||
"next to " + v.toString(),
|
||||
expected.contains(curr));
|
||||
}
|
||||
|
||||
Iterator expect = expected.iterator();
|
||||
|
||||
while (expect.hasNext())
|
||||
{
|
||||
Vertex curr = (Vertex) expect.next();
|
||||
|
||||
assertTrue(curr.toString() + " is supposed to be next to " +
|
||||
v.toString(),
|
||||
adjacent.contains(curr));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void verifyGraph(Graph g, int numVertex, int numEdge)
|
||||
throws Throwable
|
||||
{
|
||||
assertEquals("Incorrect Number of Vertices.",
|
||||
g.getVertices().size(), numVertex);
|
||||
assertEquals("Incorrect Number of Edges.",
|
||||
g.getEdges().size(), numEdge);
|
||||
}
|
||||
}
|
||||
package org.apache.commons.graph;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Apache" and "Apache Software Foundation" and
|
||||
* "Commons" must not be used to endorse or promote products
|
||||
* derived from this software without prior written permission. For
|
||||
* written permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache",
|
||||
* "Commons", nor may "Apache" appear in their name, without
|
||||
* prior written permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
/**
|
||||
* GraphTest This test will ensure that we can indeed represent a graph. We will
|
||||
* implement the following graphs. () No Vertex, No Edges @ One Vertex, No Edges
|
||||
* @ @ Two Vertices, No Edges @-@ Two Vertices, One edge /-\ @ @ Two Vertices,
|
||||
* Two Edges (Parallel Edges) \-/ @ / \ Three Vertices, Three Edges (Cycle)
|
||||
* @---@ @--@--@ Three Vertices, Two Edges (No Cycle) @ / \ @ @ 5 Vertices, 4
|
||||
* Edges (Tree) / \ @ @ @-@ @-@ 4 Vertices, 2 Edges (Disconnected)
|
||||
*/
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class UndirGraphTest extends GraphTest
|
||||
{
|
||||
/**
|
||||
* Constructor for the UndirGraphTest object
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public UndirGraphTest(String name)
|
||||
{
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the NULL Graph.
|
||||
*/
|
||||
public void testNullGraph()
|
||||
throws Throwable
|
||||
{
|
||||
verifyGraph(makeNullGraph(), 0, 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test the Single Vertex.
|
||||
*/
|
||||
public void testSingleVertex()
|
||||
throws Throwable
|
||||
{
|
||||
verifyGraph(makeSingleVertex(), 1, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests Double Vertex graph.
|
||||
*/
|
||||
public void testDoubleVertex()
|
||||
throws Throwable
|
||||
{
|
||||
verifyGraph(makeDoubleVertex(), 2, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Single Edge Graph.
|
||||
*/
|
||||
public void testSingleEdge()
|
||||
throws Throwable
|
||||
{
|
||||
Graph IUT = makeSingleEdge();
|
||||
|
||||
verifyGraph(IUT, 2, 1);
|
||||
|
||||
// Verify Edge from Vertex Count
|
||||
verifyVertexEdgeCount(IUT, V1, 1);
|
||||
verifyVertexEdgeCount(IUT, V2, 1);
|
||||
|
||||
Set expectV1 = new HashSet();
|
||||
expectV1.add(V2);
|
||||
|
||||
verifyAdjVertices(IUT, V1, expectV1);
|
||||
|
||||
Set expectV2 = new HashSet();
|
||||
expectV2.add(V1);
|
||||
|
||||
verifyAdjVertices(IUT, V2, expectV2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Parallel Edges, make sure the representation is right.
|
||||
*/
|
||||
public void testParallelEdges()
|
||||
throws Throwable
|
||||
{
|
||||
Graph IUT = makeParallelEdges();
|
||||
|
||||
verifyGraph(IUT, 2, 2);
|
||||
|
||||
// Verify Edge from Vertex Count
|
||||
verifyVertexEdgeCount(IUT, V1, 2);
|
||||
verifyVertexEdgeCount(IUT, V2, 2);
|
||||
|
||||
// Verify Adjacent Vertex
|
||||
Set expectV1 = new HashSet();
|
||||
Set expectV2 = new HashSet();
|
||||
|
||||
expectV1.add(V2);
|
||||
expectV2.add(V1);
|
||||
|
||||
verifyAdjVertices(IUT, V1, expectV1);
|
||||
verifyAdjVertices(IUT, V2, expectV2);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the Cycle Graph.
|
||||
*/
|
||||
public void testCycle()
|
||||
throws Throwable
|
||||
{
|
||||
Graph IUT = makeCycle();
|
||||
|
||||
verifyGraph(IUT, 3, 3);
|
||||
|
||||
// Verify Edge from Vertex Count
|
||||
verifyVertexEdgeCount(IUT, V1, 2);
|
||||
verifyVertexEdgeCount(IUT, V2, 2);
|
||||
verifyVertexEdgeCount(IUT, V3, 2);
|
||||
|
||||
// Verify Adjacent Vertex
|
||||
Set expectV1 = new HashSet();
|
||||
Set expectV2 = new HashSet();
|
||||
Set expectV3 = new HashSet();
|
||||
|
||||
expectV1.add(V2);
|
||||
expectV1.add(V3);
|
||||
expectV2.add(V1);
|
||||
expectV2.add(V3);
|
||||
expectV3.add(V1);
|
||||
expectV3.add(V2);
|
||||
|
||||
verifyAdjVertices(IUT, V1, expectV1);
|
||||
verifyAdjVertices(IUT, V2, expectV2);
|
||||
verifyAdjVertices(IUT, V3, expectV3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the No Cycle Graph.
|
||||
*/
|
||||
public void testNoCycle()
|
||||
throws Throwable
|
||||
{
|
||||
Graph IUT = makeNoCycle();
|
||||
|
||||
verifyGraph(IUT, 3, 2);
|
||||
|
||||
// Verify Edge from Vertex Count
|
||||
verifyVertexEdgeCount(IUT, V1, 1);
|
||||
verifyVertexEdgeCount(IUT, V2, 2);
|
||||
verifyVertexEdgeCount(IUT, V3, 1);
|
||||
|
||||
// Verify Adjacent Vertex
|
||||
Set expectV1 = new HashSet();
|
||||
Set expectV2 = new HashSet();
|
||||
Set expectV3 = new HashSet();
|
||||
|
||||
expectV1.add(V2);
|
||||
expectV2.add(V1);
|
||||
expectV2.add(V3);
|
||||
expectV3.add(V2);
|
||||
|
||||
verifyAdjVertices(IUT, V1, expectV1);
|
||||
verifyAdjVertices(IUT, V2, expectV2);
|
||||
verifyAdjVertices(IUT, V3, expectV3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the Tree Graph.
|
||||
*/
|
||||
public void testTree()
|
||||
throws Throwable
|
||||
{
|
||||
Graph IUT = makeTree();
|
||||
|
||||
verifyGraph(IUT, 5, 4);
|
||||
|
||||
// Verify Edge from Vertex Count
|
||||
verifyVertexEdgeCount(IUT, V1, 2);
|
||||
verifyVertexEdgeCount(IUT, V2, 1);
|
||||
verifyVertexEdgeCount(IUT, V3, 3);
|
||||
verifyVertexEdgeCount(IUT, V4, 1);
|
||||
verifyVertexEdgeCount(IUT, V5, 1);
|
||||
|
||||
// Verify Adjacent Vertex
|
||||
Set expectV1 = new HashSet();
|
||||
Set expectV2 = new HashSet();
|
||||
Set expectV3 = new HashSet();
|
||||
Set expectV4 = new HashSet();
|
||||
Set expectV5 = new HashSet();
|
||||
|
||||
expectV1.add(V2);
|
||||
expectV1.add(V3);
|
||||
expectV2.add(V1);
|
||||
expectV3.add(V1);
|
||||
expectV3.add(V4);
|
||||
expectV3.add(V5);
|
||||
expectV4.add(V3);
|
||||
expectV5.add(V3);
|
||||
|
||||
verifyAdjVertices(IUT, V1, expectV1);
|
||||
verifyAdjVertices(IUT, V2, expectV2);
|
||||
verifyAdjVertices(IUT, V3, expectV3);
|
||||
verifyAdjVertices(IUT, V4, expectV4);
|
||||
verifyAdjVertices(IUT, V5, expectV5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the Disconnected Graph.
|
||||
*/
|
||||
public void testDisconnected()
|
||||
throws Throwable
|
||||
{
|
||||
Graph IUT = makeDisconnected();
|
||||
|
||||
verifyGraph(IUT, 4, 2);
|
||||
|
||||
// Verify Edge from Vertex Count
|
||||
verifyVertexEdgeCount(IUT, V1, 1);
|
||||
verifyVertexEdgeCount(IUT, V2, 1);
|
||||
verifyVertexEdgeCount(IUT, V3, 1);
|
||||
verifyVertexEdgeCount(IUT, V4, 1);
|
||||
|
||||
// Verify Adjacent Vertex
|
||||
Set expectV1 = new HashSet();
|
||||
Set expectV2 = new HashSet();
|
||||
Set expectV3 = new HashSet();
|
||||
Set expectV4 = new HashSet();
|
||||
|
||||
expectV1.add(V2);
|
||||
expectV2.add(V1);
|
||||
expectV3.add(V4);
|
||||
expectV4.add(V3);
|
||||
|
||||
verifyAdjVertices(IUT, V1, expectV1);
|
||||
verifyAdjVertices(IUT, V2, expectV2);
|
||||
verifyAdjVertices(IUT, V3, expectV3);
|
||||
verifyAdjVertices(IUT, V4, expectV4);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void verifyVertexEdgeCount(Graph IUT,
|
||||
Vertex v,
|
||||
int numEdge)
|
||||
throws Throwable
|
||||
{
|
||||
assertEquals(v.toString() + " has wrong number of adjacent edges.",
|
||||
IUT.getEdges(v).size(), numEdge);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the adjVertices attribute of the UndirGraphTest object
|
||||
*/
|
||||
private Set getAdjVertices(Graph IUT,
|
||||
Vertex v)
|
||||
{
|
||||
Set RC = new HashSet();
|
||||
Iterator edges = IUT.getEdges(v).iterator();
|
||||
|
||||
while (edges.hasNext())
|
||||
{
|
||||
Edge e = (Edge) edges.next();
|
||||
|
||||
Set verts = IUT.getVertices(e);
|
||||
verts.remove(v);
|
||||
RC.addAll(verts);
|
||||
}
|
||||
|
||||
return RC;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void verifyAdjVertices(Graph IUT,
|
||||
Vertex v,
|
||||
Set expected)
|
||||
throws Throwable
|
||||
{
|
||||
Set adjacent = getAdjVertices(IUT, v);
|
||||
Iterator adjV = adjacent.iterator();
|
||||
|
||||
while (adjV.hasNext())
|
||||
{
|
||||
Vertex curr = (Vertex) adjV.next();
|
||||
|
||||
assertTrue(curr.toString() + " is not supposed to be " +
|
||||
"next to " + v.toString(),
|
||||
expected.contains(curr));
|
||||
}
|
||||
|
||||
Iterator expect = expected.iterator();
|
||||
|
||||
while (expect.hasNext())
|
||||
{
|
||||
Vertex curr = (Vertex) expect.next();
|
||||
|
||||
assertTrue(curr.toString() + " is supposed to be next to " +
|
||||
v.toString(),
|
||||
adjacent.contains(curr));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void verifyGraph(Graph g, int numVertex, int numEdge)
|
||||
throws Throwable
|
||||
{
|
||||
assertEquals("Incorrect Number of Vertices.",
|
||||
g.getVertices().size(), numVertex);
|
||||
assertEquals("Incorrect Number of Edges.",
|
||||
g.getEdges().size(), numEdge);
|
||||
}
|
||||
}
|
||||
@@ -58,8 +58,6 @@ import org.apache.commons.graph.*;
|
||||
|
||||
import java.util.BitSet;
|
||||
|
||||
import junit.framework.*;
|
||||
|
||||
public class DataFlowSolutionsTest extends GraphTest {
|
||||
private static BitSet BS_000 = null;
|
||||
private static BitSet BS_001 = null;
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,492 +1,488 @@
|
||||
package org.apache.commons.graph.algorithm.path;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Apache" and "Apache Software Foundation" and
|
||||
* "Commons" must not be used to endorse or promote products
|
||||
* derived from this software without prior written permission. For
|
||||
* written permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache",
|
||||
* "Commons", nor may "Apache" appear in their name, without
|
||||
* prior written permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* AllPathsTest This tests the All Pairs Shortest Path solution.
|
||||
*/
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
import org.apache.commons.graph.exception.*;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.AbstractSet;
|
||||
|
||||
import junit.framework.*;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class AllPathsTest
|
||||
extends GraphTest
|
||||
{
|
||||
/**
|
||||
* PathCollector
|
||||
*
|
||||
* This is the thing that collects Callbacks.
|
||||
*/
|
||||
public class PathCollector
|
||||
extends AbstractSet
|
||||
implements PathListener,
|
||||
Set
|
||||
{
|
||||
private Set paths = new HashSet();
|
||||
|
||||
public PathCollector() { }
|
||||
|
||||
public void notifyPath( Path path ) {
|
||||
paths.add( path );
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return paths.size();
|
||||
}
|
||||
|
||||
public Iterator iterator() {
|
||||
return paths.iterator();
|
||||
}
|
||||
}
|
||||
|
||||
private String name = null;
|
||||
private Random random = new Random();
|
||||
|
||||
/**
|
||||
* Constructor for the AllPathsTest object
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public AllPathsTest(String name)
|
||||
{
|
||||
super(name);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void testSelfLoop() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makeSelfLoop() );
|
||||
verifyPathCount( IUT, V1, V1, 1, 1 );
|
||||
|
||||
// Make sure it works with 0. . .
|
||||
verifyPathCount( IUT, V1, V1, 0, 0 );
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
int r = random.nextInt( 50 );
|
||||
verifyPathCount( IUT, V1, V1, r, r );
|
||||
|
||||
Iterator iter = IUT.findPaths( V1, V1 );
|
||||
for (int j = 0; j < r; j++) {
|
||||
Path path = (Path) iter.next();
|
||||
|
||||
assertEquals( "Path wrong size.",
|
||||
j + 2, path.size() );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void testDirDoubleVertex()
|
||||
throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makeDirDoubleVertex() );
|
||||
|
||||
verifyPathCount( IUT, V1, V1, 0, 2 );
|
||||
verifyPathCount( IUT, V2, V2, 0, 2 );
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 0, 2 );
|
||||
verifyPathCount( IUT, V2, V1, 0, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testDirectedEdge()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph g = makeDirectedEdge();
|
||||
final AllPaths IUT = new AllPaths(g);
|
||||
|
||||
verifyPathCount( IUT, V1, V1, 0, 2 );
|
||||
verifyPathCount( IUT, V2, V2, 0, 2 );
|
||||
|
||||
verifyPathCount( IUT, V2, V1, 0, 2 );
|
||||
verifyPathCount( IUT, V1, V2, 1, 2 );
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 1, 1 );
|
||||
|
||||
// Make sure that no matter how big it
|
||||
// is, it still only returns 1 path.
|
||||
for (int i = 0; i < 5; i++) {
|
||||
int r = random.nextInt( 50 );
|
||||
|
||||
r += 5;
|
||||
verifyPathCount( IUT, V1, V2, 1, r );
|
||||
|
||||
}
|
||||
|
||||
verifyHasNextFalse( IUT, V1, V2 );
|
||||
}
|
||||
|
||||
public void testDirParallelEdges() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makeDirParallelEdges() );
|
||||
verifyPathCount( IUT, V1, V1, 0, 2 );
|
||||
verifyPathCount( IUT, V2, V2, 0, 2 );
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 2, 2 );
|
||||
verifyPathCount( IUT, V2, V1, 0, 2 );
|
||||
}
|
||||
|
||||
public void testTwoCycle() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makeTwoCycle() );
|
||||
verifyPathCount( IUT, V1, V1, 1, 2 );
|
||||
verifyPathCount( IUT, V2, V2, 1, 2 );
|
||||
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 1, 2 );
|
||||
verifyPathCount( IUT, V2, V1, 1, 2 );
|
||||
}
|
||||
|
||||
public void testDirectedCycle() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makeDirectedCycle() );
|
||||
|
||||
verifyPathCount( IUT, V1, V1, 1, 3 );
|
||||
verifyPathCount( IUT, V2, V2, 1, 3 );
|
||||
verifyPathCount( IUT, V3, V3, 1, 3 );
|
||||
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 1, 3 );
|
||||
verifyPathCount( IUT, V1, V3, 1, 3 );
|
||||
|
||||
verifyPathCount( IUT, V2, V3, 1, 3 );
|
||||
verifyPathCount( IUT, V2, V1, 1, 3 );
|
||||
|
||||
verifyPathCount( IUT, V3, V1, 1, 3 );
|
||||
verifyPathCount( IUT, V3, V2, 1, 3 );
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
int r = random.nextInt( 50 );
|
||||
verifyPathCount( IUT, V1, V1, r, 3 * r );
|
||||
}
|
||||
|
||||
verifyHasNextTrue( IUT, V1, V1 );
|
||||
|
||||
}
|
||||
|
||||
public void testDirected4Cycle() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makeDirected4Cycle() );
|
||||
|
||||
verifyPathCount( IUT, V1, V1, 1, 4 );
|
||||
verifyPathCount( IUT, V2, V2, 1, 4 );
|
||||
verifyPathCount( IUT, V3, V3, 1, 4 );
|
||||
verifyPathCount( IUT, V4, V4, 1, 4 );
|
||||
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 1, 4 );
|
||||
verifyPathCount( IUT, V1, V3, 1, 4 );
|
||||
verifyPathCount( IUT, V1, V4, 1, 4 );
|
||||
|
||||
verifyPathCount( IUT, V2, V1, 1, 4 );
|
||||
verifyPathCount( IUT, V2, V3, 1, 4 );
|
||||
verifyPathCount( IUT, V2, V4, 1, 4 );
|
||||
|
||||
verifyPathCount( IUT, V3, V1, 1, 4 );
|
||||
verifyPathCount( IUT, V3, V2, 1, 4 );
|
||||
verifyPathCount( IUT, V3, V4, 1, 4 );
|
||||
|
||||
verifyPathCount( IUT, V4, V1, 1, 4 );
|
||||
verifyPathCount( IUT, V4, V2, 1, 4 );
|
||||
verifyPathCount( IUT, V4, V3, 1, 4 );
|
||||
|
||||
verifyHasNextTrue( IUT, V1, V1 );
|
||||
}
|
||||
|
||||
public void testCycleNoReturn() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makeCycleNoReturn() );
|
||||
|
||||
verifyHasNextFalse( IUT, V1, V1 );
|
||||
}
|
||||
public void testPipe() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makePipe() );
|
||||
verifyPathCount( IUT, V1, V1, 0, 3 );
|
||||
verifyPathCount( IUT, V2, V2, 0, 3 );
|
||||
verifyPathCount( IUT, V3, V3, 0, 3 );
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 1, 3 );
|
||||
verifyPathCount( IUT, V1, V3, 1, 3 );
|
||||
|
||||
verifyPathCount( IUT, V2, V3, 1, 3 );
|
||||
verifyPathCount( IUT, V2, V1, 0, 3 );
|
||||
|
||||
verifyPathCount( IUT, V3, V1, 0, 3 );
|
||||
verifyPathCount( IUT, V3, V2, 0, 3 );
|
||||
}
|
||||
|
||||
public void testDiamond() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makeDiamond() );
|
||||
|
||||
verifyPathCount( IUT, V1, V1, 0, 4 );
|
||||
verifyPathCount( IUT, V2, V2, 0, 4 );
|
||||
verifyPathCount( IUT, V3, V3, 0, 4 );
|
||||
verifyPathCount( IUT, V4, V4, 0, 4 );
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 1, 4 );
|
||||
verifyPathCount( IUT, V1, V3, 1, 4 );
|
||||
verifyPathCount( IUT, V1, V4, 2, 4 );
|
||||
|
||||
verifyPathCount( IUT, V2, V1, 0, 4 );
|
||||
verifyPathCount( IUT, V2, V3, 0, 4 );
|
||||
verifyPathCount( IUT, V2, V4, 1, 4 );
|
||||
|
||||
verifyPathCount( IUT, V3, V1, 0, 4 );
|
||||
verifyPathCount( IUT, V3, V2, 0, 4 );
|
||||
verifyPathCount( IUT, V3, V4, 1, 4 );
|
||||
|
||||
verifyPathCount( IUT, V4, V1, 0, 4 );
|
||||
verifyPathCount( IUT, V4, V2, 0, 4 );
|
||||
verifyPathCount( IUT, V4, V3, 0, 4 );
|
||||
}
|
||||
|
||||
public void testPipelessCycle() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makePipelessCycle() );
|
||||
|
||||
verifyPathCount( IUT, V1, V1, 0, 4 );
|
||||
verifyPathCount( IUT, V2, V2, 0, 4 );
|
||||
verifyPathCount( IUT, V3, V3, 0, 4 );
|
||||
verifyPathCount( IUT, V4, V4, 0, 4 );
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 1, 4 );
|
||||
verifyPathCount( IUT, V1, V3, 1, 4 );
|
||||
verifyPathCount( IUT, V1, V4, 0, 4 );
|
||||
|
||||
verifyPathCount( IUT, V2, V3, 0, 4 );
|
||||
verifyPathCount( IUT, V2, V4, 0, 4 );
|
||||
verifyPathCount( IUT, V2, V1, 0, 4 );
|
||||
|
||||
verifyPathCount( IUT, V3, V4, 0, 4 );
|
||||
verifyPathCount( IUT, V3, V1, 0, 4 );
|
||||
verifyPathCount( IUT, V3, V2, 0, 4 );
|
||||
|
||||
verifyPathCount( IUT, V4, V1, 0, 4 );
|
||||
verifyPathCount( IUT, V4, V2, 1, 4 );
|
||||
verifyPathCount( IUT, V4, V3, 1, 4 );
|
||||
}
|
||||
|
||||
public void testParentTree() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makeParentTree() );
|
||||
|
||||
verifyPathCount( IUT, V1, V1, 0, 5 );
|
||||
verifyPathCount( IUT, V2, V2, 0, 5 );
|
||||
verifyPathCount( IUT, V3, V3, 0, 5 );
|
||||
verifyPathCount( IUT, V4, V4, 0, 5 );
|
||||
verifyPathCount( IUT, V5, V5, 0, 5 );
|
||||
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 0, 5 );
|
||||
verifyPathCount( IUT, V1, V3, 0, 5 );
|
||||
verifyPathCount( IUT, V1, V4, 0, 5 );
|
||||
verifyPathCount( IUT, V1, V5, 0, 5 );
|
||||
|
||||
verifyPathCount( IUT, V2, V1, 1, 5 );
|
||||
verifyPathCount( IUT, V2, V3, 0, 5 );
|
||||
verifyPathCount( IUT, V2, V4, 0, 5 );
|
||||
verifyPathCount( IUT, V2, V5, 0, 5 );
|
||||
|
||||
verifyPathCount( IUT, V3, V1, 1, 5 );
|
||||
verifyPathCount( IUT, V3, V2, 0, 5 );
|
||||
verifyPathCount( IUT, V3, V4, 0, 5 );
|
||||
verifyPathCount( IUT, V3, V5, 0, 5 );
|
||||
|
||||
verifyPathCount( IUT, V4, V1, 1, 5 );
|
||||
verifyPathCount( IUT, V4, V2, 0, 5 );
|
||||
verifyPathCount( IUT, V4, V3, 1, 5 );
|
||||
verifyPathCount( IUT, V4, V5, 0, 5 );
|
||||
|
||||
verifyPathCount( IUT, V5, V1, 1, 5 );
|
||||
verifyPathCount( IUT, V5, V2, 0, 5 );
|
||||
verifyPathCount( IUT, V5, V3, 1, 5 );
|
||||
verifyPathCount( IUT, V5, V4, 0, 5 );
|
||||
}
|
||||
|
||||
public void testChildTree() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makeChildTree() );
|
||||
|
||||
verifyPathCount( IUT, V1, V1, 0, 5 );
|
||||
verifyPathCount( IUT, V2, V2, 0, 5 );
|
||||
verifyPathCount( IUT, V3, V3, 0, 5 );
|
||||
verifyPathCount( IUT, V4, V4, 0, 5 );
|
||||
verifyPathCount( IUT, V5, V5, 0, 5 );
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 1, 5 );
|
||||
verifyPathCount( IUT, V1, V3, 1, 5 );
|
||||
verifyPathCount( IUT, V1, V4, 1, 5 );
|
||||
verifyPathCount( IUT, V1, V5, 1, 5 );
|
||||
|
||||
verifyPathCount( IUT, V2, V1, 0, 5 );
|
||||
verifyPathCount( IUT, V2, V3, 0, 5 );
|
||||
verifyPathCount( IUT, V2, V4, 0, 5 );
|
||||
verifyPathCount( IUT, V2, V5, 0, 5 );
|
||||
|
||||
verifyPathCount( IUT, V3, V1, 0, 5 );
|
||||
verifyPathCount( IUT, V3, V2, 0, 5 );
|
||||
verifyPathCount( IUT, V3, V4, 1, 5 );
|
||||
verifyPathCount( IUT, V3, V5, 1, 5 );
|
||||
|
||||
verifyPathCount( IUT, V4, V1, 0, 5 );
|
||||
verifyPathCount( IUT, V4, V2, 0, 5 );
|
||||
verifyPathCount( IUT, V4, V3, 0, 5 );
|
||||
verifyPathCount( IUT, V4, V5, 0, 5 );
|
||||
|
||||
verifyPathCount( IUT, V5, V1, 0, 5 );
|
||||
verifyPathCount( IUT, V5, V2, 0, 5 );
|
||||
verifyPathCount( IUT, V5, V3, 0, 5 );
|
||||
verifyPathCount( IUT, V5, V4, 0, 5 );
|
||||
}
|
||||
|
||||
public void verifyHasNextFalse( final AllPaths IUT,
|
||||
final Vertex start,
|
||||
final Vertex end )
|
||||
throws Throwable {
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
// This is run many times, because there
|
||||
// may be a race condition in it.
|
||||
Thread t = new Thread( new Runnable() {
|
||||
public void run() {
|
||||
Iterator iter = IUT.findPaths( start, end );
|
||||
if (iter.hasNext()) iter.next();
|
||||
boolean val = iter.hasNext();
|
||||
}
|
||||
});
|
||||
|
||||
t.start();
|
||||
t.join( 1000 );
|
||||
|
||||
assertTrue("Iterator didn't return from hasNext in one second.",
|
||||
!t.isAlive());
|
||||
}
|
||||
|
||||
|
||||
Iterator iter = IUT.findPaths( start, end );
|
||||
if (iter.hasNext()) iter.next();
|
||||
|
||||
assertTrue("Iterator has too many elements.",
|
||||
!iter.hasNext());
|
||||
|
||||
}
|
||||
|
||||
public void verifyHasNextTrue( final AllPaths IUT,
|
||||
final Vertex start,
|
||||
final Vertex end )
|
||||
throws Throwable {
|
||||
|
||||
final Iterator iter = IUT.findPaths( start, end );
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
|
||||
// This is run many times, because there
|
||||
// may be a race condition in it.
|
||||
Thread t = new Thread( new Runnable() {
|
||||
public void run() {
|
||||
if (iter.hasNext()) { iter.next(); }
|
||||
}
|
||||
});
|
||||
|
||||
t.start();
|
||||
t.join( 1000 );
|
||||
|
||||
assertTrue("Iterator didn't return from hasNext in one second.",
|
||||
!t.isAlive());
|
||||
}
|
||||
|
||||
|
||||
assertTrue("Iterator terminated.",
|
||||
iter.hasNext());
|
||||
|
||||
}
|
||||
|
||||
public void verifyPathCount( AllPaths IUT,
|
||||
Vertex start,
|
||||
Vertex end,
|
||||
int expected,
|
||||
int depth ) throws Throwable {
|
||||
PathCollector paths = new PathCollector();
|
||||
|
||||
IUT.findPaths( paths, start, end, depth );
|
||||
|
||||
Iterator pathsIter = paths.iterator();
|
||||
while (pathsIter.hasNext()) {
|
||||
Path path = (Path) pathsIter.next();
|
||||
assertEquals("Path: " + path + " has wrong start.",
|
||||
start, path.getStart() );
|
||||
assertEquals("Path: " + path + " has wrong end.",
|
||||
end, path.getEnd() );
|
||||
assertTrue("Path: " + path + " is too long.",
|
||||
path.size() <= depth+1 );
|
||||
}
|
||||
|
||||
assertEquals("Wrong number of paths returned for: " + start + "->" + end,
|
||||
expected, paths.size() );
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
package org.apache.commons.graph.algorithm.path;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Apache" and "Apache Software Foundation" and
|
||||
* "Commons" must not be used to endorse or promote products
|
||||
* derived from this software without prior written permission. For
|
||||
* written permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache",
|
||||
* "Commons", nor may "Apache" appear in their name, without
|
||||
* prior written permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* AllPathsTest This tests the All Pairs Shortest Path solution.
|
||||
*/
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.Random;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.AbstractSet;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class AllPathsTest
|
||||
extends GraphTest
|
||||
{
|
||||
/**
|
||||
* PathCollector
|
||||
*
|
||||
* This is the thing that collects Callbacks.
|
||||
*/
|
||||
public class PathCollector
|
||||
extends AbstractSet
|
||||
implements PathListener,
|
||||
Set
|
||||
{
|
||||
private Set paths = new HashSet();
|
||||
|
||||
public PathCollector() { }
|
||||
|
||||
public void notifyPath( Path path ) {
|
||||
paths.add( path );
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return paths.size();
|
||||
}
|
||||
|
||||
public Iterator iterator() {
|
||||
return paths.iterator();
|
||||
}
|
||||
}
|
||||
|
||||
private String name = null;
|
||||
private Random random = new Random();
|
||||
|
||||
/**
|
||||
* Constructor for the AllPathsTest object
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public AllPathsTest(String name)
|
||||
{
|
||||
super(name);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void testSelfLoop() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makeSelfLoop() );
|
||||
verifyPathCount( IUT, V1, V1, 1, 1 );
|
||||
|
||||
// Make sure it works with 0. . .
|
||||
verifyPathCount( IUT, V1, V1, 0, 0 );
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
int r = random.nextInt( 50 );
|
||||
verifyPathCount( IUT, V1, V1, r, r );
|
||||
|
||||
Iterator iter = IUT.findPaths( V1, V1 );
|
||||
for (int j = 0; j < r; j++) {
|
||||
Path path = (Path) iter.next();
|
||||
|
||||
assertEquals( "Path wrong size.",
|
||||
j + 2, path.size() );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void testDirDoubleVertex()
|
||||
throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makeDirDoubleVertex() );
|
||||
|
||||
verifyPathCount( IUT, V1, V1, 0, 2 );
|
||||
verifyPathCount( IUT, V2, V2, 0, 2 );
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 0, 2 );
|
||||
verifyPathCount( IUT, V2, V1, 0, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testDirectedEdge()
|
||||
throws Throwable
|
||||
{
|
||||
DirectedGraph g = makeDirectedEdge();
|
||||
final AllPaths IUT = new AllPaths(g);
|
||||
|
||||
verifyPathCount( IUT, V1, V1, 0, 2 );
|
||||
verifyPathCount( IUT, V2, V2, 0, 2 );
|
||||
|
||||
verifyPathCount( IUT, V2, V1, 0, 2 );
|
||||
verifyPathCount( IUT, V1, V2, 1, 2 );
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 1, 1 );
|
||||
|
||||
// Make sure that no matter how big it
|
||||
// is, it still only returns 1 path.
|
||||
for (int i = 0; i < 5; i++) {
|
||||
int r = random.nextInt( 50 );
|
||||
|
||||
r += 5;
|
||||
verifyPathCount( IUT, V1, V2, 1, r );
|
||||
|
||||
}
|
||||
|
||||
verifyHasNextFalse( IUT, V1, V2 );
|
||||
}
|
||||
|
||||
public void testDirParallelEdges() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makeDirParallelEdges() );
|
||||
verifyPathCount( IUT, V1, V1, 0, 2 );
|
||||
verifyPathCount( IUT, V2, V2, 0, 2 );
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 2, 2 );
|
||||
verifyPathCount( IUT, V2, V1, 0, 2 );
|
||||
}
|
||||
|
||||
public void testTwoCycle() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makeTwoCycle() );
|
||||
verifyPathCount( IUT, V1, V1, 1, 2 );
|
||||
verifyPathCount( IUT, V2, V2, 1, 2 );
|
||||
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 1, 2 );
|
||||
verifyPathCount( IUT, V2, V1, 1, 2 );
|
||||
}
|
||||
|
||||
public void testDirectedCycle() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makeDirectedCycle() );
|
||||
|
||||
verifyPathCount( IUT, V1, V1, 1, 3 );
|
||||
verifyPathCount( IUT, V2, V2, 1, 3 );
|
||||
verifyPathCount( IUT, V3, V3, 1, 3 );
|
||||
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 1, 3 );
|
||||
verifyPathCount( IUT, V1, V3, 1, 3 );
|
||||
|
||||
verifyPathCount( IUT, V2, V3, 1, 3 );
|
||||
verifyPathCount( IUT, V2, V1, 1, 3 );
|
||||
|
||||
verifyPathCount( IUT, V3, V1, 1, 3 );
|
||||
verifyPathCount( IUT, V3, V2, 1, 3 );
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
int r = random.nextInt( 50 );
|
||||
verifyPathCount( IUT, V1, V1, r, 3 * r );
|
||||
}
|
||||
|
||||
verifyHasNextTrue( IUT, V1, V1 );
|
||||
|
||||
}
|
||||
|
||||
public void testDirected4Cycle() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makeDirected4Cycle() );
|
||||
|
||||
verifyPathCount( IUT, V1, V1, 1, 4 );
|
||||
verifyPathCount( IUT, V2, V2, 1, 4 );
|
||||
verifyPathCount( IUT, V3, V3, 1, 4 );
|
||||
verifyPathCount( IUT, V4, V4, 1, 4 );
|
||||
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 1, 4 );
|
||||
verifyPathCount( IUT, V1, V3, 1, 4 );
|
||||
verifyPathCount( IUT, V1, V4, 1, 4 );
|
||||
|
||||
verifyPathCount( IUT, V2, V1, 1, 4 );
|
||||
verifyPathCount( IUT, V2, V3, 1, 4 );
|
||||
verifyPathCount( IUT, V2, V4, 1, 4 );
|
||||
|
||||
verifyPathCount( IUT, V3, V1, 1, 4 );
|
||||
verifyPathCount( IUT, V3, V2, 1, 4 );
|
||||
verifyPathCount( IUT, V3, V4, 1, 4 );
|
||||
|
||||
verifyPathCount( IUT, V4, V1, 1, 4 );
|
||||
verifyPathCount( IUT, V4, V2, 1, 4 );
|
||||
verifyPathCount( IUT, V4, V3, 1, 4 );
|
||||
|
||||
verifyHasNextTrue( IUT, V1, V1 );
|
||||
}
|
||||
|
||||
public void testCycleNoReturn() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makeCycleNoReturn() );
|
||||
|
||||
verifyHasNextFalse( IUT, V1, V1 );
|
||||
}
|
||||
public void testPipe() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makePipe() );
|
||||
verifyPathCount( IUT, V1, V1, 0, 3 );
|
||||
verifyPathCount( IUT, V2, V2, 0, 3 );
|
||||
verifyPathCount( IUT, V3, V3, 0, 3 );
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 1, 3 );
|
||||
verifyPathCount( IUT, V1, V3, 1, 3 );
|
||||
|
||||
verifyPathCount( IUT, V2, V3, 1, 3 );
|
||||
verifyPathCount( IUT, V2, V1, 0, 3 );
|
||||
|
||||
verifyPathCount( IUT, V3, V1, 0, 3 );
|
||||
verifyPathCount( IUT, V3, V2, 0, 3 );
|
||||
}
|
||||
|
||||
public void testDiamond() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makeDiamond() );
|
||||
|
||||
verifyPathCount( IUT, V1, V1, 0, 4 );
|
||||
verifyPathCount( IUT, V2, V2, 0, 4 );
|
||||
verifyPathCount( IUT, V3, V3, 0, 4 );
|
||||
verifyPathCount( IUT, V4, V4, 0, 4 );
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 1, 4 );
|
||||
verifyPathCount( IUT, V1, V3, 1, 4 );
|
||||
verifyPathCount( IUT, V1, V4, 2, 4 );
|
||||
|
||||
verifyPathCount( IUT, V2, V1, 0, 4 );
|
||||
verifyPathCount( IUT, V2, V3, 0, 4 );
|
||||
verifyPathCount( IUT, V2, V4, 1, 4 );
|
||||
|
||||
verifyPathCount( IUT, V3, V1, 0, 4 );
|
||||
verifyPathCount( IUT, V3, V2, 0, 4 );
|
||||
verifyPathCount( IUT, V3, V4, 1, 4 );
|
||||
|
||||
verifyPathCount( IUT, V4, V1, 0, 4 );
|
||||
verifyPathCount( IUT, V4, V2, 0, 4 );
|
||||
verifyPathCount( IUT, V4, V3, 0, 4 );
|
||||
}
|
||||
|
||||
public void testPipelessCycle() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makePipelessCycle() );
|
||||
|
||||
verifyPathCount( IUT, V1, V1, 0, 4 );
|
||||
verifyPathCount( IUT, V2, V2, 0, 4 );
|
||||
verifyPathCount( IUT, V3, V3, 0, 4 );
|
||||
verifyPathCount( IUT, V4, V4, 0, 4 );
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 1, 4 );
|
||||
verifyPathCount( IUT, V1, V3, 1, 4 );
|
||||
verifyPathCount( IUT, V1, V4, 0, 4 );
|
||||
|
||||
verifyPathCount( IUT, V2, V3, 0, 4 );
|
||||
verifyPathCount( IUT, V2, V4, 0, 4 );
|
||||
verifyPathCount( IUT, V2, V1, 0, 4 );
|
||||
|
||||
verifyPathCount( IUT, V3, V4, 0, 4 );
|
||||
verifyPathCount( IUT, V3, V1, 0, 4 );
|
||||
verifyPathCount( IUT, V3, V2, 0, 4 );
|
||||
|
||||
verifyPathCount( IUT, V4, V1, 0, 4 );
|
||||
verifyPathCount( IUT, V4, V2, 1, 4 );
|
||||
verifyPathCount( IUT, V4, V3, 1, 4 );
|
||||
}
|
||||
|
||||
public void testParentTree() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makeParentTree() );
|
||||
|
||||
verifyPathCount( IUT, V1, V1, 0, 5 );
|
||||
verifyPathCount( IUT, V2, V2, 0, 5 );
|
||||
verifyPathCount( IUT, V3, V3, 0, 5 );
|
||||
verifyPathCount( IUT, V4, V4, 0, 5 );
|
||||
verifyPathCount( IUT, V5, V5, 0, 5 );
|
||||
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 0, 5 );
|
||||
verifyPathCount( IUT, V1, V3, 0, 5 );
|
||||
verifyPathCount( IUT, V1, V4, 0, 5 );
|
||||
verifyPathCount( IUT, V1, V5, 0, 5 );
|
||||
|
||||
verifyPathCount( IUT, V2, V1, 1, 5 );
|
||||
verifyPathCount( IUT, V2, V3, 0, 5 );
|
||||
verifyPathCount( IUT, V2, V4, 0, 5 );
|
||||
verifyPathCount( IUT, V2, V5, 0, 5 );
|
||||
|
||||
verifyPathCount( IUT, V3, V1, 1, 5 );
|
||||
verifyPathCount( IUT, V3, V2, 0, 5 );
|
||||
verifyPathCount( IUT, V3, V4, 0, 5 );
|
||||
verifyPathCount( IUT, V3, V5, 0, 5 );
|
||||
|
||||
verifyPathCount( IUT, V4, V1, 1, 5 );
|
||||
verifyPathCount( IUT, V4, V2, 0, 5 );
|
||||
verifyPathCount( IUT, V4, V3, 1, 5 );
|
||||
verifyPathCount( IUT, V4, V5, 0, 5 );
|
||||
|
||||
verifyPathCount( IUT, V5, V1, 1, 5 );
|
||||
verifyPathCount( IUT, V5, V2, 0, 5 );
|
||||
verifyPathCount( IUT, V5, V3, 1, 5 );
|
||||
verifyPathCount( IUT, V5, V4, 0, 5 );
|
||||
}
|
||||
|
||||
public void testChildTree() throws Throwable {
|
||||
AllPaths IUT = new AllPaths( makeChildTree() );
|
||||
|
||||
verifyPathCount( IUT, V1, V1, 0, 5 );
|
||||
verifyPathCount( IUT, V2, V2, 0, 5 );
|
||||
verifyPathCount( IUT, V3, V3, 0, 5 );
|
||||
verifyPathCount( IUT, V4, V4, 0, 5 );
|
||||
verifyPathCount( IUT, V5, V5, 0, 5 );
|
||||
|
||||
verifyPathCount( IUT, V1, V2, 1, 5 );
|
||||
verifyPathCount( IUT, V1, V3, 1, 5 );
|
||||
verifyPathCount( IUT, V1, V4, 1, 5 );
|
||||
verifyPathCount( IUT, V1, V5, 1, 5 );
|
||||
|
||||
verifyPathCount( IUT, V2, V1, 0, 5 );
|
||||
verifyPathCount( IUT, V2, V3, 0, 5 );
|
||||
verifyPathCount( IUT, V2, V4, 0, 5 );
|
||||
verifyPathCount( IUT, V2, V5, 0, 5 );
|
||||
|
||||
verifyPathCount( IUT, V3, V1, 0, 5 );
|
||||
verifyPathCount( IUT, V3, V2, 0, 5 );
|
||||
verifyPathCount( IUT, V3, V4, 1, 5 );
|
||||
verifyPathCount( IUT, V3, V5, 1, 5 );
|
||||
|
||||
verifyPathCount( IUT, V4, V1, 0, 5 );
|
||||
verifyPathCount( IUT, V4, V2, 0, 5 );
|
||||
verifyPathCount( IUT, V4, V3, 0, 5 );
|
||||
verifyPathCount( IUT, V4, V5, 0, 5 );
|
||||
|
||||
verifyPathCount( IUT, V5, V1, 0, 5 );
|
||||
verifyPathCount( IUT, V5, V2, 0, 5 );
|
||||
verifyPathCount( IUT, V5, V3, 0, 5 );
|
||||
verifyPathCount( IUT, V5, V4, 0, 5 );
|
||||
}
|
||||
|
||||
public void verifyHasNextFalse( final AllPaths IUT,
|
||||
final Vertex start,
|
||||
final Vertex end )
|
||||
throws Throwable {
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
// This is run many times, because there
|
||||
// may be a race condition in it.
|
||||
Thread t = new Thread( new Runnable() {
|
||||
public void run() {
|
||||
Iterator iter = IUT.findPaths( start, end );
|
||||
if (iter.hasNext()) iter.next();
|
||||
boolean val = iter.hasNext();
|
||||
}
|
||||
});
|
||||
|
||||
t.start();
|
||||
t.join( 1000 );
|
||||
|
||||
assertTrue("Iterator didn't return from hasNext in one second.",
|
||||
!t.isAlive());
|
||||
}
|
||||
|
||||
|
||||
Iterator iter = IUT.findPaths( start, end );
|
||||
if (iter.hasNext()) iter.next();
|
||||
|
||||
assertTrue("Iterator has too many elements.",
|
||||
!iter.hasNext());
|
||||
|
||||
}
|
||||
|
||||
public void verifyHasNextTrue( final AllPaths IUT,
|
||||
final Vertex start,
|
||||
final Vertex end )
|
||||
throws Throwable {
|
||||
|
||||
final Iterator iter = IUT.findPaths( start, end );
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
|
||||
// This is run many times, because there
|
||||
// may be a race condition in it.
|
||||
Thread t = new Thread( new Runnable() {
|
||||
public void run() {
|
||||
if (iter.hasNext()) { iter.next(); }
|
||||
}
|
||||
});
|
||||
|
||||
t.start();
|
||||
t.join( 1000 );
|
||||
|
||||
assertTrue("Iterator didn't return from hasNext in one second.",
|
||||
!t.isAlive());
|
||||
}
|
||||
|
||||
|
||||
assertTrue("Iterator terminated.",
|
||||
iter.hasNext());
|
||||
|
||||
}
|
||||
|
||||
public void verifyPathCount( AllPaths IUT,
|
||||
Vertex start,
|
||||
Vertex end,
|
||||
int expected,
|
||||
int depth ) throws Throwable {
|
||||
PathCollector paths = new PathCollector();
|
||||
|
||||
IUT.findPaths( paths, start, end, depth );
|
||||
|
||||
Iterator pathsIter = paths.iterator();
|
||||
while (pathsIter.hasNext()) {
|
||||
Path path = (Path) pathsIter.next();
|
||||
assertEquals("Path: " + path + " has wrong start.",
|
||||
start, path.getStart() );
|
||||
assertEquals("Path: " + path + " has wrong end.",
|
||||
end, path.getEnd() );
|
||||
assertTrue("Path: " + path + " is too long.",
|
||||
path.size() <= depth+1 );
|
||||
}
|
||||
|
||||
assertEquals("Wrong number of paths returned for: " + start + "->" + end,
|
||||
expected, paths.size() );
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -54,12 +54,6 @@ package org.apache.commons.graph.decorator;
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.Map;
|
||||
import java.util.HashSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,269 +1,269 @@
|
||||
package org.apache.commons.graph.domain.dependency;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.List;
|
||||
import java.util.HashSet;
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
import org.apache.commons.graph.dependency.exception.*;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class DependencyTest
|
||||
extends GraphTest
|
||||
{
|
||||
private DependencyGraph IUT = null;
|
||||
|
||||
/**
|
||||
* Constructor for the DependencyTest object
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public DependencyTest(String name)
|
||||
{
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* The JUnit setup method
|
||||
*/
|
||||
public void setUp()
|
||||
{
|
||||
IUT = new DependencyGraph();
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
private class Task
|
||||
{
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Constructor for the Task object
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public Task(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Field
|
||||
*/
|
||||
public Task T1 = new Task("T1");
|
||||
/**
|
||||
* Description of the Field
|
||||
*/
|
||||
public Task T2 = new Task("T2");
|
||||
/**
|
||||
* Description of the Field
|
||||
*/
|
||||
public Task T3 = new Task("T3");
|
||||
/**
|
||||
* Description of the Field
|
||||
*/
|
||||
public Task T4 = new Task("T4");
|
||||
/**
|
||||
* Description of the Field
|
||||
*/
|
||||
public Task T5 = new Task("T5");
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testNoDeps()
|
||||
throws Throwable
|
||||
{
|
||||
Set deps = new HashSet();
|
||||
|
||||
IUT.addDependencies(T1, deps);
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testTwoTasksNoDeps()
|
||||
throws Throwable
|
||||
{
|
||||
Set deps = new HashSet();
|
||||
IUT.addDependencies(T1, deps);
|
||||
IUT.addDependencies(T2, deps);
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testSelfDep()
|
||||
throws Throwable
|
||||
{
|
||||
Set deps = new HashSet();
|
||||
deps.add(T1);
|
||||
|
||||
try
|
||||
{
|
||||
IUT.addDependencies(T1, deps);
|
||||
fail("Self Dependency added without exception.");
|
||||
}
|
||||
catch (CircularDependencyException ex)
|
||||
{}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testTwoCyclicDeps()
|
||||
throws Throwable
|
||||
{
|
||||
Set t1Deps = new HashSet();
|
||||
t1Deps.add(T2);
|
||||
|
||||
Set t2Deps = new HashSet();
|
||||
t2Deps.add(T1);
|
||||
|
||||
IUT.addDependencies(T1, t1Deps);
|
||||
try
|
||||
{
|
||||
IUT.addDependencies(T2, t2Deps);
|
||||
fail("No CircularDependencyException thrown.");
|
||||
}
|
||||
catch (CircularDependencyException ex)
|
||||
{}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testThreeCyclicDeps()
|
||||
throws Throwable
|
||||
{
|
||||
Set t1Deps = new HashSet();
|
||||
t1Deps.add(T2);
|
||||
|
||||
Set t2Deps = new HashSet();
|
||||
t2Deps.add(T3);
|
||||
|
||||
Set t3Deps = new HashSet();
|
||||
t3Deps.add(T1);
|
||||
|
||||
IUT.addDependencies(T1, t1Deps);
|
||||
IUT.addDependencies(T2, t2Deps);
|
||||
|
||||
try
|
||||
{
|
||||
IUT.addDependencies(T3, t3Deps);
|
||||
fail("No CircularDependencyException Thrown.");
|
||||
}
|
||||
catch (CircularDependencyException ex)
|
||||
{}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testSortedDepsEasy()
|
||||
throws Throwable
|
||||
{
|
||||
Set t1Deps = new HashSet();
|
||||
t1Deps.add(T2);
|
||||
|
||||
Set t2Deps = new HashSet();
|
||||
t2Deps.add(T3);
|
||||
|
||||
IUT.addDependencies(T1, t1Deps);
|
||||
IUT.addDependencies(T2, t2Deps);
|
||||
IUT.addDependencies(T3, new HashSet());
|
||||
|
||||
// T3
|
||||
List t3SDeps =
|
||||
IUT.getSortedDependencies(T3);
|
||||
assertEquals("T3: Wrong number of Deps for T3",
|
||||
1, t3SDeps.size());
|
||||
|
||||
assertEquals("T3: Wrong thing at pos 0",
|
||||
T3, t3SDeps.get(0));
|
||||
|
||||
// T2
|
||||
List t2SDeps =
|
||||
IUT.getSortedDependencies(T2);
|
||||
assertEquals("T2: Wrong number of Deps for T2",
|
||||
2, t2SDeps.size());
|
||||
|
||||
try
|
||||
{
|
||||
assertEquals("T2: Wrong thing at pos 0",
|
||||
T3, t2SDeps.get(0));
|
||||
assertEquals("T2: Wrong thing at pos 1",
|
||||
T2, t2SDeps.get(1));
|
||||
}
|
||||
catch (Throwable ex)
|
||||
{
|
||||
System.err.println(t2SDeps);
|
||||
throw ex;
|
||||
}
|
||||
|
||||
// T1
|
||||
List t1SDeps =
|
||||
IUT.getSortedDependencies(T1);
|
||||
assertEquals("T1: Wrong number of Deps for T1",
|
||||
3, t1SDeps.size());
|
||||
|
||||
assertEquals("T1: Wrong thing at pos 0",
|
||||
T3, t1SDeps.get(0));
|
||||
assertEquals("T1: Wrong thing at pos 1",
|
||||
T2, t1SDeps.get(1));
|
||||
assertEquals("T1: Wrong thing at pos 2",
|
||||
T1, t1SDeps.get(2));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testSortedDepsHard()
|
||||
{
|
||||
Set t1Deps = new HashSet();
|
||||
t1Deps.add(T2);
|
||||
t1Deps.add(T3);
|
||||
t1Deps.add(T5);
|
||||
|
||||
Set t2Deps = new HashSet();
|
||||
t2Deps.add(T4);
|
||||
t2Deps.add(T5);
|
||||
|
||||
IUT.addDependencies(T1, t1Deps);
|
||||
IUT.addDependencies(T2, t2Deps);
|
||||
|
||||
List t1SDeps = IUT.getSortedDependencies(T1);
|
||||
assertEquals("T1: Wrong number of dependents.",
|
||||
5, t1SDeps.size());
|
||||
|
||||
List t2SDeps = IUT.getSortedDependencies(T2);
|
||||
assertEquals("T2: Wrong number of dependents.",
|
||||
3, t2SDeps.size());
|
||||
|
||||
List t3SDeps = IUT.getSortedDependencies(T3);
|
||||
assertEquals("T1: Wrong number of dependents.",
|
||||
1, t3SDeps.size());
|
||||
|
||||
List t4SDeps = IUT.getSortedDependencies(T4);
|
||||
assertEquals("T4: Wrong number of dependents.",
|
||||
1, t4SDeps.size());
|
||||
|
||||
List t5SDeps = IUT.getSortedDependencies(T5);
|
||||
assertEquals("T5: Wrong number of dependents.",
|
||||
1, t5SDeps.size());
|
||||
|
||||
}
|
||||
}
|
||||
package org.apache.commons.graph.domain.dependency;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.List;
|
||||
import java.util.HashSet;
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
import org.apache.commons.graph.domain.dependency.exception.*;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class DependencyTest
|
||||
extends GraphTest
|
||||
{
|
||||
private DependencyGraph IUT = null;
|
||||
|
||||
/**
|
||||
* Constructor for the DependencyTest object
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public DependencyTest(String name)
|
||||
{
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* The JUnit setup method
|
||||
*/
|
||||
public void setUp()
|
||||
{
|
||||
IUT = new DependencyGraph();
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
private class Task
|
||||
{
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Constructor for the Task object
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public Task(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Field
|
||||
*/
|
||||
public Task T1 = new Task("T1");
|
||||
/**
|
||||
* Description of the Field
|
||||
*/
|
||||
public Task T2 = new Task("T2");
|
||||
/**
|
||||
* Description of the Field
|
||||
*/
|
||||
public Task T3 = new Task("T3");
|
||||
/**
|
||||
* Description of the Field
|
||||
*/
|
||||
public Task T4 = new Task("T4");
|
||||
/**
|
||||
* Description of the Field
|
||||
*/
|
||||
public Task T5 = new Task("T5");
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testNoDeps()
|
||||
throws Throwable
|
||||
{
|
||||
Set deps = new HashSet();
|
||||
|
||||
IUT.addDependencies(T1, deps);
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testTwoTasksNoDeps()
|
||||
throws Throwable
|
||||
{
|
||||
Set deps = new HashSet();
|
||||
IUT.addDependencies(T1, deps);
|
||||
IUT.addDependencies(T2, deps);
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testSelfDep()
|
||||
throws Throwable
|
||||
{
|
||||
Set deps = new HashSet();
|
||||
deps.add(T1);
|
||||
|
||||
try
|
||||
{
|
||||
IUT.addDependencies(T1, deps);
|
||||
fail("Self Dependency added without exception.");
|
||||
}
|
||||
catch (CircularDependencyException ex)
|
||||
{}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testTwoCyclicDeps()
|
||||
throws Throwable
|
||||
{
|
||||
Set t1Deps = new HashSet();
|
||||
t1Deps.add(T2);
|
||||
|
||||
Set t2Deps = new HashSet();
|
||||
t2Deps.add(T1);
|
||||
|
||||
IUT.addDependencies(T1, t1Deps);
|
||||
try
|
||||
{
|
||||
IUT.addDependencies(T2, t2Deps);
|
||||
fail("No CircularDependencyException thrown.");
|
||||
}
|
||||
catch (CircularDependencyException ex)
|
||||
{}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testThreeCyclicDeps()
|
||||
throws Throwable
|
||||
{
|
||||
Set t1Deps = new HashSet();
|
||||
t1Deps.add(T2);
|
||||
|
||||
Set t2Deps = new HashSet();
|
||||
t2Deps.add(T3);
|
||||
|
||||
Set t3Deps = new HashSet();
|
||||
t3Deps.add(T1);
|
||||
|
||||
IUT.addDependencies(T1, t1Deps);
|
||||
IUT.addDependencies(T2, t2Deps);
|
||||
|
||||
try
|
||||
{
|
||||
IUT.addDependencies(T3, t3Deps);
|
||||
fail("No CircularDependencyException Thrown.");
|
||||
}
|
||||
catch (CircularDependencyException ex)
|
||||
{}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testSortedDepsEasy()
|
||||
throws Throwable
|
||||
{
|
||||
Set t1Deps = new HashSet();
|
||||
t1Deps.add(T2);
|
||||
|
||||
Set t2Deps = new HashSet();
|
||||
t2Deps.add(T3);
|
||||
|
||||
IUT.addDependencies(T1, t1Deps);
|
||||
IUT.addDependencies(T2, t2Deps);
|
||||
IUT.addDependencies(T3, new HashSet());
|
||||
|
||||
// T3
|
||||
List t3SDeps =
|
||||
IUT.getSortedDependencies(T3);
|
||||
assertEquals("T3: Wrong number of Deps for T3",
|
||||
1, t3SDeps.size());
|
||||
|
||||
assertEquals("T3: Wrong thing at pos 0",
|
||||
T3, t3SDeps.get(0));
|
||||
|
||||
// T2
|
||||
List t2SDeps =
|
||||
IUT.getSortedDependencies(T2);
|
||||
assertEquals("T2: Wrong number of Deps for T2",
|
||||
2, t2SDeps.size());
|
||||
|
||||
try
|
||||
{
|
||||
assertEquals("T2: Wrong thing at pos 0",
|
||||
T3, t2SDeps.get(0));
|
||||
assertEquals("T2: Wrong thing at pos 1",
|
||||
T2, t2SDeps.get(1));
|
||||
}
|
||||
catch (Throwable ex)
|
||||
{
|
||||
System.err.println(t2SDeps);
|
||||
throw ex;
|
||||
}
|
||||
|
||||
// T1
|
||||
List t1SDeps =
|
||||
IUT.getSortedDependencies(T1);
|
||||
assertEquals("T1: Wrong number of Deps for T1",
|
||||
3, t1SDeps.size());
|
||||
|
||||
assertEquals("T1: Wrong thing at pos 0",
|
||||
T3, t1SDeps.get(0));
|
||||
assertEquals("T1: Wrong thing at pos 1",
|
||||
T2, t1SDeps.get(1));
|
||||
assertEquals("T1: Wrong thing at pos 2",
|
||||
T1, t1SDeps.get(2));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A unit test for JUnit
|
||||
*/
|
||||
public void testSortedDepsHard()
|
||||
{
|
||||
Set t1Deps = new HashSet();
|
||||
t1Deps.add(T2);
|
||||
t1Deps.add(T3);
|
||||
t1Deps.add(T5);
|
||||
|
||||
Set t2Deps = new HashSet();
|
||||
t2Deps.add(T4);
|
||||
t2Deps.add(T5);
|
||||
|
||||
IUT.addDependencies(T1, t1Deps);
|
||||
IUT.addDependencies(T2, t2Deps);
|
||||
|
||||
List t1SDeps = IUT.getSortedDependencies(T1);
|
||||
assertEquals("T1: Wrong number of dependents.",
|
||||
5, t1SDeps.size());
|
||||
|
||||
List t2SDeps = IUT.getSortedDependencies(T2);
|
||||
assertEquals("T2: Wrong number of dependents.",
|
||||
3, t2SDeps.size());
|
||||
|
||||
List t3SDeps = IUT.getSortedDependencies(T3);
|
||||
assertEquals("T1: Wrong number of dependents.",
|
||||
1, t3SDeps.size());
|
||||
|
||||
List t4SDeps = IUT.getSortedDependencies(T4);
|
||||
assertEquals("T4: Wrong number of dependents.",
|
||||
1, t4SDeps.size());
|
||||
|
||||
List t5SDeps = IUT.getSortedDependencies(T5);
|
||||
assertEquals("T5: Wrong number of dependents.",
|
||||
1, t5SDeps.size());
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user