Index: trunk/src/tmcsim/highwaymodel/Highways.java
===================================================================
--- trunk/src/atmsdriver/model/Highways.java	(revision 398)
+++ trunk/src/tmcsim/highwaymodel/Highways.java	(revision 422)
@@ -1,15 +1,9 @@
-package atmsdriver.model;
+package tmcsim.highwaymodel;
 
 import atmsdriver.trafficeventseditor.TrafficLaneEvent;
-import atmsdriver.model.LoopDetector.DOTCOLOR;
-import atmsdriver.model.Station.DIRECTION;
+import tmcsim.highwaymodel.Station.DIRECTION;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.StringWriter;
-import java.io.Writer;
-import java.net.Socket;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -21,52 +15,31 @@
 import java.util.logging.Level;
 import java.util.logging.Logger;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.transform.OutputKeys;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import tmcsim.common.SimulationException;
 
 /**
- * The Highways class aggregates all Highway instances within a geographic
- * region, and all of the FEPLines within an electronic detector network, in the
- * same geographic region. An instance of Highways.java comprises the underlying
- * model for the ATMSDriver application.
+ * Highways represents California state highways supervised by CalTrans. 
+ * The highways have sensors to detect traffic flow.  In the simulation
+ * the traffic flow is artificially created and Highways maintains the
+ * current levels of traffic flow throughout the network.
  *
- * Highways uses method writeToFEP() to communicate with the FEP Simulator. It
- * creates a socket client which sends the FEP Simulator a highways status
- * message over the socket. This message is sent in the format required by the
- * FEP Simulator.
+ * Highways builds an internal representation of the highway network
+ * from VDS data (obtained from PeMS) The current state of the network
+ * is output to a json file for use by CPTMS. 
  *
- *
- * @author John A. Torres
+ * @author John A. Torres, jdalbey
  */
 final public class Highways
 {
-
-    final private String FEPHostName;
-    final private int FEPPortNum;
-    
-    final private List<FEPLine> lines;
     final public List<Highway> highways;
 
-    public Highways(String highwaysMapFileName, String FEPHostName, int FEPPortNum)
-    {
-        // load FEP Lines
-        lines = loadLines(highwaysMapFileName);
-        // build highways data structure
-        this.highways = buildHighways();
-
-        // write to FEP host and port number
-        this.FEPHostName = FEPHostName;
-        this.FEPPortNum = FEPPortNum;
-    }
-
-    private ArrayList<Highway> buildHighways()
-    {
+    public Highways(String highwaysMapFileName)
+    {
+       // build highways data structure
+        this.highways = buildHighwayNetwork(highwaysMapFileName);
+    }
+    
+    private ArrayList<Highway> buildHighwayNetwork(String highwaysMapFileName)
+    {
+        // NOTE: Could this method be streamlined? Is it necessary to have the
+        // second data structure?
         System.out.println("Building highways...");
         // The list of highways to return
@@ -76,12 +49,12 @@
         Map<Integer, ArrayList<Station>> highwayMap = new HashMap<>();
         
-        // iterate through FEPLines and get data to add to the above map
-        for (FEPLine line : lines)
-        {
-            // grab all stations from the current FEPLine
-            ArrayList<Station> lineStations = (ArrayList<Station>) line.stations;
-            // iterate through each station in the list of stations
-            for (Station station : lineStations)
-            {
+        // Open the postmile file and scan each line
+        File file = new File(highwaysMapFileName);
+        try 
+        {
+            Scanner scanner = new Scanner(file);
+            while (scanner.hasNext())
+            {
+                Station station = loadStation(scanner); 
                 Integer hwyNum = station.routeNumber;
                 // if the map does not contain an entry for the highway, create
@@ -101,6 +74,7 @@
                 }
             }
-        }
-        
+        }catch (FileNotFoundException e) {
+            e.printStackTrace();
+        }
         // get the set of highway numbers
         Set<Integer> hwyKeys = highwayMap.keySet();
@@ -229,284 +203,34 @@
     }
     
+    
     /**
-     * Loads all FEPLines from the specified highways map file.
-     * 
-     * @param highwaysMapFileName
-     * @return List of FEPLines
-     */
-    private ArrayList<FEPLine> loadLines(String highwaysMapFileName)
-    {
-        ArrayList<FEPLine> lines = new ArrayList<>();
-        try
-        {
-            Scanner sc = new Scanner(new File(highwaysMapFileName));
-            // first line of file contains number of FEP Lines
-            String firstLine = sc.nextLine();
-            Scanner linesc = new Scanner(firstLine);
-            int numLines = linesc.nextInt();
-            linesc.close();
-            // FOR each FEP Line
-            for (int i = 0; i < numLines; i++)
-            {
-                lines.add(loadLine(sc));
-            }
-            sc.close();
-
-        } catch (FileNotFoundException ex)
-        {
-            Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex);
-        }
-        return lines;
-    }
-    
-    /**
-     * Load all the stations for a single FEP Line from the highways map file.
-     * 
-     * @param sc scanner at the current FEPLine line
-     * @return FEPLine
-     */
-    private FEPLine loadLine(Scanner sc)
+     * Loads a single Station from the postmile coordinates file
+     * @param sc scanner at the current station line
+     * @return Station
+     */
+    private Station loadStation(Scanner sc)
     {
         String line = sc.nextLine();
         Scanner scline = new Scanner(line);
-        // Get the attributes of this FEP Line
-        int lineNum = scline.nextInt();
-        int count = scline.nextInt();
-        int numStations = scline.nextInt();
+        scline.useDelimiter(",");
         
-        // initialze stations array
-        ArrayList<Station> stations = new ArrayList<>();
-        // Read all the stations for thie FEP Line
-        for (int i = 0; i < numStations; i++)
-        {
-            stations.add(loadStation(sc, lineNum));
-        }
-
-        return new FEPLine(lineNum, stations, count);
-    }
-    
-    /**
-     * Loads a single Station from the highways map file
-     * @param sc scanner at the current station line
-     * @param lineNum the FEPLine number for the station
-     * @return Station
-     */
-    private Station loadStation(Scanner sc, int lineNum)
-    {
-        String line = sc.nextLine();
-        Scanner scline = new Scanner(line);
-        
-        int ldsID = scline.nextInt();
-        int drop = scline.nextInt();
-        int fwy = scline.nextInt();
-        DIRECTION dir = DIRECTION.toDirection(scline.next());
-        double postmile = scline.nextDouble();
-        int numLoops = scline.nextInt();
-        String location = getStationLoc(line);
+        String route  = scline.next(); // FWY DIR POSTMILE
+        scline.next();  // skip lat
+        scline.next();  // skip long
+        String description = scline.next(); // the description of the location
+
+        Scanner rteScan = new Scanner(route);
+        int fwy = rteScan.nextInt();
+        DIRECTION dir = DIRECTION.toDirection(rteScan.next());
+        double postmile = rteScan.nextDouble();
+
         ArrayList<LoopDetector> loops = new ArrayList<>();
-        for (int i = 0; i < numLoops; i++)
-        {
-            loops.add(loadLoop(sc));
-        }
-
-        return new Station(lineNum, ldsID, drop, location, loops, fwy, dir, postmile);
-    }
-    
-    /**
-     * Loads a single loop from the highways map file
-     *
-     * @param sc scanner at the current loop line
-     * @return LoopDetector
-     */
-    private LoopDetector loadLoop(Scanner sc)
-    {
-        String line = sc.nextLine();
-        Scanner scline = new Scanner(line);
-
-        int loopID = scline.nextInt();
-        String loopLocID = scline.next();
-        String loopLoc = scline.next();
-        scline.close();
-        return new LoopDetector(loopID, loopLocID, loopLoc);
-    }
-
-    /**
-     * Scans the LoopDetector line and grabs the String location from the line.
-     * 
-     * @param line the line containing the location
-     * @return A String loop location.
-     */
-    private String getLoopLoc(String line)
-    {
-        Scanner sc = new Scanner(line);
-        sc.nextInt();
-
-     // GRABS FROM CURRENT TO END OF LINE
-        sc.useDelimiter("\\z");
-        String loc = sc.next().trim();
-        sc.close();
-        return loc;
-    }
-
-    /**
-     * Scans the Station line and grabs the String location from the line.
-     * 
-     * @param line the line containing the location
-     * @return A String station location.
-     */
-    private String getStationLoc(String line)
-    {
-        Scanner scline = new Scanner(line);
-        scline.nextInt();
-        scline.nextInt();
-        scline.nextInt();
-        scline.next();
-        scline.nextDouble();
-        scline.nextInt();
-
-        // GRABS FROM CURRENT TO END OF LINE
-        scline.useDelimiter("\\z");
-        String loc = scline.next().trim();
-        scline.close();
-        return loc;
-    }
-    
-    /** 
-     * Creates a socket client that writes the Highways data to the FEP Simulator.
-     * 
-     * @throws SimulationException 
-     */
-    public void writeToFEP() throws SimulationException
-    {
-        try
-        {
-            // Create the socket to the FEP Simulator
-            Socket sock = new Socket(FEPHostName, FEPPortNum);
-            PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
-            
-            // Print the number of bytes the highways data message contains
-            System.out.println("Highways sending " + this.toCondensedFormat(false).toCharArray().length + 1 + "bytes to FEPSIM.");
-            String outMsg = this.toCondensedFormat(false);
-            // Write the highways data over the socket
-            out.println(outMsg);
-            
-            // close the socket
-            sock.close();
-        } catch (java.net.ConnectException ex)
-        {
-            //Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex);
-            System.out.println("writeToFEP() can't connect, no data sent to FEP.");
-            throw new SimulationException(SimulationException.BINDING);
-        } catch (IOException ex)
-        {
-            //Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex);
-            System.out.println("Highway Model failed writing to FEPSim.");
-            throw new SimulationException(SimulationException.BINDING);
-        }
-    }
-    
-    /** Returns a string of highways data. If MetaDataOnly is true, you get a full
-     *  dump of the highways meta data, which does not include dynamic loop values,
-     *  and does include the string location names. If MetaDataOnly is false,
-     *  dynamic loop values are included, and unnecessary information like string
-     *  location values are not included.
-     * 
-     *  The FEPSimulator takes in the toCondensedFormat() output, with a MetaDataOnly
-     *  value of false, over the socket.
-     * 
-     *  The MetaDataOnly flag should be used to get a full dump of the highways
-     *  information. This was used to get the highways_fullmap.txt output.
-     * 
-     * @param MetaDataOnly Whether you want meta data, or a full dump for FEPSim
-     * @return String, highways data in condensed format
-     * 
-     * Example toCondensedFormat(MetaDataOnly = false) output:
-     * 
-     * 43                       // "number of lines"
-     * 32 0 13                  // "line id" "count num" "number of stations"
-     * 1210831 1 5 S 0.9 8      // "station id" "drop num" "route num"...
-     *                          //      ..."direction" "postmile" "number of loops"
-     * 1210832  0.0 0  ML_1     // "loop id" "occ" "vol"
-     * 1210833  0.0 0  ML_2     // ..
-     * 1210834  0.0 0  ML_3     // ..
-     * 1210835  0.0 0  ML_4     // ..
-     * 1210836  0.0 0  PASSAGE  // ..
-     * 1210837  0.0 0  DEMAND   // ..
-     * 1210838  0.0 0  QUEUE    // ..
-     * 1210839  0.0 0  RAMP_OFF // ..
-     * ...
-     * 
-     * Example toCondensedFormat(MetaDataOnly = true) output:
-     * 
-     * 43                           // "number of lines"
-     * 32 0 13                      // "line id" "count num" "number of stations"
-     * 1210831 1 5 S 0.9 8 CALAFIA  // "station id" "drop num" "route num"...
-     *                              //      ..."direction" "postmile"...
-     *                              //      ..."number of loops" "string location"
-     * 1210832 ML_1                 // "loop id" "loop location"
-     * 1210833 ML_2                 // "            "
-     * 1210834 ML_3                 // "            "
-     * 1210835 ML_4                 // "            "
-     * 1210836 PASSAGE              // "            "
-     * 1210837 DEMAND               // "            "
-     * 1210838 QUEUE                // "            "
-     * 1210839 RAMP_OFF             // "            "
-     * ...
-     */
-    public String toCondensedFormat(boolean MetaDataOnly)
-    {
-        // first line: number of FEPLines
-        StringBuilder build = new StringBuilder();
-        build.append(lines.size());
-        build.append("\n");
-        // append each fep line to the string
-        for(FEPLine line : lines)
-        {
-            build.append(line.toCondensedFormat(MetaDataOnly));
-        }
-        // return the full condensed format string
-        return build.toString();
-    }
-    
-    /**
-     * Returns the Highways model data in XML format.
-     * Probably obsolete, since we aren't using exchange.xml any longer.
-     * @return highways data in XML format
-     */
-    public String toXML()
-    {
-        String xml = null;
-        try
-        {
-            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-            DocumentBuilder builder = factory.newDocumentBuilder();
-            Document theDoc = builder.newDocument();
-
-            Element networkElement = theDoc.createElement(XML_TAGS.NETWORK.tag);
-            theDoc.appendChild(networkElement);
-
-            for (FEPLine line : lines)
-            {
-                line.toXML(networkElement);
-            }
-
-            Transformer tf = TransformerFactory.newInstance().newTransformer();
-
-            tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
-            tf.setOutputProperty(OutputKeys.INDENT, "yes");
-            tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
-
-            Writer out = new StringWriter();
-            tf.transform(new DOMSource(theDoc), new StreamResult(out));
-            xml = out.toString();
-            out.close();
-        } catch (Exception ex)
-        {
-            Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex);
-        }
-        return xml;
-
-    }
-
+        // For now we'll use just a dummy loop detector
+        LoopDetector detector = new LoopDetector(1,"ML","ML_1");
+        loops.add(detector);
+
+        return new Station(11, 123, 99, description, loops, fwy, dir, postmile);
+    }
+     
     /**
      * Returns a highway by given highway number.
@@ -531,5 +255,5 @@
     }
 
-    /** Return a string representation of the Highways */
+    /** Return a string representation of the Highways - for debugging */
     public String toString()
     {
@@ -543,10 +267,10 @@
                 StringBuilder lineout = new StringBuilder();
                 // Examine every station on this highway and direction
-                for (Station stat: hwy.stations)
-                {
-                    if (stat.direction.equals(dir))
+                for (Station station: hwy.stations)
+                {
+                    if (station.direction.equals(dir))
                     {
                     //lineout.append("" + dir.getLetter() + stat.postmile);
-                    lineout.append(stat.getColor());
+                    lineout.append(station.getColor().symbol());
                     //lineout.append("  ");
                     }
@@ -616,5 +340,5 @@
                     String outString = currentPM.toJson();
                     // replace the color code with the color name
-                    String colorName=stat.getColorName();
+                    String colorName=stat.getColor().htmlColor();
                     outString = outString.replace("desiredcolor",colorName);
                     lineout.append(outString);
@@ -664,14 +388,14 @@
     }
     
-    public void reset()
-    {
-        for(FEPLine line : lines)
-        {
-            for(Station stn : line.stations)
+    /** Reset all the traffic levels to free flowing (green) */
+    public void reset()            
+    {
+        for(Highway hwy: highways)
+        {
+            for(Station stn : hwy.stations)
             {
                 for(LoopDetector ld : stn.loops)
                 {
-                    ld.occ = 0;
-                    ld.vol = 0;
+                    ld.setAttributes(LoopDetector.DOTCOLOR.GREEN);
                 }
             }
