Changeset 87 in tmcsimulator for trunk/src/atmsdriver/model/Highways.java
- Timestamp:
- 10/10/2017 01:09:50 AM (9 years ago)
- File:
-
- 1 moved
-
trunk/src/atmsdriver/model/Highways.java (moved) (moved from trunk/src/atmsdriver/model/Network.java) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/atmsdriver/model/Highways.java
r79 r87 2 2 3 3 import atmsdriver.NetworkLoader; 4 import atmsdriver.model.Station.DIRECTION; 4 5 import java.io.File; 6 import java.io.FileNotFoundException; 5 7 import java.io.FileWriter; 6 8 import java.io.IOException; 9 import java.io.PrintWriter; 7 10 import java.io.StringWriter; 8 11 import java.io.Writer; 12 import java.net.Socket; 9 13 import java.util.ArrayList; 14 import java.util.Scanner; 10 15 import java.util.logging.Level; 11 16 import java.util.logging.Logger; 12 17 import javax.xml.parsers.DocumentBuilder; 13 18 import javax.xml.parsers.DocumentBuilderFactory; 14 import javax.xml.parsers.ParserConfigurationException;15 19 import javax.xml.transform.OutputKeys; 16 20 import javax.xml.transform.Transformer; 17 import javax.xml.transform.TransformerConfigurationException;18 import javax.xml.transform.TransformerException;19 21 import javax.xml.transform.TransformerFactory; 20 22 import javax.xml.transform.dom.DOMSource; … … 27 29 * @author andrew 28 30 */ 29 public class Network { 31 public class Highways { 32 30 33 final private ArrayList<FEPLine> lines; 31 final private File networkFile; 32 33 public Network(File LDSFile, File loopFile, File networkFile) 34 final private String FEPHostName; 35 final private int FEPPortNum; 36 37 // NEED FINISH final private ArrayList<Highway> highways; 38 39 public Highways(String ldsFileName, String loopsFileName, 40 String highwayMetaFileName, String FEPHostName, int FEPPortNum) { 41 /* 42 lines = loadLines(highwayMetaFileName); 43 System.out.println("SIZE: " + toXML().toCharArray().length); 44 */ 45 46 NetworkLoader ldr = new NetworkLoader(new File(ldsFileName), new File(loopsFileName)); 47 this.lines = (ArrayList<FEPLine>) ldr.getFEPLines(); 48 this.FEPHostName = FEPHostName; 49 this.FEPPortNum = FEPPortNum; 50 // NEED FINISH this.highways = loadHighways(); 51 //writeHighwaysMeta("hard.txt"); 52 } 53 54 // NEED FINISH 55 private void loadHighways() 34 56 { 35 lines = (ArrayList<FEPLine>) 36 new NetworkLoader(LDSFile, loopFile).getFEPLines(); 37 this.networkFile = networkFile; 38 } 39 40 public void toXML() 41 { 57 58 //highways.add(new Highway(lines, )); 59 60 } 61 /* 62 private ArrayList<FEPLine> loadLines(String highwayMetaFileName) { 63 ArrayList<FEPLine> lines = new ArrayList<>(); 64 try { 65 Scanner sc = new Scanner(new File(highwayMetaFileName)); 66 String firstLine = sc.nextLine(); 67 68 Scanner linesc = new Scanner(firstLine); 69 int numLines = linesc.nextInt(); 70 linesc.close(); 71 72 for (int i = 0; i < numLines; i++) { 73 System.out.println("CURR: " + i); 74 lines.add(loadLine(sc)); 75 } 76 sc.close(); 77 78 } catch (FileNotFoundException ex) { 79 Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex); 80 } 81 return lines; 82 } 83 84 private FEPLine loadLine(Scanner sc) { 85 String line = sc.nextLine(); 86 System.out.println(line); 87 Scanner scline = new Scanner(line); 88 89 int lineNum = scline.nextInt(); 90 int count = scline.nextInt(); 91 int numStations = scline.nextInt(); 92 ArrayList<Station> stations = new ArrayList<>(); 93 for (int i = 0; i < numStations; i++) { 94 stations.add(loadStation(sc, lineNum)); 95 } 96 97 return new FEPLine(lineNum, stations, count); 98 } 99 100 private Station loadStation(Scanner sc, int lineNum) { 101 String line = sc.nextLine(); 102 System.out.println(line); 103 Scanner scline = new Scanner(line); 104 int ldsID = scline.nextInt(); 105 int drop = scline.nextInt(); 106 int fwy = scline.nextInt(); 107 DIRECTION dir = DIRECTION.getEnum(scline.next()); 108 double postmile = scline.nextDouble(); 109 int numLoops = scline.nextInt(); 110 String location = getStationLoc(line); 111 ArrayList<LoopDetector> loops = new ArrayList<>(); 112 for (int i = 0; i < numLoops; i++) { 113 loops.add(loadLoop(sc)); 114 } 115 116 return new Station(lineNum, ldsID, drop, location, loops, fwy, dir, postmile); 117 } 118 119 private LoopDetector loadLoop(Scanner sc) { 120 String line = sc.nextLine(); 121 Scanner scline = new Scanner(line); 122 123 int loopID = scline.nextInt(); 124 int laneNum = scline.nextInt(); 125 String loopLoc = getLoopLoc(line); // NEED GET LOOPLOC 126 scline.close(); 127 return new LoopDetector(loopID, loopLoc, laneNum); 128 } 129 130 private String getLoopLoc(String line) { 131 Scanner sc = new Scanner(line); 132 sc.nextInt(); 133 sc.nextInt(); 134 135 // GRABS FROM CURRENT TO END OF LINE 136 137 sc.useDelimiter("\\z"); 138 String loc = sc.next().trim(); 139 sc.close(); 140 return loc; 141 } 142 143 // Returns the loction given the whole line from the lookup file 144 private String getStationLoc(String line) { 145 Scanner scline = new Scanner(line); 146 scline.nextInt(); 147 scline.nextInt(); 148 scline.nextInt(); 149 scline.next(); 150 scline.nextDouble(); 151 scline.nextInt(); 152 153 // GRABS FROM CURRENT TO END OF LINE 154 scline.useDelimiter("\\z"); 155 String loc = scline.next().trim(); 156 scline.close(); 157 return loc; 158 } 159 */ 160 public void writeToFEP() { 161 try { 162 Socket sock = new Socket(FEPHostName, FEPPortNum); 163 PrintWriter out = new PrintWriter(sock.getOutputStream(), true); 164 out.println(this.toXML()); 165 sock.close(); 166 } catch (IOException ex) { 167 Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex); 168 } 169 } 170 171 // CHECK: DO WE EVEN NEED TO DO THIS? 172 private void updateSequences() { 173 for (FEPLine line : lines) { 174 line.updateSequences(); 175 } 176 } 177 178 /** 179 * Returns the network metadata in condensed form. This is just a quick 180 * script function to make a proper highway metadata configuration file, so 181 * that we can read the network faster. 182 * 183 * @return Network metadata 184 */ 185 public void writeHighwaysMeta(String fileName) { 186 187 try { 188 FileWriter fw = new FileWriter(new File(fileName)); 189 StringBuilder build = new StringBuilder(); 190 build.append(lines.size()); 191 build.append("\n"); 192 System.out.println(lines.size()); 193 fw.write(build.toString()); 194 int count = 1; 195 for (FEPLine line : lines) { 196 System.out.println("Writing num: " + count); 197 count++; 198 fw.write(line.getLineMeta()); 199 200 } 201 } catch (IOException ex) { 202 Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex); 203 } 204 } 205 206 public String toXML() { 207 String xml = null; 42 208 try { 43 209 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 44 210 DocumentBuilder builder = factory.newDocumentBuilder(); 45 211 Document theDoc = builder.newDocument(); 46 212 47 213 Element networkElement = theDoc.createElement(XML_TAGS.NETWORK.tag); 48 214 theDoc.appendChild(networkElement); 49 50 for(FEPLine line : lines) 51 { 215 216 for (FEPLine line : lines) { 52 217 line.toXML(networkElement); 53 218 } 54 219 55 220 Transformer tf = TransformerFactory.newInstance().newTransformer(); 56 221 57 222 tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); 58 223 tf.setOutputProperty(OutputKeys.INDENT, "yes"); 59 224 tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); 60 225 61 226 Writer out = new StringWriter(); 62 227 tf.transform(new DOMSource(theDoc), new StreamResult(out)); 63 Stringxml = out.toString();228 xml = out.toString(); 64 229 out.close(); 65 66 Writer fileWr = new FileWriter(networkFile);67 fileWr.write(xml);68 fileWr.close();69 70 71 230 } catch (Exception ex) { 72 Logger.getLogger(Network.class.getName()).log(Level.SEVERE, null, ex); 73 } 74 } 75 76 private static enum XML_TAGS 77 { 231 Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex); 232 } 233 return xml; 234 235 } 236 237 private static enum XML_TAGS { 238 78 239 NETWORK("Network"); 79 240 80 241 String tag; 81 82 private XML_TAGS(String n) 83 { 242 243 private XML_TAGS(String n) { 84 244 tag = n; 85 245 }
Note: See TracChangeset
for help on using the changeset viewer.
