source: tmcsimulator/trunk/test/atmsdriver/TrafficModelEventDriver.java @ 194

Revision 194, 5.1 KB checked in by jdalbey, 9 years ago (diff)

TrafficModelEventDriver?: Clean up code and comments. Highways.java improve formatting of toString output.

Line 
1package atmsdriver;
2
3import atmsdriver.model.Highways;
4import atmsdriver.model.TrafficEvent;
5import java.io.FileInputStream;
6import java.io.FileNotFoundException;
7import java.rmi.RemoteException;
8import java.text.ParseException;
9import java.text.SimpleDateFormat;
10import java.util.ArrayList;
11import java.util.Collections;
12import java.util.HashMap;
13import java.util.LinkedList;
14import java.util.List;
15import java.util.Map;
16import java.util.Scanner;
17import java.util.logging.Level;
18import java.util.logging.Logger;
19import javax.swing.JOptionPane;
20import javax.swing.JWindow;
21
22/**
23 * Skeleton for ATMS Driver that reads a "batch" file of highway status update
24 * commands.  A console display of the highway network is output
25 * for each event.
26 * @author jdalbey
27 */
28public class TrafficModelEventDriver 
29{
30    /**
31     * Error logger.
32     */
33    private static Logger logger = Logger.getLogger("trafficmodeleventdriver");
34
35    /**
36     * Highways in traffic network
37     */
38    final private Highways highways;
39
40    /**
41     * LinkedList of batch events
42     */
43    private LinkedList<TrafficEvent> eventQueue;
44    /**
45     * Map of incidents to events
46     */
47    private Map<String, List<TrafficEvent>> incidents;
48
49    /**
50     * Constructor. Load highways and events.
51     *
52     */
53    public TrafficModelEventDriver() throws RemoteException
54    {
55        // Initialize the highway model
56        incidents = new HashMap<String, List<TrafficEvent>>();
57        highways = new Highways(
58                "config/vds_data/highways_fullmap.txt",
59                //        "192.168.251.46", 8080);  //IP address of FEP Sim Linux VM
60                "localhost", 8080);
61
62        // READ THE BATCH FILE OF COMMANDS and put in a queue
63        readBatchFile();
64    }
65    public void run()
66    {
67        // If we have any events left to process
68        while (!eventQueue.isEmpty())
69        {
70            // Get next event
71            TrafficEvent nextEvent = eventQueue.peek();
72            System.out.println("LAUNCHING EVENT: " + nextEvent.toString());
73            // apply colorization to highways
74            highways.applyColorToHighwayStretch(nextEvent.routeNumber, nextEvent.dir,
75                    nextEvent.postmile, nextEvent.range, nextEvent.color);
76            System.out.println(highways.toString());
77            // Remove this event from the queue, we're done with it.
78            eventQueue.remove();
79        }
80    }
81
82    private void readBatchFile()
83    {
84        FileInputStream fis;
85        try
86        {
87            fis = new FileInputStream("config/vds_data/atmsBatchEvents.txt");
88            eventQueue = new LinkedList<TrafficEvent>();
89            // Read all lines from the file of events
90            Scanner scan = new Scanner(fis);
91            while (scan.hasNext())
92            {
93                // Read a line and add it to the event queue
94                String line = scan.nextLine().trim();
95                if (line.charAt(0) != '#')
96                {
97                    TrafficEvent evt;
98                    try
99                    {
100                        evt = new TrafficEvent(line);
101                        eventQueue.add(evt);
102                        String incident = evt.incident;
103                        // Add the line to the list for the corresponding incident
104                        List evtList;
105                        if (incidents.containsKey(evt.incident))
106                        {
107                            evtList = incidents.get(evt.incident);
108                        }
109                        else
110                        {
111                            evtList = new ArrayList<String>();
112                        }
113                        evtList.add(evt);
114                        // and put it back in the map
115                        incidents.put(incident, evtList);
116                    }
117                    catch (ParseException ex)
118                    {
119                        Logger.getLogger(TrafficModelEventDriver.class.getName()).log(Level.SEVERE, null, ex);
120                        System.out.println("Wrong format data in batch event file: " + line + " \nskipping.");
121                        System.out.println("Skipping badly formatted event.");
122                    }
123                }
124            }
125        }
126        catch (FileNotFoundException ex)
127        {
128            Logger.getLogger(TrafficModelEventDriver.class.getName()).log(Level.SEVERE, null, ex);
129        }
130        System.out.println("Events file read, " + eventQueue.size() + " events queued.");
131        // Put the events in chronological order
132        Collections.sort(eventQueue);
133    }
134
135    /**
136     * local main to launch the app.
137     *
138     * @param args Command line arguments.
139     */
140    public static void main(String[] args)
141    {
142        try
143        {
144            TrafficModelEventDriver driver = new TrafficModelEventDriver();
145            driver.run();
146        }
147        catch (Exception e)
148        {
149            logger.logp(Level.SEVERE, "Traffic Model Event Driver", "Main",
150                    "Error initializing application.");
151
152            JOptionPane.showMessageDialog(new JWindow(), e.getMessage(),
153                    "Error - Program Exiting", JOptionPane.ERROR_MESSAGE);
154
155            System.exit(-1);
156        }
157
158    }
159
160}
Note: See TracBrowser for help on using the repository browser.