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