| 1 | package atmsdriver.model; |
|---|
| 2 | |
|---|
| 3 | import atmsdriver.NetworkLoader; |
|---|
| 4 | import atmsdriver.model.Station.DIRECTION; |
|---|
| 5 | import java.io.File; |
|---|
| 6 | import java.io.FileNotFoundException; |
|---|
| 7 | import java.io.FileWriter; |
|---|
| 8 | import java.io.IOException; |
|---|
| 9 | import java.io.PrintWriter; |
|---|
| 10 | import java.io.StringWriter; |
|---|
| 11 | import java.io.Writer; |
|---|
| 12 | import java.net.Socket; |
|---|
| 13 | import java.util.ArrayList; |
|---|
| 14 | import java.util.Collections; |
|---|
| 15 | import java.util.Enumeration; |
|---|
| 16 | import java.util.Hashtable; |
|---|
| 17 | import java.util.Map; |
|---|
| 18 | import java.util.Scanner; |
|---|
| 19 | import java.util.Set; |
|---|
| 20 | import java.util.logging.Level; |
|---|
| 21 | import java.util.logging.Logger; |
|---|
| 22 | import javax.xml.parsers.DocumentBuilder; |
|---|
| 23 | import javax.xml.parsers.DocumentBuilderFactory; |
|---|
| 24 | import javax.xml.transform.OutputKeys; |
|---|
| 25 | import javax.xml.transform.Transformer; |
|---|
| 26 | import javax.xml.transform.TransformerFactory; |
|---|
| 27 | import javax.xml.transform.dom.DOMSource; |
|---|
| 28 | import javax.xml.transform.stream.StreamResult; |
|---|
| 29 | import org.w3c.dom.Document; |
|---|
| 30 | import org.w3c.dom.Element; |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | * |
|---|
| 34 | * @author John A. Torres |
|---|
| 35 | */ |
|---|
| 36 | public class Highways { |
|---|
| 37 | |
|---|
| 38 | final private ArrayList<FEPLine> lines; |
|---|
| 39 | final private String FEPHostName; |
|---|
| 40 | final private int FEPPortNum; |
|---|
| 41 | final private ArrayList<Highway> highways; |
|---|
| 42 | |
|---|
| 43 | // NEED FINISH final private ArrayList<Highway> highways; |
|---|
| 44 | public Highways(String ldsFileName, String loopsFileName, |
|---|
| 45 | String highwayMetaFileName, String FEPHostName, int FEPPortNum) { |
|---|
| 46 | /* |
|---|
| 47 | lines = loadLines(highwayMetaFileName); |
|---|
| 48 | System.out.println("SIZE: " + toXML().toCharArray().length); |
|---|
| 49 | */ |
|---|
| 50 | |
|---|
| 51 | NetworkLoader ldr = new NetworkLoader(new File(ldsFileName), new File(loopsFileName)); |
|---|
| 52 | this.lines = (ArrayList<FEPLine>) ldr.getFEPLines(); |
|---|
| 53 | this.FEPHostName = FEPHostName; |
|---|
| 54 | this.FEPPortNum = FEPPortNum; |
|---|
| 55 | this.highways = loadHighways(); |
|---|
| 56 | //writeHighwaysMeta("hard.txt"); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | private ArrayList<Highway> loadHighways() { |
|---|
| 61 | System.out.println("Loading highways..."); |
|---|
| 62 | ArrayList<Highway> highways = new ArrayList<Highway>(); |
|---|
| 63 | Hashtable<Hashtable<Integer, DIRECTION>, ArrayList<Station>> |
|---|
| 64 | highwayTable = new Hashtable<>(); |
|---|
| 65 | |
|---|
| 66 | for (FEPLine line : lines) |
|---|
| 67 | { |
|---|
| 68 | ArrayList<Station> lineStations = (ArrayList<Station>) line.getStations(); |
|---|
| 69 | for(Station station : lineStations) |
|---|
| 70 | { |
|---|
| 71 | Integer hwyNum = station.getHighwayNumber(); |
|---|
| 72 | DIRECTION dir = station.getDirection(); |
|---|
| 73 | Hashtable<Integer, DIRECTION> check = new Hashtable<>(); |
|---|
| 74 | check.put(hwyNum, dir); |
|---|
| 75 | if(highwayTable.get(check) == null) |
|---|
| 76 | { |
|---|
| 77 | ArrayList<Station> stnList = new ArrayList<>(); |
|---|
| 78 | stnList.add(station); |
|---|
| 79 | Hashtable<Hashtable<Integer, DIRECTION>, ArrayList<Station>> |
|---|
| 80 | newEntry = new Hashtable(); |
|---|
| 81 | newEntry.put(check, stnList); |
|---|
| 82 | highwayTable.putAll(newEntry); |
|---|
| 83 | } |
|---|
| 84 | else |
|---|
| 85 | { |
|---|
| 86 | highwayTable.get(check).add(station); |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | Set<Hashtable<Integer, DIRECTION>> hwyKeys = highwayTable.keySet(); |
|---|
| 92 | for(Hashtable<Integer, DIRECTION> hwyKey : hwyKeys) |
|---|
| 93 | { |
|---|
| 94 | Enumeration<Integer> hwyNumEnum = hwyKey.keys(); |
|---|
| 95 | Integer hwyNum = hwyNumEnum.nextElement(); |
|---|
| 96 | ArrayList<Station> hwyStations = highwayTable.get(hwyKey); |
|---|
| 97 | DIRECTION hwyDir = hwyKey.get(hwyNum); |
|---|
| 98 | Collections.sort(highwayTable.get(hwyKey)); |
|---|
| 99 | System.out.println("Adding highway: " + hwyNum + " " + hwyDir.name + " " + hwyStations.size()); |
|---|
| 100 | highways.add(new Highway(hwyNum, |
|---|
| 101 | hwyDir, hwyStations)); |
|---|
| 102 | } |
|---|
| 103 | return highways; |
|---|
| 104 | } |
|---|
| 105 | /* |
|---|
| 106 | private ArrayList<FEPLine> loadLines(String highwayMetaFileName) { |
|---|
| 107 | ArrayList<FEPLine> lines = new ArrayList<>(); |
|---|
| 108 | try { |
|---|
| 109 | Scanner sc = new Scanner(new File(highwayMetaFileName)); |
|---|
| 110 | String firstLine = sc.nextLine(); |
|---|
| 111 | |
|---|
| 112 | Scanner linesc = new Scanner(firstLine); |
|---|
| 113 | int numLines = linesc.nextInt(); |
|---|
| 114 | linesc.close(); |
|---|
| 115 | |
|---|
| 116 | for (int i = 0; i < numLines; i++) { |
|---|
| 117 | System.out.println("CURR: " + i); |
|---|
| 118 | lines.add(loadLine(sc)); |
|---|
| 119 | } |
|---|
| 120 | sc.close(); |
|---|
| 121 | |
|---|
| 122 | } catch (FileNotFoundException ex) { |
|---|
| 123 | Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 124 | } |
|---|
| 125 | return lines; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | private FEPLine loadLine(Scanner sc) { |
|---|
| 129 | String line = sc.nextLine(); |
|---|
| 130 | System.out.println(line); |
|---|
| 131 | Scanner scline = new Scanner(line); |
|---|
| 132 | |
|---|
| 133 | int lineNum = scline.nextInt(); |
|---|
| 134 | int count = scline.nextInt(); |
|---|
| 135 | int numStations = scline.nextInt(); |
|---|
| 136 | ArrayList<Station> stations = new ArrayList<>(); |
|---|
| 137 | for (int i = 0; i < numStations; i++) { |
|---|
| 138 | stations.add(loadStation(sc, lineNum)); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | return new FEPLine(lineNum, stations, count); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | private Station loadStation(Scanner sc, int lineNum) { |
|---|
| 145 | String line = sc.nextLine(); |
|---|
| 146 | System.out.println(line); |
|---|
| 147 | Scanner scline = new Scanner(line); |
|---|
| 148 | int ldsID = scline.nextInt(); |
|---|
| 149 | int drop = scline.nextInt(); |
|---|
| 150 | int fwy = scline.nextInt(); |
|---|
| 151 | DIRECTION dir = DIRECTION.getEnum(scline.next()); |
|---|
| 152 | double postmile = scline.nextDouble(); |
|---|
| 153 | int numLoops = scline.nextInt(); |
|---|
| 154 | String location = getStationLoc(line); |
|---|
| 155 | ArrayList<LoopDetector> loops = new ArrayList<>(); |
|---|
| 156 | for (int i = 0; i < numLoops; i++) { |
|---|
| 157 | loops.add(loadLoop(sc)); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | return new Station(lineNum, ldsID, drop, location, loops, fwy, dir, postmile); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | private LoopDetector loadLoop(Scanner sc) { |
|---|
| 164 | String line = sc.nextLine(); |
|---|
| 165 | Scanner scline = new Scanner(line); |
|---|
| 166 | |
|---|
| 167 | int loopID = scline.nextInt(); |
|---|
| 168 | int laneNum = scline.nextInt(); |
|---|
| 169 | String loopLoc = getLoopLoc(line); // NEED GET LOOPLOC |
|---|
| 170 | scline.close(); |
|---|
| 171 | return new LoopDetector(loopID, loopLoc, laneNum); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | private String getLoopLoc(String line) { |
|---|
| 175 | Scanner sc = new Scanner(line); |
|---|
| 176 | sc.nextInt(); |
|---|
| 177 | sc.nextInt(); |
|---|
| 178 | |
|---|
| 179 | // GRABS FROM CURRENT TO END OF LINE |
|---|
| 180 | |
|---|
| 181 | sc.useDelimiter("\\z"); |
|---|
| 182 | String loc = sc.next().trim(); |
|---|
| 183 | sc.close(); |
|---|
| 184 | return loc; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | // Returns the loction given the whole line from the lookup file |
|---|
| 188 | private String getStationLoc(String line) { |
|---|
| 189 | Scanner scline = new Scanner(line); |
|---|
| 190 | scline.nextInt(); |
|---|
| 191 | scline.nextInt(); |
|---|
| 192 | scline.nextInt(); |
|---|
| 193 | scline.next(); |
|---|
| 194 | scline.nextDouble(); |
|---|
| 195 | scline.nextInt(); |
|---|
| 196 | |
|---|
| 197 | // GRABS FROM CURRENT TO END OF LINE |
|---|
| 198 | scline.useDelimiter("\\z"); |
|---|
| 199 | String loc = scline.next().trim(); |
|---|
| 200 | scline.close(); |
|---|
| 201 | return loc; |
|---|
| 202 | } |
|---|
| 203 | */ |
|---|
| 204 | |
|---|
| 205 | public void writeToFEP() { |
|---|
| 206 | try { |
|---|
| 207 | Socket sock = new Socket(FEPHostName, FEPPortNum); |
|---|
| 208 | PrintWriter out = new PrintWriter(sock.getOutputStream(), true); |
|---|
| 209 | out.println(this.toXML()); |
|---|
| 210 | sock.close(); |
|---|
| 211 | } catch (IOException ex) { |
|---|
| 212 | Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | // CHECK: DO WE EVEN NEED TO DO THIS? |
|---|
| 217 | private void updateSequences() { |
|---|
| 218 | for (FEPLine line : lines) { |
|---|
| 219 | line.updateSequences(); |
|---|
| 220 | } |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | /** |
|---|
| 224 | * Returns the network metadata in condensed form. This is just a quick |
|---|
| 225 | * script function to make a proper highway metadata configuration file, so |
|---|
| 226 | * that we can read the network faster. |
|---|
| 227 | * |
|---|
| 228 | * @return Network metadata |
|---|
| 229 | */ |
|---|
| 230 | public void writeHighwaysMeta(String fileName) { |
|---|
| 231 | |
|---|
| 232 | try { |
|---|
| 233 | FileWriter fw = new FileWriter(new File(fileName)); |
|---|
| 234 | StringBuilder build = new StringBuilder(); |
|---|
| 235 | build.append(lines.size()); |
|---|
| 236 | build.append("\n"); |
|---|
| 237 | System.out.println(lines.size()); |
|---|
| 238 | fw.write(build.toString()); |
|---|
| 239 | int count = 1; |
|---|
| 240 | for (FEPLine line : lines) { |
|---|
| 241 | System.out.println("Writing num: " + count); |
|---|
| 242 | count++; |
|---|
| 243 | fw.write(line.getLineMeta()); |
|---|
| 244 | |
|---|
| 245 | } |
|---|
| 246 | } catch (IOException ex) { |
|---|
| 247 | Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 248 | } |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | public String toXML() { |
|---|
| 252 | String xml = null; |
|---|
| 253 | try { |
|---|
| 254 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
|---|
| 255 | DocumentBuilder builder = factory.newDocumentBuilder(); |
|---|
| 256 | Document theDoc = builder.newDocument(); |
|---|
| 257 | |
|---|
| 258 | Element networkElement = theDoc.createElement(XML_TAGS.NETWORK.tag); |
|---|
| 259 | theDoc.appendChild(networkElement); |
|---|
| 260 | |
|---|
| 261 | for (FEPLine line : lines) { |
|---|
| 262 | line.toXML(networkElement); |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | Transformer tf = TransformerFactory.newInstance().newTransformer(); |
|---|
| 266 | |
|---|
| 267 | tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); |
|---|
| 268 | tf.setOutputProperty(OutputKeys.INDENT, "yes"); |
|---|
| 269 | tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); |
|---|
| 270 | |
|---|
| 271 | Writer out = new StringWriter(); |
|---|
| 272 | tf.transform(new DOMSource(theDoc), new StreamResult(out)); |
|---|
| 273 | xml = out.toString(); |
|---|
| 274 | out.close(); |
|---|
| 275 | } catch (Exception ex) { |
|---|
| 276 | Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 277 | } |
|---|
| 278 | return xml; |
|---|
| 279 | |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | private static enum XML_TAGS { |
|---|
| 283 | |
|---|
| 284 | NETWORK("Network"); |
|---|
| 285 | |
|---|
| 286 | String tag; |
|---|
| 287 | |
|---|
| 288 | private XML_TAGS(String n) { |
|---|
| 289 | tag = n; |
|---|
| 290 | } |
|---|
| 291 | } |
|---|
| 292 | } |
|---|