2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.system.graph;
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
23 * Represents a graph consisting of nodes and edges.
25 * @author Erik Brakkee
28 private List<Node> nodes;
30 private List<Edge> edges;
33 * Constructs the graph.
36 nodes = new ArrayList<Node>();
37 edges = new ArrayList<Edge>();
46 * @throws IllegalArgumentException
47 * In case the node already exists. Node equality is checked
48 * using <code>equals</code>.
50 public void addNode(Node aNode) {
51 if (nodes.contains(aNode)) {
52 throw new IllegalArgumentException("Node '" + aNode.getName() +
60 * Finds a node with the given name.
65 * @return Node or null if not found.
67 public Node findNode(String aName) {
68 for (Node node : nodes) {
69 if (node.getName().equals(aName)) {
83 * @return True iff the node was removed.
85 * @throws IllegalArgumentException
86 * In case there are edges of which the node is a part.
88 public boolean removeNode(Node aNode) {
89 if (!findOutgoing(aNode).isEmpty() || !findIncoming(aNode).isEmpty()) {
90 throw new IllegalArgumentException("Cannot remove node '" +
92 "' because it is connected to one or more edges");
95 return nodes.remove(aNode);
99 * Adds a list of nodes.
104 * @see #addNode(Node)
106 public void addNodes(List<Node> aNodes) {
107 for (Node node : aNodes) {
118 * @throws IllegalArgumentException
119 * In case one of the nodes of the edges is not part of the
120 * graph or if the same edge (as determined by
121 * {@link #equals(Object)} is already a part of the graph.
123 public void addEdge(Edge aEdge) {
124 if (edges.contains(aEdge)) {
125 throw new IllegalArgumentException("Edge '" + aEdge +
129 if (!nodes.contains(aEdge.getFrom())) {
130 throw new IllegalArgumentException("From node '" + aEdge.getFrom() +
131 "' from edge '" + aEdge + "' is not part of the graph");
134 if (!nodes.contains(aEdge.getTo())) {
135 throw new IllegalArgumentException("To node '" + aEdge.getTo() +
136 "' from edge '" + aEdge + "' is not part of the graph");
148 * @return True if the edge was removed.
150 public boolean removeEdge(Edge aEdge) {
151 return edges.remove(aEdge);
155 * Adds a number of edges.
160 public void addEdges(List<Edge> aEdges) {
161 for (Edge edge : aEdges) {
169 * @return Copy of the list of nodes of the graph.
171 public List<Node> getNodes() {
172 return new ArrayList<Node>(nodes);
178 * @return Copy of the list of edges of the graph.
180 public List<Edge> getEdges() {
181 return new ArrayList<Edge>(edges);
185 * Extends the graph with edges using an edge factory. All combinations of
186 * nodes are passed to the factory which creates additional edges.
191 public void extend(EdgeFactory aFactory) {
192 for (Node from : nodes) {
193 for (Node to : nodes) {
194 edges.addAll(aFactory.create(from, to));
200 * Applies a filter to the graph for removing elements.
205 public void applyFilter(EdgeFilter aFilter) {
206 for (Iterator<Edge> edge = edges.iterator(); edge.hasNext();) {
207 if (aFilter.isViolated(edge.next())) {
214 * Finds all outgoing edges of a node. More specifically, finds all edges
215 * <code>e</code> for which <code>e.getFrom().getName() =
216 * aNode.getName()</code>.
219 * Node for which to find outgoing edges.
221 * @return List of outgoing edges.
223 public List<Edge> findOutgoing(Node aNode) {
224 List<Edge> result = new ArrayList<Edge>();
226 for (Edge edge : edges) {
227 if (edge.getFrom().getName().equals(aNode.getName())) {
236 * Finds all incoming edges of a node. More specifically, finds all edges
237 * <code>e</code> for which <code>e.getTo().getName() =
238 * aNode.getName()</code>.
241 * Node for which to find incoming edges.
243 * @return List of incoming edges.
245 public List<Edge> findIncoming(Node aNode) {
246 List<Edge> result = new ArrayList<Edge>();
248 for (Edge edge : edges) {
249 if (edge.getTo().getName().equals(aNode.getName())) {
258 * Implements a visitor design pattern. This loops over all nodes and all
259 * edges and invokes the appropriate visit methods on the visitor.
264 public void accept(Visitor aVisitor) {
265 List<Node> allNodes = getNodes(); // copy to make sure the visitor can
267 // list of nodes as part of the loop.
269 List<Edge> allEdges = getEdges(); // copy ..... (see above).
271 for (Node node : allNodes) {
272 aVisitor.visitNode(node);
275 for (Edge edge : allEdges) {
276 aVisitor.visitEdge(edge);