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

Revision 255, 11.7 KB checked in by jdalbey, 7 years ago (diff)

TrafficEventsEditor? - modified to use JD's custom TimeSelectionDialog? for choosing new time frames.

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