Changeset 183 in tmcsimulator for trunk/src/tmcsim/client/ATMSBatchDriver.java


Ignore:
Timestamp:
10/28/2017 03:51:23 PM (9 years ago)
Author:
jdalbey
Message:

ATMSBatchDriver.java Enhanced to automatically sort events chronologically (using new class TrafficEvent?).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/tmcsim/client/ATMSBatchDriver.java

    r180 r183  
    11package tmcsim.client; 
    22 
    3 import atmsdriver.ATMSDriver; 
    43import atmsdriver.ConsoleTrafficDriver; 
    5 import atmsdriver.ExchangeInfo; 
    64import atmsdriver.model.Highways; 
    7 import atmsdriver.model.Station; 
     5import atmsdriver.model.TrafficEvent; 
    86import atmsdriver.model.LoopDetector.DOTCOLOR; 
    97import java.awt.event.ActionEvent; 
     
    1715import java.text.SimpleDateFormat; 
    1816import java.util.ArrayList; 
     17import java.util.Collections; 
    1918import java.util.Date; 
    2019import java.util.HashMap; 
    21 import java.util.InputMismatchException; 
    2220import java.util.LinkedList; 
    2321import java.util.List; 
    2422import java.util.Map; 
    2523import java.util.Properties; 
    26 import java.util.Queue; 
    2724import java.util.Scanner; 
    2825import java.util.concurrent.TimeUnit; 
     
    3330import javax.swing.Timer; 
    3431import javax.swing.UIManager; 
    35 import tmcsim.cadsimulator.CADServer; 
    3632import tmcsim.common.SimulationException; 
    3733import tmcsim.interfaces.CADClientInterface; 
     
    3935 
    4036/** 
    41  * Skeleton for ATMS Driver that reads a "batch" file of highway 
    42  * status update commands.  
    43  * It operates as a client of the 
    44  * CAD server, using RMI to poll the server every second for the current 
    45  * simulation clock time.  It uses the simulation clock time 
    46  * to fire update commands at the desired time. 
    47  * Note: Sim Mgr must be running before starting this application. 
    48  * TODO: We probably want to be able to "override" a command, to force  
    49  * clearing an incident. 
    50  
     37 * Skeleton for ATMS Driver that reads a "batch" file of highway status update 
     38 * commands. It operates as a client of the CAD server, using RMI to poll the 
     39 * server every second for the current simulation clock time. It uses the 
     40 * simulation clock time to fire update commands at the desired time. Note: Sim 
     41 * Mgr must be running before starting this application. TODO: We probably want 
     42 * to be able to "override" a command, to force clearing an incident. 
     43 * 
    5144 * @author jdalbey 
    5245 */ 
     
    5447        CADClientInterface 
    5548{ 
     49 
    5650    private static final String CONFIG_FILE_NAME = "cad_client_config.properties"; 
    5751    private final static int ONE_SECOND = 1000; 
     
    7973    { 
    8074        CAD_SIM_HOST("CADSimulatorHost"), CAD_SIM_PORT("CADSimulatorSocketPort"), CAD_RMI_PORT( 
    81         "CADRmiPort"), CLIENT_CAD_POS("CADPosition"), CLIENT_USER_ID( 
    82         "CADUserID"), KEYBOARD_TYPE("KeyboardType"), DISPLAY_TYPE( 
    83         "DisplayType"); 
     75                "CADRmiPort"), CLIENT_CAD_POS("CADPosition"), CLIENT_USER_ID( 
     76                "CADUserID"), KEYBOARD_TYPE("KeyboardType"), DISPLAY_TYPE( 
     77                "DisplayType"); 
    8478        public String name; 
    8579 
     
    112106     */ 
    113107    final private Highways highways; 
    114      
    115     /**  
    116      * Queue of batch events 
    117      */ 
    118     private Queue<String> eventQueue; 
     108 
     109    /** 
     110     * LinkedList of batch events 
     111     */ 
     112    private LinkedList<TrafficEvent> eventQueue; 
    119113    /** 
    120114     * Map of incidents to events 
    121115     */ 
    122     private Map<String, List<String>> incidents; 
    123      
    124     /** Instance of ConsoleTrafficDriver that contains the highway model */ 
     116    private Map<String, List<TrafficEvent>> incidents; 
     117 
     118    /** 
     119     * Instance of ConsoleTrafficDriver that contains the highway model 
     120     */ 
    125121    private ConsoleTrafficDriver console; 
    126      
    127     /** GUI for this driver */ 
     122 
     123    /** 
     124     * GUI for this driver 
     125     */ 
    128126    private ATMSBatchViewer theView; 
    129      
     127 
    130128    /** 
    131129     * Constructor. Initialize data from parsed properties file. Create a socket 
     
    143141        } 
    144142        // Initialize the highway model 
    145         incidents = new HashMap<String, List<String>> (); 
     143        incidents = new HashMap<String, List<TrafficEvent>>(); 
    146144        highways = new Highways( 
    147         "config/vds_data/lds.txt", 
    148         "config/vds_data/loop.txt", 
    149         "config/vds_data/highwaysMeta.txt", 
    150 //        "192.168.251.46", 8080);  //IP address of FEP Sim Linux VM 
    151        "localhost", 8080); 
    152          
     145                "config/vds_data/lds.txt", 
     146                "config/vds_data/loop.txt", 
     147                "config/vds_data/highwaysMeta.txt", 
     148                //        "192.168.251.46", 8080);  //IP address of FEP Sim Linux VM 
     149                "localhost", 8080); 
     150 
    153151        connect(cadClientProp.getProperty(PROPERTIES.CAD_SIM_HOST.name).trim(), 
    154152                cadClientProp.getProperty(PROPERTIES.CAD_RMI_PORT.name).trim()); 
     
    169167                String currentClock = ""; 
    170168                String currentATMStime = ""; 
    171                 Date simClock = new Date();                 
     169                Date simClock = new Date(); 
    172170                // Obtain the simulation time from the CAD server 
    173171                try 
     
    179177//                    Date atmsdate = new Date(ATMStime); 
    180178//                    currentATMStime = formatter.format(atmsdate); 
    181                     try { 
     179                    try 
     180                    { 
    182181                        simClock = formatter.parse(currentClock); 
    183                     } catch (ParseException ex) { 
     182                    } 
     183                    catch (ParseException ex) 
     184                    { 
    184185                        Logger.getLogger(ATMSBatchDriver.class.getName()).log(Level.SEVERE, null, ex); 
    185186                        System.out.println("Invalid simulation clock time found in ATMSDriverClient"); 
    186187                        System.exit(-1); 
    187                     }                     
     188                    } 
    188189                    //System.out.println("Current clock: " + currentClock); 
    189                 } catch (RemoteException ex) 
     190                } 
     191                catch (RemoteException ex) 
    190192                { 
    191193                    System.out.println("Remote Exception reading sim or ATMS clock time"); 
     
    196198                { 
    197199                    // Get the time to launch the next event 
    198                     String nextEvent = eventQueue.peek(); 
    199 //                    String eventTimeField = nextEvent.substring(0,8); 
    200                     Scanner evtScan = new Scanner(nextEvent); 
    201                     String inci = evtScan.next();       
    202                     String eventTimeField = evtScan.next(); 
    203                     Date eventTime = new Date(); 
    204                     try { 
    205                         eventTime = formatter.parse(eventTimeField); 
    206                     } catch (ParseException ex) { 
    207                         Logger.getLogger(ATMSBatchDriver.class.getName()).log(Level.WARNING, null, ex); 
    208                         System.out.println("Unable to parse event time: " + nextEvent + " skipping."); 
    209                         eventQueue.remove(); 
    210                     } 
     200                    TrafficEvent nextEvent = eventQueue.peek(); 
     201                    Date eventTime = nextEvent.eventDate; 
    211202                    //System.out.println("Next event will be launched at: " + formatter.format(eventTime)); 
    212203                    // Check the queue of events to see if the first 
     
    215206                    if (eventTime.before(simClock) || eventTime.equals(simClock)) 
    216207                    { 
    217                         System.out.println("LAUNCHING EVENT at " + nextEvent ); 
    218                         // Extract fields from event and prepare them  
    219                         Scanner lineScan = new Scanner(nextEvent); 
    220                         try 
    221                         { 
    222                         lineScan.next(); // skip incident number field 
    223                         lineScan.next(); // skip time field 
    224                         int routeNumber = lineScan.nextInt(); 
    225                         Station.DIRECTION dir = Station.DIRECTION.toDirection(lineScan.next()); 
    226                         double postmile = lineScan.nextDouble(); 
    227                         double range = lineScan.nextDouble(); 
    228                         DOTCOLOR dotcolor = DOTCOLOR.toDotColor(lineScan.next()); 
     208                        System.out.println("LAUNCHING EVENT: " + nextEvent.toString()); 
    229209                        // apply colorization to highways 
    230                         highways.applyColorToHighwayStretch(routeNumber, dir, postmile, range, dotcolor); 
     210                        highways.applyColorToHighwayStretch(nextEvent.routeNumber, nextEvent.dir, 
     211                                nextEvent.postmile, nextEvent.range, nextEvent.color); 
    231212                        // Remove this event from the queue, we're done with it. 
    232213                        eventQueue.remove(); 
    233                         } 
    234                         catch (InputMismatchException ex) 
    235                         { 
    236                             System.out.println("Wrong format data in batch event file: " + nextEvent + " \nskipping."); 
    237                             eventQueue.remove(); 
    238                         } 
    239                     } 
    240                      
     214                    } 
     215 
    241216                    theView.update(currentClock, currentATMStime, eventQueue); 
    242217                } 
     
    255230    { 
    256231        FileInputStream fis; 
    257         try { 
     232        try 
     233        { 
    258234            fis = new FileInputStream("config/vds_data/atmsBatchEvents.txt"); 
    259             eventQueue = new LinkedList<String>(); 
     235            eventQueue = new LinkedList<TrafficEvent>(); 
    260236            // Read all lines from the file of events 
    261237            Scanner scan = new Scanner(fis); 
     
    263239            { 
    264240                // Read a line and add it to the event queue 
    265                 String line = scan.nextLine(); 
     241                String line = scan.nextLine().trim(); 
    266242                if (line.charAt(0) != '#') 
    267243                { 
    268                     eventQueue.add(line); 
    269                     // Parse the incident from the line 
    270                     Scanner lineScan = new Scanner(line); 
    271                     String incident = lineScan.next(); 
    272                     // Add the line to the list for the corresponding incident 
    273                     List evtList; 
    274                     if (incidents.containsKey(incident)) 
    275                     { 
    276                         evtList = incidents.get(incident); 
    277                     } 
    278                     else 
    279                     { 
    280                         evtList = new ArrayList<String>(); 
    281                     }                
    282                     evtList.add(line); 
    283                     // and put it back in the map 
    284                     incidents.put(incident, evtList); 
     244                    TrafficEvent evt; 
     245                    try 
     246                    { 
     247                        evt = new TrafficEvent(line); 
     248                        eventQueue.add(evt); 
     249                        String incident = evt.incident; 
     250                        // Add the line to the list for the corresponding incident 
     251                        List evtList; 
     252                        if (incidents.containsKey(evt.incident)) 
     253                        { 
     254                            evtList = incidents.get(evt.incident); 
     255                        } 
     256                        else 
     257                        { 
     258                            evtList = new ArrayList<String>(); 
     259                        } 
     260                        evtList.add(evt); 
     261                        // and put it back in the map 
     262                        incidents.put(incident, evtList); 
     263                    } 
     264                    catch (ParseException ex) 
     265                    { 
     266                        Logger.getLogger(ATMSBatchDriver.class.getName()).log(Level.SEVERE, null, ex); 
     267                        System.out.println("Wrong format data in batch event file: " + line + " \nskipping."); 
     268                        System.out.println("Skipping badly formatted event."); 
     269                    } 
    285270                } 
    286271            } 
    287         } catch (FileNotFoundException ex) { 
     272        } 
     273        catch (FileNotFoundException ex) 
     274        { 
    288275            Logger.getLogger(ATMSBatchDriver.class.getName()).log(Level.SEVERE, null, ex); 
    289276        } 
    290277        System.out.println("Events file read, " + eventQueue.size() + " events queued."); 
    291     } 
    292      
    293     /** Clear an incident.  For each event associated with an incident, 
    294      * turn the dots in its range Green and remove it from the event queue. 
     278        // Put the events in chronological order 
     279        Collections.sort(eventQueue); 
     280    } 
     281 
     282    /** 
     283     * Clear an incident. For each event associated with an incident, turn the 
     284     * dots in its range Green and remove it from the event queue. 
     285     * 
    295286     * @param incidentNumber incident to be cleared. 
    296287     */ 
     
    298289    { 
    299290        boolean ok = incidents.containsKey(incidentNumber); 
    300         if (!ok)  
     291        if (!ok) 
    301292        { 
    302293            System.out.println("Sorry, that incident number isn't found."); 
     
    304295        } 
    305296        System.out.println("Clearing incident " + incidentNumber); 
    306         List<String> events = incidents.get(incidentNumber); 
     297        List<TrafficEvent> events = incidents.get(incidentNumber); 
    307298        // Process each event associated with this incident  
    308         for (String event: events) 
     299        for (TrafficEvent event : events) 
    309300        { 
    310301            System.out.println("Event: " + event + " cleared."); 
    311302            eventQueue.remove(event); 
    312             // Extract fields from event and prepare them  
    313             Scanner lineScan = new Scanner(event); 
    314             try 
    315             { 
    316                 lineScan.next(); // skip incident number field 
    317                 lineScan.next(); // skip time field 
    318                 int routeNumber = lineScan.nextInt(); 
    319                 Station.DIRECTION dir = Station.DIRECTION.toDirection(lineScan.next()); 
    320                 double postmile = lineScan.nextDouble(); 
    321                 double range = lineScan.nextDouble(); 
    322                 // apply colorization to highways, forcing to green, indicating cleared 
    323                 highways.applyColorToHighwayStretch(routeNumber, dir, postmile, range, DOTCOLOR.GREEN); 
    324             } 
    325             catch (InputMismatchException ex) 
    326             { 
    327                 System.out.println("Internal error, please report to programmers." + event); 
    328             } 
    329              
     303 
     304            // apply colorization to highways, forcing to green, indicating cleared 
     305            highways.applyColorToHighwayStretch(event.routeNumber, event.dir, 
     306                    event.postmile, event.range, DOTCOLOR.GREEN); 
     307 
    330308        } 
    331309        // Now refresh the view with the updated queue of events 
    332310        theView.update("0:00", "0:00", eventQueue); 
    333311    } 
    334      
     312 
    335313    /** 
    336314     * Connect to the Coordinator's RMI object, and register this object for 
     
    354332            theCoorInt = (CoordinatorInterface) Naming.lookup(coorIntURL); 
    355333            theCoorInt.registerForCallback(this); 
    356         } catch (Exception e) 
     334        } 
     335        catch (Exception e) 
    357336        { 
    358337            throw new SimulationException(SimulationException.CAD_SIM_CONNECT, 
    359338                    e); 
    360              
     339 
    361340        } 
    362341    } 
     
    384363            cadClientProp = new Properties(); 
    385364            cadClientProp.load(new FileInputStream(propertiesFile)); 
    386         } catch (Exception e) 
     365        } 
     366        catch (Exception e) 
    387367        { 
    388368            cadClientLogger.logp(Level.SEVERE, "SimulationManager", 
     
    392372                    e); 
    393373        } 
    394  
    395374 
    396375        // Ensure that the properties file does not have null values for the 
     
    430409                { 
    431410                    theCoorInt.unregisterForCallback(client); 
    432                 } catch (RemoteException e) 
     411                } 
     412                catch (RemoteException e) 
    433413                { 
    434414                    e.printStackTrace(); 
     
    456436            new ATMSBatchDriver(System.getProperty("CONFIG_DIR") + System.getProperty("file.separator") + CONFIG_FILE_NAME); 
    457437 
    458         } catch (Exception e) 
     438        } 
     439        catch (Exception e) 
    459440        { 
    460441            cadClientLogger.logp(Level.SEVERE, "SimulationManager", "Main", 
     
    478459            while (true) 
    479460            { 
    480                 try { 
     461                try 
     462                { 
    481463                    // Write the highway network status to the FEP Simulator 
    482464                    highways.writeToFEP(); 
    483                 } catch (SimulationException ex)  
     465                } 
     466                catch (SimulationException ex) 
    484467                { 
    485468                    // Ask user if they want to proceed without FEP Sim connection 
     
    487470                    if (reply == JOptionPane.NO_OPTION) 
    488471                    { 
    489                       System.exit(0); 
     472                        System.exit(0); 
    490473                    } 
    491474                    System.out.println("Skipping writeToFEP..."); 
Note: See TracChangeset for help on using the changeset viewer.