| 1 | package atmsdriver; |
|---|
| 2 | |
|---|
| 3 | import atmsdriver.model.Highways; |
|---|
| 4 | import atmsdriver.model.Station.DIRECTION; |
|---|
| 5 | import atmsdriver.model.Highway; |
|---|
| 6 | import atmsdriver.model.Station; |
|---|
| 7 | import java.io.FileInputStream; |
|---|
| 8 | import java.util.ArrayList; |
|---|
| 9 | import java.util.Arrays; |
|---|
| 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 | |
|---|
| 17 | /** |
|---|
| 18 | * A console application to drive the ATMS Server. |
|---|
| 19 | * |
|---|
| 20 | * @author jdalbey, John A. Torres |
|---|
| 21 | * @version 10/11/2017 |
|---|
| 22 | */ |
|---|
| 23 | public final class ConsoleDriver { |
|---|
| 24 | // highways model |
|---|
| 25 | private final Highways highways; |
|---|
| 26 | |
|---|
| 27 | // lists used for user input validation |
|---|
| 28 | private final List<Integer> routeNumInputList; |
|---|
| 29 | private final List<String> dotColorInputList; |
|---|
| 30 | /** |
|---|
| 31 | * Properties for the ConsoleDriver |
|---|
| 32 | */ |
|---|
| 33 | private static Properties ConsoleDriverProperties; |
|---|
| 34 | |
|---|
| 35 | /** Entry point for the application. |
|---|
| 36 | * |
|---|
| 37 | * @param args unused |
|---|
| 38 | */ |
|---|
| 39 | public static void main(String[] args) { |
|---|
| 40 | try { |
|---|
| 41 | if (System.getProperty("ATMSDRIVER_PROPERTIES") != null) |
|---|
| 42 | { |
|---|
| 43 | // Load properties of runtime parameters |
|---|
| 44 | if (!loadProperties()) |
|---|
| 45 | { |
|---|
| 46 | System.exit(0); |
|---|
| 47 | } |
|---|
| 48 | // Create the Highway Model |
|---|
| 49 | Highways highways = new Highways( |
|---|
| 50 | "config/vds_data/lds.txt", |
|---|
| 51 | "config/vds_data/loop.txt", |
|---|
| 52 | "config/vds_data/highwaysMeta.txt", |
|---|
| 53 | ConsoleDriverProperties.getProperty( |
|---|
| 54 | "FEPWriterHost"), |
|---|
| 55 | Integer.parseInt(ConsoleDriverProperties.getProperty( |
|---|
| 56 | "FEPWriterPort"))); |
|---|
| 57 | |
|---|
| 58 | // Construct the console driver using the highways model |
|---|
| 59 | ConsoleDriver driver = new ConsoleDriver(highways); |
|---|
| 60 | driver.runConsole(); |
|---|
| 61 | } else { |
|---|
| 62 | throw new Exception("ATMSDRIVER_PROPERTIES system property not defined."); |
|---|
| 63 | } |
|---|
| 64 | } catch (Exception e) { |
|---|
| 65 | Logger.getLogger("ConsoleDriver").logp(Level.SEVERE, "ConsoleDriver", "Main", |
|---|
| 66 | "Error occured initializing application", e); |
|---|
| 67 | System.exit(-1); |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | /** |
|---|
| 71 | * Load the properties file containing values for runtime parameters. |
|---|
| 72 | * |
|---|
| 73 | * @param propertiesFile |
|---|
| 74 | * @return |
|---|
| 75 | */ |
|---|
| 76 | private static boolean loadProperties() |
|---|
| 77 | { |
|---|
| 78 | // Load the properties file. |
|---|
| 79 | try { |
|---|
| 80 | ConsoleDriverProperties = new Properties(); |
|---|
| 81 | ConsoleDriverProperties.load(new FileInputStream(System.getProperty("ATMSDRIVER_PROPERTIES"))); |
|---|
| 82 | } catch (Exception e) { |
|---|
| 83 | Logger.getLogger("CosoleDriver").logp(Level.SEVERE, "ConsoleDriver", |
|---|
| 84 | "Constructor", "Exception in reading properties file.", e); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | return true; |
|---|
| 88 | } |
|---|
| 89 | /** |
|---|
| 90 | * Constructor. Sets the highways model and generates the input validation |
|---|
| 91 | * lists, and then runs the console driver application. |
|---|
| 92 | * @param highways |
|---|
| 93 | */ |
|---|
| 94 | public ConsoleDriver(Highways highways) { |
|---|
| 95 | // set highways model |
|---|
| 96 | this.highways = highways; |
|---|
| 97 | |
|---|
| 98 | // set input validation lists |
|---|
| 99 | routeNumInputList = generateRouteNumInputList(); |
|---|
| 100 | dotColorInputList = new ArrayList<>(Arrays.asList("R", "Y", "G")); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | /** |
|---|
| 104 | * Generates the route number list, used for user input validation. |
|---|
| 105 | * @return list of route numbers. |
|---|
| 106 | */ |
|---|
| 107 | private ArrayList<Integer> generateRouteNumInputList() |
|---|
| 108 | { |
|---|
| 109 | ArrayList<Integer> routeNums = new ArrayList<>(); |
|---|
| 110 | // add the route number for each highway to the list |
|---|
| 111 | for(Highway hwy : highways.highways) |
|---|
| 112 | { |
|---|
| 113 | routeNums.add(hwy.routeNumber); |
|---|
| 114 | } |
|---|
| 115 | return routeNums; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | /** |
|---|
| 119 | * Runs the console driver application. |
|---|
| 120 | */ |
|---|
| 121 | public void runConsole() { |
|---|
| 122 | Scanner sc = new Scanner(System.in); |
|---|
| 123 | // Run continuously |
|---|
| 124 | while (true) { |
|---|
| 125 | // Get necessary values for colorization of highways |
|---|
| 126 | Integer routeNumber = getRouteNumber(sc); |
|---|
| 127 | DIRECTION direction = getDirection(sc, routeNumber); |
|---|
| 128 | Double postmile = getPostmile(sc, routeNumber, direction); |
|---|
| 129 | Double range = getRange(sc, postmile); |
|---|
| 130 | DOTCOLOR dotcolor = getDotColor(sc); |
|---|
| 131 | |
|---|
| 132 | // apply colorization to highways |
|---|
| 133 | applyColorToHighwayStretch(routeNumber, direction, postmile, range, dotcolor); |
|---|
| 134 | } |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | /** |
|---|
| 138 | * Applies specified color to the specified highway stretch. Route number and |
|---|
| 139 | * direction specify the highway. Postmile and range specify the stretch of |
|---|
| 140 | * specified highway. Dot color is the color to be applied to the stretch. |
|---|
| 141 | * |
|---|
| 142 | * @param routeNumber highway route number |
|---|
| 143 | * @param direction highway direction |
|---|
| 144 | * @param postmile origin postmile value |
|---|
| 145 | * @param range range from origin postmile |
|---|
| 146 | * @param dotColor the color to be applied to specified highway stretch |
|---|
| 147 | */ |
|---|
| 148 | public void applyColorToHighwayStretch(Integer routeNumber, DIRECTION direction, |
|---|
| 149 | Double postmile, Double range, DOTCOLOR dotColor) { |
|---|
| 150 | System.out.println("Applying " + dotColor.name() + " dots to highway " |
|---|
| 151 | + routeNumber + " " + direction.name() + " at postmile " |
|---|
| 152 | + postmile + " with a range of " + range + " miles..."); |
|---|
| 153 | |
|---|
| 154 | // Get the highway by route number |
|---|
| 155 | Highway highway = highways.getHighwayByRouteNumber(routeNumber); |
|---|
| 156 | |
|---|
| 157 | // start value for highway section, and end value for highway section |
|---|
| 158 | // by postmile |
|---|
| 159 | Double startPost; |
|---|
| 160 | Double endPost; |
|---|
| 161 | |
|---|
| 162 | // postmiles increase from s to n and w to e |
|---|
| 163 | |
|---|
| 164 | // if the direction is south or west |
|---|
| 165 | if(direction.equals(DIRECTION.SOUTH) || direction.equals(DIRECTION.WEST)) |
|---|
| 166 | { |
|---|
| 167 | // add range value to startPost to get |
|---|
| 168 | // the end postmile value of the highway section |
|---|
| 169 | startPost = postmile; |
|---|
| 170 | endPost = postmile + range; |
|---|
| 171 | |
|---|
| 172 | // iterate through the stations, if within the specified highway |
|---|
| 173 | // stretch, update the station by direction and apply dot color |
|---|
| 174 | for(Station station : highway.stations) |
|---|
| 175 | { |
|---|
| 176 | if(station.postmile >= startPost && station.postmile <= endPost) |
|---|
| 177 | { |
|---|
| 178 | station.updateByDirection(direction, dotColor); |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | // if the direction is north or east |
|---|
| 183 | else |
|---|
| 184 | { |
|---|
| 185 | //subtract range value from startPost |
|---|
| 186 | // to get the end postmile value of the highway section |
|---|
| 187 | startPost = postmile; |
|---|
| 188 | endPost = postmile - range; |
|---|
| 189 | |
|---|
| 190 | // iterate through the stations, if within the specified highway |
|---|
| 191 | // section, update the station by direction and apply dot color |
|---|
| 192 | for(Station station : highway.stations) |
|---|
| 193 | { |
|---|
| 194 | if(station.postmile <= startPost && station.postmile >= endPost) |
|---|
| 195 | { |
|---|
| 196 | station.updateByDirection(direction, dotColor); |
|---|
| 197 | } |
|---|
| 198 | } |
|---|
| 199 | } |
|---|
| 200 | System.out.println(""); |
|---|
| 201 | // Omit this since it's now running as a task in ATMSBatchDriver |
|---|
| 202 | // try { |
|---|
| 203 | // highways.writeToFEP(); |
|---|
| 204 | // } catch (SimulationException ex) { |
|---|
| 205 | // System.out.println("Skipping writeToFEP..."); |
|---|
| 206 | // } |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | /** |
|---|
| 210 | * Gets the highway route number from user and validates the input. |
|---|
| 211 | * |
|---|
| 212 | * @param sc stdIn scanner |
|---|
| 213 | * @return highway route number |
|---|
| 214 | */ |
|---|
| 215 | private Integer getRouteNumber(Scanner sc) { |
|---|
| 216 | Integer routeNum = null; |
|---|
| 217 | Boolean verified = false; |
|---|
| 218 | |
|---|
| 219 | // validation loop |
|---|
| 220 | while(!verified) |
|---|
| 221 | { |
|---|
| 222 | // Prints out available route numbers to user to select from |
|---|
| 223 | System.out.print("Available route numbers: ["); |
|---|
| 224 | for(Integer rtNum : routeNumInputList) |
|---|
| 225 | { |
|---|
| 226 | System.out.print(rtNum.toString() + ", "); |
|---|
| 227 | } |
|---|
| 228 | System.out.print("]"); |
|---|
| 229 | System.out.println(""); |
|---|
| 230 | |
|---|
| 231 | // Prompt user to input a route number |
|---|
| 232 | System.out.println("Enter a route number: "); |
|---|
| 233 | routeNum = sc.nextInt(); |
|---|
| 234 | System.out.println(""); |
|---|
| 235 | |
|---|
| 236 | // validate the user's input |
|---|
| 237 | if(routeNumInputList.contains(routeNum)) |
|---|
| 238 | { |
|---|
| 239 | verified = true; |
|---|
| 240 | } |
|---|
| 241 | else |
|---|
| 242 | { |
|---|
| 243 | System.out.println("Invalid route number, please re-enter: "); |
|---|
| 244 | } |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | return routeNum; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | /** |
|---|
| 251 | * Gets the highway direction from the user and validates the input. |
|---|
| 252 | * |
|---|
| 253 | * @param sc stdIn scanner |
|---|
| 254 | * @return highway direction |
|---|
| 255 | */ |
|---|
| 256 | private DIRECTION getDirection(Scanner sc, Integer routeNum) { |
|---|
| 257 | DIRECTION direction; |
|---|
| 258 | String directionInput = null; |
|---|
| 259 | Boolean verified = false; |
|---|
| 260 | |
|---|
| 261 | // validation loop |
|---|
| 262 | while(!verified) |
|---|
| 263 | { |
|---|
| 264 | // Get available directions for route |
|---|
| 265 | ArrayList<DIRECTION> availDirs = new ArrayList<>(); |
|---|
| 266 | for(Station stn : highways.getHighwayByRouteNumber(routeNum).stations) |
|---|
| 267 | { |
|---|
| 268 | if(!availDirs.contains(stn.direction)) |
|---|
| 269 | { |
|---|
| 270 | availDirs.add(stn.direction); |
|---|
| 271 | } |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | // prompt user for input |
|---|
| 275 | System.out.print("Available directions for highway " + routeNum + ": ["); |
|---|
| 276 | for(DIRECTION dir : availDirs) |
|---|
| 277 | { |
|---|
| 278 | System.out.print(dir.getLetter() + ", "); |
|---|
| 279 | } |
|---|
| 280 | System.out.print("]"); |
|---|
| 281 | System.out.println(""); |
|---|
| 282 | System.out.println("Enter a direction:"); |
|---|
| 283 | directionInput = sc.next().toUpperCase(); |
|---|
| 284 | System.out.println(""); |
|---|
| 285 | |
|---|
| 286 | // validate the user's input |
|---|
| 287 | if(availDirs.contains(DIRECTION.toDirection(directionInput))) |
|---|
| 288 | { |
|---|
| 289 | verified = true; |
|---|
| 290 | } |
|---|
| 291 | else |
|---|
| 292 | { |
|---|
| 293 | System.out.println("Invalid direction, please re-enter: "); |
|---|
| 294 | } |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | return DIRECTION.toDirection(directionInput); |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | /** |
|---|
| 301 | * Gets the starting/origin postmile value for the highway section from the |
|---|
| 302 | * user and validates the input. |
|---|
| 303 | * |
|---|
| 304 | * @param sc stdIn scanner |
|---|
| 305 | * @param routeNumber highway route number |
|---|
| 306 | * @param dir highway direction |
|---|
| 307 | * @return highway section start/origin postmile value |
|---|
| 308 | */ |
|---|
| 309 | private Double getPostmile(Scanner sc, Integer routeNumber, DIRECTION dir) { |
|---|
| 310 | Double postmile = null; |
|---|
| 311 | Boolean verified = false; |
|---|
| 312 | |
|---|
| 313 | // validation loop |
|---|
| 314 | while(!verified) |
|---|
| 315 | { |
|---|
| 316 | // Get highway, and grab the floor and ceiling for postmile values |
|---|
| 317 | // from the highway stations to present to the user |
|---|
| 318 | Highway hwy = highways.getHighwayByRouteNumber(routeNumber); |
|---|
| 319 | Double floorPostmile = hwy.stations.get(0).postmile; |
|---|
| 320 | Double ceilPostmile = hwy.stations |
|---|
| 321 | .get(hwy.stations.size() - 1).postmile; |
|---|
| 322 | |
|---|
| 323 | // present user with range of postmiles for given highway |
|---|
| 324 | System.out.println("Route " + hwy.routeNumber + " " + dir |
|---|
| 325 | + " postmile range: [" + floorPostmile + ", " |
|---|
| 326 | + ceilPostmile + "]"); |
|---|
| 327 | |
|---|
| 328 | // prompt user for postmile value |
|---|
| 329 | System.out.println("Enter a postmile value (Integer/Double): "); |
|---|
| 330 | postmile = sc.nextDouble(); |
|---|
| 331 | System.out.println(""); |
|---|
| 332 | |
|---|
| 333 | // validate user's input, ensures that the postmile is within given |
|---|
| 334 | // postmile range (floorPostmile, ceilPostmile) |
|---|
| 335 | if(postmile >= floorPostmile && postmile <= ceilPostmile) |
|---|
| 336 | { |
|---|
| 337 | verified = true; |
|---|
| 338 | } |
|---|
| 339 | else |
|---|
| 340 | { |
|---|
| 341 | System.out.println("Postmile must be within postmile range: [" + floorPostmile + ", " |
|---|
| 342 | + ceilPostmile + "] please re-enter: "); |
|---|
| 343 | } |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | return postmile; |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | /** |
|---|
| 350 | * Gets the range to extend the highway stretch from the start/origin postmile |
|---|
| 351 | * value from the user and validates the input. |
|---|
| 352 | * |
|---|
| 353 | * @param sc stdIn scanner |
|---|
| 354 | * @param postmile origin/start postmile value for highway stretch |
|---|
| 355 | * @return range value |
|---|
| 356 | */ |
|---|
| 357 | private Double getRange(Scanner sc, Double postmile) { |
|---|
| 358 | Double range = null; |
|---|
| 359 | Boolean verified = false; |
|---|
| 360 | |
|---|
| 361 | // validation loop |
|---|
| 362 | while(!verified) |
|---|
| 363 | { |
|---|
| 364 | // prompt user for range value |
|---|
| 365 | System.out.println("Enter a range value (decimal):"); |
|---|
| 366 | range = sc.nextDouble(); |
|---|
| 367 | System.out.println(""); |
|---|
| 368 | |
|---|
| 369 | // range must be greater than or equal to 0 |
|---|
| 370 | if(range >= 0) |
|---|
| 371 | { |
|---|
| 372 | verified = true; |
|---|
| 373 | } |
|---|
| 374 | else |
|---|
| 375 | { |
|---|
| 376 | System.out.println("Range must be >= 0"); |
|---|
| 377 | } |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | return range; |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | /** |
|---|
| 384 | * Gets the dot color from the user, to be applied to specified highway |
|---|
| 385 | * stretch and validates the user's input. |
|---|
| 386 | * |
|---|
| 387 | * @param sc stdIn scanner |
|---|
| 388 | * @return dot color to be applied to highway stretch |
|---|
| 389 | */ |
|---|
| 390 | private DOTCOLOR getDotColor(Scanner sc) { |
|---|
| 391 | DOTCOLOR dotColor; |
|---|
| 392 | String dotColorInput = null; |
|---|
| 393 | Boolean verified = false; |
|---|
| 394 | |
|---|
| 395 | // validationloop |
|---|
| 396 | while(!verified) |
|---|
| 397 | { |
|---|
| 398 | // prompt user for color |
|---|
| 399 | System.out.println("Enter a dot color (G/Y/R):"); |
|---|
| 400 | dotColorInput = sc.next(); |
|---|
| 401 | System.out.println(""); |
|---|
| 402 | // validate user's input |
|---|
| 403 | if(dotColorInputList.contains(dotColorInput)) |
|---|
| 404 | { |
|---|
| 405 | verified = true; |
|---|
| 406 | } |
|---|
| 407 | else |
|---|
| 408 | { |
|---|
| 409 | System.out.println("Invalid dot color, please re-enter: "); |
|---|
| 410 | } |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | return DOTCOLOR.toDotColor(dotColorInput); |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | /** |
|---|
| 417 | * Enum for highway status dot colors. Each color has associated volume |
|---|
| 418 | * and occupancy constants. |
|---|
| 419 | * |
|---|
| 420 | * @author John A. Torres, jdalbey |
|---|
| 421 | * @version 10/11/2017 |
|---|
| 422 | */ |
|---|
| 423 | public static enum DOTCOLOR { |
|---|
| 424 | |
|---|
| 425 | RED(10,10), |
|---|
| 426 | YELLOW(30,20), // speed = 26 |
|---|
| 427 | GREEN(0,0); |
|---|
| 428 | |
|---|
| 429 | // All the first letters of the values, in order. |
|---|
| 430 | private static String allLetters = "RYG"; |
|---|
| 431 | |
|---|
| 432 | private int vol; /* volume */ |
|---|
| 433 | private int occ; /* occupancy */ |
|---|
| 434 | |
|---|
| 435 | private DOTCOLOR(int v, int o) |
|---|
| 436 | { |
|---|
| 437 | vol = v; |
|---|
| 438 | occ = o; |
|---|
| 439 | } |
|---|
| 440 | /** |
|---|
| 441 | * Return the first letter of this enum. |
|---|
| 442 | * |
|---|
| 443 | * @return String first letter of this enum. |
|---|
| 444 | */ |
|---|
| 445 | public String getLetter() { |
|---|
| 446 | return this.toString().substring(0, 1); |
|---|
| 447 | } |
|---|
| 448 | |
|---|
| 449 | public int volume() |
|---|
| 450 | { |
|---|
| 451 | return vol; |
|---|
| 452 | } |
|---|
| 453 | public int occupancy() |
|---|
| 454 | { |
|---|
| 455 | return occ; |
|---|
| 456 | } |
|---|
| 457 | /** |
|---|
| 458 | * Returns a dot color given its first character. |
|---|
| 459 | * |
|---|
| 460 | * @param letter the first character of a dot color |
|---|
| 461 | * @return dot color corresponding to letter |
|---|
| 462 | * @pre letter must be one of allLetters |
|---|
| 463 | */ |
|---|
| 464 | public static DOTCOLOR toDotColor(String letter) { |
|---|
| 465 | return values()[allLetters.indexOf(letter.charAt(0))]; |
|---|
| 466 | } |
|---|
| 467 | } |
|---|
| 468 | } |
|---|