Changeset 87 in tmcsimulator for trunk/src/atmsdriver/model


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.

Location:
trunk/src/atmsdriver/model
Files:
1 added
3 edited
1 moved

Legend:

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

    r84 r87  
    2525    final private int lineInfo; 
    2626    final private long systemKey; 
    27     final private long globalSeq; 
    28     final private long scheduleSeq; 
     27    private long globalSeq; 
     28    private long scheduleSeq; 
    2929     
    3030    public FEPLine(int lineNum, List<Station> stations, int count, 
     
    4040        this.globalSeq = globalSeq; 
    4141        this.scheduleSeq = scheduleSeq; 
     42    } 
     43     
     44    // NEED TO CHECK NUMBERS? DO WE EVEN NEED THIS? 
     45    public void updateSequences() 
     46    {         
     47        this.scheduleSeq += 1; 
     48        this.globalSeq += 51; 
     49    } 
     50     
     51    public String getLineMeta() 
     52    { 
     53        StringBuilder build = new StringBuilder(); 
     54        build.append(Integer.toString(this.lineNum)); 
     55        build.append(" "); 
     56        build.append(Integer.toString(this.count)); 
     57        build.append(" "); 
     58        build.append(Integer.toString(this.stations.size())); 
     59        build.append("\n"); 
     60        for(Station station : stations) 
     61        { 
     62            build.append(station.getStationMeta()); 
     63        } 
     64        return build.toString(); 
    4265    } 
    4366     
  • 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        } 
  • trunk/src/atmsdriver/model/LoopDetector.java

    r79 r87  
    5757    } 
    5858     
     59    /** 
     60     * Returns the loop metadata in condensed form. 
     61     * This is just a quick script function to make a proper highway 
     62     * metadata configuration file, so that we can read the network 
     63     * faster. 
     64     *  
     65     * @return loop metadata 
     66     */ 
     67    public String getLoopMeta() 
     68    { 
     69        StringBuilder build = new StringBuilder(); 
     70        build.append(Integer.toString(this.loopID)); 
     71        build.append(" "); 
     72        build.append(Integer.toString(this.laneNum)); 
     73        build.append(" "); 
     74        build.append(this.loopLocation); 
     75        build.append("\n"); 
     76        return build.toString(); 
     77    } 
     78     
    5979    /**  
    6080     * Updates loop detector dynamic attributes. 
  • trunk/src/atmsdriver/model/Station.java

    r84 r87  
    55import org.w3c.dom.Element; 
    66 
    7 /** A Station is a simulation of a station in a traffic network. 
    8  *   
    9  *  A Station (LDS) contains static meta data about the station, and 
    10   two dynamic attributes, MLTotVol and OppTotVol.  
    11   
    12   A single LDS contains multiple LoopDetectors, which 
    13   contain data for a single lane on one direction of the freeway. A LDS 
    14   is specific to a single freeway, direction, and postmile. 
     7/** 
     8 * A Station is a simulation of a station in a traffic network. 
     9 * 
     10 * A Station (LDS) contains static meta data about the station, and two dynamic 
     11 * attributes, MLTotVol and OppTotVol.  * 
     12 * A single LDS contains multiple LoopDetectors, which contain data for a single 
     13 * lane on one direction of the freeway. A LDS is specific to a single freeway, 
     14 * direction, and postmile. 
    1515 * 
    1616 * @author John A. Torres 
     
    1919public class Station implements Comparable { 
    2020    /* Static Station meta data */ 
     21 
    2122    final private int lineNum; 
    2223    final private int ldsID; // double check 
     
    2728    final private double postmile; 
    2829    final private DIRECTION direction; 
    29      
     30 
    3031    /* Dynamic Station data */ 
    3132    private int MLTotVol; 
    3233    private int OppTotVol; 
    33      
     34 
    3435    /* Constructor */ 
    3536    public Station(int lineNum, int ldsID, int drop, 
    36             String location, List<LoopDetector> loops, int fwy,  
    37             DIRECTION direction, double postmile) 
    38     { 
     37            String location, List<LoopDetector> loops, int fwy, 
     38            DIRECTION direction, double postmile) { 
    3939        this.lineNum = lineNum; 
    4040        this.ldsID = ldsID; 
     
    4545        this.direction = direction; 
    4646        this.freeway = fwy; 
    47          
     47 
    4848        this.MLTotVol = 0; 
    4949        this.OppTotVol = 0; 
    5050    } 
    5151 
     52    /** 
     53     * Returns the station metadata in condensed form. This is just a quick 
     54     * script function to make a proper highway metadata configuration file, so 
     55     * that we can read the network faster. 
     56     * 
     57     * @return station metadata 
     58     */ 
     59    public String getStationMeta() { 
     60        StringBuilder build = new StringBuilder(); 
     61        build.append(Integer.toString(this.ldsID)); 
     62        build.append(" "); 
     63        build.append(Integer.toString(this.drop)); 
     64        build.append(" "); 
     65        build.append(Integer.toString(this.freeway)); 
     66        build.append(" "); 
     67        build.append(this.direction.name); 
     68        build.append(" "); 
     69        build.append(Double.toString(this.postmile)); 
     70        build.append(" "); 
     71        build.append(Integer.toString(loops.size())); 
     72        build.append(" "); 
     73        build.append(this.location); 
     74        build.append("\n"); 
     75        for (LoopDetector loop : loops) { 
     76            build.append(loop.getLoopMeta()); 
     77        } 
     78        return build.toString(); 
     79    } 
     80 
     81    public double getPostmile() { 
     82        return postmile; 
     83    } 
     84 
    5285    @Override 
    53     public int compareTo(Object o) { 
    54          
    55     } 
    56      
    57     private static enum XML_TAGS 
    58     { 
     86    public int compareTo(Object otherStation) { 
     87        // check that Object is of type Station, if not throw exception 
     88        if (!(otherStation instanceof Station)) { 
     89            throw new ClassCastException("A Station object expected."); 
     90        } 
     91 
     92        // get difference of values 
     93        double otherStationPostmile = ((Station) otherStation).getPostmile(); 
     94        double val = this.postmile - otherStationPostmile; 
     95 
     96        // set appropriate comparable return value 
     97        int retval = 0; 
     98        if (val > 0) { 
     99            retval = 1; 
     100        } else if (val < 0) { 
     101            retval = -1; 
     102        } 
     103 
     104        return retval; 
     105    } 
     106 
     107    private static enum XML_TAGS { 
     108 
    59109        STATION("Station"), 
    60110        LDS_ID("LDS_ID"), 
     
    68118        ML_TOT_VOL("ML_Tot_Vol"), 
    69119        OPP_TOT_VOL("Opp_Tot_Vol"); 
    70          
     120 
    71121        String tag; 
    72          
    73         private XML_TAGS(String n) 
    74         { 
     122 
     123        private XML_TAGS(String n) { 
    75124            tag = n; 
    76125        } 
    77126    } 
    78      
    79     public void toXML(Element currElem) 
    80     { 
     127 
     128    public void toXML(Element currElem) { 
    81129        Document theDoc = currElem.getOwnerDocument(); 
    82          
     130 
    83131        Element stationElement = theDoc.createElement(XML_TAGS.STATION.tag); 
    84132        currElem.appendChild(stationElement); 
    85          
     133 
    86134        Element ldsIDElement = theDoc.createElement(XML_TAGS.LDS_ID.tag); 
    87135        ldsIDElement.appendChild(theDoc.createTextNode(String.valueOf(this.ldsID))); 
    88136        stationElement.appendChild(ldsIDElement); 
    89          
     137 
    90138        Element lineNumElement = theDoc.createElement(XML_TAGS.LINE_NUM.tag); 
    91139        lineNumElement.appendChild(theDoc.createTextNode(String.valueOf(this.lineNum))); 
    92140        stationElement.appendChild(lineNumElement); 
    93          
     141 
    94142        Element dropElement = theDoc.createElement(XML_TAGS.DROP.tag); 
    95143        dropElement.appendChild(theDoc.createTextNode(String.valueOf(this.drop))); 
    96144        stationElement.appendChild(dropElement); 
    97          
     145 
    98146        Element locationElement = theDoc.createElement(XML_TAGS.LOCATION.tag); 
    99147        locationElement.appendChild(theDoc.createTextNode(this.location)); 
    100148        stationElement.appendChild(locationElement); 
    101          
     149 
    102150        Element postMileElement = theDoc.createElement(XML_TAGS.POST_MILE.tag); 
    103151        postMileElement.appendChild(theDoc.createTextNode(String.valueOf(this.postmile))); 
    104152        stationElement.appendChild(postMileElement); 
    105          
     153 
    106154        Element directionElement = theDoc.createElement(XML_TAGS.DIRECTION.tag); 
    107155        directionElement.appendChild(theDoc.createTextNode(this.direction.name)); 
    108156        stationElement.appendChild(directionElement); 
    109          
     157 
    110158        Element freewayElement = theDoc.createElement(XML_TAGS.FREEWAY.tag); 
    111159        freewayElement.appendChild(theDoc.createTextNode(String.valueOf(this.freeway))); 
    112160        stationElement.appendChild(freewayElement); 
    113          
     161 
    114162        Element mlElement = theDoc.createElement(XML_TAGS.ML_TOT_VOL.tag); 
    115163        mlElement.appendChild(theDoc.createTextNode(String.valueOf(this.MLTotVol))); 
    116164        stationElement.appendChild(mlElement); 
    117          
     165 
    118166        Element oppElement = theDoc.createElement(XML_TAGS.OPP_TOT_VOL.tag); 
    119167        oppElement.appendChild(theDoc.createTextNode(String.valueOf(this.OppTotVol))); 
    120168        stationElement.appendChild(oppElement); 
    121          
     169 
    122170        Element loopsElement = theDoc.createElement(XML_TAGS.LOOPS.tag); 
    123171        stationElement.appendChild(loopsElement); 
    124          
    125         for(LoopDetector loop : loops) 
    126         { 
     172 
     173        for (LoopDetector loop : loops) { 
    127174            loop.toXML(loopsElement); 
    128175        } 
    129176    } 
    130      
     177 
    131178    /** 
    132179     * Enum for freeway direction. 
    133      *  
     180     * 
    134181     * @author John A. Torres 
    135182     * @version 9/10/2017 
    136183     */ 
    137     public static enum DIRECTION 
    138     { 
     184    public static enum DIRECTION { 
     185 
    139186        NORTH("N"), 
    140187        SOUTH("S"), 
    141188        EAST("E"), 
    142189        WEST("W"); 
    143          
     190 
    144191        String name; 
    145          
    146         DIRECTION(String name) 
    147         { 
     192 
     193        DIRECTION(String name) { 
    148194            this.name = name; 
    149195        } 
    150          
     196 
    151197        /** 
    152198         * Returns the direction enum, given a string value. 
     199         * 
    153200         * @param name str value for enum 
    154201         * @return enum for given str value 
    155202         */ 
    156         public static DIRECTION getEnum(String name) 
    157         { 
    158             switch(name) 
    159             { 
     203        public static DIRECTION getEnum(String name) { 
     204            switch (name) { 
    160205                case "S": 
    161206                    return SOUTH; 
Note: See TracChangeset for help on using the changeset viewer.