001    package ps3.test;
002    
003    import java.io.*;
004    import java.util.*;
005    
006    import ps3.graph.*;
007    
008    
009    /**
010     * This class implements a testing driver which reads test scripts
011     * from files for testing Graph and PathFinder.
012     **/
013    public class PS3TestDriver {
014    
015        public static void main(String args[]) {
016            try {
017                if (args.length > 1) {
018                    printUsage();
019                    return;
020                }
021          
022                PS3TestDriver td;
023          
024                if (args.length == 0) {
025                    td = new PS3TestDriver(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 PS3TestDriver(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 ps3.GraphTestDriver <name of input script>");
053            System.err.println("to read from standard in: java ps3.GraphTestDriver");
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        /** String -> WeightedNode: maps the names of nodes to the actual node **/
061        private final Map<String, WeightedNode> nodes = new HashMap<String, WeightedNode>();
062        private final PrintWriter output;
063        private final BufferedReader input;
064    
065        /**
066         * @requires r != null && w != null
067         *
068         * @effects Creates a new PS3TestDriver which reads command from
069         * <tt>r</tt> and writes results to <tt>w</tt>.
070         **/
071        public PS3TestDriver(Reader r, Writer w) {
072            input = new BufferedReader(r);
073            output = new PrintWriter(w);
074        }
075    
076        /**
077         * @effects Executes the commands read from the input and writes results to the output
078         * @throws IOException if the input or output sources encounter an IOException
079         **/
080        public void runTests()
081            throws IOException
082        {
083            String inputLine;
084            while ((inputLine = input.readLine()) != null) {
085                if (inputLine.trim().length() == 0 ||
086                    inputLine.charAt(0) == '#') {
087                    // echo blank and comment lines
088                    output.println(inputLine);
089                }
090                else
091                {
092                        // separate the input line on white space
093                        StringTokenizer st = new StringTokenizer(inputLine);
094                        if (st.hasMoreTokens()) {
095                            String command = st.nextToken();
096                      
097                            List<String> arguments = new ArrayList<String>();
098                            while (st.hasMoreTokens()) {
099                                arguments.add(st.nextToken());
100                            }
101            
102                            executeCommand(command, arguments); 
103                        }
104                }
105                output.flush();
106            }  
107        }
108    
109        private void executeCommand(String command, List<String> arguments) {
110            try {
111                if (command.equals("CreateGraph")) {
112                    createGraph(arguments);
113                } else if (command.equals("CreateNode")) {
114                    createNode(arguments);
115                } else if (command.equals("AddNode")) {
116                    addNode(arguments);
117                } else if (command.equals("AddEdge")) {
118                    addEdge(arguments);
119                } else if (command.equals("ListNodes")) {
120                    listNodes(arguments);
121                } else if (command.equals("ListChildren")) {
122                    listChildren(arguments);
123                } else if (command.equals("FindPath")) {
124                    findPath(arguments);
125                } else {
126                    output.println("Unrecognized command: " + command);
127                }
128            } catch (Exception e) {
129                output.println("Exception: " + e.toString());
130            }
131        }
132    
133        private void createGraph(List<String> arguments) {
134            if (arguments.size() != 1) {
135                throw new CommandException("Bad arguments to CreateGraph: " + arguments);
136            }
137    
138            String graphName = arguments.get(0);
139            createGraph(graphName);
140        }
141    
142        private void createGraph(String graphName) {
143            // Insert your code here.
144    
145            // graphs.put(graphName, ___);
146            // output.println(...);
147        }
148    
149        private void createNode(List<String> arguments) {
150            if (arguments.size() != 2) {
151                throw new CommandException("Bad arguments to createNode: " + arguments);
152            }
153          
154            String nodeName = arguments.get(0);
155            String cost = arguments.get(1);
156    
157            createNode(nodeName, cost);
158        }
159    
160        private void createNode(String nodeName, String cost) {
161            // Insert your code here.
162    
163            // nodes.put(nodeName, ___);
164            // output.println(...);
165        }
166    
167        private void addNode(List<String> arguments) {
168            if (arguments.size() != 2) {
169                throw new CommandException("Bad arguments to addNode: " + arguments);
170            }
171        
172            String graphName = arguments.get(0);
173            String nodeName = arguments.get(1);
174    
175            addNode(graphName, nodeName);
176        }
177      
178        private void addNode(String graphName, String nodeName) {
179            // Insert your code here.
180    
181            // ___ = graphs.get(graphName);
182            // ___ = nodes.get(nodeName);
183            // output.println(...);
184        }
185    
186        private void addEdge(List<String> arguments) {
187            if (arguments.size() != 3) {
188                throw new CommandException("Bad arguments to addEdge: " + arguments);
189            }
190        
191            String graphName = arguments.get(0);
192            String parentName = arguments.get(1);
193            String childName = arguments.get(2);
194    
195            addEdge(graphName, parentName, childName);
196        }
197    
198        private void addEdge(String graphName, String parentName, String childName) {
199            // Insert your code here.
200    
201            // ___ = graphs.get(graphName);
202            // ___ = nodes.get(parentName);
203            // ___ = nodes.get(childName);
204            // output.println(...);
205        }
206    
207    
208        private void listNodes(List<String> arguments) {
209            if (arguments.size() != 1) {
210                throw new CommandException("Bad arguments to listNodes: " + arguments);
211            }
212        
213            String graphName = arguments.get(0);
214            listNodes(graphName);
215        }
216    
217        private void listNodes(String graphName) {
218            // Insert your code here.
219    
220            // ___ = graphs.get(graphName);
221            // output.println(...);
222        }
223    
224        private void listChildren(List<String> arguments) {
225            if (arguments.size() != 2) {
226                throw new CommandException("Bad arguments to listChildren: " + arguments);
227            }
228        
229            String graphName = arguments.get(0);
230            String parentName = arguments.get(1);
231            listChildren(graphName, parentName);
232        }
233        
234        private void listChildren(String graphName, String parentName) {
235            // Insert your code here.
236    
237            // ___ = graphs.get(graphName);
238            // ___ = nodes.get(parentName);
239            // output.println(...);
240        }
241    
242        private void findPath(List<String> arguments) {
243            String graphName;
244            List<String> sourceArgs = new ArrayList<String>();
245            List<String> destArgs = new ArrayList<String>();
246    
247            if (arguments.size() < 1) {
248                throw new CommandException("Bad arguments to FindPath: " + arguments);
249            }
250        
251            Iterator<String> iter = arguments.iterator();
252    
253            graphName = iter.next();
254    
255            while (iter.hasNext()) {
256                String s =  iter.next();
257                if (s.equals("->")) {
258                    break;
259                }
260                sourceArgs.add(s);
261            }
262            while (iter.hasNext()) {
263                destArgs.add(iter.next());
264            }
265    
266            if (sourceArgs.size() < 1) {
267                throw new CommandException("Too few source args for FindPath");
268            }
269            if (destArgs.size() < 1) {
270                throw new CommandException("Too few dest args for FindPath");
271            }
272    
273            findPath(graphName, sourceArgs, destArgs);
274        }
275    
276        private void findPath(String graphName, List<String> sourceArgs, List<String> destArgs) {
277            // Insert your code here.
278    
279            // ___ = graphs.get(graphName);
280            // ___ = nodes.get(sourceArgs.get(i));
281            // ___ = nodes.get(destArgs.get(i));
282            // output.println(...);
283    
284        }
285    
286        /**
287         * This exception results when the input file cannot be parsed properly
288         **/
289        static class CommandException extends RuntimeException {
290      
291            public CommandException() {
292                super();
293            }
294            public CommandException(String s) {
295                super(s);
296            }
297            
298            public static final long serialVersionUID = 3495;
299        }
300    }