| 1 | package atmsdriver; |
|---|
| 2 | |
|---|
| 3 | import atmsdriver.model.FEPLine; |
|---|
| 4 | import atmsdriver.model.Station; |
|---|
| 5 | import atmsdriver.model.LoopDetector; |
|---|
| 6 | import atmsdriver.model.Station.DIRECTION; |
|---|
| 7 | import java.io.File; |
|---|
| 8 | import java.io.FileNotFoundException; |
|---|
| 9 | import java.util.ArrayList; |
|---|
| 10 | import java.util.List; |
|---|
| 11 | import java.util.Scanner; |
|---|
| 12 | import java.util.logging.Level; |
|---|
| 13 | import java.util.logging.Logger; |
|---|
| 14 | |
|---|
| 15 | /** The NetworkReader loads all static data for the traffic network through |
|---|
| 16 | * network lookup files. |
|---|
| 17 | * |
|---|
| 18 | * The public method getFEPLines() method can be used to get all the FEPLines |
|---|
| 19 | within a network. All other methods are private and are used to construct |
|---|
| 20 | the FEPLines. |
|---|
| 21 | |
|---|
| 22 | The "LDSFile" contains Station and FEPLine information. |
|---|
| 23 | ex. |
|---|
| 24 | < Insert file here > |
|---|
| 25 | * The "loopFile" contains individual LoopDetector information. |
|---|
| 26 | * ex. |
|---|
| 27 | * < Insert file here > |
|---|
| 28 | * |
|---|
| 29 | * @author John A. Torres |
|---|
| 30 | * @version 09/10/2017 |
|---|
| 31 | */ |
|---|
| 32 | public class NetworkLoader { |
|---|
| 33 | // Two network config files |
|---|
| 34 | private final File LDSFile; |
|---|
| 35 | private final File loopFile; |
|---|
| 36 | |
|---|
| 37 | // The list of FEPLines |
|---|
| 38 | private List<FEPLine> lines; |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * Constructor |
|---|
| 42 | * @param LDSFile contains FEPLine and Station static data |
|---|
| 43 | * @param loopFile contains individual LoopDetector static data |
|---|
| 44 | */ |
|---|
| 45 | public NetworkLoader(File LDSFile, File loopFile) |
|---|
| 46 | { |
|---|
| 47 | this.LDSFile = LDSFile; |
|---|
| 48 | this.loopFile = loopFile; |
|---|
| 49 | this.lines = new ArrayList<>(); |
|---|
| 50 | constructFEPLines(); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * Returns the list of FEPLines in the configured network |
|---|
| 55 | */ |
|---|
| 56 | public List<FEPLine> getFEPLines() |
|---|
| 57 | { |
|---|
| 58 | return lines; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | // returns a list of the line numbers found in LDSFile |
|---|
| 62 | private ArrayList<Integer> getLineNums(Scanner sc) |
|---|
| 63 | { |
|---|
| 64 | ArrayList<Integer> lineNums = new ArrayList<>(); |
|---|
| 65 | |
|---|
| 66 | sc.nextLine(); // skip first line |
|---|
| 67 | |
|---|
| 68 | while(sc.hasNext()) |
|---|
| 69 | { |
|---|
| 70 | sc.nextInt(); // skip to line no |
|---|
| 71 | Integer lineNum = sc.nextInt(); // get the line number |
|---|
| 72 | |
|---|
| 73 | /* If the line is new, and has not been added, add it to lineNums */ |
|---|
| 74 | if(!lineNums.contains(lineNum)) |
|---|
| 75 | { |
|---|
| 76 | lineNums.add(lineNum); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | sc.nextLine(); // skip to next line |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | sc.close(); |
|---|
| 83 | |
|---|
| 84 | return lineNums; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | // returns a list of station numbers for a given line |
|---|
| 88 | private ArrayList<Integer> getStationNums(Integer lineNum, Scanner sc) |
|---|
| 89 | { |
|---|
| 90 | ArrayList<Integer> stnNums = new ArrayList<>(); |
|---|
| 91 | |
|---|
| 92 | sc.nextLine(); // skip first line |
|---|
| 93 | |
|---|
| 94 | while(sc.hasNext()) |
|---|
| 95 | { |
|---|
| 96 | Integer stnNum = sc.nextInt(); |
|---|
| 97 | Integer theLine = sc.nextInt(); |
|---|
| 98 | if(theLine.equals(lineNum)) |
|---|
| 99 | { |
|---|
| 100 | stnNums.add(stnNum); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | sc.nextLine(); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | sc.close(); |
|---|
| 107 | |
|---|
| 108 | return stnNums; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | // returns a list of loop detectors for a given station |
|---|
| 112 | private ArrayList<LoopDetector> getLoops(Integer stnNum, Scanner sc) |
|---|
| 113 | { |
|---|
| 114 | ArrayList<LoopDetector> loops = new ArrayList<>(); |
|---|
| 115 | |
|---|
| 116 | sc.nextLine(); // skip first line |
|---|
| 117 | |
|---|
| 118 | while(sc.hasNext()) |
|---|
| 119 | { |
|---|
| 120 | sc.next(); // skip FWY |
|---|
| 121 | sc.next(); // skip dir |
|---|
| 122 | sc.next(); // skip postmile |
|---|
| 123 | Integer ldsID = sc.nextInt(); // lds id |
|---|
| 124 | sc.next(); // skip vdsID |
|---|
| 125 | Integer loopID = sc.nextInt(); // loop id |
|---|
| 126 | sc.next(); // skip LOC |
|---|
| 127 | Integer laneNum = sc.nextInt(); // lane number |
|---|
| 128 | String loopLoc = sc.next(); |
|---|
| 129 | if(stnNum.equals(ldsID)) |
|---|
| 130 | { |
|---|
| 131 | LoopDetector loop = new LoopDetector(loopID, loopLoc, laneNum); |
|---|
| 132 | loops.add(loop); |
|---|
| 133 | } |
|---|
| 134 | sc.nextLine(); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | sc.close(); |
|---|
| 138 | |
|---|
| 139 | return loops; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | // method called to actually build the network from config files |
|---|
| 143 | private void constructFEPLines() |
|---|
| 144 | { |
|---|
| 145 | try { |
|---|
| 146 | // Get FEPLine IDs |
|---|
| 147 | ArrayList<Integer> lineNums = getLineNums(new Scanner(LDSFile)); |
|---|
| 148 | |
|---|
| 149 | // Create each FEPLine in the network |
|---|
| 150 | for(Integer lineNum : lineNums) |
|---|
| 151 | { |
|---|
| 152 | // Get Station IDs for the current FEPLine |
|---|
| 153 | ArrayList<Integer> stnNums = getStationNums(lineNum, |
|---|
| 154 | new Scanner(LDSFile)); |
|---|
| 155 | |
|---|
| 156 | // Create each Station for the current FEPLine |
|---|
| 157 | ArrayList<Station> stns = new ArrayList<>(); |
|---|
| 158 | for(Integer stnNum : stnNums) |
|---|
| 159 | { |
|---|
| 160 | // Get Loops for the current Station |
|---|
| 161 | ArrayList<LoopDetector> loops = |
|---|
| 162 | getLoops(stnNum, new Scanner(loopFile)); |
|---|
| 163 | |
|---|
| 164 | // Create the Station and add to list for curr FEPLine |
|---|
| 165 | Station stn = |
|---|
| 166 | createStation(stnNum, new Scanner(LDSFile), loops); |
|---|
| 167 | stns.add(stn); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | // Now that stations are created, create the actual FEPLine and |
|---|
| 171 | // and to FEPLines list |
|---|
| 172 | FEPLine line = createLine(lineNum, new Scanner(LDSFile), stns); |
|---|
| 173 | lines.add(line); |
|---|
| 174 | } |
|---|
| 175 | } catch (FileNotFoundException ex) { |
|---|
| 176 | Logger.getLogger(NetworkLoader.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | // Creates line |
|---|
| 181 | private FEPLine createLine(int lineNum, Scanner scLine, ArrayList<Station> stns) |
|---|
| 182 | { |
|---|
| 183 | FEPLine line = null; |
|---|
| 184 | |
|---|
| 185 | scLine.nextLine(); |
|---|
| 186 | |
|---|
| 187 | while(scLine.hasNext()) |
|---|
| 188 | { |
|---|
| 189 | scLine.nextInt(); // skip ldsID |
|---|
| 190 | int currLineNum = scLine.nextInt(); // linenum |
|---|
| 191 | if(currLineNum == lineNum) |
|---|
| 192 | { |
|---|
| 193 | scLine.nextInt(); // skip drop |
|---|
| 194 | int sch = scLine.nextInt(); // schedule |
|---|
| 195 | int lineinfo = scLine.nextInt(); // lineinfo |
|---|
| 196 | int sysKey = scLine.nextInt(); // sysKey |
|---|
| 197 | int schSeq = scLine.nextInt(); // schSeq |
|---|
| 198 | int globSeq = scLine.nextInt(); // globSeq |
|---|
| 199 | int count = scLine.nextInt(); // count |
|---|
| 200 | |
|---|
| 201 | line = new FEPLine(lineNum, stns, count, sch, lineinfo, sysKey, globSeq, schSeq); |
|---|
| 202 | |
|---|
| 203 | break; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | scLine.nextLine(); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | scLine.close(); |
|---|
| 210 | |
|---|
| 211 | return line; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | // Returns the loction given the whole line from the lookup file |
|---|
| 215 | private String getLocation(String line) |
|---|
| 216 | { |
|---|
| 217 | Scanner sc = new Scanner(line); |
|---|
| 218 | sc.nextInt(); // skip lds num |
|---|
| 219 | sc.nextInt(); // skip line num |
|---|
| 220 | int drop = sc.nextInt(); // drop num |
|---|
| 221 | sc.nextInt(); // skip schedule |
|---|
| 222 | sc.nextInt(); // skip lineinfo |
|---|
| 223 | sc.nextInt(); // skp systemkey |
|---|
| 224 | sc.nextInt(); // skip sch_seq |
|---|
| 225 | sc.nextInt(); // skip glob_seq |
|---|
| 226 | sc.nextInt(); // skip count |
|---|
| 227 | sc.nextInt(); // fwy |
|---|
| 228 | DIRECTION dir = DIRECTION.getEnum(sc.next()); // direction |
|---|
| 229 | sc.nextDouble(); |
|---|
| 230 | |
|---|
| 231 | /** GRABS FROM CURRENT TO END OF LINE */ |
|---|
| 232 | sc.useDelimiter("\\z"); |
|---|
| 233 | String loc = sc.next().trim(); |
|---|
| 234 | sc.close(); |
|---|
| 235 | return loc; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | // creates a Station |
|---|
| 239 | private Station createStation(int stnNum, Scanner sc, ArrayList<LoopDetector> detectors) |
|---|
| 240 | { |
|---|
| 241 | Station LDS = null; |
|---|
| 242 | |
|---|
| 243 | sc.nextLine(); |
|---|
| 244 | |
|---|
| 245 | while(sc.hasNext()) |
|---|
| 246 | { |
|---|
| 247 | String strLine = sc.nextLine(); |
|---|
| 248 | Scanner scLine = new Scanner(strLine); |
|---|
| 249 | int ldsID = scLine.nextInt(); // get curr lds id |
|---|
| 250 | if(ldsID == stnNum) // if we are on correct stn |
|---|
| 251 | { |
|---|
| 252 | int lineNum = scLine.nextInt(); // skip line num |
|---|
| 253 | int drop = scLine.nextInt(); // drop num |
|---|
| 254 | scLine.nextInt(); // skip schedule |
|---|
| 255 | scLine.nextInt(); // skip lineinfo |
|---|
| 256 | scLine.nextInt(); // skp systemkey |
|---|
| 257 | scLine.nextInt(); // skip sch_seq |
|---|
| 258 | scLine.nextInt(); // skip glob_seq |
|---|
| 259 | scLine.nextInt(); // skip count |
|---|
| 260 | int fwy = scLine.nextInt(); // fwy |
|---|
| 261 | DIRECTION dir = DIRECTION.getEnum(scLine.next()); // direction |
|---|
| 262 | double postmile = scLine.nextDouble(); |
|---|
| 263 | String ldsName = getLocation(strLine); /************* DOESNT GRAB WHOLE???? *///// |
|---|
| 264 | LDS = new Station(lineNum, ldsID, drop, ldsName, detectors, fwy, dir, postmile); |
|---|
| 265 | |
|---|
| 266 | break; |
|---|
| 267 | } |
|---|
| 268 | scLine.close(); |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | sc.close(); |
|---|
| 272 | |
|---|
| 273 | return LDS; |
|---|
| 274 | } |
|---|
| 275 | } |
|---|