public String getGraphCommands() { String commands = ""; for (Node aNode : nodeList) { commands += ("node " + aNode.getName() + "\n"); } for (DirectedArc dirArc : dirArcList) { commands += ("dir-arc " + dirArc.getParent().getName() + " " + dirArc.getChild().getName() + " " + dirArc.getRelation() + "\n"); } for (UndirectedArc undirArc : undirArcList) { Node[] nodes = undirArc.getNodes(); commands += ("undir-arc " + nodes[0].getName() + " " + nodes[1].getName() + " " + undirArc.getRelation() + "\n"); } return commands; }
/** * Tests whether a directed arc exists between two nodes in the graph * * @param parent parent node * @param child child node * @return true if arc exists */ public boolean dirArcExists(Node parent, Node child) { for (DirectedArc dirArc : dirArcList) { if (dirArc.getParent().equals(parent) && dirArc.getChild().equals(child)) { return true; } } return false; }
/** * Gets directed arc between two nodes in the graph * * @param parent parent node * @param child child node * @return Directed arc between nodes if arc exists * @throws Exception if arc does not exist */ public DirectedArc getDirArc(Node parent, Node child) throws Exception { for (DirectedArc dirArc : dirArcList) { if (dirArc.getParent().equals(parent) && dirArc.getChild().equals(child)) { return dirArc; } } throw new Exception("Directed arc does not exist between the two nodes"); }
/** * Executes the "Printout" command * * @param commandList the command along with the necessary parameters specified by the user */ public void executePrintout(ArrayList<String> commandList) throws Exception { if (commandList.size() != 2 && commandList.size() != 3) { throw new Exception( "\nInvalid arguments, code not executed" + "\nCorrect Syntax: \nprintout node_name" + "\nprintout node1_name node2_name\n"); } if (commandList.size() == 2) { Node node = getNode(commandList.get(1)); System.out.println(node.toString()); // Also printout all the arcs the node is connected to // I kept it because it helped in debugging for (UndirectedArc undirArc : node.getArcs()) { System.out.println(undirArc.toString()); } for (DirectedArc dirArc : node.getInArcs()) { System.out.println(dirArc.toString()); } for (DirectedArc dirArc : node.getOutArcs()) { System.out.println(dirArc.toString()); } } else { Node node1 = getNode(commandList.get(1)); Node node2 = getNode(commandList.get(2)); Boolean arcExists = false; try { System.out.println(getDirArc(node1, node2).toString()); arcExists = true; } catch (Exception e) { } try { System.out.println(getDirArc(node2, node1).toString()); arcExists = true; } catch (Exception e) { } try { System.out.println(getUndirArc(node1, node2).toString()); arcExists = true; } catch (Exception e) { } if (!arcExists) { System.out.println("No arcs exist between node"); } } }
/** * Executes the "Arcs" command * * @param commandList the command along with the necessary parameters specified by the user */ public void executeArcs(ArrayList<String> commandList) throws Exception { if (commandList.size() != 1) { throw new Exception("\nInvalid arguments, code not executed" + "\nCorrect Syntax: \narcs\n"); } HashSet<DirectedArc> dirArcList = getDirectedArcs(); HashSet<UndirectedArc> undirArcList = getUndirectedArcs(); for (DirectedArc dirArc : dirArcList) { System.out.println(dirArc.toString()); } for (UndirectedArc undirArc : undirArcList) { System.out.println(undirArc.toString()); } }
/** * Removes a directed arc from the graph * * @param arc directed arc */ public void removeArc(DirectedArc arc) { dirArcList.remove(arc); arc.clear(); notifyArcDeletion(arc); }