Changeset 87 in tmcsimulator for trunk/src/atmsdriver/model/Highways.java


Ignore:
Timestamp:
10/10/2017 01:09:50 AM (9 years ago)
Author:
jtorres
Message:

merged branches/trunk into regular tmcsim/trunk. Changed Network.java to Highways.java. Highways.java: added writeToFEP(), updateSequences(), loadHighways(), and writeHighwaysMeta(). writeToFEP() is a socket client that communicates with FEPSimulator socket server. updateSequences() updates global and schedule sequences per the existing UCI plugin code and may not be necessary. loadHighways() loads the ArrayList? of Highways used in highways class, in sorted order. Stations are now implementing comparable to sort by postmile. writeHighwaysMeta() is an attempt at writing the highways meta data in condensed form, because loading time in NetworkLoader? is super long. We will eventually get rid of NetworkLoader? and do it within the Highways class for good OOP practice. There are new properties in atms_driver_properties.properties in config to support all these changes. Highway.java: new class, represents a highway which is a sorted ArrayList? of stations. This is a good checkpoint, for persistent green dots and all is working within the triangle. There is much commented out code in Highways.java which does not quite work. This commented code is an attempt at writing the condensed form highway meta data to improve loading times as described above.

File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/atmsdriver/model/Highways.java

    r79 r87  
    22 
    33import atmsdriver.NetworkLoader; 
     4import atmsdriver.model.Station.DIRECTION; 
    45import java.io.File; 
     6import java.io.FileNotFoundException; 
    57import java.io.FileWriter; 
    68import java.io.IOException; 
     9import java.io.PrintWriter; 
    710import java.io.StringWriter; 
    811import java.io.Writer; 
     12import java.net.Socket; 
    913import java.util.ArrayList; 
     14import java.util.Scanner; 
    1015import java.util.logging.Level; 
    1116import java.util.logging.Logger; 
    1217import javax.xml.parsers.DocumentBuilder; 
    1318import javax.xml.parsers.DocumentBuilderFactory; 
    14 import javax.xml.parsers.ParserConfigurationException; 
    1519import javax.xml.transform.OutputKeys; 
    1620import javax.xml.transform.Transformer; 
    17 import javax.xml.transform.TransformerConfigurationException; 
    18 import javax.xml.transform.TransformerException; 
    1921import javax.xml.transform.TransformerFactory; 
    2022import javax.xml.transform.dom.DOMSource; 
     
    2729 * @author andrew 
    2830 */ 
    29 public class Network { 
     31public class Highways { 
     32 
    3033    final private ArrayList<FEPLine> lines; 
    31     final private File networkFile; 
    32      
    33     public Network(File LDSFile, File loopFile, File networkFile) 
     34    final private String FEPHostName; 
     35    final private int FEPPortNum; 
     36     
     37    // NEED FINISH final private ArrayList<Highway> highways; 
     38     
     39    public Highways(String ldsFileName, String loopsFileName,  
     40            String highwayMetaFileName, String FEPHostName, int FEPPortNum) { 
     41        /* 
     42        lines = loadLines(highwayMetaFileName); 
     43        System.out.println("SIZE: " + toXML().toCharArray().length); 
     44        */ 
     45         
     46        NetworkLoader ldr = new NetworkLoader(new File(ldsFileName), new File(loopsFileName)); 
     47        this.lines = (ArrayList<FEPLine>) ldr.getFEPLines(); 
     48        this.FEPHostName = FEPHostName; 
     49        this.FEPPortNum = FEPPortNum; 
     50        // NEED FINISH this.highways = loadHighways(); 
     51        //writeHighwaysMeta("hard.txt"); 
     52    } 
     53     
     54    // NEED FINISH 
     55    private void loadHighways() 
    3456    { 
    35         lines = (ArrayList<FEPLine>)  
    36                 new NetworkLoader(LDSFile, loopFile).getFEPLines(); 
    37         this.networkFile = networkFile; 
    38     } 
    39      
    40     public void toXML() 
    41     { 
     57         
     58        //highways.add(new Highway(lines, )); 
     59         
     60    } 
     61/* 
     62    private ArrayList<FEPLine> loadLines(String highwayMetaFileName) { 
     63        ArrayList<FEPLine> lines = new ArrayList<>(); 
     64        try { 
     65            Scanner sc = new Scanner(new File(highwayMetaFileName)); 
     66            String firstLine = sc.nextLine(); 
     67 
     68            Scanner linesc = new Scanner(firstLine); 
     69            int numLines = linesc.nextInt(); 
     70            linesc.close(); 
     71 
     72            for (int i = 0; i < numLines; i++) { 
     73                System.out.println("CURR: " + i); 
     74                lines.add(loadLine(sc)); 
     75            } 
     76            sc.close(); 
     77 
     78        } catch (FileNotFoundException ex) { 
     79            Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex); 
     80        } 
     81        return lines; 
     82    } 
     83 
     84    private FEPLine loadLine(Scanner sc) { 
     85        String line = sc.nextLine(); 
     86        System.out.println(line); 
     87        Scanner scline = new Scanner(line); 
     88 
     89        int lineNum = scline.nextInt(); 
     90        int count = scline.nextInt(); 
     91        int numStations = scline.nextInt(); 
     92        ArrayList<Station> stations = new ArrayList<>(); 
     93        for (int i = 0; i < numStations; i++) { 
     94            stations.add(loadStation(sc, lineNum)); 
     95        } 
     96 
     97        return new FEPLine(lineNum, stations, count); 
     98    } 
     99 
     100    private Station loadStation(Scanner sc, int lineNum) { 
     101        String line = sc.nextLine(); 
     102        System.out.println(line); 
     103        Scanner scline = new Scanner(line); 
     104        int ldsID = scline.nextInt(); 
     105        int drop = scline.nextInt(); 
     106        int fwy = scline.nextInt(); 
     107        DIRECTION dir = DIRECTION.getEnum(scline.next()); 
     108        double postmile = scline.nextDouble(); 
     109        int numLoops = scline.nextInt(); 
     110        String location = getStationLoc(line); 
     111        ArrayList<LoopDetector> loops = new ArrayList<>(); 
     112        for (int i = 0; i < numLoops; i++) { 
     113            loops.add(loadLoop(sc)); 
     114        } 
     115 
     116        return new Station(lineNum, ldsID, drop, location, loops, fwy, dir, postmile); 
     117    } 
     118 
     119    private LoopDetector loadLoop(Scanner sc) { 
     120        String line = sc.nextLine(); 
     121        Scanner scline = new Scanner(line); 
     122 
     123        int loopID = scline.nextInt(); 
     124        int laneNum = scline.nextInt(); 
     125        String loopLoc = getLoopLoc(line); // NEED GET LOOPLOC 
     126        scline.close(); 
     127        return new LoopDetector(loopID, loopLoc, laneNum); 
     128    } 
     129 
     130    private String getLoopLoc(String line) { 
     131        Scanner sc = new Scanner(line); 
     132        sc.nextInt(); 
     133        sc.nextInt(); 
     134 
     135        // GRABS FROM CURRENT TO END OF LINE 
     136          
     137        sc.useDelimiter("\\z"); 
     138        String loc = sc.next().trim(); 
     139        sc.close(); 
     140        return loc; 
     141    } 
     142 
     143    // Returns the loction given the whole line from the lookup file 
     144    private String getStationLoc(String line) { 
     145        Scanner scline = new Scanner(line); 
     146        scline.nextInt(); 
     147        scline.nextInt(); 
     148        scline.nextInt(); 
     149        scline.next(); 
     150        scline.nextDouble(); 
     151        scline.nextInt(); 
     152 
     153        // GRABS FROM CURRENT TO END OF LINE 
     154        scline.useDelimiter("\\z"); 
     155        String loc = scline.next().trim(); 
     156        scline.close(); 
     157        return loc; 
     158    } 
     159*/ 
     160    public void writeToFEP() { 
     161        try { 
     162            Socket sock = new Socket(FEPHostName, FEPPortNum); 
     163            PrintWriter out = new PrintWriter(sock.getOutputStream(), true); 
     164            out.println(this.toXML()); 
     165            sock.close(); 
     166        } catch (IOException ex) { 
     167            Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex); 
     168        } 
     169    } 
     170     
     171    // CHECK: DO WE EVEN NEED TO DO THIS? 
     172    private void updateSequences() { 
     173        for (FEPLine line : lines) { 
     174            line.updateSequences(); 
     175        } 
     176    } 
     177 
     178    /** 
     179     * Returns the network metadata in condensed form. This is just a quick 
     180     * script function to make a proper highway metadata configuration file, so 
     181     * that we can read the network faster. 
     182     * 
     183     * @return Network metadata 
     184     */ 
     185    public void writeHighwaysMeta(String fileName) { 
     186 
     187        try { 
     188            FileWriter fw = new FileWriter(new File(fileName)); 
     189            StringBuilder build = new StringBuilder(); 
     190            build.append(lines.size()); 
     191            build.append("\n"); 
     192            System.out.println(lines.size()); 
     193            fw.write(build.toString()); 
     194            int count = 1; 
     195            for (FEPLine line : lines) { 
     196                System.out.println("Writing num: " + count); 
     197                count++; 
     198                fw.write(line.getLineMeta()); 
     199 
     200            } 
     201        } catch (IOException ex) { 
     202            Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex); 
     203        } 
     204    } 
     205 
     206    public String toXML() { 
     207        String xml = null; 
    42208        try { 
    43209            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
    44210            DocumentBuilder builder = factory.newDocumentBuilder(); 
    45211            Document theDoc = builder.newDocument(); 
    46              
     212 
    47213            Element networkElement = theDoc.createElement(XML_TAGS.NETWORK.tag); 
    48214            theDoc.appendChild(networkElement); 
    49              
    50             for(FEPLine line : lines) 
    51             { 
     215 
     216            for (FEPLine line : lines) { 
    52217                line.toXML(networkElement); 
    53218            } 
    54              
     219 
    55220            Transformer tf = TransformerFactory.newInstance().newTransformer(); 
    56              
     221 
    57222            tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); 
    58223            tf.setOutputProperty(OutputKeys.INDENT, "yes"); 
    59224            tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); 
    60              
     225 
    61226            Writer out = new StringWriter(); 
    62227            tf.transform(new DOMSource(theDoc), new StreamResult(out)); 
    63             String xml = out.toString(); 
     228            xml = out.toString(); 
    64229            out.close(); 
    65              
    66             Writer fileWr = new FileWriter(networkFile); 
    67             fileWr.write(xml); 
    68             fileWr.close(); 
    69              
    70              
    71230        } catch (Exception ex) { 
    72             Logger.getLogger(Network.class.getName()).log(Level.SEVERE, null, ex); 
    73         } 
    74     } 
    75      
    76     private static enum XML_TAGS 
    77     { 
     231            Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex); 
     232        } 
     233        return xml; 
     234 
     235    } 
     236 
     237    private static enum XML_TAGS { 
     238 
    78239        NETWORK("Network"); 
    79          
     240 
    80241        String tag; 
    81          
    82         private XML_TAGS(String n) 
    83         { 
     242 
     243        private XML_TAGS(String n) { 
    84244            tag = n; 
    85245        } 
Note: See TracChangeset for help on using the changeset viewer.