source: tmcsimulator/trunk/src/atmsdriver/TrafficEventsAnimator.java @ 226

Revision 226, 6.7 KB checked in by jdalbey, 8 years ago (diff)

Added new tool: TrafficEventsAnimator? and updated build.xml to create a Jar for it.

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