| 1 | package tmcsim.highwaymodel; |
|---|
| 2 | |
|---|
| 3 | import tmcsim.highwaymodel.Highways; |
|---|
| 4 | import tmcsim.highwaymodel.TrafficEvent; |
|---|
| 5 | import java.io.FileInputStream; |
|---|
| 6 | import java.io.FileNotFoundException; |
|---|
| 7 | import java.rmi.RemoteException; |
|---|
| 8 | import java.util.HashMap; |
|---|
| 9 | import java.util.LinkedList; |
|---|
| 10 | import java.util.List; |
|---|
| 11 | import java.util.Map; |
|---|
| 12 | import java.util.Properties; |
|---|
| 13 | import java.util.Scanner; |
|---|
| 14 | import java.util.logging.Level; |
|---|
| 15 | import java.util.logging.Logger; |
|---|
| 16 | import javax.swing.JOptionPane; |
|---|
| 17 | import javax.swing.JWindow; |
|---|
| 18 | import tmcsim.cadsimulator.managers.TrafficModelManager; |
|---|
| 19 | import tmcsim.common.SimulationException; |
|---|
| 20 | |
|---|
| 21 | /** |
|---|
| 22 | * OBSOLETE: An interactive version is now available: |
|---|
| 23 | * atmsdriver.TrafficEventsAnimator.java |
|---|
| 24 | * Read and process all Traffic Events in a file and show |
|---|
| 25 | * resulting highway network. |
|---|
| 26 | * A console display of the highway network is output |
|---|
| 27 | * for each event. |
|---|
| 28 | * This application is used by Traffic Event authors to assist |
|---|
| 29 | * in verifying the correctness of their data file. |
|---|
| 30 | * Because the output is sent immediately to the console the |
|---|
| 31 | * author doesn't have to sit through simulation time to |
|---|
| 32 | * observe dots changing on ATMS client. |
|---|
| 33 | * @author jdalbey |
|---|
| 34 | */ |
|---|
| 35 | public class TrafficModelEventDriver |
|---|
| 36 | { |
|---|
| 37 | /** |
|---|
| 38 | * Error logger. |
|---|
| 39 | */ |
|---|
| 40 | private static Logger logger = Logger.getLogger("trafficmodeleventdriver"); |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * Highways in traffic network |
|---|
| 44 | */ |
|---|
| 45 | final private Highways highways; |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * LinkedList of batch events |
|---|
| 49 | */ |
|---|
| 50 | private LinkedList<TrafficEvent> eventQueue; |
|---|
| 51 | /** |
|---|
| 52 | * Map of incidents to events |
|---|
| 53 | */ |
|---|
| 54 | private Map<String, List<TrafficEvent>> incidents; |
|---|
| 55 | |
|---|
| 56 | /** |
|---|
| 57 | * Constructor. Load highways and events. |
|---|
| 58 | * |
|---|
| 59 | */ |
|---|
| 60 | public TrafficModelEventDriver() throws RemoteException, SimulationException |
|---|
| 61 | { |
|---|
| 62 | // Initialize the highway model |
|---|
| 63 | incidents = new HashMap<String, List<TrafficEvent>>(); |
|---|
| 64 | highways = new Highways( |
|---|
| 65 | "config/vds_data/highways_fullmap.txt"); |
|---|
| 66 | final String CONFIG_FILE_NAME = "traffic_model_config.properties"; |
|---|
| 67 | String propertiesFile = "config" + System.getProperty("file.separator") |
|---|
| 68 | + CONFIG_FILE_NAME; |
|---|
| 69 | Properties props = TrafficModelManager.loadProperties(propertiesFile); |
|---|
| 70 | |
|---|
| 71 | FileInputStream fis = null; |
|---|
| 72 | try |
|---|
| 73 | { |
|---|
| 74 | fis = new FileInputStream(props.getProperty("Events_File")); |
|---|
| 75 | } catch (FileNotFoundException ex) |
|---|
| 76 | { |
|---|
| 77 | Logger.getLogger(TrafficModelManager.class.getName()).log(Level.SEVERE, null, |
|---|
| 78 | "Missing Traffic Events file " + props.getProperty("Events_File")); |
|---|
| 79 | System.exit(-1); |
|---|
| 80 | } |
|---|
| 81 | Scanner fileScanner = new Scanner(fis); |
|---|
| 82 | // Read all lines from the file of events and put in a queue |
|---|
| 83 | eventQueue = TrafficModelManager.readBatchFile(fileScanner); |
|---|
| 84 | } |
|---|
| 85 | public void run() |
|---|
| 86 | { |
|---|
| 87 | // If we have any events left to process |
|---|
| 88 | while (!eventQueue.isEmpty()) |
|---|
| 89 | { |
|---|
| 90 | // Get next event |
|---|
| 91 | TrafficEvent nextEvent = eventQueue.peek(); |
|---|
| 92 | System.out.println("LAUNCHING EVENT: " + nextEvent.toString()); |
|---|
| 93 | // apply colorization to highways |
|---|
| 94 | highways.applyColorToHighwayStretch(nextEvent.routeNumber, nextEvent.dir, |
|---|
| 95 | nextEvent.postmile, nextEvent.range, nextEvent.color); |
|---|
| 96 | System.out.println(highways.toString()); |
|---|
| 97 | // Remove this event from the queue, we're done with it. |
|---|
| 98 | eventQueue.remove(); |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | * local main to launch the app. |
|---|
| 104 | * |
|---|
| 105 | * @param args Command line arguments. |
|---|
| 106 | */ |
|---|
| 107 | public static void main(String[] args) |
|---|
| 108 | { |
|---|
| 109 | try |
|---|
| 110 | { |
|---|
| 111 | TrafficModelEventDriver driver = new TrafficModelEventDriver(); |
|---|
| 112 | driver.run(); |
|---|
| 113 | } |
|---|
| 114 | catch (Exception e) |
|---|
| 115 | { |
|---|
| 116 | logger.logp(Level.SEVERE, "Traffic Model Event Driver", "Main", |
|---|
| 117 | "Error initializing application."); |
|---|
| 118 | |
|---|
| 119 | JOptionPane.showMessageDialog(new JWindow(), e.getMessage(), |
|---|
| 120 | "Error - Program Exiting", JOptionPane.ERROR_MESSAGE); |
|---|
| 121 | |
|---|
| 122 | System.exit(-1); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | } |
|---|