001package hw4.test; 002 003import java.io.*; 004import java.util.*; 005 006import hw4.*; 007 008 009/** 010 * This class implements a testing driver which reads test scripts 011 * from files for testing Graph. 012 **/ 013public class HW4TestDriver { 014 015 public static void main(String args[]) { 016 try { 017 if (args.length > 1) { 018 printUsage(); 019 return; 020 } 021 022 HW4TestDriver td; 023 024 if (args.length == 0) { 025 td = new HW4TestDriver(new InputStreamReader(System.in), 026 new OutputStreamWriter(System.out)); 027 } else { 028 029 String fileName = args[0]; 030 File tests = new File (fileName); 031 032 if (tests.exists() || tests.canRead()) { 033 td = new HW4TestDriver(new FileReader(tests), 034 new OutputStreamWriter(System.out)); 035 } else { 036 System.err.println("Cannot read from " + tests.toString()); 037 printUsage(); 038 return; 039 } 040 } 041 042 td.runTests(); 043 044 } catch (IOException e) { 045 System.err.println(e.toString()); 046 e.printStackTrace(System.err); 047 } 048 } 049 050 private static void printUsage() { 051 System.err.println("Usage:"); 052 System.err.println("to read from a file: java hw4.test.HW4TestDriver <name of input script>"); 053 System.err.println("to read from standard in: java hw4.test.HW4TestDriver"); 054 } 055 056 057 /** String -> Graph: maps the names of graphs to the actual graph **/ 058 //TODO for the student: Parameterize the next line correctly. 059 //private final Map<String, _______> graphs = new HashMap<String, ________>(); 060 private final PrintWriter output; 061 private final BufferedReader input; 062 063 /** 064 * @requires r != null && w != null 065 * 066 * @effects Creates a new HW4TestDriver which reads command from 067 * <tt>r</tt> and writes results to <tt>w</tt>. 068 **/ 069 public HW4TestDriver(Reader r, Writer w) { 070 input = new BufferedReader(r); 071 output = new PrintWriter(w); 072 } 073 074 /** 075 * @effects Executes the commands read from the input and writes results to the output 076 * @throws IOException if the input or output sources encounter an IOException 077 **/ 078 public void runTests() 079 throws IOException 080 { 081 String inputLine; 082 while ((inputLine = input.readLine()) != null) { 083 if ((inputLine.trim().length() == 0) || 084 (inputLine.charAt(0) == '#')) { 085 // echo blank and comment lines 086 output.println(inputLine); 087 } 088 else 089 { 090 // separate the input line on white space 091 StringTokenizer st = new StringTokenizer(inputLine); 092 if (st.hasMoreTokens()) { 093 String command = st.nextToken(); 094 095 List<String> arguments = new ArrayList<String>(); 096 while (st.hasMoreTokens()) { 097 arguments.add(st.nextToken()); 098 } 099 100 executeCommand(command, arguments); 101 } 102 } 103 output.flush(); 104 } 105 } 106 107 private void executeCommand(String command, List<String> arguments) { 108 try { 109 if (command.equals("CreateGraph")) { 110 createGraph(arguments); 111 } else if (command.equals("AddNode")) { 112 addNode(arguments); 113 } else if (command.equals("AddEdge")) { 114 addEdge(arguments); 115 } else if (command.equals("ListNodes")) { 116 listNodes(arguments); 117 } else if (command.equals("ListChildren")) { 118 listChildren(arguments); 119 } else { 120 output.println("Unrecognized command: " + command); 121 } 122 } catch (Exception e) { 123 output.println("Exception: " + e.toString()); 124 } 125 } 126 127 private void createGraph(List<String> arguments) { 128 if (arguments.size() != 1) { 129 throw new CommandException("Bad arguments to CreateGraph: " + arguments); 130 } 131 132 String graphName = arguments.get(0); 133 createGraph(graphName); 134 } 135 136 private void createGraph(String graphName) { 137 // Insert your code here. 138 139 // graphs.put(graphName, ___); 140 // output.println(...); 141 } 142 143 private void addNode(List<String> arguments) { 144 if (arguments.size() != 2) { 145 throw new CommandException("Bad arguments to addNode: " + arguments); 146 } 147 148 String graphName = arguments.get(0); 149 String nodeName = arguments.get(1); 150 151 addNode(graphName, nodeName); 152 } 153 154 private void addNode(String graphName, String nodeName) { 155 // Insert your code here. 156 157 // ___ = graphs.get(graphName); 158 // output.println(...); 159 } 160 161 private void addEdge(List<String> arguments) { 162 if (arguments.size() != 4) { 163 throw new CommandException("Bad arguments to addEdge: " + arguments); 164 } 165 166 String graphName = arguments.get(0); 167 String parentName = arguments.get(1); 168 String childName = arguments.get(2); 169 String edgeLabel = arguments.get(3); 170 171 addEdge(graphName, parentName, childName, edgeLabel); 172 } 173 174 private void addEdge(String graphName, String parentName, String childName, 175 String edgeLabel) { 176 // Insert your code here. 177 178 // ___ = graphs.get(graphName); 179 // output.println(...); 180 } 181 182 183 private void listNodes(List<String> arguments) { 184 if (arguments.size() != 1) { 185 throw new CommandException("Bad arguments to listNodes: " + arguments); 186 } 187 188 String graphName = arguments.get(0); 189 listNodes(graphName); 190 } 191 192 private void listNodes(String graphName) { 193 // Insert your code here. 194 195 // ___ = graphs.get(graphName); 196 // output.println(...); 197 } 198 199 private void listChildren(List<String> arguments) { 200 if (arguments.size() != 2) { 201 throw new CommandException("Bad arguments to listChildren: " + arguments); 202 } 203 204 String graphName = arguments.get(0); 205 String parentName = arguments.get(1); 206 listChildren(graphName, parentName); 207 } 208 209 private void listChildren(String graphName, String parentName) { 210 // Insert your code here. 211 212 // ___ = graphs.get(graphName); 213 // output.println(...); 214 } 215 216 /** 217 * This exception results when the input file cannot be parsed properly 218 **/ 219 static class CommandException extends RuntimeException { 220 221 public CommandException() { 222 super(); 223 } 224 public CommandException(String s) { 225 super(s); 226 } 227 228 public static final long serialVersionUID = 3495; 229 } 230}