| 1 | package tmcsim.highwaymodel; |
|---|
| 2 | |
|---|
| 3 | import tmcsim.highwaymodel.Highways; |
|---|
| 4 | import tmcsim.highwaymodel.Station.DIRECTION; |
|---|
| 5 | import tmcsim.highwaymodel.Highway; |
|---|
| 6 | import tmcsim.highwaymodel.Station; |
|---|
| 7 | import tmcsim.highwaymodel.LoopDetector.DOTCOLOR; |
|---|
| 8 | import java.io.FileInputStream; |
|---|
| 9 | import java.util.ArrayList; |
|---|
| 10 | import java.util.List; |
|---|
| 11 | import java.util.Properties; |
|---|
| 12 | import java.util.Scanner; |
|---|
| 13 | import java.util.logging.Level; |
|---|
| 14 | import java.util.logging.Logger; |
|---|
| 15 | import tmcsim.common.SimulationException; |
|---|
| 16 | import tmcsim.highwaymodel.Highway; |
|---|
| 17 | import tmcsim.highwaymodel.Highways; |
|---|
| 18 | import tmcsim.highwaymodel.Station; |
|---|
| 19 | |
|---|
| 20 | /** |
|---|
| 21 | * A console application to drive the ATMS Server. |
|---|
| 22 | * Use for test driving the Highway Model. |
|---|
| 23 | * |
|---|
| 24 | * @author jdalbey, John A. Torres |
|---|
| 25 | * @version 10/11/2017 |
|---|
| 26 | */ |
|---|
| 27 | public final class ConsoleTrafficDriver { |
|---|
| 28 | // highways model |
|---|
| 29 | private final Highways highways; |
|---|
| 30 | |
|---|
| 31 | // lists used for user input validation |
|---|
| 32 | private final List<Integer> routeNumInputList; |
|---|
| 33 | /** |
|---|
| 34 | * Properties for the ConsoleTrafficDriver |
|---|
| 35 | */ |
|---|
| 36 | private static Properties ConsoleDriverProperties; |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | /** Entry point for the application. |
|---|
| 40 | * |
|---|
| 41 | * @param args unused |
|---|
| 42 | */ |
|---|
| 43 | public static void main(String[] args) { |
|---|
| 44 | try { |
|---|
| 45 | if (System.getProperty("ATMSDRIVER_PROPERTIES") != null) |
|---|
| 46 | { |
|---|
| 47 | // Load properties of runtime parameters |
|---|
| 48 | if (!loadProperties()) |
|---|
| 49 | { |
|---|
| 50 | System.exit(0); |
|---|
| 51 | } |
|---|
| 52 | // Create the Highway Model |
|---|
| 53 | Highways highways = new Highways( |
|---|
| 54 | "config/vds_data/highways_fullmap.txt"); |
|---|
| 55 | |
|---|
| 56 | // Construct the console driver using the highways model |
|---|
| 57 | ConsoleTrafficDriver driver = new ConsoleTrafficDriver(highways); |
|---|
| 58 | driver.runConsole(); |
|---|
| 59 | } else { |
|---|
| 60 | throw new Exception("ATMSDRIVER_PROPERTIES system property not defined."); |
|---|
| 61 | } |
|---|
| 62 | } catch (Exception e) { |
|---|
| 63 | Logger.getLogger("ConsoleDriver").logp(Level.SEVERE, "ConsoleDriver", "Main", |
|---|
| 64 | "Error occured initializing application", e); |
|---|
| 65 | System.exit(-1); |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | /** |
|---|
| 69 | * Load the properties file containing values for runtime parameters. |
|---|
| 70 | * |
|---|
| 71 | * @param propertiesFile |
|---|
| 72 | * @return |
|---|
| 73 | */ |
|---|
| 74 | private static boolean loadProperties() |
|---|
| 75 | { |
|---|
| 76 | // Load the properties file. |
|---|
| 77 | try { |
|---|
| 78 | ConsoleDriverProperties = new Properties(); |
|---|
| 79 | ConsoleDriverProperties.load(new FileInputStream(System.getProperty("ATMSDRIVER_PROPERTIES"))); |
|---|
| 80 | } catch (Exception e) { |
|---|
| 81 | Logger.getLogger("ConsoleDriver").logp(Level.SEVERE, "ConsoleDriver", |
|---|
| 82 | "Constructor", "Exception in reading properties file.", e); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | return true; |
|---|
| 86 | } |
|---|
| 87 | /** |
|---|
| 88 | * Constructor. Sets the highways model and generates the input validation |
|---|
| 89 | * lists, and then runs the console driver application. |
|---|
| 90 | * @param highways |
|---|
| 91 | */ |
|---|
| 92 | public ConsoleTrafficDriver(Highways highways) { |
|---|
| 93 | // set highways model |
|---|
| 94 | this.highways = highways; |
|---|
| 95 | |
|---|
| 96 | // set input validation lists |
|---|
| 97 | routeNumInputList = generateRouteNumInputList(); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | /** |
|---|
| 101 | * Generates the route number list, used for user input validation. |
|---|
| 102 | * @return list of route numbers. |
|---|
| 103 | */ |
|---|
| 104 | private ArrayList<Integer> generateRouteNumInputList() |
|---|
| 105 | { |
|---|
| 106 | ArrayList<Integer> routeNums = new ArrayList<>(); |
|---|
| 107 | // add the route number for each highway to the list |
|---|
| 108 | for(Highway hwy : highways.highways) |
|---|
| 109 | { |
|---|
| 110 | routeNums.add(hwy.routeNumber); |
|---|
| 111 | } |
|---|
| 112 | return routeNums; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | /** |
|---|
| 116 | * Runs the console driver application. |
|---|
| 117 | */ |
|---|
| 118 | public void runConsole() { |
|---|
| 119 | Scanner sc = new Scanner(System.in); |
|---|
| 120 | char choice = 'A'; |
|---|
| 121 | // Run continuously |
|---|
| 122 | while (choice != 'Q') |
|---|
| 123 | { |
|---|
| 124 | // Get necessary values for colorization of highways |
|---|
| 125 | Integer routeNumber = getRouteNumber(sc); |
|---|
| 126 | DIRECTION direction = getDirection(sc, routeNumber); |
|---|
| 127 | Double postmile = getPostmile(sc, routeNumber, direction); |
|---|
| 128 | Double range = getRange(sc, postmile); |
|---|
| 129 | DOTCOLOR dotcolor = getDotColor(sc); |
|---|
| 130 | |
|---|
| 131 | // apply colorization to highways |
|---|
| 132 | highways.applyColorToHighwayStretch(routeNumber, direction, postmile, range, dotcolor); |
|---|
| 133 | |
|---|
| 134 | System.out.println("Add another entry or Send now? (A/S)"); |
|---|
| 135 | choice = sc.next().toUpperCase().trim().charAt(0); |
|---|
| 136 | System.out.println(""); |
|---|
| 137 | if (choice == 'S') |
|---|
| 138 | { |
|---|
| 139 | // Send highway model to FEP for transmit to ATMS |
|---|
| 140 | highways.toJson(); |
|---|
| 141 | System.out.println("Add another entry or Quit? (A/Q)"); |
|---|
| 142 | choice = sc.next().toUpperCase().trim().charAt(0); |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | /** |
|---|
| 149 | * Gets the highway route number from user and validates the input. |
|---|
| 150 | * |
|---|
| 151 | * @param sc stdIn scanner |
|---|
| 152 | * @return highway route number |
|---|
| 153 | */ |
|---|
| 154 | private Integer getRouteNumber(Scanner sc) { |
|---|
| 155 | Integer routeNum = null; |
|---|
| 156 | Boolean verified = false; |
|---|
| 157 | |
|---|
| 158 | // validation loop |
|---|
| 159 | while(!verified) |
|---|
| 160 | { |
|---|
| 161 | // Prints out available route numbers to user to select from |
|---|
| 162 | System.out.print("Available route numbers: ["); |
|---|
| 163 | for(Integer rtNum : routeNumInputList) |
|---|
| 164 | { |
|---|
| 165 | System.out.print(rtNum.toString() + ", "); |
|---|
| 166 | } |
|---|
| 167 | System.out.print("]"); |
|---|
| 168 | System.out.println(""); |
|---|
| 169 | |
|---|
| 170 | // Prompt user to input a route number |
|---|
| 171 | System.out.println("Enter a route number: "); |
|---|
| 172 | routeNum = sc.nextInt(); |
|---|
| 173 | System.out.println(""); |
|---|
| 174 | |
|---|
| 175 | // validate the user's input |
|---|
| 176 | if(routeNumInputList.contains(routeNum)) |
|---|
| 177 | { |
|---|
| 178 | verified = true; |
|---|
| 179 | } |
|---|
| 180 | else |
|---|
| 181 | { |
|---|
| 182 | System.out.println("Invalid route number, please re-enter: "); |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | return routeNum; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | /** |
|---|
| 190 | * Gets the highway direction from the user and validates the input. |
|---|
| 191 | * |
|---|
| 192 | * @param sc stdIn scanner |
|---|
| 193 | * @return highway direction |
|---|
| 194 | */ |
|---|
| 195 | private DIRECTION getDirection(Scanner sc, Integer routeNum) { |
|---|
| 196 | DIRECTION direction; |
|---|
| 197 | String directionInput = null; |
|---|
| 198 | Boolean verified = false; |
|---|
| 199 | |
|---|
| 200 | // validation loop |
|---|
| 201 | while(!verified) |
|---|
| 202 | { |
|---|
| 203 | // Get available directions for route |
|---|
| 204 | ArrayList<DIRECTION> availDirs = new ArrayList<>(); |
|---|
| 205 | for(Station stn : highways.getHighwayByRouteNumber(routeNum).stations) |
|---|
| 206 | { |
|---|
| 207 | if(!availDirs.contains(stn.direction)) |
|---|
| 208 | { |
|---|
| 209 | availDirs.add(stn.direction); |
|---|
| 210 | } |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | // prompt user for input |
|---|
| 214 | System.out.print("Available directions for highway " + routeNum + ": ["); |
|---|
| 215 | for(DIRECTION dir : availDirs) |
|---|
| 216 | { |
|---|
| 217 | System.out.print(dir.getLetter() + ", "); |
|---|
| 218 | } |
|---|
| 219 | System.out.print("]"); |
|---|
| 220 | System.out.println(""); |
|---|
| 221 | System.out.println("Enter a direction:"); |
|---|
| 222 | directionInput = sc.next().toUpperCase(); |
|---|
| 223 | System.out.println(""); |
|---|
| 224 | |
|---|
| 225 | // validate the user's input |
|---|
| 226 | if(availDirs.contains(DIRECTION.toDirection(directionInput))) |
|---|
| 227 | { |
|---|
| 228 | verified = true; |
|---|
| 229 | } |
|---|
| 230 | else |
|---|
| 231 | { |
|---|
| 232 | System.out.println("Invalid direction, please re-enter: "); |
|---|
| 233 | } |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | return DIRECTION.toDirection(directionInput); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | /** |
|---|
| 240 | * Gets the starting/origin postmile value for the highway section from the |
|---|
| 241 | * user and validates the input. |
|---|
| 242 | * |
|---|
| 243 | * @param sc stdIn scanner |
|---|
| 244 | * @param routeNumber highway route number |
|---|
| 245 | * @param dir highway direction |
|---|
| 246 | * @return highway section start/origin postmile value |
|---|
| 247 | */ |
|---|
| 248 | private Double getPostmile(Scanner sc, Integer routeNumber, DIRECTION dir) { |
|---|
| 249 | Double postmile = null; |
|---|
| 250 | Boolean verified = false; |
|---|
| 251 | |
|---|
| 252 | // validation loop |
|---|
| 253 | while(!verified) |
|---|
| 254 | { |
|---|
| 255 | // Get highway, and grab the floor and ceiling for postmile values |
|---|
| 256 | // from the highway stations to present to the user |
|---|
| 257 | Highway hwy = highways.getHighwayByRouteNumber(routeNumber); |
|---|
| 258 | Double floorPostmile = hwy.stations.get(0).postmile; |
|---|
| 259 | Double ceilPostmile = hwy.stations |
|---|
| 260 | .get(hwy.stations.size() - 1).postmile; |
|---|
| 261 | |
|---|
| 262 | // present user with range of postmiles for given highway |
|---|
| 263 | System.out.println("Route " + hwy.routeNumber + " " + dir |
|---|
| 264 | + " postmile range: [" + floorPostmile + ", " |
|---|
| 265 | + ceilPostmile + "]"); |
|---|
| 266 | |
|---|
| 267 | // prompt user for postmile value |
|---|
| 268 | System.out.println("Enter a postmile value (Integer/Double): "); |
|---|
| 269 | postmile = sc.nextDouble(); |
|---|
| 270 | System.out.println(""); |
|---|
| 271 | |
|---|
| 272 | // validate user's input, ensures that the postmile is within given |
|---|
| 273 | // postmile range (floorPostmile, ceilPostmile) |
|---|
| 274 | if(postmile >= floorPostmile && postmile <= ceilPostmile) |
|---|
| 275 | { |
|---|
| 276 | verified = true; |
|---|
| 277 | } |
|---|
| 278 | else |
|---|
| 279 | { |
|---|
| 280 | System.out.println("Postmile must be within postmile range: [" + floorPostmile + ", " |
|---|
| 281 | + ceilPostmile + "] please re-enter: "); |
|---|
| 282 | } |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | return postmile; |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | /** |
|---|
| 289 | * Gets the range to extend the highway stretch from the start/origin postmile |
|---|
| 290 | * value from the user and validates the input. |
|---|
| 291 | * |
|---|
| 292 | * @param sc stdIn scanner |
|---|
| 293 | * @param postmile origin/start postmile value for highway stretch |
|---|
| 294 | * @return range value |
|---|
| 295 | */ |
|---|
| 296 | private Double getRange(Scanner sc, Double postmile) { |
|---|
| 297 | Double range = null; |
|---|
| 298 | Boolean verified = false; |
|---|
| 299 | |
|---|
| 300 | // validation loop |
|---|
| 301 | while(!verified) |
|---|
| 302 | { |
|---|
| 303 | // prompt user for range value |
|---|
| 304 | System.out.println("Enter a range value (decimal):"); |
|---|
| 305 | range = sc.nextDouble(); |
|---|
| 306 | System.out.println(""); |
|---|
| 307 | |
|---|
| 308 | // range must be greater than or equal to 0 |
|---|
| 309 | if(range >= 0) |
|---|
| 310 | { |
|---|
| 311 | verified = true; |
|---|
| 312 | } |
|---|
| 313 | else |
|---|
| 314 | { |
|---|
| 315 | System.out.println("Range must be >= 0"); |
|---|
| 316 | } |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | return range; |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | /** |
|---|
| 323 | * Gets the dot color from the user, to be applied to specified highway |
|---|
| 324 | * stretch and validates the user's input. |
|---|
| 325 | * |
|---|
| 326 | * @param sc stdIn scanner |
|---|
| 327 | * @return dot color to be applied to highway stretch |
|---|
| 328 | */ |
|---|
| 329 | private DOTCOLOR getDotColor(Scanner sc) { |
|---|
| 330 | DOTCOLOR dotColor; |
|---|
| 331 | String dotColorInput = null; |
|---|
| 332 | Boolean verified = false; |
|---|
| 333 | |
|---|
| 334 | // validationloop |
|---|
| 335 | while(!verified) |
|---|
| 336 | { |
|---|
| 337 | // prompt user for color |
|---|
| 338 | System.out.println("Enter a dot color (G/Y/R):"); |
|---|
| 339 | dotColorInput = sc.next().toUpperCase(); |
|---|
| 340 | System.out.println(""); |
|---|
| 341 | // validate user's input |
|---|
| 342 | if(DOTCOLOR.allLetters.contains(dotColorInput)) |
|---|
| 343 | { |
|---|
| 344 | verified = true; |
|---|
| 345 | } |
|---|
| 346 | else |
|---|
| 347 | { |
|---|
| 348 | System.out.println("Invalid dot color, please re-enter: "); |
|---|
| 349 | } |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | return DOTCOLOR.toDotColor(dotColorInput); |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | } |
|---|