Changeset 103 in tmcsimulator for trunk/src/atmsdriver/model/Highways.java


Ignore:
Timestamp:
10/12/2017 12:28:28 AM (9 years ago)
Author:
jtorres
Message:

trunk/src/atmsdriver/ConsoleDriver.java: Created console driver for ATMSDriver, completed and very nice. Still need to apply correct vol/occ values to actually change the colors. Still need to write a JUnit test for it. trunk/src/atmsdriver/ATMSDriver.java: Refactored to run the console driver. ATMSDriver runs in thread, console driver runs, and they share the highways instance. Renamed NetworkLoader?.java to FEPLineLoader.java. trunk/src/atmsdriver/model/Highways.java: refactored loadHighways() method to conform to new undirected highway abstraction. trunk/src/atmsdriver/model/Station.java: added updateByDirection(DIRECTION dir) method and supporting utility methods. trunk/test/atmsdriver/model/LoadHighwaysTest.java: Conformed LoadHighways? test to new undirected highway abstraction. trunk/test/atmsdriver/model/StationTest.java: Conformed StationTest?.java to new changes - very minor stuff. Went through all model classes and changed any final privates with a getter method to final public, for good OOP practice and simplicity. Went through ALL FILES and commented everything very well. minor application custom configuration changes. Removed all .class or .o.d files from svn repository

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/atmsdriver/model/Highways.java

    r101 r103  
    11package atmsdriver.model; 
    22 
    3 import atmsdriver.NetworkLoader; 
    4 import atmsdriver.model.Station.DIRECTION; 
     3import atmsdriver.FEPLineLoader; 
    54import java.io.File; 
    6 import java.io.FileNotFoundException; 
    75import java.io.FileWriter; 
    86import java.io.IOException; 
     
    1311import java.util.ArrayList; 
    1412import java.util.Collections; 
    15 import java.util.Enumeration; 
    1613import java.util.HashMap; 
    17 import java.util.Hashtable; 
    18 import java.util.List; 
    1914import java.util.Map; 
    2015import java.util.Observable; 
    2116import java.util.Observer; 
    22 import java.util.Scanner; 
    2317import java.util.Set; 
    2418import java.util.logging.Level; 
     
    3428import org.w3c.dom.Element; 
    3529 
    36 /** 
     30/** The Highways class aggregates all Highway instances within a geographic 
     31 *  region, and all of the FEPLines within an electronic detector  
     32 *  network, in the same geographic region. An instance of Highways.java  
     33 *  comprises the underlying model for the ATMSDriver application. 
     34 *  
     35 *  Highways uses method writeToFEP() to communicate with the FEP Simulator.  
     36 *  It creates a socket client which sends the FEP Simulator a highways status 
     37 *  message over the socket. This message is in the XML format required by the  
     38 *  FEP Simulator, which is provided by the resident toXML() method. 
     39 *  
     40 *  Currently, there is no driving logic within the highways class. It is only 
     41 *  done through an instance of the ConsoleDriver.java class. Eventually, it 
     42 *  will receive incident data (from exchange.xml or better yet, directly from  
     43 *  the TMCSimulator itself) and drive the highways using resident logic. 
    3744 * 
    3845 * @author John A. Torres 
    3946 */ 
    40 public class Highways implements Observer { 
     47public class Highways { 
    4148 
    4249    final private ArrayList<FEPLine> lines; 
    4350    final private String FEPHostName; 
    4451    final private int FEPPortNum; 
    45     final private ArrayList<Highway> highways; 
    46     private Map<Integer, List<Station>> HiwayMap = new HashMap<Integer, List<Station>>(); 
    4752     
    48     // NEED FINISH final private ArrayList<Highway> highways; 
     53    final public ArrayList<Highway> highways; 
     54     
    4955    public Highways(String ldsFileName, String loopsFileName, 
    5056            String highwayMetaFileName, String FEPHostName, int FEPPortNum) { 
     
    5460         */ 
    5561 
    56         NetworkLoader ldr = new NetworkLoader(new File(ldsFileName), new File(loopsFileName)); 
     62        FEPLineLoader ldr = new FEPLineLoader(new File(ldsFileName), new File(loopsFileName)); 
    5763        this.lines = (ArrayList<FEPLine>) ldr.getFEPLines(); 
    5864        this.FEPHostName = FEPHostName; 
     
    6167        //writeHighwaysMeta("hard.txt"); 
    6268    } 
    63  
    64     public ArrayList<Highway> getHighways() 
    65     { 
    66         return this.highways; 
    67     } 
    68      
    6969     
    7070    private ArrayList<Highway> loadHighways() { 
     
    7272        // The list of highways to return 
    7373        ArrayList<Highway> highways = new ArrayList<Highway>(); 
    74         Hashtable<Hashtable<Integer, DIRECTION>, ArrayList<Station>> 
    75                 highwayTable = new Hashtable<>(); 
     74         
     75        Map<Integer, ArrayList<Station>> 
     76                highwayMap = new HashMap<>(); 
    7677         
    7778        for (FEPLine line : lines) 
    7879        { 
    79             ArrayList<Station> lineStations = (ArrayList<Station>) line.getStations(); 
     80            ArrayList<Station> lineStations = (ArrayList<Station>) line.stations; 
    8081            for(Station station : lineStations) 
    8182            { 
    82                 Integer hwyNum = station.getHighwayNumber(); 
    83                 DIRECTION dir = station.getDirection(); 
    84                 Hashtable<Integer, DIRECTION> check = new Hashtable<>(); 
    85                 check.put(hwyNum, dir); 
    86                 if(highwayTable.get(check) == null) 
     83                Integer hwyNum = station.routeNumber; 
     84 
     85                if(!highwayMap.containsKey(hwyNum)) 
    8786                { 
    8887                    ArrayList<Station> stnList = new ArrayList<>(); 
    8988                    stnList.add(station); 
    90                     Hashtable<Hashtable<Integer, DIRECTION>, ArrayList<Station>> 
    91                             newEntry = new Hashtable(); 
    92                     newEntry.put(check, stnList); 
    93                     highwayTable.putAll(newEntry); 
     89                    highwayMap.put(hwyNum, stnList); 
    9490                } 
    9591                else 
    9692                { 
    97                     highwayTable.get(check).add(station); 
     93                    highwayMap.get(hwyNum).add(station); 
    9894                } 
    9995            } 
    10096        } 
    10197         
    102         Set<Hashtable<Integer, DIRECTION>> hwyKeys = highwayTable.keySet(); 
    103         for(Hashtable<Integer, DIRECTION> hwyKey : hwyKeys) 
     98        Set<Integer> hwyKeys = highwayMap.keySet(); 
     99        for(Integer hwyKey : hwyKeys) 
    104100        { 
    105             Enumeration<Integer> hwyNumEnum = hwyKey.keys(); 
    106             Integer hwyNum = hwyNumEnum.nextElement(); 
    107             ArrayList<Station> hwyStations = highwayTable.get(hwyKey); 
    108             DIRECTION hwyDir = hwyKey.get(hwyNum); 
    109             Collections.sort(highwayTable.get(hwyKey)); 
    110             System.out.println("Adding highway: " + hwyNum + " " + hwyDir.toString().charAt(0)); 
    111             highways.add(new Highway(hwyNum,  
     101            ArrayList<Station> hwyStations = highwayMap.get(hwyKey); 
     102            Collections.sort(hwyStations); 
     103            System.out.println("Loaded highway " + hwyKey + "..."); 
     104            highways.add(new Highway(hwyKey,  
    112105                    hwyStations)); 
    113106        } 
     107        System.out.println(""); 
    114108        return highways; 
    115109    } 
     110     
    116111    /* 
    117112     private ArrayList<FEPLine> loadLines(String highwayMetaFileName) { 
     
    215210 
    216211    public void writeToFEP() { 
    217                     System.out.println(this.toXML().toCharArray().length); 
    218  
    219212        try { 
    220213            Socket sock = new Socket(FEPHostName, FEPPortNum); 
     
    293286    } 
    294287 
    295     @Override 
    296     public void update(Observable o, Object arg) { 
    297         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
     288    public Highway getHighwayByRouteNumber(Integer routeNum) { 
     289        Highway returnHwy = null; 
     290        for(Highway hwy : highways) 
     291        { 
     292            if(hwy.routeNumber.equals(routeNum)) 
     293            { 
     294                returnHwy = hwy; 
     295                break; 
     296            } 
     297        } 
     298        return returnHwy; 
    298299    } 
    299300 
Note: See TracChangeset for help on using the changeset viewer.