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