Initial revision
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/sandbox/graph2/trunk@144595 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
1 parent
e65f14b050
commit
30be5ce736
65 files changed
+10080
No files matched your search
@@ -0,0 +1,64 @@
|
||||
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 AcyclicGraph
|
||||
extends Graph
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
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 DirectedAcyclicGraph
|
||||
extends Graph, AcyclicGraph
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
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.Set;
|
||||
|
||||
/**
|
||||
* Description of the Interface
|
||||
*/
|
||||
public interface DirectedGraph
|
||||
extends Graph
|
||||
{
|
||||
/**
|
||||
* getInbound( Vertex ) Returns the set of edges which are inbound to the
|
||||
* Vertex.
|
||||
*/
|
||||
public Set getInbound(Vertex v);
|
||||
|
||||
/**
|
||||
* getOutbound( Vertex ) Returns the set of edges which lead away from the
|
||||
* Vertex.
|
||||
*/
|
||||
public Set getOutbound(Vertex v);
|
||||
|
||||
/**
|
||||
* getSource( Edge ) Returns the vertex which originates the edge.
|
||||
*/
|
||||
public Vertex getSource(Edge e);
|
||||
|
||||
/**
|
||||
* getTarget( Edge ) Returns the vertex which terminates the edge.
|
||||
*/
|
||||
public Vertex getTarget(Edge e);
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
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 Edge
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
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/>.
|
||||
*/
|
||||
/**
|
||||
* Graph This is the basic interface for a graph. G = { v, e }
|
||||
* getAdjacentVertices and getAdjacentEdges helps to define the behavior of
|
||||
* Edges.
|
||||
*/
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Description of the Interface
|
||||
*/
|
||||
public interface Graph
|
||||
{
|
||||
/**
|
||||
* getVertices - Returns the total set of Vertices in the graph.
|
||||
*/
|
||||
public Set getVertices();
|
||||
|
||||
/**
|
||||
* getEdges - Returns the total set of Edges in the graph.
|
||||
*/
|
||||
public Set getEdges();
|
||||
|
||||
/**
|
||||
* getEdges( Vertex ) - This method will return all edges which touch this
|
||||
* vertex.
|
||||
*/
|
||||
public Set getEdges(Vertex v);
|
||||
|
||||
/**
|
||||
* getVertices( Edge ) - This method will return the set of Verticies on
|
||||
* this Edge. (2 for normal edges, > 2 for HyperEdges.)
|
||||
*/
|
||||
public Set getVertices(Edge e);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
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 org.apache.commons.graph.exception.*;
|
||||
|
||||
/**
|
||||
* Description of the Interface
|
||||
*/
|
||||
public interface MutableDirectedGraph
|
||||
extends DirectedGraph
|
||||
{
|
||||
/**
|
||||
* Adds a feature to the Vertex attribute of the MutableDirectedGraph object
|
||||
*/
|
||||
public void addVertex(Vertex v)
|
||||
throws GraphException;
|
||||
|
||||
/**
|
||||
* Adds a feature to the Edge attribute of the MutableDirectedGraph object
|
||||
*/
|
||||
public void addEdge(Edge e,
|
||||
Vertex source,
|
||||
Vertex target)
|
||||
throws GraphException;
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void removeVertex(Vertex v)
|
||||
throws GraphException;
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void removeEdge(Edge e)
|
||||
throws GraphException;
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
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 org.apache.commons.graph.exception.*;
|
||||
|
||||
/**
|
||||
* Description of the Interface
|
||||
*/
|
||||
public interface MutableGraph
|
||||
extends Graph
|
||||
{
|
||||
/**
|
||||
* Adds a feature to the Vertex attribute of the MutableGraph object
|
||||
*/
|
||||
public void addVertex(Vertex v)
|
||||
throws GraphException;
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void removeVertex(Vertex v)
|
||||
throws GraphException;
|
||||
|
||||
/**
|
||||
* Adds a feature to the Edge attribute of the MutableGraph object
|
||||
*/
|
||||
public void addEdge(Edge e)
|
||||
throws GraphException;
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void removeEdge(Edge e)
|
||||
throws GraphException;
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void connect(Edge e, Vertex v)
|
||||
throws GraphException;
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void disconnect(Edge e, Vertex v)
|
||||
throws GraphException;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.apache.commons.graph;
|
||||
|
||||
/**
|
||||
* Description of the Interface
|
||||
*/
|
||||
public interface Named
|
||||
{
|
||||
/**
|
||||
* Gets the name attribute of the Named object
|
||||
*/
|
||||
public String getName();
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
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.List;
|
||||
|
||||
/**
|
||||
* Description of the Interface
|
||||
*/
|
||||
public interface Path
|
||||
{
|
||||
/**
|
||||
* Returns the start of the path.
|
||||
*/
|
||||
public Vertex getStart();
|
||||
|
||||
/**
|
||||
* Returns the end of the path.
|
||||
*/
|
||||
public Vertex getEnd();
|
||||
|
||||
/**
|
||||
* getVertices() - This returns a list of Vertices, in order as they go from
|
||||
* Start to End. This includes the Start and End vertex, and will have one
|
||||
* more entry than the Edges list.
|
||||
*/
|
||||
public List getVertices();
|
||||
|
||||
/**
|
||||
* getEdges() - This returns a list of Edges which comprise the path. It
|
||||
* will have one less than the list of Vertices.
|
||||
*/
|
||||
public List getEdges();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
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 UndirectedGraph
|
||||
extends Graph
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
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 Vertex
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
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 WeightedEdge
|
||||
extends Edge
|
||||
{
|
||||
/**
|
||||
* Gets the weight attribute of the WeightedEdge object
|
||||
*/
|
||||
public double getWeight();
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
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 WeightedPath
|
||||
extends Path
|
||||
{
|
||||
/**
|
||||
* Gets the weight attribute of the WeightedPath object
|
||||
*/
|
||||
public double getWeight();
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.apache.commons.graph.contract;
|
||||
|
||||
/**
|
||||
* Description of the Interface
|
||||
*/
|
||||
public interface Acyclic
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
package org.apache.commons.graph.contract;
|
||||
|
||||
/* ====================================================================
|
||||
* 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.Iterator;
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
import org.apache.commons.graph.search.*;
|
||||
import org.apache.commons.graph.exception.*;
|
||||
import org.apache.commons.graph.decorator.*;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class AcyclicContract
|
||||
implements Contract
|
||||
{
|
||||
private DDirectedGraph graph = null;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class CycleDetector
|
||||
implements Visitor
|
||||
{
|
||||
private DFS dfs = null;
|
||||
private boolean isCyclic = false;
|
||||
private DirectedGraph graph = null;
|
||||
|
||||
/**
|
||||
* Constructor for the CycleDetector object
|
||||
*
|
||||
* @param graph
|
||||
*/
|
||||
public CycleDetector(DirectedGraph graph)
|
||||
{
|
||||
this.dfs = new DFS();
|
||||
this.graph = graph;
|
||||
Iterator verts = graph.getVertices().iterator();
|
||||
|
||||
if (verts.hasNext())
|
||||
{
|
||||
dfs.visit(graph, (Vertex) verts.next(), this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void discoverGraph(Graph graph) { }
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void discoverVertex(Vertex v) { }
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void discoverEdge(Edge e)
|
||||
{
|
||||
if (dfs.getColor(graph.getTarget(e)) == DFS.GRAY)
|
||||
{
|
||||
this.isCyclic = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void finishEdge(Edge e) { }
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void finishVertex(Vertex v) { }
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void finishGraph(Graph graph) { }
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public boolean hasCycle()
|
||||
{
|
||||
return isCyclic;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the AcyclicContract object
|
||||
*/
|
||||
public AcyclicContract() { }
|
||||
|
||||
/**
|
||||
* Sets the impl attribute of the AcyclicContract object
|
||||
*/
|
||||
public void setImpl(DirectedGraph graph)
|
||||
{
|
||||
this.graph = DDirectedGraph.decorateGraph(graph);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the interface attribute of the AcyclicContract object
|
||||
*/
|
||||
public Class getInterface()
|
||||
{
|
||||
return org.apache.commons.graph.contract.Acyclic.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void verify()
|
||||
throws CycleException
|
||||
{
|
||||
CycleDetector cd = new CycleDetector(graph);
|
||||
if (cd.hasCycle())
|
||||
{
|
||||
throw new CycleException("Cycle detected in Graph.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a feature to the Vertex attribute of the AcyclicContract object
|
||||
*/
|
||||
public void addVertex(Vertex v) { }
|
||||
|
||||
/**
|
||||
* Adds a feature to the Edge attribute of the AcyclicContract object
|
||||
*/
|
||||
public void addEdge(Edge e,
|
||||
Vertex start,
|
||||
Vertex end)
|
||||
throws GraphException
|
||||
{
|
||||
if (graph.hasConnection(end, start))
|
||||
{
|
||||
throw new CycleException("Introducing edge will cause a Cycle.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void removeVertex(Vertex v) { }
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void removeEdge(Edge e) { }
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package org.apache.commons.graph.contract;
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
import org.apache.commons.graph.exception.*;
|
||||
|
||||
/**
|
||||
* Description of the Interface
|
||||
*/
|
||||
public interface Contract
|
||||
{
|
||||
/**
|
||||
* The impl that gets passed in is read-only. This is the representation of
|
||||
* the graph you should work off of. If an edge or vertex addition is
|
||||
* illegal to the contract, raise a GraphException with and explanation.
|
||||
*/
|
||||
public void setImpl(DirectedGraph impl);
|
||||
|
||||
/**
|
||||
* getInterface This returns the marker interface which is associated with
|
||||
* the Contract. For instance, AcyclicContract will return AcyclicGraph
|
||||
* here.
|
||||
*/
|
||||
public Class getInterface();
|
||||
|
||||
/**
|
||||
* verify - This verifies that the graph it is working on complies.
|
||||
*/
|
||||
public void verify()
|
||||
throws GraphException;
|
||||
|
||||
/**
|
||||
* Adds a feature to the Edge attribute of the Contract object
|
||||
*/
|
||||
public void addEdge(Edge e,
|
||||
Vertex start,
|
||||
Vertex end)
|
||||
throws GraphException;
|
||||
|
||||
/**
|
||||
* Adds a feature to the Vertex attribute of the Contract object
|
||||
*/
|
||||
public void addVertex(Vertex v)
|
||||
throws GraphException;
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void removeEdge(Edge e)
|
||||
throws GraphException;
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void removeVertex(Vertex v)
|
||||
throws GraphException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
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.impl.*;
|
||||
import org.apache.commons.graph.exception.*;
|
||||
|
||||
/**
|
||||
* 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 AllPaths allPaths = null;
|
||||
|
||||
/**
|
||||
* Constructor for the DDirectedGraph object
|
||||
*
|
||||
* @param impl
|
||||
*/
|
||||
private 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 AllPaths(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
allPaths.update(this);
|
||||
}
|
||||
|
||||
WeightedPath path =
|
||||
allPaths.getShortestPath(start, end);
|
||||
}
|
||||
catch (GraphException ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.apache.commons.graph.dependency;
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class Dependency implements Edge
|
||||
{
|
||||
private Object head = null;
|
||||
private Object dep = null;
|
||||
|
||||
/**
|
||||
* Constructor for the Dependency object
|
||||
*
|
||||
* @param head
|
||||
* @param dep
|
||||
*/
|
||||
public Dependency(Object head,
|
||||
Object dep)
|
||||
{
|
||||
this.head = head;
|
||||
this.dep = dep;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public String description()
|
||||
{
|
||||
return head + " depends on " + dep;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package org.apache.commons.graph.dependency;
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
import org.apache.commons.graph.impl.*;
|
||||
import org.apache.commons.graph.contract.*;
|
||||
import org.apache.commons.graph.exception.*;
|
||||
import org.apache.commons.graph.dependency.exception.*;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.List;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class DependencyGraph
|
||||
extends DirectedGraphWrapper
|
||||
{
|
||||
private GraphFactory factory = new GraphFactory();
|
||||
private AcyclicContract acyclic = new AcyclicContract();
|
||||
private DependencyVisitor visitor = new DependencyVisitor();
|
||||
|
||||
private Map vertices = new HashMap();
|
||||
|
||||
private MutableDirectedGraph DAG = null;
|
||||
|
||||
/**
|
||||
* Constructor for the DependencyGraph object
|
||||
*/
|
||||
public DependencyGraph()
|
||||
{
|
||||
super();
|
||||
init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
private void init()
|
||||
{
|
||||
Contract[] dagContracts = new Contract[1];
|
||||
dagContracts[0] = acyclic;
|
||||
DAG = factory.makeMutableDirectedGraph(dagContracts,
|
||||
false,
|
||||
null);
|
||||
setDirGraph(DAG);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a feature to the Dependencies attribute of the DependencyGraph
|
||||
* object
|
||||
*/
|
||||
public void addDependencies(Object head,
|
||||
Collection deps)
|
||||
throws GraphException,
|
||||
CircularDependencyException
|
||||
{
|
||||
DependencyVertex vHead = findVertex(head);
|
||||
|
||||
if (!getVertices().contains(vHead))
|
||||
{
|
||||
DAG.addVertex(vHead);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Iterator v = deps.iterator();
|
||||
|
||||
while (v.hasNext())
|
||||
{
|
||||
DependencyVertex vDep = findVertex(v.next());
|
||||
|
||||
if (!getVertices().contains(vDep))
|
||||
{
|
||||
DAG.addVertex(vDep);
|
||||
}
|
||||
|
||||
DAG.addEdge(new Dependency(vHead.getValue(),
|
||||
vDep.getValue()),
|
||||
vHead, vDep);
|
||||
}
|
||||
}
|
||||
catch (CycleException ex)
|
||||
{
|
||||
throw new CircularDependencyException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public DependencyVertex findVertex(Object o)
|
||||
{
|
||||
if (vertices.containsKey(o))
|
||||
{
|
||||
return (DependencyVertex) vertices.get(o);
|
||||
}
|
||||
else
|
||||
{
|
||||
DependencyVertex RC = new DependencyVertex(o);
|
||||
vertices.put(o, RC);
|
||||
return RC;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the sortedDependencies attribute of the DependencyGraph object
|
||||
*/
|
||||
public List getSortedDependencies(Object head)
|
||||
{
|
||||
return visitor.getSortedDependencies(this, findVertex(head));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.apache.commons.graph.dependency;
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class DependencyVertex
|
||||
implements Vertex
|
||||
{
|
||||
private Object value;
|
||||
|
||||
/**
|
||||
* Constructor for the DependencyVertex object
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
public DependencyVertex(Object value)
|
||||
{
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value attribute of the DependencyVertex object
|
||||
*/
|
||||
public Object getValue()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package org.apache.commons.graph.dependency;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
import org.apache.commons.graph.search.*;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class DependencyVisitor
|
||||
implements Visitor
|
||||
{
|
||||
private List deps = null;
|
||||
private DFS dfs = new DFS();
|
||||
|
||||
|
||||
/**
|
||||
* Constructor for the DependencyVisitor object
|
||||
*/
|
||||
public DependencyVisitor() { }
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void discoverGraph(Graph g) { }
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void discoverVertex(Vertex v) { }
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void discoverEdge(Edge e) { }
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void finishGraph(Graph g) { }
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void finishVertex(Vertex v)
|
||||
{
|
||||
if (v instanceof DependencyVertex)
|
||||
{
|
||||
deps.add(((DependencyVertex) v).getValue());
|
||||
}
|
||||
else
|
||||
{
|
||||
deps.add(v);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void finishEdge(Edge e) { }
|
||||
|
||||
/**
|
||||
* Gets the sortedDependencies attribute of the DependencyVisitor object
|
||||
*/
|
||||
public synchronized List
|
||||
getSortedDependencies(DependencyGraph dg,
|
||||
Vertex root)
|
||||
{
|
||||
deps = new LinkedList();
|
||||
dfs.visit(dg, root, this);
|
||||
return deps;
|
||||
}
|
||||
}
|
||||
|
||||
+38
@@ -0,0 +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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.apache.commons.graph.exception;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class ContractVerificationException extends GraphException
|
||||
{
|
||||
/**
|
||||
* Constructor for the ContractVerificationException object
|
||||
*/
|
||||
public ContractVerificationException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the ContractVerificationException object
|
||||
*
|
||||
* @param msg
|
||||
*/
|
||||
public ContractVerificationException(String msg)
|
||||
{
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the ContractVerificationException object
|
||||
*
|
||||
* @param cause
|
||||
*/
|
||||
public ContractVerificationException(Throwable cause)
|
||||
{
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package org.apache.commons.graph.exception;
|
||||
/* ====================================================================
|
||||
* 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 Class
|
||||
*/
|
||||
public class CycleException extends GraphException
|
||||
{
|
||||
/**
|
||||
* Constructor for the CycleException object
|
||||
*/
|
||||
public CycleException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the CycleException object
|
||||
*
|
||||
* @param msg
|
||||
*/
|
||||
public CycleException(String msg)
|
||||
{
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the CycleException object
|
||||
*
|
||||
* @param cause
|
||||
*/
|
||||
public CycleException(Throwable cause)
|
||||
{
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package org.apache.commons.graph.exception;
|
||||
|
||||
/* ====================================================================
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* GraphException This is the superclass of all exceptions that can be thrown.
|
||||
*/
|
||||
|
||||
public class GraphException extends RuntimeException
|
||||
{
|
||||
private Throwable cause = null;
|
||||
|
||||
/**
|
||||
* Constructor for the GraphException object
|
||||
*/
|
||||
public GraphException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the GraphException object
|
||||
*
|
||||
* @param msg
|
||||
*/
|
||||
public GraphException(String msg)
|
||||
{
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the GraphException object
|
||||
*
|
||||
* @param cause
|
||||
*/
|
||||
public GraphException(Throwable cause)
|
||||
{
|
||||
super(cause.getMessage());
|
||||
this.cause = cause;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package org.apache.commons.graph.exception;
|
||||
|
||||
/* ====================================================================
|
||||
* 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 Class
|
||||
*/
|
||||
public class NegativeCycleException extends GraphException
|
||||
{
|
||||
/**
|
||||
* Constructor for the NegativeCycleException object
|
||||
*/
|
||||
public NegativeCycleException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the NegativeCycleException object
|
||||
*
|
||||
* @param msg
|
||||
*/
|
||||
public NegativeCycleException(String msg)
|
||||
{
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the NegativeCycleException object
|
||||
*
|
||||
* @param cause
|
||||
*/
|
||||
public NegativeCycleException(Throwable cause)
|
||||
{
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package org.apache.commons.graph.exception;
|
||||
|
||||
/* ====================================================================
|
||||
* 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 Class
|
||||
*/
|
||||
public class NoPathException
|
||||
extends GraphException
|
||||
{
|
||||
/**
|
||||
* Constructor for the NoPathException object
|
||||
*/
|
||||
public NoPathException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the NoPathException object
|
||||
*
|
||||
* @param msg
|
||||
*/
|
||||
public NoPathException(String msg)
|
||||
{
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the NoPathException object
|
||||
*
|
||||
* @param t
|
||||
*/
|
||||
public NoPathException(Throwable t)
|
||||
{
|
||||
super(t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,393 @@
|
||||
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)]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,467 @@
|
||||
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)
|
||||
{
|
||||
//!! David can fix this, this needs to run on 1.3
|
||||
return null;
|
||||
//throw ex.getCause();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,291 @@
|
||||
package org.apache.commons.graph.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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
package org.apache.commons.graph.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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
package org.apache.commons.graph.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/>.
|
||||
*/
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
|
||||
/**
|
||||
* Description of the Interface
|
||||
*/
|
||||
public interface Visitor
|
||||
{
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void discoverGraph(Graph graph);
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void discoverVertex(Vertex vertex);
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void discoverEdge(Edge edge);
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void finishEdge(Edge edge);
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void finishVertex(Vertex vertex);
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void finishGraph(Graph graph);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.apache.commons.graph.statemachine;
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class State
|
||||
implements Vertex, Named
|
||||
{
|
||||
private String name;
|
||||
private StateMachine subMachine = null;
|
||||
|
||||
/**
|
||||
* Constructor for the State object
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public State(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name attribute of the State object
|
||||
*/
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the submachine attribute of the State object
|
||||
*/
|
||||
public void setSubmachine(StateMachine subMachine)
|
||||
{
|
||||
this.subMachine = subMachine;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the submachine attribute of the State object
|
||||
*/
|
||||
public StateMachine getSubmachine()
|
||||
{
|
||||
return this.subMachine;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package org.apache.commons.graph.statemachine;
|
||||
|
||||
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.impl.*;
|
||||
import org.apache.commons.graph.exception.*;
|
||||
import org.apache.commons.graph.statemachine.exception.*;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class StateMachine
|
||||
extends DirectedGraphImpl
|
||||
{
|
||||
private Map states = new HashMap();// NAME X STATE
|
||||
private Map transes = new HashMap();// NAME X TRANSITION
|
||||
private Set finalStates = new HashSet();
|
||||
private State startState = null;
|
||||
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Constructor for the StateMachine object
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public StateMachine(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name attribute of the StateMachine object
|
||||
*/
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a feature to the State attribute of the StateMachine object
|
||||
*/
|
||||
public void addState(State state)
|
||||
throws GraphException
|
||||
{
|
||||
states.put(state.getName(), state);
|
||||
addVertex(state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a feature to the State attribute of the StateMachine object
|
||||
*/
|
||||
public void addState(String name)
|
||||
throws GraphException
|
||||
{
|
||||
State newState = new State(name);
|
||||
addState(new State(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the startState attribute of the StateMachine object
|
||||
*/
|
||||
public void setStartState(State state)
|
||||
{
|
||||
startState = state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the finalState attribute of the StateMachine object
|
||||
*/
|
||||
public void setFinalState(State state)
|
||||
{
|
||||
finalStates.add(state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a feature to the Transition attribute of the StateMachine object
|
||||
*/
|
||||
public void addTransition(String name,
|
||||
String source,
|
||||
String target)
|
||||
throws GraphException
|
||||
{
|
||||
addTransition(name,
|
||||
getState(source),
|
||||
getState(target));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a feature to the Transition attribute of the StateMachine object
|
||||
*/
|
||||
public void addTransition(String name,
|
||||
State source,
|
||||
State target)
|
||||
throws GraphException
|
||||
{
|
||||
Transition trans = new Transition(name, source, target);
|
||||
addTransition(trans);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a feature to the Transition attribute of the StateMachine object
|
||||
*/
|
||||
public void addTransition(Transition trans)
|
||||
throws GraphException
|
||||
{
|
||||
transes.put(trans.getName(), trans);
|
||||
addEdge(trans, trans.getSource(), trans.getTarget());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the state attribute of the StateMachine object
|
||||
*/
|
||||
public State getState(String name)
|
||||
{
|
||||
return (State) states.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the transition attribute of the StateMachine object
|
||||
*/
|
||||
public Transition getTransition(String name)
|
||||
{
|
||||
return (Transition) transes.get(name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package org.apache.commons.graph.statemachine;
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class Transition
|
||||
implements Edge, Named
|
||||
{
|
||||
private String name;
|
||||
private State source;
|
||||
private State target;
|
||||
|
||||
private String action = null;
|
||||
private String guard = null;
|
||||
private String output = null;
|
||||
|
||||
/**
|
||||
* Description of the Field
|
||||
*/
|
||||
public final static String EPSILON = "\u03B5";
|
||||
|
||||
/**
|
||||
* Constructor for the Transition object
|
||||
*
|
||||
* @param name
|
||||
* @param source
|
||||
* @param target
|
||||
*/
|
||||
public Transition(String name,
|
||||
State source,
|
||||
State target)
|
||||
{
|
||||
this.name = name;
|
||||
this.source = source;
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name attribute of the Transition object
|
||||
*/
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the source attribute of the Transition object
|
||||
*/
|
||||
public State getSource()
|
||||
{
|
||||
return source;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the target attribute of the Transition object
|
||||
*/
|
||||
public State getTarget()
|
||||
{
|
||||
return target;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the action attribute of the Transition object
|
||||
*/
|
||||
public void setAction()
|
||||
{
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the action attribute of the Transition object
|
||||
*/
|
||||
public String getAction()
|
||||
{
|
||||
return action;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the guard attribute of the Transition object
|
||||
*/
|
||||
public void setGuard(String guard)
|
||||
{
|
||||
this.guard = guard;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the guard attribute of the Transition object
|
||||
*/
|
||||
public String getGuard()
|
||||
{
|
||||
return guard;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the output attribute of the Transition object
|
||||
*/
|
||||
public String getOutput()
|
||||
{
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the output attribute of the Transition object
|
||||
*/
|
||||
public void setOutput(String output)
|
||||
{
|
||||
this.output = output;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package org.apache.commons.graph.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.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.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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
package org.apache.commons.graph.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.uml.exception.*;
|
||||
|
||||
import org.apache.log4j.Category;
|
||||
|
||||
import org.apache.commons.graph.exception.*;
|
||||
import org.apache.commons.graph.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.*;
|
||||
|
||||
import org.apache.commons.graph.uml.exception.*;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class StateMachineFactory
|
||||
{
|
||||
private static Category log =
|
||||
Category.getInstance(org.apache.commons.graph.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.setFinalState(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 event = trans.getTrigger();
|
||||
String trigger = null;
|
||||
|
||||
if (event != null)
|
||||
{
|
||||
trigger = event.getName();
|
||||
}
|
||||
else
|
||||
{
|
||||
trigger = 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);
|
||||
|
||||
Transition tranx =
|
||||
new Transition(source + "-" + target + "/" + trigger,
|
||||
source, target);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.apache.commons.graph.uml.exception;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class ModelNotFoundException extends Exception
|
||||
{
|
||||
private Throwable cause = null;
|
||||
|
||||
/**
|
||||
* Constructor for the ModelNotFoundException object
|
||||
*/
|
||||
public ModelNotFoundException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the ModelNotFoundException object
|
||||
*
|
||||
* @param msg
|
||||
*/
|
||||
public ModelNotFoundException(String msg)
|
||||
{
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the ModelNotFoundException object
|
||||
*
|
||||
* @param cause
|
||||
*/
|
||||
public ModelNotFoundException(Throwable cause)
|
||||
{
|
||||
super(cause.getMessage());
|
||||
this.cause = cause;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
package org.apache.commons.graph.visualize;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.Iterator;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
import org.apache.commons.graph.*;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*/
|
||||
public class TouchGraph
|
||||
{
|
||||
private Color vertexColor = Color.yellow;
|
||||
private Color textColor = Color.black;
|
||||
private Color edgeColor = Color.red;
|
||||
|
||||
private double lengthFactor = 500.0;
|
||||
private double defaultLength = 1.0;
|
||||
private int fontSize = 18;
|
||||
|
||||
private Random random = new Random();
|
||||
|
||||
/**
|
||||
* Constructor for the TouchGraph object
|
||||
*/
|
||||
public TouchGraph() { }
|
||||
|
||||
/**
|
||||
* Gets the colorText attribute of the TouchGraph object
|
||||
*/
|
||||
private String getColorText(Color color)
|
||||
{
|
||||
return Integer.toHexString(color.getRGB()).toUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the vertexColor attribute of the TouchGraph object
|
||||
*/
|
||||
public void setVertexColor(Color vertexColor)
|
||||
{
|
||||
this.vertexColor = vertexColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the vertexColor attribute of the TouchGraph object
|
||||
*/
|
||||
public Color getVertexColor()
|
||||
{
|
||||
return this.vertexColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the vertexColorText attribute of the TouchGraph object
|
||||
*/
|
||||
private String getVertexColorText()
|
||||
{
|
||||
return getColorText(getVertexColor());
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the textColor attribute of the TouchGraph object
|
||||
*/
|
||||
public void setTextColor(Color vertexColor)
|
||||
{
|
||||
this.vertexColor = vertexColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the textColor attribute of the TouchGraph object
|
||||
*/
|
||||
public Color getTextColor()
|
||||
{
|
||||
return this.vertexColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the textColorText attribute of the TouchGraph object
|
||||
*/
|
||||
private String getTextColorText()
|
||||
{
|
||||
return getColorText(getTextColor());
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the edgeColor attribute of the TouchGraph object
|
||||
*/
|
||||
public void setEdgeColor(Color edgeColor)
|
||||
{
|
||||
this.edgeColor = edgeColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the edgeColor attribute of the TouchGraph object
|
||||
*/
|
||||
public Color getEdgeColor()
|
||||
{
|
||||
return this.edgeColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the edgeColorText attribute of the TouchGraph object
|
||||
*/
|
||||
private String getEdgeColorText()
|
||||
{
|
||||
return getColorText(getEdgeColor());
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the fontSize attribute of the TouchGraph object
|
||||
*/
|
||||
public void setFontSize(int size)
|
||||
{
|
||||
this.fontSize = size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the fontSize attribute of the TouchGraph object
|
||||
*/
|
||||
public int getFontSize()
|
||||
{
|
||||
return fontSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the defaultEdgeLength attribute of the TouchGraph object
|
||||
*/
|
||||
public void setDefaultEdgeLength(int length)
|
||||
{
|
||||
this.defaultLength = length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the defaultEdgeLength attribute of the TouchGraph object
|
||||
*/
|
||||
public double getDefaultEdgeLength()
|
||||
{
|
||||
return defaultLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
protected void writeNodeset(PrintWriter pw,
|
||||
DirectedGraph graph)
|
||||
{
|
||||
pw.println("<NODESET>");
|
||||
Iterator vertices =
|
||||
graph.getVertices().iterator();
|
||||
while (vertices.hasNext())
|
||||
{
|
||||
Vertex v = (Vertex) vertices.next();
|
||||
|
||||
pw.println("<NODE nodeID=\"" + v.toString() + "\">");
|
||||
pw.println("<NODE_LOCATION x=\"" + random.nextInt(25) +
|
||||
"\" y = \"" + random.nextInt(25) +
|
||||
"\" visible=\"true\" />");
|
||||
|
||||
String label;
|
||||
if (v instanceof Named)
|
||||
{
|
||||
label = ((Named) v).getName();
|
||||
}
|
||||
else
|
||||
{
|
||||
label = v.toString();
|
||||
}
|
||||
|
||||
pw.println("<NODE_LABEL label=\"" + label + "\" " +
|
||||
"shape=\"2\" " +
|
||||
"backColor=\"" + getVertexColorText() + "\" " +
|
||||
"textColor=\"" + getTextColorText() + "\" " +
|
||||
"fontSize=\"" + fontSize + "\" />");
|
||||
|
||||
pw.println("</NODE>");
|
||||
}
|
||||
|
||||
pw.println("</NODESET>");
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
protected void writeEdgeset(PrintWriter pw, DirectedGraph graph)
|
||||
{
|
||||
pw.println("<EDGESET>");
|
||||
|
||||
Iterator edges = graph.getEdges().iterator();
|
||||
while (edges.hasNext())
|
||||
{
|
||||
Edge next = (Edge) edges.next();
|
||||
|
||||
int length = new Double(lengthFactor *
|
||||
defaultLength).intValue();
|
||||
|
||||
if (graph instanceof WeightedGraph)
|
||||
{
|
||||
length =
|
||||
new Double(lengthFactor *
|
||||
((WeightedGraph) graph)
|
||||
.getWeight(next)).intValue();
|
||||
}
|
||||
|
||||
pw.println("<EDGE fromID=\"" + graph.getSource(next) + "\" " +
|
||||
"toID=\"" + graph.getTarget(next) + "\" " +
|
||||
"type=\"2\" " +
|
||||
"visible=\"true\" " +
|
||||
"length=\"" + length + "\" />");
|
||||
|
||||
}
|
||||
pw.println("</EDGESET>");
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the Method
|
||||
*/
|
||||
public void toXML(DirectedGraph graph,
|
||||
OutputStream os)
|
||||
{
|
||||
PrintWriter pw = new PrintWriter(os);
|
||||
pw.println("<?xml version=\"1.0\"?>");
|
||||
pw.println("<TOUCHGRAPH_LB version=\"1.20\">");
|
||||
writeNodeset(pw, graph);
|
||||
writeEdgeset(pw, graph);
|
||||
pw.println("</TOUCHGRAPH_LB>");
|
||||
pw.flush();
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user