package jimDij;

import java.io.*;

/*****
 * ReadData builds the Graph from a given file.<p>
 * 
 * The nodes and edges of the graph are defined in 
 * the file g5.obj, which is the same data file used
 * in the applet in ../applet_one/.
 * (http://www-b2.is.tokushima-u.ac.jp/~ikeda/suuri/dijkstra/Dijkstra.shtml)
 * <p>
 *
 * Most of the code here is very close to that in the applet,
 * with minor modifications to fit my data structures.<p>
 *
 * If I had been doing this as a project for the intro Java class,
 * I would probably have just put the data right into this file, 
 * with code something like
 *
 *  <pre>
 *    Node brattleboro = new Node("Boston");
 *    Node wilmington = new Node("Wilmington");
 *    Edge Route9 = new Edge(brattleboro, wilmington, 20);
 *    theGraph.addNode(brattleboro);
 *    theGraph.addNode(wilmington);
 *  </pre>
 *
 * and so on to build up the nodes and the connections and distances
 * between them.
 *
 * @author Jim Mahoney (mahoney@marlboro.edu)
 * @version 0.1 Nov 26 2002
 ***/

public class ReadData {

    /*******
     * initData defines a single instance of a Graph
     *          by reading in Node and Edge definitions 
     *          from a given file.
     *
     * This should be called once to set everything up.
     *
     * @param   startNodeName  the name of the node where the search starts
     * @param   fileName       the name of the data file
     * @return  the Graph object that holds the Nodes and Edges.
     *
     *****/

    ///////////////////////////////////////////////////////////////////
    // The data file has the following form, where "#" marks comments.
    // (Though I'm not using (x,y) here, nor the names of the edges.)
    //
    //    2 1 graph               # 2 nodes, 1 edge, not a digraph
    //    Boston   100 200        # name x y
    //    Chicago  600 700        # 
    //    #
    //    Rt9 Boston Chicago 23   # name fromNode toNode distance
    //    #
    ///////////////////////////////////////////////////////////////////

    public static Graph initData(String fileName, String startNodeName){
        int n,m;
        String s;
        Graph theGraph = new Graph();
        boolean isdigraph;

        // Since we're doing file input and output, which
        // might fail, all the work happens in a "try" block.
        try {
            InputStream is = new FileInputStream(fileName);

            Reader r = new BufferedReader(new InputStreamReader(is));
            StreamTokenizer st = new StreamTokenizer(r);
            st.commentChar('#');

            // read in the number of vertices and edges from the data file
            st.nextToken();     
            n = (int)st.nval;
            st.nextToken();     
            m = (int)st.nval;
            st.nextToken();     
            s = st.sval;
            isdigraph = "digraph".equals(s);

            // read in nodes and their names from the data file
            for (int i = 0; i<n; i++) {
                // Node node = new Node();
                Node node = new Node();
                st.nextToken();         
                node.setName(st.sval);
                st.nextToken();         
                //node.setX((int)st.nval);
                st.nextToken();         
                //node.setY((int)st.nval);
                theGraph.addNode(node);
            }

            // read in the edges from the data file
            for (int i = 0; i<m; i++) {
                Edge edge1 = new Edge();
                Edge edge2 = new Edge();
                Node node1, node2;
                st.nextToken(); // edge.name = st.sval;  // skip edge name

                st.nextToken();         
                node1 = theGraph.getNode(st.sval); 
                st.nextToken();         
                node2 = theGraph.getNode(st.sval);
                st.nextToken();         
                edge1.setDistance( (int)st.nval );
                edge2.setDistance( (int)st.nval );

                // Each edge goes from a node, to another node.
                // And each is in a list in the "from" node.
                edge1.setFromNode(node1);  
                edge1.setToNode(node2);
                node1.addEdge(edge1);

                // A "digraph" is a directional Graph.
                // If this isn't directional, then each edge
                // can be traversed both ways.  In this code,
                // I treat that as two edges, one going in each direction.
                if (! isdigraph){
                    edge2.setFromNode(node2);
                    edge2.setToNode(node1);
                    node2.addEdge(edge2);
                }
            } 
            // set starting node
            theGraph.setStartNode( theGraph.getNode(startNodeName) );
        }
        catch (Exception e){
            System.out.println(" Oops: " + e.getMessage() );
        }
        return theGraph;
    } 


}


syntax highlighted by Code2HTML, v. 0.9.1