source: tmcsimulator/trunk/src/atmsdriver/ConsoleTrafficDriver.java @ 422

Revision 422, 11.3 KB checked in by jdalbey, 7 years ago (diff)

Remove ATMS functionality. Reworked and simplified the Highway model to use only VDS data from PeMS. Updated all unit tests.

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