Impl has moved under the domain/basic package.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/sandbox/graph2/trunk@144612 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
ddp committed 2002-05-08 17:56:25 +00:00
1 parent 29c751760b
commit e180946494
9 files changed
-1770

No files matched your search

@@ -1,393 +0,0 @@
package org.apache.commons.graph.impl;
/* ====================================================================
* 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/>.
*/
/**
* AllPairs solves the All Points Shortest Path problem for a DirectedGraph. (If
* it is weighted, it will do shortest path by weight. Otherwise shortest path
* by jumps.) Uses Floyd's Algorithm.
*/
import java.util.Set;
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.AbstractList;
import org.apache.commons.graph.*;
import org.apache.commons.graph.exception.*;
/**
* Description of the Class
*/
public class AllPaths
{
private int pred[][];
private double cost[][];
private Vertex vArray[];
private DirectedGraph graph;
private Map vertexIndex = new HashMap();// VERTEX X INTEGER
/**
* Description of the Class
*/
public class EdgeList
extends AbstractList
{
private DirectedGraph graph;
private List vertices;
/**
* Constructor for the EdgeList object
*
* @param graph
* @param vertices
*/
public EdgeList(DirectedGraph graph,
List vertices)
{
this.graph = graph;
this.vertices = vertices;
}
/**
* Description of the Method
*/
public int size()
{
return vertices.size() - 1;
}
/**
* Description of the Method
*/
public Object get(int index)
{
Edge RC = null;
Vertex source = (Vertex) vertices.get(index);
Vertex target = (Vertex) vertices.get(index + 1);
Set outboundSet = graph.getOutbound(source);
if (outboundSet == null)
{
return null;
}
Iterator outbound = outboundSet.iterator();
while (outbound.hasNext())
{
RC = (Edge) outbound.next();
if (graph.getTarget(RC) == target)
{
break;
}
}
if (graph.getTarget(RC) != target)
{
return null;
}
return RC;
}
}
/**
* Description of the Class
*/
public class WPath
implements WeightedPath
{
private Vertex start;
private Vertex finish;
private List vertexList = new ArrayList();
private DirectedGraph graph;
private double cost;
/**
* Constructor for the WPath object
*
* @param graph
* @param vArray
* @param pred
* @param start
* @param finish
* @param cost
* @exception GraphException
*/
public WPath(DirectedGraph graph,
Vertex vArray[], int pred[][],
int start, int finish, double cost)
throws GraphException
{
this.start = vArray[start];
this.finish = vArray[finish];
this.cost = cost;
this.graph = graph;
vertexList.addAll(segment(vArray, pred,
start, finish));
vertexList.add(vArray[finish]);
}
/**
* Returns a List of Vectors in order. Includes the start, but not the
* finish.
*/
private List segment(Vertex vArray[], int pred[][],
int start, int finish)
throws GraphException
{
int mid = pred[start][finish];
if (mid == -1)
{
throw new NoPathException("No SubPath Available: " +
vArray[start] + " -> " +
vArray[finish]);
}
List RC = new ArrayList();
if (start == finish)
{
return RC;
}
if (start == mid)
{
RC.add(vArray[start]);
}
else
{
RC.addAll(segment(vArray, pred,
start, mid));
RC.add(vArray[mid]);
}
if (mid == pred[mid][finish])
{
}
else
{
RC.addAll(segment(vArray, pred,
mid, finish));
}
return RC;
}
/**
* Gets the weight attribute of the WPath object
*/
public double getWeight()
{
return cost;
}
/**
* Gets the vertices attribute of the WPath object
*/
public List getVertices()
{
return vertexList;
}
/**
* Gets the edges attribute of the WPath object
*/
public List getEdges()
{
return new EdgeList(graph, vertexList);
}
/**
* Gets the start attribute of the WPath object
*/
public Vertex getStart()
{
return start;
}
/**
* Gets the end attribute of the WPath object
*/
public Vertex getEnd()
{
return finish;
}
}
/**
* Constructor for the AllPaths object
*
* @param graph
* @exception NegativeCycleException
*/
public AllPaths(DirectedGraph graph)
throws NegativeCycleException
{
update(graph);
}
/**
* Description of the Method
*/
private void initIndex(Vertex vArray[])
{
for (int i = 0; i < vArray.length; i++)
{
vertexIndex.put(vArray[i], new Integer(i));
}
}
/**
* Description of the Method
*/
public void update(DirectedGraph graph)
{
this.graph = graph;
Set vertexSet = graph.getVertices();
vArray = (Vertex[]) vertexSet.toArray(new Vertex[vertexSet.size()]);
initIndex(vArray);
pred = new int[vArray.length][vArray.length];
cost = new double[vArray.length][vArray.length];
for (int i = 0; i < vArray.length; i++)
{
for (int j = 0; j < vArray.length; j++)
{
pred[i][j] = -1;
cost[i][j] = Double.POSITIVE_INFINITY;
}
// First round values need to be in the matrix.
Iterator edgeSet = graph.getOutbound(vArray[i]).iterator();
while (edgeSet.hasNext())
{
Edge e = (Edge) edgeSet.next();
int j = index(graph.getTarget(e));
pred[i][j] = i;
if (graph instanceof WeightedGraph)
{
cost[i][j] = ((WeightedGraph) graph).getWeight(e);
}
else
{
cost[i][j] = 1.0;
}
}
// Cost from any node to itself is 0.0
cost[i][i] = 0.0;
pred[i][i] = i;
}
compute(graph, vArray);
}
/**
* Description of the Method
*/
private int index(Vertex v)
{
return ((Integer) vertexIndex.get(v)).intValue();
}
/**
* Description of the Method
*/
private void compute(DirectedGraph graph, Vertex vArray[])
throws NegativeCycleException
{
for (int k = 0; k < vArray.length; k++)
{// Mid Point
for (int i = 0; i < vArray.length; i++)
{// Source
for (int j = 0; j < vArray.length; j++)
{// Target
if (cost[i][k] + cost[k][j] < cost[i][j])
{
if (i == j)
{
throw new NegativeCycleException();
}
// It is cheaper to go through K.
cost[i][j] = cost[i][k] + cost[k][j];
pred[i][j] = k;
}
}
}
}
}
/**
* Gets the shortestPath attribute of the AllPaths object
*/
public WeightedPath getShortestPath(Vertex start, Vertex end)
throws GraphException
{
return new WPath(graph, vArray, pred,
index(start), index(end),
cost[index(start)][index(end)]);
}
}
@@ -1,96 +0,0 @@
package org.apache.commons.graph.impl;
/* ====================================================================
* 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 org.apache.commons.graph.*;
import org.apache.commons.graph.exception.*;
/**
* Description of the Class
*/
public class DirectedAcyclicGraphImpl
extends DirectedGraphImpl
{
/**
* Constructor for the DirectedAcyclicGraphImpl object
*/
public DirectedAcyclicGraphImpl() { }
/**
* Description of the Method
*/
public boolean connected(Vertex start, Vertex end)
{
return false;
}
/**
* Adds a feature to the Edge attribute of the DirectedAcyclicGraphImpl
* object
*/
public void addEdge(Edge e,
Vertex start,
Vertex end)
throws GraphException
{
if (connected(end, start))
{
// We are going to introduce a cycle.
throw new CycleException("Adding Edge will add a Cycle.");
}
super.addEdge(e, start, end);
}
}
@@ -1,464 +0,0 @@
package org.apache.commons.graph.impl;
/* ====================================================================
* 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.lang.reflect.*;
import java.util.Set;
import java.util.Map;
import java.util.List;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ArrayList;
import org.apache.commons.graph.*;
import org.apache.commons.graph.contract.*;
import org.apache.commons.graph.exception.*;
/**
* Description of the Class
*/
public class DirectedGraphImpl
implements DirectedGraph,
WeightedGraph,
MutableDirectedGraph,
InvocationHandler
{
private Set vertices = new HashSet();
private Set edges = new HashSet();
private List contracts = new ArrayList();
private Map inbound = new HashMap();// VERTEX X SET( EDGE )
private Map outbound = new HashMap();// - " " -
private Map edgeSource = new HashMap();// EDGE X VERTEX
private Map edgeTarget = new HashMap();// EDGE X TARGET
private Map edgeWeights = new HashMap();// EDGE X WEIGHT
/**
* Constructor for the DirectedGraphImpl object
*/
public DirectedGraphImpl() { }
/**
* Constructor for the DirectedGraphImpl object
*
* @param dg
*/
public DirectedGraphImpl(DirectedGraph dg)
{
Iterator v = dg.getVertices().iterator();
while (v.hasNext())
{
addVertexI((Vertex) v.next());
}
Iterator e = dg.getEdges().iterator();
while (e.hasNext())
{
Edge edge = (Edge) e.next();
addEdgeI(edge,
dg.getSource(edge),
dg.getTarget(edge));
if (dg instanceof WeightedGraph)
{
setWeight(edge, ((WeightedGraph) dg).getWeight(edge));
}
}
}
/**
* Adds a feature to the Contract attribute of the DirectedGraphImpl object
*/
public void addContract(Contract c)
throws GraphException
{
c.setImpl(this);
c.verify();
contracts.add(c);
}
/**
* Description of the Method
*/
public void removeContract(Contract c)
{
contracts.remove(c);
}
/**
* Sets the weight attribute of the DirectedGraphImpl object
*/
public void setWeight(Edge e, double value)
{
if (edgeWeights.containsKey(e))
{
edgeWeights.remove(e);
}
edgeWeights.put(e, new Double(value));
}
// Interface Methods
// Graph
/**
* Gets the vertices attribute of the DirectedGraphImpl object
*/
public Set getVertices()
{
return new HashSet(vertices);
}
/**
* Gets the vertices attribute of the DirectedGraphImpl object
*/
public Set getVertices(Edge e)
{
Set RC = new HashSet();
if (edgeSource.containsKey(e))
{
RC.add(edgeSource.get(e));
}
if (edgeTarget.containsKey(e))
{
RC.add(edgeTarget.get(e));
}
return RC;
}
/**
* Gets the edges attribute of the DirectedGraphImpl object
*/
public Set getEdges()
{
return new HashSet(edges);
}
/**
* Gets the edges attribute of the DirectedGraphImpl object
*/
public Set getEdges(Vertex v)
{
Set RC = new HashSet();
if (inbound.containsKey(v))
{
RC.addAll((Set) inbound.get(v));
}
if (outbound.containsKey(v))
{
RC.addAll((Set) outbound.get(v));
}
return RC;
}
// Directed Graph
/**
* Gets the source attribute of the DirectedGraphImpl object
*/
public Vertex getSource(Edge e)
{
return (Vertex) edgeSource.get(e);
}
/**
* Gets the target attribute of the DirectedGraphImpl object
*/
public Vertex getTarget(Edge e)
{
return (Vertex) edgeTarget.get(e);
}
/**
* Gets the inbound attribute of the DirectedGraphImpl object
*/
public Set getInbound(Vertex v)
{
if (inbound.containsKey(v))
{
return new HashSet((Set) inbound.get(v));
}
else
{
return new HashSet();
}
}
/**
* Gets the outbound attribute of the DirectedGraphImpl object
*/
public Set getOutbound(Vertex v)
{
if (outbound.containsKey(v))
{
return new HashSet((Set) outbound.get(v));
}
else
{
return new HashSet();
}
}
// MutableDirectedGraph
/**
* Adds a feature to the VertexI attribute of the DirectedGraphImpl object
*/
private void addVertexI(Vertex v)
throws GraphException
{
vertices.add(v);
}
/**
* Adds a feature to the Vertex attribute of the DirectedGraphImpl object
*/
public void addVertex(Vertex v)
throws GraphException
{
Iterator conts = contracts.iterator();
while (conts.hasNext())
{
((Contract) conts.next()).addVertex(v);
}
addVertexI(v);
}
/**
* Description of the Method
*/
private void removeVertexI(Vertex v)
throws GraphException
{
try
{
vertices.remove(v);
}
catch (Exception ex)
{
throw new GraphException(ex);
}
}
/**
* Description of the Method
*/
public void removeVertex(Vertex v)
throws GraphException
{
Iterator conts = contracts.iterator();
while (conts.hasNext())
{
((Contract) conts.next()).removeVertex(v);
}
removeVertexI(v);
}
/**
* Adds a feature to the EdgeI attribute of the DirectedGraphImpl object
*/
private void addEdgeI(Edge e, Vertex start, Vertex end)
throws GraphException
{
edges.add(e);
if (e instanceof WeightedEdge)
{
edgeWeights.put(e, new Double(((WeightedEdge) e).getWeight()));
}
else
{
edgeWeights.put(e, new Double(1.0));
}
edgeSource.put(e, start);
edgeTarget.put(e, end);
if (!outbound.containsKey(start))
{
Set edgeSet = new HashSet();
edgeSet.add(e);
outbound.put(start, edgeSet);
}
else
{
((Set) outbound.get(start)).add(e);
}
if (!inbound.containsKey(end))
{
Set edgeSet = new HashSet();
edgeSet.add(e);
inbound.put(end, edgeSet);
}
else
{
((Set) inbound.get(end)).add(e);
}
}
/**
* Adds a feature to the Edge attribute of the DirectedGraphImpl object
*/
public void addEdge(Edge e,
Vertex start,
Vertex end)
throws GraphException
{
try
{
Iterator conts = contracts.iterator();
while (conts.hasNext())
{
((Contract) conts.next()).addEdge(e, start, end);
}
}
catch (GraphException ex)
{
throw ex;
}
addEdgeI(e, start, end);
}
/**
* Description of the Method
*/
private void removeEdgeI(Edge e)
throws GraphException
{
try
{
Set edgeSet = null;
Vertex source = (Vertex) edgeSource.get(e);
edgeSource.remove(e);
edgeSet = (Set) outbound.get(e);
edgeSet.remove(e);
Vertex target = (Vertex) edgeTarget.get(e);
edgeTarget.remove(e);
edgeSet = (Set) inbound.get(e);
edgeSet.remove(e);
if (edgeWeights.containsKey(e))
{
edgeWeights.remove(e);
}
}
catch (Exception ex)
{
throw new GraphException(ex);
}
}
/**
* Description of the Method
*/
public void removeEdge(Edge e)
throws GraphException
{
Iterator conts = contracts.iterator();
while (conts.hasNext())
{
((Contract) conts.next()).removeEdge(e);
}
removeEdgeI(e);
}
// WeightedGraph
/**
* Gets the weight attribute of the DirectedGraphImpl object
*/
public double getWeight(Edge e)
{
if (edgeWeights.containsKey(e))
{
return ((Double) edgeWeights.get(e)).doubleValue();
}
else
{
return 1.0;
}
}
/**
* Description of the Method
*/
public Object invoke(Object proxy,
Method method,
Object args[])
throws Throwable
{
try
{
return method.invoke(this, args);
}
catch (InvocationTargetException ex)
{
throw ex.getTargetException();
}
}
}
@@ -1,135 +0,0 @@
package org.apache.commons.graph.impl;
/* ====================================================================
* 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/>.
*/
/**
* DirectedGraphWrapper This is a superclass to all wrappers that work over
* DirectedGraphs.
*/
import java.util.Set;
import org.apache.commons.graph.*;
/**
* Description of the Class
*/
public class DirectedGraphWrapper
extends GraphWrapper
implements DirectedGraph
{
private DirectedGraph impl = null;
/**
* Constructor for the DirectedGraphWrapper object
*
* @param graph
*/
public DirectedGraphWrapper(DirectedGraph graph)
{
super(graph);
impl = graph;
}
/**
* Constructor for the DirectedGraphWrapper object
*/
public DirectedGraphWrapper()
{
super();
}
/**
* Sets the dirGraph attribute of the DirectedGraphWrapper object
*/
public void setDirGraph(DirectedGraph graph)
{
impl = graph;
setGraph(graph);
}
/**
* Gets the inbound attribute of the DirectedGraphWrapper object
*/
public Set getInbound(Vertex v)
{
return impl.getInbound(v);
}
/**
* Gets the outbound attribute of the DirectedGraphWrapper object
*/
public Set getOutbound(Vertex v)
{
return impl.getOutbound(v);
}
/**
* Gets the source attribute of the DirectedGraphWrapper object
*/
public Vertex getSource(Edge e)
{
return impl.getSource(e);
}
/**
* Gets the target attribute of the DirectedGraphWrapper object
*/
public Vertex getTarget(Edge e)
{
return impl.getTarget(e);
}
}
@@ -1,190 +0,0 @@
package org.apache.commons.graph.impl;
/* ====================================================================
* 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.lang.reflect.*;
import org.apache.commons.graph.*;
import org.apache.commons.graph.contract.*;
import org.apache.commons.graph.exception.*;
/**
* Description of the Class
*/
public class GraphFactory
{
/**
* Constructor for the GraphFactory object
*/
public GraphFactory() { }
/**
* makeGraph
*
* @param contracts Which contracts to enforce.
* @param baseGraph Is the actual *GraphImpl which will be at the core of
* the Proxy.
* @param baseGraphType Interface which is returned.
* @param isWeighted Does the graph handle Weights?
* @param init Initialization Graph.
*/
private Object makeGraph(Contract contracts[],
InvocationHandler baseGraph,
Class baseGraphType,
boolean isWeighted)
throws GraphException
{
int interfaceCount = contracts.length;
interfaceCount++;// BaseGraph Type
if (isWeighted)
{
interfaceCount++;
}// WeightedGraph Type
Class inter[] = new Class[interfaceCount];
int pos = 0;
for (pos = 0; pos < contracts.length; pos++)
{
inter[pos] = contracts[pos].getInterface();
}
if (isWeighted)
{
inter[pos] = org.apache.commons.graph.WeightedGraph.class;
pos++;
}
inter[pos] = baseGraphType;
return Proxy.newProxyInstance(baseGraph.getClass().getClassLoader(),
inter, baseGraph);
}
/**
* makeDirectedGraph
*
* @param contracts - Array of Contracts this Graph should meet.
* @param isWeighted - If true, the Graph will implement the WeightedGraph
* interface.
* @param graph - If it is provided, the graph will initially be equal to
* the graph.
*/
public DirectedGraph makeDirectedGraph(Contract contracts[],
boolean isWeighted,
DirectedGraph graph)
throws GraphException
{
DirectedGraphImpl dgi = null;
if (graph != null)
{
dgi = new DirectedGraphImpl(graph);
}
else
{
dgi = new DirectedGraphImpl();
}
for (int i = 0; i < contracts.length; i++)
{
dgi.addContract(contracts[i]);
}
return (DirectedGraph)
makeGraph(contracts,
dgi, org.apache.commons.graph.DirectedGraph.class,
isWeighted);
}
/**
* makeMutableDirectedGraph
*
* @param contracts - Array of Contracts this Graph should meet.
* @param isWeighted - If true, the Graph will implement the WeightedGraph
* interface.
* @param graph - If it is provided, the graph will initially be equal to
* the graph.
*/
public MutableDirectedGraph
makeMutableDirectedGraph(Contract contracts[],
boolean isWeighted,
DirectedGraph graph)
throws GraphException
{
DirectedGraphImpl dgi = null;
if (graph != null)
{
dgi = new DirectedGraphImpl(graph);
}
else
{
dgi = new DirectedGraphImpl();
}
for (int i = 0; i < contracts.length; i++)
{
dgi.addContract(contracts[i]);
}
return (MutableDirectedGraph)
makeGraph(contracts,
dgi,
org.apache.commons.graph.MutableDirectedGraph.class,
isWeighted);
}
}
@@ -1,74 +0,0 @@
package org.apache.commons.graph.impl;
/**
* GraphWrapper This is a superclass of all Wrapper implementations. It
* basically does a redirection to the graph.
*/
import java.util.Set;
import org.apache.commons.graph.*;
/**
* Description of the Class
*/
public class GraphWrapper
{
private Graph impl = null;
/**
* Constructor for the GraphWrapper object
*
* @param impl
*/
public GraphWrapper(Graph impl)
{
this.impl = impl;
}
/**
* Constructor for the GraphWrapper object
*/
public GraphWrapper() { }
/**
* Sets the graph attribute of the GraphWrapper object
*/
public void setGraph(Graph impl)
{
this.impl = impl;
}
// Graph Implementation. . .
/**
* Gets the vertices attribute of the GraphWrapper object
*/
public Set getVertices()
{
return impl.getVertices();
}
/**
* Gets the edges attribute of the GraphWrapper object
*/
public Set getEdges()
{
return impl.getEdges();
}
/**
* Gets the vertices attribute of the GraphWrapper object
*/
public Set getVertices(Edge e)
{
return impl.getVertices(e);
}
/**
* Gets the edges attribute of the GraphWrapper object
*/
public Set getEdges(Vertex v)
{
return impl.getEdges(v);
}
}
@@ -1,128 +0,0 @@
package org.apache.commons.graph.impl;
import java.util.Map;
import java.util.Set;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import org.apache.commons.graph.*;
import org.apache.commons.graph.exception.*;
/**
* Description of the Class
*/
public class SingleSourcePaths
{
private WeightedGraph weights = null;
private Map pred = new HashMap();
private Map weight = new HashMap();
/**
* Description of the Class
*/
public class WeightedVertex
implements Comparable
{
private Vertex v;
private double value;
/**
* Constructor for the WeightedVertex object
*
* @param v
* @param value
*/
public WeightedVertex(Vertex v, double value)
{
this.v = v;
this.value = value;
}
/**
* Description of the Method
*/
public void relax(double value)
{
this.value = value;
}
/**
* Description of the Method
*/
public int compareTo(Object o)
{
WeightedVertex wv = (WeightedVertex) o;
if (wv.value > this.value)
{
return 1;
}
if (wv.value == this.value)
{
return 0;
}
if (wv.value < this.value)
{
return -1;
}
return 0;
}
}
/**
* Constructor for the SingleSourcePaths object
*
* @param graph
* @param root
* @exception GraphException
*/
public SingleSourcePaths(WeightedGraph graph,
Vertex root)
throws GraphException
{
this.weights = graph;
if (graph instanceof DirectedGraph)
{
init((DirectedGraph) graph);
}
else
{
init((Graph) graph);
}
}
/**
* Description of the Method
*/
public void init(DirectedGraph graph,
Vertex root)
throws GraphException
{
Iterator i = graph.getVertices().iterator();
while (i.hasNext())
{
Vertex v = (Vertex) i.next();
if (v != root)
{
weight.put(v, new Double(Double.POSITIVE_INFINITY));
}
else
{
weight.put(v, new Double(0));
}
}
i = graph.getVertices().iterator();
}
/**
* Description of the Method
*/
public void init(Graph graph)
throws GraphException
{
throw new GraphException("Not Supported");
}
}
@@ -1,182 +0,0 @@
package org.apache.commons.graph.impl;
/* ====================================================================
* 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.*;
/**
* Description of the Class
*/
public class UndirectedGraphImpl
implements UndirectedGraph
{
private Set vertices = new HashSet();
private Set edges = new HashSet();
private Map edgeVerts = new HashMap();// EDGE X SET( VERTS )
private Map vertEdges = new HashMap();// VERTEX X SET( EDGE )
/**
* Constructor for the UndirectedGraphImpl object
*/
public UndirectedGraphImpl() { }
/**
* Adds a feature to the Vertex attribute of the UndirectedGraphImpl object
*/
public void addVertex(Vertex v)
throws GraphException
{
vertices.add(v);
}
/**
* Adds a feature to the Edge attribute of the UndirectedGraphImpl object
*/
public void addEdge(Edge e,
Set vertices)
throws GraphException
{
edges.add(e);
// Set up Edge -> Vertices
Set edgeVert = null;
if (!edgeVerts.containsKey(e))
{
edgeVert = new HashSet();
edgeVerts.put(e, edgeVert);
}
else
{
edgeVert = (Set) edgeVerts.get(e);
}
edgeVert.addAll(vertices);
// Setup Vertex -> Edges
Iterator i = vertices.iterator();
while (i.hasNext())
{
Vertex v = (Vertex) i.next();
if (!vertEdges.containsKey(v))
{
Set edgeSet = new HashSet();
edgeSet.add(e);
vertEdges.put(v, edgeSet);
}
else
{
((Set) vertEdges.get(v)).add(e);
}
}
}
// Interface Methods
/**
* Gets the vertices attribute of the UndirectedGraphImpl object
*/
public Set getVertices()
{
return new HashSet(vertices);
}
/**
* Gets the vertices attribute of the UndirectedGraphImpl object
*/
public Set getVertices(Edge e)
{
if (edgeVerts.containsKey(e))
{
return new HashSet((Set) edgeVerts.get(e));
}
else
{
return new HashSet();
}
}
/**
* Gets the edges attribute of the UndirectedGraphImpl object
*/
public Set getEdges()
{
return new HashSet(edges);
}
/**
* Gets the edges attribute of the UndirectedGraphImpl object
*/
public Set getEdges(Vertex v)
{
if (vertEdges.containsKey(v))
{
return new HashSet((Set) vertEdges.get(v));
}
else
{
return new HashSet();
}
}
}
@@ -1,108 +0,0 @@
package org.apache.commons.graph.impl;
/* ====================================================================
* 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 simple wrapper to wrap around a graph, and create a weighted graph.
*/
import java.util.Map;
import java.util.HashMap;
import org.apache.commons.graph.*;
/**
* Description of the Class
*/
public class WeightedGraphWrapper
extends GraphWrapper
implements WeightedGraph
{
private Map weights = new HashMap();// EDGE X WEIGHT
/**
* Constructor for the WeightedGraphWrapper object
*
* @param graph
*/
public WeightedGraphWrapper(Graph graph)
{
super(graph);
}
/**
* Gets the weight attribute of the WeightedGraphWrapper object
*/
public double getWeight(Edge e)
{
if (weights.containsKey(e))
{
return ((Double) weights.get(e)).doubleValue();
}
else
{
return 1.0;
}
}
/**
* Sets the weight attribute of the WeightedGraphWrapper object
*/
public void setWeight(Edge e, double weight)
{
weights.put(e, new Double(weight));
}
}