| 1 | package atmsdriver; |
|---|
| 2 | |
|---|
| 3 | import atmsdriver.model.Highways; |
|---|
| 4 | import atmsdriver.model.TrafficEvent; |
|---|
| 5 | import java.io.FileInputStream; |
|---|
| 6 | import java.io.FileNotFoundException; |
|---|
| 7 | import java.rmi.RemoteException; |
|---|
| 8 | import java.util.ArrayList; |
|---|
| 9 | import java.util.HashMap; |
|---|
| 10 | import java.util.LinkedList; |
|---|
| 11 | import java.util.List; |
|---|
| 12 | import java.util.Map; |
|---|
| 13 | import java.util.Properties; |
|---|
| 14 | import java.util.Scanner; |
|---|
| 15 | import java.util.logging.Level; |
|---|
| 16 | import java.util.logging.Logger; |
|---|
| 17 | import javax.swing.JFrame; |
|---|
| 18 | import tmcsim.cadsimulator.managers.TrafficModelManager; |
|---|
| 19 | import tmcsim.common.SimulationException; |
|---|
| 20 | |
|---|
| 21 | /** |
|---|
| 22 | * Read and process all Traffic Events in a file and show |
|---|
| 23 | * resulting highway network. |
|---|
| 24 | * A snapshot of the highway model after each event is saved in a buffer |
|---|
| 25 | * and a graphical display allows the user to scroll through the history. |
|---|
| 26 | * This application is used by Traffic Event authors to assist |
|---|
| 27 | * in verifying the correctness of their data file. |
|---|
| 28 | * @author jdalbey |
|---|
| 29 | */ |
|---|
| 30 | public class TrafficEventsAnimator extends javax.swing.JPanel |
|---|
| 31 | { |
|---|
| 32 | /** |
|---|
| 33 | * Error logger. |
|---|
| 34 | */ |
|---|
| 35 | private final static Logger logger = Logger.getLogger("trafficmodeleventdriver"); |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * Highways in traffic network |
|---|
| 39 | */ |
|---|
| 40 | final private Highways highways; |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * LinkedList of batch events |
|---|
| 44 | */ |
|---|
| 45 | private final LinkedList<TrafficEvent> eventQueue; |
|---|
| 46 | /** |
|---|
| 47 | * Map of incidents to events |
|---|
| 48 | */ |
|---|
| 49 | private Map<String, List<TrafficEvent>> incidents; |
|---|
| 50 | /** |
|---|
| 51 | * Highway history |
|---|
| 52 | */ |
|---|
| 53 | private List<String> history; |
|---|
| 54 | /** |
|---|
| 55 | * name of events file obtained from properties |
|---|
| 56 | */ |
|---|
| 57 | private String events_file = ""; |
|---|
| 58 | /** |
|---|
| 59 | * Creates the JPanel and builds the highways and events. |
|---|
| 60 | */ |
|---|
| 61 | public TrafficEventsAnimator() throws RemoteException, SimulationException |
|---|
| 62 | { |
|---|
| 63 | initComponents(); |
|---|
| 64 | |
|---|
| 65 | // Initialize the highway model |
|---|
| 66 | incidents = new HashMap<String, List<TrafficEvent>>(); |
|---|
| 67 | highways = new Highways( |
|---|
| 68 | "config/vds_data/highways_fullmap.txt", |
|---|
| 69 | // following aren't used by this application |
|---|
| 70 | "localhost", 8080); |
|---|
| 71 | final String CONFIG_FILE_NAME = "traffic_model_config.properties"; |
|---|
| 72 | String propertiesFile = "config" + System.getProperty("file.separator") |
|---|
| 73 | + CONFIG_FILE_NAME; |
|---|
| 74 | Properties props = TrafficModelManager.loadProperties(propertiesFile); |
|---|
| 75 | events_file = props.getProperty("Events_File"); |
|---|
| 76 | FileInputStream fis = null; |
|---|
| 77 | try |
|---|
| 78 | { |
|---|
| 79 | fis = new FileInputStream(events_file); |
|---|
| 80 | } catch (FileNotFoundException ex) |
|---|
| 81 | { |
|---|
| 82 | Logger.getLogger(TrafficModelManager.class.getName()).log(Level.SEVERE, null, |
|---|
| 83 | "Missing Traffic Events file " + events_file); |
|---|
| 84 | System.exit(-1); |
|---|
| 85 | } |
|---|
| 86 | Scanner fileScanner = new Scanner(fis); |
|---|
| 87 | // Read all lines from the file of events and put in a queue |
|---|
| 88 | eventQueue = TrafficModelManager.readBatchFile(fileScanner); |
|---|
| 89 | } |
|---|
| 90 | /** Load the traffic events into a history buffer */ |
|---|
| 91 | public void load() |
|---|
| 92 | { |
|---|
| 93 | txtDisplay.setText("Loading traffic events from: " + events_file); |
|---|
| 94 | history = new ArrayList<String>(); |
|---|
| 95 | // If we have any events left to process |
|---|
| 96 | while (!eventQueue.isEmpty()) |
|---|
| 97 | { |
|---|
| 98 | // Get next event |
|---|
| 99 | TrafficEvent nextEvent = eventQueue.peek(); |
|---|
| 100 | System.out.println(nextEvent.eventTime); |
|---|
| 101 | // apply colorization to highways |
|---|
| 102 | highways.applyColorToHighwayStretch(nextEvent.routeNumber, nextEvent.dir, |
|---|
| 103 | nextEvent.postmile, nextEvent.range, nextEvent.color); |
|---|
| 104 | history.add(highways.toString()+"\n"+nextEvent.eventTime); |
|---|
| 105 | // Remove this event from the queue, we're done with it. |
|---|
| 106 | eventQueue.remove(); |
|---|
| 107 | } |
|---|
| 108 | scrollBar.setMaximum(history.size()); |
|---|
| 109 | // display the first item from the history |
|---|
| 110 | txtDisplay.setText(history.get(0)); |
|---|
| 111 | } |
|---|
| 112 | /** |
|---|
| 113 | * This method is called from within the constructor to initialize the form. |
|---|
| 114 | * WARNING: Do NOT modify this code. The content of this method is always |
|---|
| 115 | * regenerated by the Form Editor. |
|---|
| 116 | */ |
|---|
| 117 | @SuppressWarnings("unchecked") |
|---|
| 118 | // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents |
|---|
| 119 | private void initComponents() |
|---|
| 120 | { |
|---|
| 121 | |
|---|
| 122 | scrollBar = new javax.swing.JScrollBar(); |
|---|
| 123 | jScrollPane1 = new javax.swing.JScrollPane(); |
|---|
| 124 | txtDisplay = new javax.swing.JTextArea(); |
|---|
| 125 | |
|---|
| 126 | scrollBar.addAdjustmentListener(new java.awt.event.AdjustmentListener() |
|---|
| 127 | { |
|---|
| 128 | public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt) |
|---|
| 129 | { |
|---|
| 130 | scrollbarValueChanged(evt); |
|---|
| 131 | } |
|---|
| 132 | }); |
|---|
| 133 | |
|---|
| 134 | txtDisplay.setColumns(20); |
|---|
| 135 | txtDisplay.setFont(new java.awt.Font("Courier New", 0, 15)); // NOI18N |
|---|
| 136 | txtDisplay.setRows(5); |
|---|
| 137 | jScrollPane1.setViewportView(txtDisplay); |
|---|
| 138 | |
|---|
| 139 | javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); |
|---|
| 140 | this.setLayout(layout); |
|---|
| 141 | layout.setHorizontalGroup( |
|---|
| 142 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
|---|
| 143 | .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() |
|---|
| 144 | .addComponent(scrollBar, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE) |
|---|
| 145 | .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
|---|
| 146 | .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)) |
|---|
| 147 | ); |
|---|
| 148 | layout.setVerticalGroup( |
|---|
| 149 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
|---|
| 150 | .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE) |
|---|
| 151 | .addComponent(scrollBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) |
|---|
| 152 | ); |
|---|
| 153 | }// </editor-fold>//GEN-END:initComponents |
|---|
| 154 | |
|---|
| 155 | private void scrollbarValueChanged(java.awt.event.AdjustmentEvent evt)//GEN-FIRST:event_scrollbarValueChanged |
|---|
| 156 | {//GEN-HEADEREND:event_scrollbarValueChanged |
|---|
| 157 | // when the scroll bar is moved show the corresponding snapshot from history |
|---|
| 158 | txtDisplay.setText(history.get(evt.getValue()) ); |
|---|
| 159 | repaint(); |
|---|
| 160 | }//GEN-LAST:event_scrollbarValueChanged |
|---|
| 161 | |
|---|
| 162 | /** Entry point for the application. Builds a surrounding JFrame and |
|---|
| 163 | * runs the application. |
|---|
| 164 | * @param args |
|---|
| 165 | * @throws RemoteException |
|---|
| 166 | * @throws SimulationException |
|---|
| 167 | */ |
|---|
| 168 | public static void main(String[] args) throws RemoteException, SimulationException |
|---|
| 169 | { |
|---|
| 170 | JFrame frame = new JFrame("Traffic Events Animator"); |
|---|
| 171 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|---|
| 172 | frame.setSize(1050,450); |
|---|
| 173 | TrafficEventsAnimator display = new TrafficEventsAnimator(); |
|---|
| 174 | frame.add(display); |
|---|
| 175 | frame.setVisible(true); |
|---|
| 176 | display.load(); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | // Variables declaration - do not modify//GEN-BEGIN:variables |
|---|
| 180 | private javax.swing.JScrollPane jScrollPane1; |
|---|
| 181 | private javax.swing.JScrollBar scrollBar; |
|---|
| 182 | private javax.swing.JTextArea txtDisplay; |
|---|
| 183 | // End of variables declaration//GEN-END:variables |
|---|
| 184 | } |
|---|