- Timestamp:
- 10/10/2017 01:09:50 AM (9 years ago)
- Location:
- trunk/src
- Files:
-
- 1 added
- 5 edited
- 1 moved
-
atmsdriver/ATMSDriver.java (modified) (7 diffs)
-
atmsdriver/model/FEPLine.java (modified) (2 diffs)
-
atmsdriver/model/Highway.java (added)
-
atmsdriver/model/Highways.java (moved) (moved from trunk/src/atmsdriver/model/Network.java) (2 diffs)
-
atmsdriver/model/LoopDetector.java (modified) (1 diff)
-
atmsdriver/model/Station.java (modified) (5 diffs)
-
tmcsim/application.properties (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/atmsdriver/ATMSDriver.java
r79 r87 1 1 package atmsdriver; 2 2 3 import atmsdriver.model. Network;3 import atmsdriver.model.Highways; 4 4 import java.io.File; 5 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.PrintWriter; 6 8 import java.util.Properties; 7 9 import java.util.logging.Level; … … 9 11 10 12 /** 13 * ATMS Driver reads the current simulation traffic conditions from the 14 * EXCHANGE.XML file and constructs the Highway Network status info in the 15 * format required by the FEP. It then sends this XML data over a socket to the 16 * FEP Simulator. 11 17 * 12 18 * @author John A. Torres … … 15 21 public class ATMSDriver implements Runnable { 16 22 17 /** ATMSDriver Error logger. */ 23 /** 24 * ATMSDriver Error logger. 25 */ 18 26 private static Logger ATMSDriverLogger = Logger.getLogger("atmsdriver"); 19 27 20 28 private static final String CONFIG_FILE_NAME = "atms_driver_config.properties"; 21 29 22 /** Properties object for the CADClient class. */ 30 /** 31 * Properties object for the CADClient class. 32 */ 23 33 private Properties ATMSDriverProperties; 24 34 … … 31 41 */ 32 42 private static enum PROPERTIES { 33 34 43 LDS_FILE_NAME("LDSFileName"), 35 LOOP_FILE_NAME("LoopFileName"), 36 NETWORK_FILE_NAME("NetworkFileName"), 37 EXCHANGE_FILE_NAME("ExchangeFileName"); 44 LOOPS_FILE_NAME("LoopsFileName"), 45 HIGHWAY_META_FILE("HighwayMetaFileName"), 46 EXCHANGE_FILE_NAME("ExchangeFileName"), 47 FEP_WRITER_HOST("FEPWriterHost"), 48 FEP_WRITER_PORT("FEPWriterPort"); 38 49 39 50 public String name; … … 44 55 } 45 56 46 /** Network model. */ 47 final private Network network; 48 49 /** Sleep Time (10 seconds). **/ 50 private static final int SLEEP_TIME = 10000; 51 52 /** Exchange Reader */ 57 /** 58 * Highways in traffic network 59 */ 60 final private Highways highways; 61 62 /** 63 * Sleep Time (30 seconds). * 64 */ 65 private static final int SLEEP_TIME = 30000; 66 67 /** 68 * Exchange Reader 69 */ 53 70 private ExchangeReader exchangeReader; 54 71 55 72 @Override 56 73 public void run() { 57 74 // Check for packets and update the simulator 58 for (;;) {75 while (true) { 59 76 // Flush the input file 60 77 ExchangeInfo exInfo = exchangeReader.parse(ATMSDriverProperties 61 78 .getProperty(PROPERTIES.EXCHANGE_FILE_NAME.name)); 62 network.toXML(); 63 // Update if packet is recieved 79 80 System.out.println(highways.toXML()); 81 highways.writeToFEP(); 82 // Update if exchangeInfo is recieved 64 83 if (exInfo != null) { 65 System.out.println("Grabbed"); 84 // TODO: handle this condition 85 Logger.getLogger("ATMSDriver").log(Level.INFO, "exInfo is not null"); 66 86 } 67 87 68 // Sleep88 // Wait for FEP Sim to process the data we just sent 69 89 try { 70 90 Thread.sleep(SLEEP_TIME); 71 91 } catch (InterruptedException ie) { 72 92 ie.printStackTrace(); 73 93 } 74 94 } … … 81 101 } 82 102 83 network = new Network( 84 new File(ATMSDriverProperties.getProperty( 85 PROPERTIES.LDS_FILE_NAME.name)), 86 new File(ATMSDriverProperties.getProperty( 87 PROPERTIES.LOOP_FILE_NAME.name)), 88 new File(ATMSDriverProperties.getProperty( 89 PROPERTIES.NETWORK_FILE_NAME.name))); 90 network.toXML(); 103 highways = new Highways( 104 ATMSDriverProperties.getProperty( 105 PROPERTIES.LDS_FILE_NAME.name), 106 ATMSDriverProperties.getProperty( 107 PROPERTIES.LOOPS_FILE_NAME.name), 108 ATMSDriverProperties.getProperty( 109 PROPERTIES.HIGHWAY_META_FILE.name), 110 ATMSDriverProperties.getProperty(PROPERTIES.FEP_WRITER_HOST.name), 111 Integer.parseInt(ATMSDriverProperties.getProperty( 112 PROPERTIES.FEP_WRITER_PORT.name))); 113 91 114 exchangeReader = new ExchangeReader(); 92 115 } … … 106 129 107 130 /** 108 * Runs the Paramics simulator.131 * Runs the ATMS Driver. 109 132 */ 110 133 public static void main(String[] args) { -
trunk/src/atmsdriver/model/FEPLine.java
r84 r87 25 25 final private int lineInfo; 26 26 final private long systemKey; 27 finalprivate long globalSeq;28 finalprivate long scheduleSeq;27 private long globalSeq; 28 private long scheduleSeq; 29 29 30 30 public FEPLine(int lineNum, List<Station> stations, int count, … … 40 40 this.globalSeq = globalSeq; 41 41 this.scheduleSeq = scheduleSeq; 42 } 43 44 // NEED TO CHECK NUMBERS? DO WE EVEN NEED THIS? 45 public void updateSequences() 46 { 47 this.scheduleSeq += 1; 48 this.globalSeq += 51; 49 } 50 51 public String getLineMeta() 52 { 53 StringBuilder build = new StringBuilder(); 54 build.append(Integer.toString(this.lineNum)); 55 build.append(" "); 56 build.append(Integer.toString(this.count)); 57 build.append(" "); 58 build.append(Integer.toString(this.stations.size())); 59 build.append("\n"); 60 for(Station station : stations) 61 { 62 build.append(station.getStationMeta()); 63 } 64 return build.toString(); 42 65 } 43 66 -
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 } -
trunk/src/atmsdriver/model/LoopDetector.java
r79 r87 57 57 } 58 58 59 /** 60 * Returns the loop metadata in condensed form. 61 * This is just a quick script function to make a proper highway 62 * metadata configuration file, so that we can read the network 63 * faster. 64 * 65 * @return loop metadata 66 */ 67 public String getLoopMeta() 68 { 69 StringBuilder build = new StringBuilder(); 70 build.append(Integer.toString(this.loopID)); 71 build.append(" "); 72 build.append(Integer.toString(this.laneNum)); 73 build.append(" "); 74 build.append(this.loopLocation); 75 build.append("\n"); 76 return build.toString(); 77 } 78 59 79 /** 60 80 * Updates loop detector dynamic attributes. -
trunk/src/atmsdriver/model/Station.java
r84 r87 5 5 import org.w3c.dom.Element; 6 6 7 /** A Station is a simulation of a station in a traffic network.8 * 9 * A Station (LDS) contains static meta data about the station, and10 two dynamic attributes, MLTotVol and OppTotVol.11 12 A single LDS contains multiple LoopDetectors, which13 contain data for a single lane on one direction of the freeway. A LDS14 is specific to a single freeway,direction, and postmile.7 /** 8 * A Station is a simulation of a station in a traffic network. 9 * 10 * A Station (LDS) contains static meta data about the station, and two dynamic 11 * attributes, MLTotVol and OppTotVol. * 12 * A single LDS contains multiple LoopDetectors, which contain data for a single 13 * lane on one direction of the freeway. A LDS is specific to a single freeway, 14 * direction, and postmile. 15 15 * 16 16 * @author John A. Torres … … 19 19 public class Station implements Comparable { 20 20 /* Static Station meta data */ 21 21 22 final private int lineNum; 22 23 final private int ldsID; // double check … … 27 28 final private double postmile; 28 29 final private DIRECTION direction; 29 30 30 31 /* Dynamic Station data */ 31 32 private int MLTotVol; 32 33 private int OppTotVol; 33 34 34 35 /* Constructor */ 35 36 public Station(int lineNum, int ldsID, int drop, 36 String location, List<LoopDetector> loops, int fwy, 37 DIRECTION direction, double postmile) 38 { 37 String location, List<LoopDetector> loops, int fwy, 38 DIRECTION direction, double postmile) { 39 39 this.lineNum = lineNum; 40 40 this.ldsID = ldsID; … … 45 45 this.direction = direction; 46 46 this.freeway = fwy; 47 47 48 48 this.MLTotVol = 0; 49 49 this.OppTotVol = 0; 50 50 } 51 51 52 /** 53 * Returns the station metadata in condensed form. This is just a quick 54 * script function to make a proper highway metadata configuration file, so 55 * that we can read the network faster. 56 * 57 * @return station metadata 58 */ 59 public String getStationMeta() { 60 StringBuilder build = new StringBuilder(); 61 build.append(Integer.toString(this.ldsID)); 62 build.append(" "); 63 build.append(Integer.toString(this.drop)); 64 build.append(" "); 65 build.append(Integer.toString(this.freeway)); 66 build.append(" "); 67 build.append(this.direction.name); 68 build.append(" "); 69 build.append(Double.toString(this.postmile)); 70 build.append(" "); 71 build.append(Integer.toString(loops.size())); 72 build.append(" "); 73 build.append(this.location); 74 build.append("\n"); 75 for (LoopDetector loop : loops) { 76 build.append(loop.getLoopMeta()); 77 } 78 return build.toString(); 79 } 80 81 public double getPostmile() { 82 return postmile; 83 } 84 52 85 @Override 53 public int compareTo(Object o) { 54 55 } 56 57 private static enum XML_TAGS 58 { 86 public int compareTo(Object otherStation) { 87 // check that Object is of type Station, if not throw exception 88 if (!(otherStation instanceof Station)) { 89 throw new ClassCastException("A Station object expected."); 90 } 91 92 // get difference of values 93 double otherStationPostmile = ((Station) otherStation).getPostmile(); 94 double val = this.postmile - otherStationPostmile; 95 96 // set appropriate comparable return value 97 int retval = 0; 98 if (val > 0) { 99 retval = 1; 100 } else if (val < 0) { 101 retval = -1; 102 } 103 104 return retval; 105 } 106 107 private static enum XML_TAGS { 108 59 109 STATION("Station"), 60 110 LDS_ID("LDS_ID"), … … 68 118 ML_TOT_VOL("ML_Tot_Vol"), 69 119 OPP_TOT_VOL("Opp_Tot_Vol"); 70 120 71 121 String tag; 72 73 private XML_TAGS(String n) 74 { 122 123 private XML_TAGS(String n) { 75 124 tag = n; 76 125 } 77 126 } 78 79 public void toXML(Element currElem) 80 { 127 128 public void toXML(Element currElem) { 81 129 Document theDoc = currElem.getOwnerDocument(); 82 130 83 131 Element stationElement = theDoc.createElement(XML_TAGS.STATION.tag); 84 132 currElem.appendChild(stationElement); 85 133 86 134 Element ldsIDElement = theDoc.createElement(XML_TAGS.LDS_ID.tag); 87 135 ldsIDElement.appendChild(theDoc.createTextNode(String.valueOf(this.ldsID))); 88 136 stationElement.appendChild(ldsIDElement); 89 137 90 138 Element lineNumElement = theDoc.createElement(XML_TAGS.LINE_NUM.tag); 91 139 lineNumElement.appendChild(theDoc.createTextNode(String.valueOf(this.lineNum))); 92 140 stationElement.appendChild(lineNumElement); 93 141 94 142 Element dropElement = theDoc.createElement(XML_TAGS.DROP.tag); 95 143 dropElement.appendChild(theDoc.createTextNode(String.valueOf(this.drop))); 96 144 stationElement.appendChild(dropElement); 97 145 98 146 Element locationElement = theDoc.createElement(XML_TAGS.LOCATION.tag); 99 147 locationElement.appendChild(theDoc.createTextNode(this.location)); 100 148 stationElement.appendChild(locationElement); 101 149 102 150 Element postMileElement = theDoc.createElement(XML_TAGS.POST_MILE.tag); 103 151 postMileElement.appendChild(theDoc.createTextNode(String.valueOf(this.postmile))); 104 152 stationElement.appendChild(postMileElement); 105 153 106 154 Element directionElement = theDoc.createElement(XML_TAGS.DIRECTION.tag); 107 155 directionElement.appendChild(theDoc.createTextNode(this.direction.name)); 108 156 stationElement.appendChild(directionElement); 109 157 110 158 Element freewayElement = theDoc.createElement(XML_TAGS.FREEWAY.tag); 111 159 freewayElement.appendChild(theDoc.createTextNode(String.valueOf(this.freeway))); 112 160 stationElement.appendChild(freewayElement); 113 161 114 162 Element mlElement = theDoc.createElement(XML_TAGS.ML_TOT_VOL.tag); 115 163 mlElement.appendChild(theDoc.createTextNode(String.valueOf(this.MLTotVol))); 116 164 stationElement.appendChild(mlElement); 117 165 118 166 Element oppElement = theDoc.createElement(XML_TAGS.OPP_TOT_VOL.tag); 119 167 oppElement.appendChild(theDoc.createTextNode(String.valueOf(this.OppTotVol))); 120 168 stationElement.appendChild(oppElement); 121 169 122 170 Element loopsElement = theDoc.createElement(XML_TAGS.LOOPS.tag); 123 171 stationElement.appendChild(loopsElement); 124 125 for(LoopDetector loop : loops) 126 { 172 173 for (LoopDetector loop : loops) { 127 174 loop.toXML(loopsElement); 128 175 } 129 176 } 130 177 131 178 /** 132 179 * Enum for freeway direction. 133 * 180 * 134 181 * @author John A. Torres 135 182 * @version 9/10/2017 136 183 */ 137 public static enum DIRECTION 138 { 184 public static enum DIRECTION { 185 139 186 NORTH("N"), 140 187 SOUTH("S"), 141 188 EAST("E"), 142 189 WEST("W"); 143 190 144 191 String name; 145 146 DIRECTION(String name) 147 { 192 193 DIRECTION(String name) { 148 194 this.name = name; 149 195 } 150 196 151 197 /** 152 198 * Returns the direction enum, given a string value. 199 * 153 200 * @param name str value for enum 154 201 * @return enum for given str value 155 202 */ 156 public static DIRECTION getEnum(String name) 157 { 158 switch(name) 159 { 203 public static DIRECTION getEnum(String name) { 204 switch (name) { 160 205 case "S": 161 206 return SOUTH; -
trunk/src/tmcsim/application.properties
r79 r87 1 #T hu, 05 Oct 2017 10:44:21 -07001 #Tue, 10 Oct 2017 02:07:01 -0700 2 2 3 Application.revision= 673 Application.revision=84 4 4 5 Application.buildnumber=4 65 Application.buildnumber=48
Note: See TracChangeset
for help on using the changeset viewer.
