| 1 | package atmsdriver; |
|---|
| 2 | |
|---|
| 3 | import atmsdriver.network.model.FEPLine; |
|---|
| 4 | import atmsdriver.network.model.LoopDetectorStation; |
|---|
| 5 | import atmsdriver.network.model.LoopDetector; |
|---|
| 6 | import atmsdriver.network.model.LoopDetectorStation.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 LoopDetectorStation 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 NetworkReader { |
|---|
| 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 NetworkReader(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 | System.out.println("Building network..."); |
|---|
| 147 | |
|---|
| 148 | // Get FEPLine IDs |
|---|
| 149 | ArrayList<Integer> lineNums = getLineNums(new Scanner(LDSFile)); |
|---|
| 150 | |
|---|
| 151 | // Create each FEPLine in the network |
|---|
| 152 | for(Integer lineNum : lineNums) |
|---|
| 153 | { |
|---|
| 154 | // Get Station IDs for the current FEPLine |
|---|
| 155 | ArrayList<Integer> stnNums = getStationNums(lineNum, |
|---|
| 156 | new Scanner(LDSFile)); |
|---|
| 157 | |
|---|
| 158 | // Create each Station for the current FEPLine |
|---|
| 159 | ArrayList<LoopDetectorStation> stns = new ArrayList<>(); |
|---|
| 160 | for(Integer stnNum : stnNums) |
|---|
| 161 | { |
|---|
| 162 | // Get Loops for the current Station |
|---|
| 163 | ArrayList<LoopDetector> loops = |
|---|
| 164 | getLoops(stnNum, new Scanner(loopFile)); |
|---|
| 165 | |
|---|
| 166 | // Create the Station and add to list for curr FEPLine |
|---|
| 167 | LoopDetectorStation stn = |
|---|
| 168 | createStation(stnNum, new Scanner(LDSFile), loops); |
|---|
| 169 | stns.add(stn); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | // Now that stations are created, create the actual FEPLine and |
|---|
| 173 | // and to FEPLines list |
|---|
| 174 | FEPLine line = createLine(lineNum, new Scanner(LDSFile), stns); |
|---|
| 175 | lines.add(line); |
|---|
| 176 | } |
|---|
| 177 | } catch (FileNotFoundException ex) { |
|---|
| 178 | Logger.getLogger(NetworkReader.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | // Creates line |
|---|
| 183 | private FEPLine createLine(int lineNum, Scanner scLine, ArrayList<LoopDetectorStation> stns) |
|---|
| 184 | { |
|---|
| 185 | FEPLine line = null; |
|---|
| 186 | |
|---|
| 187 | scLine.nextLine(); |
|---|
| 188 | |
|---|
| 189 | while(scLine.hasNext()) |
|---|
| 190 | { |
|---|
| 191 | scLine.nextInt(); // skip ldsID |
|---|
| 192 | int currLineNum = scLine.nextInt(); // linenum |
|---|
| 193 | if(currLineNum == lineNum) |
|---|
| 194 | { |
|---|
| 195 | scLine.nextInt(); // skip drop |
|---|
| 196 | int sch = scLine.nextInt(); // schedule |
|---|
| 197 | int lineinfo = scLine.nextInt(); // lineinfo |
|---|
| 198 | int sysKey = scLine.nextInt(); // sysKey |
|---|
| 199 | int schSeq = scLine.nextInt(); // schSeq |
|---|
| 200 | int globSeq = scLine.nextInt(); // globSeq |
|---|
| 201 | int count = scLine.nextInt(); // count |
|---|
| 202 | |
|---|
| 203 | line = new FEPLine(lineNum, stns, count, sch, lineinfo, sysKey, globSeq, schSeq); |
|---|
| 204 | |
|---|
| 205 | break; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | scLine.nextLine(); |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | scLine.close(); |
|---|
| 212 | |
|---|
| 213 | return line; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | // Returns the loction given the whole line from the lookup file |
|---|
| 217 | private String getLocation(String line) |
|---|
| 218 | { |
|---|
| 219 | Scanner sc = new Scanner(line); |
|---|
| 220 | sc.nextInt(); // skip lds num |
|---|
| 221 | sc.nextInt(); // skip line num |
|---|
| 222 | int drop = sc.nextInt(); // drop num |
|---|
| 223 | sc.nextInt(); // skip schedule |
|---|
| 224 | sc.nextInt(); // skip lineinfo |
|---|
| 225 | sc.nextInt(); // skp systemkey |
|---|
| 226 | sc.nextInt(); // skip sch_seq |
|---|
| 227 | sc.nextInt(); // skip glob_seq |
|---|
| 228 | sc.nextInt(); // skip count |
|---|
| 229 | sc.nextInt(); // fwy |
|---|
| 230 | DIRECTION dir = DIRECTION.getEnum(sc.next()); // direction |
|---|
| 231 | sc.nextDouble(); |
|---|
| 232 | |
|---|
| 233 | /** GRABS FROM CURRENT TO END OF LINE */ |
|---|
| 234 | sc.useDelimiter("\\z"); |
|---|
| 235 | String loc = sc.next().trim(); |
|---|
| 236 | sc.close(); |
|---|
| 237 | return loc; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | // creates a Station |
|---|
| 241 | private LoopDetectorStation createStation(int stnNum, Scanner sc, ArrayList<LoopDetector> detectors) |
|---|
| 242 | { |
|---|
| 243 | LoopDetectorStation LDS = null; |
|---|
| 244 | |
|---|
| 245 | sc.nextLine(); |
|---|
| 246 | |
|---|
| 247 | while(sc.hasNext()) |
|---|
| 248 | { |
|---|
| 249 | String strLine = sc.nextLine(); |
|---|
| 250 | Scanner scLine = new Scanner(strLine); |
|---|
| 251 | int ldsID = scLine.nextInt(); // get curr lds id |
|---|
| 252 | if(ldsID == stnNum) // if we are on correct stn |
|---|
| 253 | { |
|---|
| 254 | int lineNum = scLine.nextInt(); // skip line num |
|---|
| 255 | int drop = scLine.nextInt(); // drop num |
|---|
| 256 | scLine.nextInt(); // skip schedule |
|---|
| 257 | scLine.nextInt(); // skip lineinfo |
|---|
| 258 | scLine.nextInt(); // skp systemkey |
|---|
| 259 | scLine.nextInt(); // skip sch_seq |
|---|
| 260 | scLine.nextInt(); // skip glob_seq |
|---|
| 261 | scLine.nextInt(); // skip count |
|---|
| 262 | int fwy = scLine.nextInt(); // fwy |
|---|
| 263 | DIRECTION dir = DIRECTION.getEnum(scLine.next()); // direction |
|---|
| 264 | double postmile = scLine.nextDouble(); |
|---|
| 265 | String ldsName = getLocation(strLine); /************* DOESNT GRAB WHOLE???? *///// |
|---|
| 266 | LDS = new LoopDetectorStation(lineNum, ldsID, drop, ldsName, detectors, fwy, dir, postmile); |
|---|
| 267 | |
|---|
| 268 | break; |
|---|
| 269 | } |
|---|
| 270 | scLine.close(); |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | sc.close(); |
|---|
| 274 | |
|---|
| 275 | return LDS; |
|---|
| 276 | } |
|---|
| 277 | } |
|---|