Changeset 103 in tmcsimulator for trunk/src/atmsdriver/model/Highways.java
- Timestamp:
- 10/12/2017 12:28:28 AM (9 years ago)
- File:
-
- 1 edited
-
trunk/src/atmsdriver/model/Highways.java (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/atmsdriver/model/Highways.java
r101 r103 1 1 package atmsdriver.model; 2 2 3 import atmsdriver.NetworkLoader; 4 import atmsdriver.model.Station.DIRECTION; 3 import atmsdriver.FEPLineLoader; 5 4 import java.io.File; 6 import java.io.FileNotFoundException;7 5 import java.io.FileWriter; 8 6 import java.io.IOException; … … 13 11 import java.util.ArrayList; 14 12 import java.util.Collections; 15 import java.util.Enumeration;16 13 import java.util.HashMap; 17 import java.util.Hashtable;18 import java.util.List;19 14 import java.util.Map; 20 15 import java.util.Observable; 21 16 import java.util.Observer; 22 import java.util.Scanner;23 17 import java.util.Set; 24 18 import java.util.logging.Level; … … 34 28 import org.w3c.dom.Element; 35 29 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. 37 44 * 38 45 * @author John A. Torres 39 46 */ 40 public class Highways implements Observer{47 public class Highways { 41 48 42 49 final private ArrayList<FEPLine> lines; 43 50 final private String FEPHostName; 44 51 final private int FEPPortNum; 45 final private ArrayList<Highway> highways;46 private Map<Integer, List<Station>> HiwayMap = new HashMap<Integer, List<Station>>();47 52 48 // NEED FINISH final private ArrayList<Highway> highways; 53 final public ArrayList<Highway> highways; 54 49 55 public Highways(String ldsFileName, String loopsFileName, 50 56 String highwayMetaFileName, String FEPHostName, int FEPPortNum) { … … 54 60 */ 55 61 56 NetworkLoader ldr = new NetworkLoader(new File(ldsFileName), new File(loopsFileName));62 FEPLineLoader ldr = new FEPLineLoader(new File(ldsFileName), new File(loopsFileName)); 57 63 this.lines = (ArrayList<FEPLine>) ldr.getFEPLines(); 58 64 this.FEPHostName = FEPHostName; … … 61 67 //writeHighwaysMeta("hard.txt"); 62 68 } 63 64 public ArrayList<Highway> getHighways()65 {66 return this.highways;67 }68 69 69 70 70 private ArrayList<Highway> loadHighways() { … … 72 72 // The list of highways to return 73 73 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<>(); 76 77 77 78 for (FEPLine line : lines) 78 79 { 79 ArrayList<Station> lineStations = (ArrayList<Station>) line. getStations();80 ArrayList<Station> lineStations = (ArrayList<Station>) line.stations; 80 81 for(Station station : lineStations) 81 82 { 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)) 87 86 { 88 87 ArrayList<Station> stnList = new ArrayList<>(); 89 88 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); 94 90 } 95 91 else 96 92 { 97 highway Table.get(check).add(station);93 highwayMap.get(hwyNum).add(station); 98 94 } 99 95 } 100 96 } 101 97 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) 104 100 { 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, 112 105 hwyStations)); 113 106 } 107 System.out.println(""); 114 108 return highways; 115 109 } 110 116 111 /* 117 112 private ArrayList<FEPLine> loadLines(String highwayMetaFileName) { … … 215 210 216 211 public void writeToFEP() { 217 System.out.println(this.toXML().toCharArray().length);218 219 212 try { 220 213 Socket sock = new Socket(FEPHostName, FEPPortNum); … … 293 286 } 294 287 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; 298 299 } 299 300
Note: See TracChangeset
for help on using the changeset viewer.
