package de.unibremen.informatik.hets.graphviz;
/*
import uk.ac.man.cs.mig.util.graph.graph.Edge;
import uk.ac.man.cs.mig.util.graph.graph.Graph;
import uk.ac.man.cs.mig.util.graph.graph.Node;
*/
import java.awt.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* User: matthewhorridge
* The Univeristy Of Manchester
* Medical Informatics Group
* Date: Jan 14, 2004
*
* matthew.horridge@cs.man.ac.uk
* www.cs.man.ac.uk/~horridgm
*
*/
public class Graph
{
private Map nodes;
private Map edges;
private Shape shape;
public Graph()
{
nodes = new HashMap();
edges = new HashMap();
shape = new Rectangle();
}
/**
* Adds a Node
to the graph. If the Graph
* already contains the Node
then the Node
* will not be added.
* @param node The node to be added.
*/
public void add(Node node)
{
if(nodes.containsKey(node) == false)
{
nodes.put(node, node);
}
}
/**
* Removes the specified Node
from the Graph
.
* Note that any edges that have this node as their head node, or tail node
* will also be removed.
* @param node The Node
to be removed.
*/
public void remove(Node node)
{
if(nodes.containsKey(node))
{
// Remove the node
nodes.remove(node);
Iterator it = getEdgeIterator();
Edge edge;
// Remove any edges that are connected to the node.
while(it.hasNext())
{
edge = (Edge)it.next();
if(node.equals(edge.getTailNode()))
{
it.remove();
}
else if(node.equals(edge.getHeadNode()))
{
it.remove();
}
}
}
}
/**
* Adds an Edge
to the graph. Note that the Edge will
* only be added if the nodes that the edge connects are contained
* in the graph.
* @param edge The Edge
to be added.
*/
public void add(Edge edge)
{
if(nodes.containsKey(edge.getTailNode()) && nodes.containsKey(edge.getHeadNode()))
{
EdgeKey key = new EdgeKey(edge);
edges.put(key, edge);
}
}
/**
* Removes an Edge
from the Graph
.
* @param edge The Edge
to be removed.
*/
public void remove(Edge edge)
{
edges.remove(new EdgeKey(edge));
}
/**
* Removes the Edge
that connects the specified
* Nodes
(if the Nodes
are contained
* in the Graph
.
* @param tailNode The Egde
's tail Node
.
* @param headNode The Edge
's head Node
.
*/
public void remove(Node tailNode, Node headNode)
{
edges.remove(new EdgeKey(tailNode, headNode));
}
/**
* Removes all the edges and nodes from the graph.
*/
public void removeAll()
{
edges = new HashMap();
nodes = new HashMap();
}
/**
* Returns the number of Nodes
that the Graph
* contains.
* @return The number of Nodes
.
*/
public int getNodeCount()
{
return nodes.size();
}
/**
* Returns the number of Edges
that the Graph
* contains.
* @return The number of Edges
.
*/
public int getEdgeCount()
{
return edges.size();
}
/**
* Retrieves an Iterator
that can be
* used to traverse the Nodes
in the Graph
.
* @return The Iterator
.
*/
public Iterator getNodeIterator()
{
return nodes.keySet().iterator();
}
/**
* Retrieves an Iterator
that can be
* used to traverse the Edges
in the Graph
.
* @return The Iterator
.
*/
public Iterator getEdgeIterator()
{
return edges.values().iterator();
}
/**
* Gets the shape that isShown all of the Nodes
and
* Edges
belonging to the Graph
.
* @return A shape that describes the Graphs
bounds.
*/
public Shape getShape()
{
return shape;
}
/**
* Sets the shape that describes the Graph
bounds.
* @param shape The Shape
bounding the graph.
*/
public void setShape(Shape shape)
{
this.shape = shape;
}
/**
* Determines if the Graph
isShown the specified Node
.
* @param node The Node
* @return true
if the Graph
isShown the Node
* or false
if the Graph
does not contain the Node
.
*/
public boolean contains(Node node)
{
return nodes.containsKey(node);
}
/**
* Determines if the Graph
isShown the specified Edge
.
* @param edge The Edge
.
* @return true
if the Graph
isShown the Edge
* or false
if the Graph
does not contain the Edge
.
*/
public boolean contains(Edge edge)
{
return edges.containsKey(new EdgeKey(edge));
}
/**
* Gets the Nodes
contained in the Graph
* as an array.
* @return An array containing the Nodes
in the Graph
.
*/
public Node[] getNodes()
{
Object [] array = nodes.keySet().toArray();
Node [] nodeArray = new Node [array.length];
System.arraycopy(array, 0, nodeArray, 0, array.length);
return nodeArray;
}
/**
* Gets the Edges
contained in the Graph
as an array.
* @return An array containing the Edges
in the Graph
.
*/
public Edge[] getEdges()
{
Object [] array = edges.values().toArray();
Edge [] edgeArray = new Edge [array.length];
System.arraycopy(array, 0, edgeArray, 0, array.length);
return edgeArray;
}
public class EdgeKey
{
private Node tailNode;
private Node headNode;
public EdgeKey(Edge edge)
{
this.tailNode = edge.getTailNode();
this.headNode = edge.getHeadNode();
}
public EdgeKey(Node tailNode, Node headNode)
{
this.tailNode = tailNode;
this.headNode = headNode;
}
public int hashCode()
{
return tailNode.hashCode() * 13 + headNode.hashCode() * 37;
}
public boolean equals(Object obj)
{
if(obj.getClass().equals(this.getClass()) == false)
{
return false;
}
EdgeKey key = (EdgeKey)obj;
if(key.tailNode.equals(tailNode) && key.headNode.equals(headNode))
{
return true;
}
else
{
return false;
}
}
}
}