Changeset 103 in tmcsimulator for trunk/src/atmsdriver/model


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

Location:
trunk/src/atmsdriver/model
Files:
5 edited

Legend:

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

    r88 r103  
    44import org.w3c.dom.Document; 
    55import org.w3c.dom.Element; 
    6  
    7  
    86 
    97/** An FEPLine is a simulated line of communication from the FEP to  
     
    1816public class FEPLine { 
    1917    /* Static FEPLine meta data */ 
    20     final private int lineNum; 
    21     final private List<Station> stations; 
     18    final public int lineNum; 
     19    final public List<Station> stations; 
    2220    final private int count; 
    23     // NOT SURE IF NEXT IS FINAL 
     21     
    2422    final private int schedule; 
    2523    final private int lineInfo; 
    2624    final private long systemKey; 
     25     
     26    // THESE WILL NEED TO BE UPDATED MAYBE, NOT SURE, NOT A PRIORITY. SEE METHOD 
     27    // UPDATESEQUENCES() 
    2728    private long globalSeq; 
    2829    private long scheduleSeq; 
     
    4041        this.globalSeq = globalSeq; 
    4142        this.scheduleSeq = scheduleSeq; 
    42     } 
    43      
    44     public int getLineNumber() 
    45     { 
    46         return this.lineNum; 
    47     } 
    48      
    49     public List<Station> getStations() 
    50     { 
    51         return this.stations; 
    5243    } 
    5344     
  • trunk/src/atmsdriver/model/Highway.java

    r93 r103  
    1010 * @author jdalbey 
    1111 */ 
    12 final class Highway 
     12final public class Highway 
    1313{ 
    1414    /** The identifying number for this highway, e.g., 101 */ 
    15     public final Integer highwayNumber; 
     15    public final Integer routeNumber; 
    1616    /** The ordered list of stations (lane detector stations) on this highway */ 
    1717    public final ArrayList<Station> stations; 
     
    2222     * @param stations ordered list of stations on this highway 
    2323     */ 
    24     public Highway(Integer highwayNum, ArrayList<Station> stations) 
     24    public Highway(Integer routeNumber, ArrayList<Station> stations) 
    2525    { 
    26         this.highwayNumber = highwayNum; 
     26        this.routeNumber = routeNumber; 
    2727        this.stations = stations; 
    2828    } 
  • 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 
  • trunk/src/atmsdriver/model/LoopDetector.java

    r87 r103  
    1717{ 
    1818    /* static data */ 
    19     final private int loopID; 
    20     final private String loopLocation; 
    21     final private int laneNum; 
     19    final public int loopID; 
     20    final public String loopLocation; 
     21    final public int laneNum; 
    2222     
    2323    /* dynamic data */ 
  • trunk/src/atmsdriver/model/Station.java

    r94 r103  
    11package atmsdriver.model; 
    22 
     3import atmsdriver.ConsoleDriver; 
     4import atmsdriver.ConsoleDriver.DOTCOLOR; 
    35import java.util.List; 
    46import org.w3c.dom.Document; 
     
    2123 
    2224    /* Static Station meta data */ 
    23     final private int lineNum; 
    24     final private int ldsID; // double check 
    25     final private int drop; 
    26     final private String location; 
    27     final private List<LoopDetector> loops; 
    28     final private int highwayNum; 
    29     final private double postmile; 
    30     final private DIRECTION direction; 
     25    final public int lineNum; 
     26    final public int ldsID; // double check 
     27    final public int drop; 
     28    final public String location; 
     29    final public List<LoopDetector> loops; 
     30    final public int routeNumber; 
     31    final public double postmile; 
     32    final public DIRECTION direction; 
    3133 
    3234    /* Dynamic Station data */ 
     
    4648        this.postmile = postmile; 
    4749        this.direction = direction; 
    48         this.highwayNum = hwy; 
     50        this.routeNumber = hwy; 
    4951 
    5052        this.MLTotVol = 0; 
    5153        this.OppTotVol = 0; 
    52     } 
    53  
    54     public int getHighwayNumber() 
    55     { 
    56         return this.highwayNum; 
    57     } 
    58  
    59     public DIRECTION getDirection() 
    60     { 
    61         return this.direction; 
    6254    } 
    6355 
     
    7668        build.append(Integer.toString(this.drop)); 
    7769        build.append(" "); 
    78         build.append(Integer.toString(this.highwayNum)); 
     70        build.append(Integer.toString(this.routeNumber)); 
    7971        build.append(" "); 
    8072        build.append(this.direction.getLetter()); 
     
    9183        } 
    9284        return build.toString(); 
    93     } 
    94  
    95     public double getPostmile() 
    96     { 
    97         return postmile; 
    9885    } 
    9986 
     
    117104 
    118105        // get difference of values 
    119         double otherStationPostmile = ((Station) otherStation).getPostmile(); 
     106        double otherStationPostmile = ((Station) otherStation).postmile; 
    120107        double val = this.postmile - otherStationPostmile; 
    121108 
     
    134121    } 
    135122 
     123    public void updateByDirection(DIRECTION direction, DOTCOLOR dotColor) { 
     124        if(direction.equals(this.direction)) 
     125        { 
     126            updateML(dotColor); 
     127        } 
     128        else 
     129        { 
     130            updateOPP(dotColor); 
     131        } 
     132    } 
     133     
     134    private void updateML(DOTCOLOR dotcolor) 
     135    { 
     136        outputUpdateMessage(dotcolor, "ML"); 
     137         
     138        for(LoopDetector loop : loops) 
     139        { 
     140            if(loop.loopLocation.startsWith("ML")) 
     141            { 
     142                // UPDATE LOOP WITH VALUES 
     143            } 
     144        } 
     145    } 
     146     
     147    private void updateOPP(DOTCOLOR dotcolor) 
     148    { 
     149        outputUpdateMessage(dotcolor, "OPP"); 
     150        for(LoopDetector loop : loops) 
     151        { 
     152            if(loop.loopLocation.startsWith("OP")) 
     153            { 
     154                // UPDATE LOOP WITH VALUES 
     155            } 
     156        } 
     157    } 
     158     
     159    private void outputUpdateMessage(DOTCOLOR dotcolor, String OPP_ML) 
     160    { 
     161        System.out.printf("Updating route %-3.3s %-5.5s %-3.3s lanes\t %-12.12s " 
     162                + "at postmile %-6.6s to %-7.7s\n",  
     163                Integer.toString(this.routeNumber), this.direction.name(),  
     164                OPP_ML, this.location, Double.toString(this.postmile),  
     165                dotcolor.name()); 
     166    } 
     167     
    136168    private static enum XML_TAGS 
    137169    { 
     
    189221 
    190222        Element freewayElement = theDoc.createElement(XML_TAGS.FREEWAY.tag); 
    191         freewayElement.appendChild(theDoc.createTextNode(String.valueOf(this.highwayNum))); 
     223        freewayElement.appendChild(theDoc.createTextNode(String.valueOf(this.routeNumber))); 
    192224        stationElement.appendChild(freewayElement); 
    193225 
Note: See TracChangeset for help on using the changeset viewer.