Changeset 204 in tmcsimulator for trunk/test/atmsdriver


Ignore:
Timestamp:
10/31/2017 02:08:33 PM (9 years ago)
Author:
jdalbey
Message:

TrafficModelManager?.java Refactor for code sharing with TrafficModelEventDriver?. Write unit test.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/atmsdriver/TrafficModelEventDriver.java

    r194 r204  
    66import java.io.FileNotFoundException; 
    77import java.rmi.RemoteException; 
    8 import java.text.ParseException; 
    9 import java.text.SimpleDateFormat; 
    10 import java.util.ArrayList; 
    11 import java.util.Collections; 
    128import java.util.HashMap; 
    139import java.util.LinkedList; 
    1410import java.util.List; 
    1511import java.util.Map; 
     12import java.util.Properties; 
    1613import java.util.Scanner; 
    1714import java.util.logging.Level; 
     
    1916import javax.swing.JOptionPane; 
    2017import javax.swing.JWindow; 
     18import tmcsim.cadsimulator.managers.TrafficModelManager; 
     19import tmcsim.common.SimulationException; 
    2120 
    2221/** 
    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 
     22 * Read and process all Traffic Events in a file and show 
     23 * resulting highway network.  
     24 * A console display of the highway network is output 
    2525 * for each event. 
     26 * This application is used by Traffic Event authors to assist 
     27 * in verifying the correctness of their data file. 
     28 * Because the output is sent immediately to the console the 
     29 * author doesn't have to sit through simulation time to 
     30 * observe dots changing on ATMS client. 
    2631 * @author jdalbey 
    2732 */ 
     
    5156     * 
    5257     */ 
    53     public TrafficModelEventDriver() throws RemoteException 
     58    public TrafficModelEventDriver() throws RemoteException, SimulationException 
    5459    { 
    5560        // Initialize the highway model 
     
    5762        highways = new Highways( 
    5863                "config/vds_data/highways_fullmap.txt", 
    59                 //        "192.168.251.46", 8080);  //IP address of FEP Sim Linux VM 
     64                // following aren't used by this application 
    6065                "localhost", 8080); 
     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); 
    6170 
    62         // READ THE BATCH FILE OF COMMANDS and put in a queue 
    63         readBatchFile(); 
     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); 
    6484    } 
    6585    public void run() 
     
    7898            eventQueue.remove(); 
    7999        } 
    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); 
    133100    } 
    134101 
Note: See TracChangeset for help on using the changeset viewer.