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

Revision 237, 11.6 KB checked in by jtorres, 8 years ago (diff)

Added new package atmsdriver.batchbuilder. Includes BatchBuilderGUI, TimeFrame?, TimeFrames?, and TrafficLaneEvent? classes. Added some auxillary methods to Highways.java, Highway.java, and Station.java.

Line 
1package atmsdriver;
2
3import atmsdriver.model.Highways;
4import atmsdriver.model.Station.DIRECTION;
5import atmsdriver.model.Highway;
6import atmsdriver.model.Station;
7import atmsdriver.model.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 *
20 * @author jdalbey, John A. Torres
21 * @version 10/11/2017
22 */
23public 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
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/highways_fullmap.txt",
51                    ConsoleDriverProperties.getProperty(
52                        "FEPWriterHost"),
53                    Integer.parseInt(ConsoleDriverProperties.getProperty(
54                        "FEPWriterPort")));
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                try {
141                    highways.writeToFEP();
142                } catch (SimulationException ex) {
143                    System.out.println("Skipping writeToFEP...");
144                }
145                System.out.println("Add another entry or Quit? (A/Q)");
146                choice = sc.next().toUpperCase().trim().charAt(0);
147            }
148        }
149    }
150   
151   
152    /**
153     * Gets the highway route number from user and validates the input.
154     *
155     * @param sc stdIn scanner
156     * @return highway route number
157     */
158    private Integer getRouteNumber(Scanner sc) {
159        Integer routeNum = null;
160        Boolean verified = false;
161       
162        // validation loop
163        while(!verified)
164        {
165            // Prints out available route numbers to user to select from
166            System.out.print("Available route numbers: [");
167            for(Integer rtNum : routeNumInputList)
168            {
169                System.out.print(rtNum.toString() + ", ");
170            }
171            System.out.print("]");
172            System.out.println("");
173           
174            // Prompt user to input a route number
175            System.out.println("Enter a route number: ");
176            routeNum = sc.nextInt();
177            System.out.println("");
178           
179            // validate the user's input
180            if(routeNumInputList.contains(routeNum))
181            {
182                verified = true;
183            }
184            else
185            {
186                System.out.println("Invalid route number, please re-enter: ");
187            }
188        }
189       
190        return routeNum;
191    }
192   
193    /**
194     * Gets the highway direction from the user and validates the input.
195     *
196     * @param sc stdIn scanner
197     * @return highway direction
198     */
199    private DIRECTION getDirection(Scanner sc, Integer routeNum) {
200        DIRECTION direction;
201        String directionInput = null;
202        Boolean verified = false;
203       
204        // validation loop
205        while(!verified)
206        {
207            // Get available directions for route
208            ArrayList<DIRECTION> availDirs = new ArrayList<>();
209            for(Station stn : highways.getHighwayByRouteNumber(routeNum).stations)
210            {
211                if(!availDirs.contains(stn.direction))
212                {
213                    availDirs.add(stn.direction);
214                }
215            }
216           
217            // prompt user for input
218            System.out.print("Available directions for highway " + routeNum + ": [");
219            for(DIRECTION dir : availDirs)
220            {
221                System.out.print(dir.getLetter() + ", ");
222            }
223            System.out.print("]");
224            System.out.println("");
225            System.out.println("Enter a direction:");
226            directionInput = sc.next().toUpperCase();
227            System.out.println("");
228           
229            // validate the user's input
230            if(availDirs.contains(DIRECTION.toDirection(directionInput)))
231            {
232                verified = true;
233            }
234            else
235            {
236                System.out.println("Invalid direction, please re-enter: ");
237            }
238        }
239       
240        return DIRECTION.toDirection(directionInput);
241    }
242   
243    /**
244     * Gets the starting/origin postmile value for the highway section from the
245     * user and validates the input.
246     *
247     * @param sc stdIn scanner
248     * @param routeNumber highway route number
249     * @param dir highway direction
250     * @return highway section start/origin postmile value
251     */
252    private Double getPostmile(Scanner sc, Integer routeNumber, DIRECTION dir) {
253        Double postmile = null;
254        Boolean verified = false;
255       
256        // validation loop
257        while(!verified)
258        {
259            // Get highway, and grab the floor and ceiling for postmile values
260            // from the highway stations to present to the user
261            Highway hwy = highways.getHighwayByRouteNumber(routeNumber);
262            Double floorPostmile = hwy.stations.get(0).postmile;
263            Double ceilPostmile = hwy.stations
264                    .get(hwy.stations.size() - 1).postmile;
265           
266            // present user with range of postmiles for given highway
267            System.out.println("Route " + hwy.routeNumber + " " + dir
268                    + " postmile range: [" + floorPostmile + ", " 
269                    + ceilPostmile + "]");
270           
271            // prompt user for postmile value
272            System.out.println("Enter a postmile value (Integer/Double): ");
273            postmile = sc.nextDouble();
274            System.out.println("");
275           
276            // validate user's input, ensures that the postmile is within given
277            // postmile range (floorPostmile, ceilPostmile)
278            if(postmile >= floorPostmile && postmile <= ceilPostmile)
279            {
280                verified = true;
281            }
282            else
283            {
284                System.out.println("Postmile must be within postmile range: [" + floorPostmile + ", " 
285                    + ceilPostmile + "] please re-enter: ");
286            }
287        }
288       
289        return postmile;
290    }
291   
292    /**
293     * Gets the range to extend the highway stretch from the start/origin postmile
294     * value from the user and validates the input.
295     *
296     * @param sc stdIn scanner
297     * @param postmile origin/start postmile value for highway stretch
298     * @return range value
299     */
300    private Double getRange(Scanner sc, Double postmile) {
301        Double range = null;
302        Boolean verified = false;
303       
304        // validation loop
305        while(!verified)
306        {
307            // prompt user for range value
308            System.out.println("Enter a range value (decimal):");
309            range = sc.nextDouble();
310            System.out.println("");
311           
312            // range must be greater than or equal to 0
313            if(range >= 0)
314            {
315                verified = true;
316            }
317            else
318            {
319                System.out.println("Range must be >= 0");
320            }
321        }
322       
323        return range;
324    }
325
326    /**
327     * Gets the dot color from the user, to be applied to specified highway
328     * stretch and validates the user's input.
329     *
330     * @param sc stdIn scanner
331     * @return dot color to be applied to highway stretch
332     */
333    private DOTCOLOR getDotColor(Scanner sc) {
334        DOTCOLOR dotColor;
335        String dotColorInput = null;
336        Boolean verified = false;
337       
338        // validationloop
339        while(!verified)
340        {
341            // prompt user for color
342            System.out.println("Enter a dot color (G/Y/R):");
343            dotColorInput = sc.next().toUpperCase();
344            System.out.println("");
345            // validate user's input
346            if(DOTCOLOR.allLetters.contains(dotColorInput))
347            {
348                verified = true;
349            }
350            else
351            {
352                System.out.println("Invalid dot color, please re-enter: ");
353            }
354        }
355       
356        return DOTCOLOR.toDotColor(dotColorInput);
357    }
358   
359}
Note: See TracBrowser for help on using the repository browser.