Changeset 88 in tmcsimulator for trunk/src/atmsdriver


Ignore:
Timestamp:
10/10/2017 12:07:14 PM (9 years ago)
Author:
jtorres
Message:

Created a Highway.java class with list of sorted stations (by postmile). Highways.java: now has a loadHighways method which returns the List of highways with sorted stations. Highways.java handles loading and sorting, Highway.java just holds the data.

Location:
trunk/src/atmsdriver
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/atmsdriver/ATMSDriver.java

    r87 r88  
    7878                    .getProperty(PROPERTIES.EXCHANGE_FILE_NAME.name)); 
    7979 
    80             System.out.println(highways.toXML()); 
    8180            highways.writeToFEP(); 
    8281            // Update if exchangeInfo is recieved 
  • trunk/src/atmsdriver/model/FEPLine.java

    r87 r88  
    4040        this.globalSeq = globalSeq; 
    4141        this.scheduleSeq = scheduleSeq; 
     42    } 
     43     
     44    public int getLineNumber() 
     45    { 
     46        return this.lineNum; 
     47    } 
     48     
     49    public List<Station> getStations() 
     50    { 
     51        return this.stations; 
    4252    } 
    4353     
  • trunk/src/atmsdriver/model/Highway.java

    r87 r88  
    11package atmsdriver.model; 
    22 
     3import atmsdriver.model.Station.DIRECTION; 
    34import java.util.ArrayList; 
    45import java.util.Collections; 
     
    910 */ 
    1011class Highway { 
     12     
    1113    final private ArrayList<Station> stations; 
     14    final private Integer highwayNumber; 
     15    final private DIRECTION direction; 
    1216     
    13     public Highway(ArrayList<FEPLine> lines, int highway) 
     17    public Highway(Integer highwayNum, DIRECTION direction, ArrayList<Station> stations) 
    1418    { 
    15         stations = new ArrayList<Station>(); 
    16         loadHighwayStations(lines); 
    17     } 
    18      
    19     private void loadHighwayStations(ArrayList<FEPLine> lines) 
    20     { 
    21         Collections.sort(stations); 
     19        this.highwayNumber = highwayNum; 
     20        this.direction = direction; 
     21        this.stations = stations; 
    2222    } 
    2323} 
  • trunk/src/atmsdriver/model/Highways.java

    r87 r88  
    1212import java.net.Socket; 
    1313import java.util.ArrayList; 
     14import java.util.Collections; 
     15import java.util.Enumeration; 
     16import java.util.Hashtable; 
     17import java.util.Map; 
    1418import java.util.Scanner; 
     19import java.util.Set; 
    1520import java.util.logging.Level; 
    1621import java.util.logging.Logger; 
     
    2732/** 
    2833 * 
    29  * @author andrew 
     34 * @author John A. Torres 
    3035 */ 
    3136public class Highways { 
     
    3439    final private String FEPHostName; 
    3540    final private int FEPPortNum; 
     41    final private ArrayList<Highway> highways; 
    3642     
    3743    // NEED FINISH final private ArrayList<Highway> highways; 
    38      
    39     public Highways(String ldsFileName, String loopsFileName,  
     44    public Highways(String ldsFileName, String loopsFileName, 
    4045            String highwayMetaFileName, String FEPHostName, int FEPPortNum) { 
    4146        /* 
    42         lines = loadLines(highwayMetaFileName); 
    43         System.out.println("SIZE: " + toXML().toCharArray().length); 
    44         */ 
    45          
     47         lines = loadLines(highwayMetaFileName); 
     48         System.out.println("SIZE: " + toXML().toCharArray().length); 
     49         */ 
     50 
    4651        NetworkLoader ldr = new NetworkLoader(new File(ldsFileName), new File(loopsFileName)); 
    4752        this.lines = (ArrayList<FEPLine>) ldr.getFEPLines(); 
    4853        this.FEPHostName = FEPHostName; 
    4954        this.FEPPortNum = FEPPortNum; 
    50         // NEED FINISH this.highways = loadHighways(); 
     55        this.highways = loadHighways(); 
    5156        //writeHighwaysMeta("hard.txt"); 
    5257    } 
     58 
    5359     
    54     // NEED FINISH 
    55     private void loadHighways() 
    56     { 
     60    private ArrayList<Highway> loadHighways() { 
     61        System.out.println("Loading highways..."); 
     62        ArrayList<Highway> highways = new ArrayList<Highway>(); 
     63        Hashtable<Hashtable<Integer, DIRECTION>, ArrayList<Station>> 
     64                highwayTable = new Hashtable<>(); 
    5765         
    58         //highways.add(new Highway(lines, )); 
     66        for (FEPLine line : lines) 
     67        { 
     68            ArrayList<Station> lineStations = (ArrayList<Station>) line.getStations(); 
     69            for(Station station : lineStations) 
     70            { 
     71                Integer hwyNum = station.getHighwayNumber(); 
     72                DIRECTION dir = station.getDirection(); 
     73                Hashtable<Integer, DIRECTION> check = new Hashtable<>(); 
     74                check.put(hwyNum, dir); 
     75                if(highwayTable.get(check) == null) 
     76                { 
     77                    ArrayList<Station> stnList = new ArrayList<>(); 
     78                    stnList.add(station); 
     79                    Hashtable<Hashtable<Integer, DIRECTION>, ArrayList<Station>> 
     80                            newEntry = new Hashtable(); 
     81                    newEntry.put(check, stnList); 
     82                    highwayTable.putAll(newEntry); 
     83                } 
     84                else 
     85                { 
     86                    highwayTable.get(check).add(station); 
     87                } 
     88            } 
     89        } 
    5990         
    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 
     91        Set<Hashtable<Integer, DIRECTION>> hwyKeys = highwayTable.keySet(); 
     92        for(Hashtable<Integer, DIRECTION> hwyKey : hwyKeys) 
     93        { 
     94            Enumeration<Integer> hwyNumEnum = hwyKey.keys(); 
     95            Integer hwyNum = hwyNumEnum.nextElement(); 
     96            ArrayList<Station> hwyStations = highwayTable.get(hwyKey); 
     97            DIRECTION hwyDir = hwyKey.get(hwyNum); 
     98            Collections.sort(highwayTable.get(hwyKey)); 
     99            System.out.println("Adding highway: " + hwyNum + " " + hwyDir.name + " " + hwyStations.size()); 
     100            highways.add(new Highway(hwyNum,  
     101                    hwyDir, hwyStations)); 
     102        } 
     103        return highways; 
     104    } 
     105    /* 
     106     private ArrayList<FEPLine> loadLines(String highwayMetaFileName) { 
     107     ArrayList<FEPLine> lines = new ArrayList<>(); 
     108     try { 
     109     Scanner sc = new Scanner(new File(highwayMetaFileName)); 
     110     String firstLine = sc.nextLine(); 
     111 
     112     Scanner linesc = new Scanner(firstLine); 
     113     int numLines = linesc.nextInt(); 
     114     linesc.close(); 
     115 
     116     for (int i = 0; i < numLines; i++) { 
     117     System.out.println("CURR: " + i); 
     118     lines.add(loadLine(sc)); 
     119     } 
     120     sc.close(); 
     121 
     122     } catch (FileNotFoundException ex) { 
     123     Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex); 
     124     } 
     125     return lines; 
     126     } 
     127 
     128     private FEPLine loadLine(Scanner sc) { 
     129     String line = sc.nextLine(); 
     130     System.out.println(line); 
     131     Scanner scline = new Scanner(line); 
     132 
     133     int lineNum = scline.nextInt(); 
     134     int count = scline.nextInt(); 
     135     int numStations = scline.nextInt(); 
     136     ArrayList<Station> stations = new ArrayList<>(); 
     137     for (int i = 0; i < numStations; i++) { 
     138     stations.add(loadStation(sc, lineNum)); 
     139     } 
     140 
     141     return new FEPLine(lineNum, stations, count); 
     142     } 
     143 
     144     private Station loadStation(Scanner sc, int lineNum) { 
     145     String line = sc.nextLine(); 
     146     System.out.println(line); 
     147     Scanner scline = new Scanner(line); 
     148     int ldsID = scline.nextInt(); 
     149     int drop = scline.nextInt(); 
     150     int fwy = scline.nextInt(); 
     151     DIRECTION dir = DIRECTION.getEnum(scline.next()); 
     152     double postmile = scline.nextDouble(); 
     153     int numLoops = scline.nextInt(); 
     154     String location = getStationLoc(line); 
     155     ArrayList<LoopDetector> loops = new ArrayList<>(); 
     156     for (int i = 0; i < numLoops; i++) { 
     157     loops.add(loadLoop(sc)); 
     158     } 
     159 
     160     return new Station(lineNum, ldsID, drop, location, loops, fwy, dir, postmile); 
     161     } 
     162 
     163     private LoopDetector loadLoop(Scanner sc) { 
     164     String line = sc.nextLine(); 
     165     Scanner scline = new Scanner(line); 
     166 
     167     int loopID = scline.nextInt(); 
     168     int laneNum = scline.nextInt(); 
     169     String loopLoc = getLoopLoc(line); // NEED GET LOOPLOC 
     170     scline.close(); 
     171     return new LoopDetector(loopID, loopLoc, laneNum); 
     172     } 
     173 
     174     private String getLoopLoc(String line) { 
     175     Scanner sc = new Scanner(line); 
     176     sc.nextInt(); 
     177     sc.nextInt(); 
     178 
     179     // GRABS FROM CURRENT TO END OF LINE 
    136180          
    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 */ 
     181     sc.useDelimiter("\\z"); 
     182     String loc = sc.next().trim(); 
     183     sc.close(); 
     184     return loc; 
     185     } 
     186 
     187     // Returns the loction given the whole line from the lookup file 
     188     private String getStationLoc(String line) { 
     189     Scanner scline = new Scanner(line); 
     190     scline.nextInt(); 
     191     scline.nextInt(); 
     192     scline.nextInt(); 
     193     scline.next(); 
     194     scline.nextDouble(); 
     195     scline.nextInt(); 
     196 
     197     // GRABS FROM CURRENT TO END OF LINE 
     198     scline.useDelimiter("\\z"); 
     199     String loc = scline.next().trim(); 
     200     scline.close(); 
     201     return loc; 
     202     } 
     203     */ 
     204 
    160205    public void writeToFEP() { 
    161206        try { 
     
    168213        } 
    169214    } 
    170      
     215 
    171216    // CHECK: DO WE EVEN NEED TO DO THIS? 
    172217    private void updateSequences() { 
  • trunk/src/atmsdriver/model/Station.java

    r87 r88  
    5050    } 
    5151 
     52    public int getHighwayNumber() 
     53    { 
     54        return this.freeway; 
     55    } 
     56     
     57    public DIRECTION getDirection() 
     58    { 
     59        return this.direction; 
     60    } 
     61     
    5262    /** 
    5363     * Returns the station metadata in condensed form. This is just a quick 
Note: See TracChangeset for help on using the changeset viewer.