@@ -32,7 +32,7 @@ abstract class AbstractGraph { |
32 | 32 | * A HashMap of the Graph nodes with the Node's ID as the key - used for quicker,easier and safer algorithmic use of Node data |
33 | 33 | */ |
34 | 34 | @JsonIgnore |
35 | | - protected HashMap<Integer, Node> nodes; |
| 35 | + protected HashMap<String, Node> nodes; |
36 | 36 |
|
37 | 37 |
|
38 | 38 | /** |
@@ -69,7 +69,7 @@ public AbstractGraph() { |
69 | 69 | * @param input The Graph's input/sentence. |
70 | 70 | * @param edges An array list of the Graph's edges. |
71 | 71 | */ |
72 | | - public AbstractGraph(String id, String source, String input, HashMap<Integer, Node> nodes, ArrayList<Edge> edges, String top) { |
| 72 | + public AbstractGraph(String id, String source, String input, HashMap<String, Node> nodes, ArrayList<Edge> edges, String top) { |
73 | 73 | this.id = id; |
74 | 74 | this.source = source; |
75 | 75 | this.input = input; |
@@ -96,7 +96,7 @@ public AbstractGraph(String id, String source, String input, ArrayList<Node> nod |
96 | 96 | this.source = source; |
97 | 97 | this.input = input; |
98 | 98 | this.nodelist = nodelist; |
99 | | - this.nodes = new HashMap<Integer, Node>(); |
| 99 | + this.nodes = new HashMap<String, Node>(); |
100 | 100 | for (Node n : nodelist) { |
101 | 101 | this.nodes.put(n.getId(), n); |
102 | 102 | } |
@@ -217,13 +217,101 @@ public void setTops(String top) { |
217 | 217 | this.top = top; |
218 | 218 | } |
219 | 219 |
|
| 220 | + /** |
| 221 | + * Getter method for the Graph's nodes HashMap. |
| 222 | + * |
| 223 | + * @return HashMap<Integer, Node> The Graph's nodes HashMap. |
| 224 | + */ |
| 225 | + public HashMap<String, Node> getNodes() { |
| 226 | + return nodes; |
| 227 | + } |
| 228 | + |
| 229 | + /** |
| 230 | + * Setter method for the Graph's nodes HashMap. This setter also resets and populates the linear list of nodes "nodeslist" |
| 231 | + * |
| 232 | + * @param nodes The Graph's nodes HashMap. |
| 233 | + */ |
| 234 | + public void setNodes(HashMap<String, Node> nodes) { |
| 235 | + this.nodelist.clear(); |
| 236 | + this.nodes = nodes; |
| 237 | + for (Node n : nodes.values()) { |
| 238 | + this.nodelist.add(n); |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + /** |
| 243 | + * This method checks to see if a Graph has a dangling Edge i.e an Edge that doesnt connect to any Node |
| 244 | + * |
| 245 | + * @return boolean returns true if the Graph has a dangling Edge otherwise returns false. |
| 246 | + */ |
| 247 | + public boolean hasDanglingEdge() { |
| 248 | + for (Edge e : edges) { |
| 249 | + if (!nodes.containsKey(e.getTarget()) || !nodes.containsKey(e.getSource())) { |
| 250 | + return true; |
| 251 | + } |
| 252 | + } |
| 253 | + return false; |
| 254 | + } |
| 255 | + |
| 256 | + /** |
| 257 | + * Assigns all the nodes in the Graph their directed and undirected neighbouring nodes, which will be used for analysis. |
| 258 | + */ |
| 259 | + public void setNodeNeighbours() { |
| 260 | + String source; |
| 261 | + String target; |
| 262 | + |
| 263 | + if (edges.size() == 0) { |
| 264 | + //Graph has no edges |
| 265 | + return; |
| 266 | + } else { |
| 267 | + |
| 268 | + source = edges.get(0).getSource(); |
| 269 | + |
| 270 | + if (!nodes.containsKey(source) || nodes.get(source).getDirectedNeighbours().size() != 0) { |
| 271 | + //Node neighbours have already been set |
| 272 | + return; |
| 273 | + } |
| 274 | + } |
| 275 | + |
| 276 | + //Iterate through each Edge and set the corresponding Node's thier neighbours. |
| 277 | + for (int i = 0; i < edges.size(); i++) { |
| 278 | + Edge currentEdge = edges.get(i); |
| 279 | + |
| 280 | + source = currentEdge.getSource(); |
| 281 | + target = currentEdge.getTarget(); |
| 282 | + |
| 283 | + Node sourceNode = nodes.get(source); |
| 284 | + sourceNode.addDirectedNeighbour(nodes.get(target)); |
| 285 | + nodes.get(target).addUndirectedNeighbour(sourceNode); |
| 286 | + |
| 287 | + sourceNode.addDirectedEdgeNeighbour(currentEdge); |
| 288 | + nodes.get(target).addUndirectedEdgeNeighbour(currentEdge); |
| 289 | + |
| 290 | + } |
| 291 | + |
| 292 | + } |
| 293 | + |
| 294 | + /** |
| 295 | + * Combines the directed and undirected Node neighbours of a given Node. |
| 296 | + * |
| 297 | + * @param nodeID The ID of the Node. |
| 298 | + * @return ArrayList<Node> List of the Node's directed and undirected neighbours. |
| 299 | + */ |
| 300 | + public ArrayList<Node> combineNeighbours(String nodeID) { |
| 301 | + ArrayList<Node> allNeighbours = new ArrayList<>(nodes.get(nodeID).getDirectedNeighbours()); |
| 302 | + ArrayList<Node> undirectedNeighbours = new ArrayList<>(nodes.get(nodeID).getUndirectedNeighbours()); |
| 303 | + //Adds all a Node's undirected neighbours and its directed neighbours together. |
| 304 | + allNeighbours.addAll(undirectedNeighbours); |
| 305 | + return allNeighbours; |
| 306 | + } |
| 307 | + |
220 | 308 | /** |
221 | 309 | * Analysis Tool for finding the longest paths in the Graph. |
222 | 310 | * |
223 | 311 | * @param directed This is the boolean to decide if the longest path found is directed or undirected. |
224 | 312 | * @return ArrayList<ArrayList < Integer>> The a list of longest paths in the Graph. |
225 | 313 | */ |
226 | | - public abstract ArrayList<ArrayList<Integer>> findLongest(boolean directed); |
| 314 | + public abstract ArrayList<ArrayList<String>> findLongest(boolean directed); |
227 | 315 |
|
228 | 316 |
|
229 | 317 | /** |
|