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