| 1 | package atmsdriver.model; |
|---|
| 2 | |
|---|
| 3 | import atmsdriver.ConsoleTrafficDriver; |
|---|
| 4 | import atmsdriver.model.LoopDetector.DOTCOLOR; |
|---|
| 5 | import java.util.List; |
|---|
| 6 | import org.w3c.dom.Document; |
|---|
| 7 | import org.w3c.dom.Element; |
|---|
| 8 | |
|---|
| 9 | /** |
|---|
| 10 | *A Station (LDS or Loop Detector Station) represents a group of lane detectors |
|---|
| 11 | * across all lanes at a particular point on a highway. A station is |
|---|
| 12 | * identified by its highway number and postmile. A station has an associated |
|---|
| 13 | * direction used to establish which direction is Main and which is Opposite. |
|---|
| 14 | * The MLTotVol and OppTotVol for a station can be dynamically updated. |
|---|
| 15 | * A station has other attributes: lineNum, ldsID, drop, and location used |
|---|
| 16 | * by the FEP. A station can be compared to other stations by its postmile. |
|---|
| 17 | * |
|---|
| 18 | * @author John A. Torres |
|---|
| 19 | * @version 9/10/2017 |
|---|
| 20 | */ |
|---|
| 21 | public class Station implements Comparable |
|---|
| 22 | { |
|---|
| 23 | |
|---|
| 24 | /* Static Station meta data */ |
|---|
| 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; |
|---|
| 33 | |
|---|
| 34 | /* Dynamic Station data */ |
|---|
| 35 | private int MLTotVol; |
|---|
| 36 | private int OppTotVol; |
|---|
| 37 | |
|---|
| 38 | /* Constructor */ |
|---|
| 39 | public Station(int lineNum, int ldsID, int drop, |
|---|
| 40 | String location, List<LoopDetector> loops, int hwy, |
|---|
| 41 | DIRECTION direction, double postmile) |
|---|
| 42 | { |
|---|
| 43 | this.lineNum = lineNum; |
|---|
| 44 | this.ldsID = ldsID; |
|---|
| 45 | this.drop = drop; |
|---|
| 46 | this.loops = loops; |
|---|
| 47 | this.location = location; |
|---|
| 48 | this.postmile = postmile; |
|---|
| 49 | this.direction = direction; |
|---|
| 50 | this.routeNumber = hwy; |
|---|
| 51 | |
|---|
| 52 | this.MLTotVol = getMLTotVol(); |
|---|
| 53 | this.OppTotVol = getOPPTotVol(); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | private int getMLTotVol() |
|---|
| 57 | { |
|---|
| 58 | int mlTotVol = 0; |
|---|
| 59 | for(LoopDetector loop : loops) |
|---|
| 60 | { |
|---|
| 61 | if(loop.loopLocation.startsWith("ML")) |
|---|
| 62 | { |
|---|
| 63 | mlTotVol += loop.vol; |
|---|
| 64 | } |
|---|
| 65 | } |
|---|
| 66 | return mlTotVol; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | private int getOPPTotVol() |
|---|
| 70 | { |
|---|
| 71 | int oppTotVol = 0; |
|---|
| 72 | for(LoopDetector loop : loops) |
|---|
| 73 | { |
|---|
| 74 | if(loop.loopLocation.startsWith("OS")) |
|---|
| 75 | { |
|---|
| 76 | oppTotVol += loop.vol; |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | return oppTotVol; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * Returns the station metadata in condensed form. This is just a quick |
|---|
| 84 | * script function to make a proper highway metadata configuration file, so |
|---|
| 85 | * that we can read the network faster. |
|---|
| 86 | * |
|---|
| 87 | * @return station metadata |
|---|
| 88 | */ |
|---|
| 89 | public String getStationMeta() |
|---|
| 90 | { |
|---|
| 91 | StringBuilder build = new StringBuilder(); |
|---|
| 92 | build.append(Integer.toString(this.ldsID)); |
|---|
| 93 | build.append(" "); |
|---|
| 94 | build.append(Integer.toString(this.drop)); |
|---|
| 95 | build.append(" "); |
|---|
| 96 | build.append(Integer.toString(this.routeNumber)); |
|---|
| 97 | build.append(" "); |
|---|
| 98 | build.append(this.direction.getLetter()); |
|---|
| 99 | build.append(" "); |
|---|
| 100 | build.append(Double.toString(this.postmile)); |
|---|
| 101 | build.append(" "); |
|---|
| 102 | build.append(Integer.toString(loops.size())); |
|---|
| 103 | build.append(" "); |
|---|
| 104 | build.append(this.location); |
|---|
| 105 | build.append("\n"); |
|---|
| 106 | for (LoopDetector loop : loops) |
|---|
| 107 | { |
|---|
| 108 | build.append(loop.getLoopMeta()); |
|---|
| 109 | } |
|---|
| 110 | return build.toString(); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | /** |
|---|
| 114 | * Compare this Station to another by postmile. Note: This might be better |
|---|
| 115 | * as a Comparator since it checks only one field. |
|---|
| 116 | */ |
|---|
| 117 | @Override |
|---|
| 118 | public int compareTo(Object otherStation) |
|---|
| 119 | { |
|---|
| 120 | // check for identity |
|---|
| 121 | if (this == otherStation) |
|---|
| 122 | { |
|---|
| 123 | return 0; |
|---|
| 124 | } |
|---|
| 125 | // check that Object is of type Station, if not throw exception |
|---|
| 126 | if (!(otherStation instanceof Station)) |
|---|
| 127 | { |
|---|
| 128 | throw new ClassCastException("A Station object expected."); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | // get difference of values |
|---|
| 132 | double otherStationPostmile = ((Station) otherStation).postmile; |
|---|
| 133 | double val = this.postmile - otherStationPostmile; |
|---|
| 134 | |
|---|
| 135 | // set appropriate comparable return value |
|---|
| 136 | int retval = 0; |
|---|
| 137 | if (val > 0) |
|---|
| 138 | { |
|---|
| 139 | retval = 1; |
|---|
| 140 | } |
|---|
| 141 | else if (val < 0) |
|---|
| 142 | { |
|---|
| 143 | retval = -1; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | return retval; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | /** Determine which lane fields to update based on given direction |
|---|
| 150 | * and update all the loop detectors with the given color. |
|---|
| 151 | * @param direction desired highway direction |
|---|
| 152 | * @param dotColor desired dot color |
|---|
| 153 | */ |
|---|
| 154 | public void updateByDirection(DIRECTION direction, DOTCOLOR dotColor) |
|---|
| 155 | { |
|---|
| 156 | String laneDir = "OS"; |
|---|
| 157 | if(direction.equals(this.direction)) |
|---|
| 158 | { |
|---|
| 159 | laneDir = "ML"; |
|---|
| 160 | } |
|---|
| 161 | outputUpdateMessage(dotColor, laneDir); |
|---|
| 162 | |
|---|
| 163 | for(LoopDetector loop : loops) |
|---|
| 164 | { |
|---|
| 165 | if(loop.loopLocation.startsWith(laneDir)) |
|---|
| 166 | { |
|---|
| 167 | // UPDATE LOOP WITH VALUES |
|---|
| 168 | int speed = 0; |
|---|
| 169 | loop.updateLoop(dotColor.volume(), dotColor.occupancy(), speed); |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | private void outputUpdateMessage(DOTCOLOR dotcolor, String OPP_ML) |
|---|
| 175 | { |
|---|
| 176 | System.out.printf("Updating %-3.3s %-5.5s %-3.3s lanes\t %-12.12s " |
|---|
| 177 | + "at postmile %-6.6s to %-7.7s\n", |
|---|
| 178 | Integer.toString(this.routeNumber), this.direction.name(), |
|---|
| 179 | OPP_ML, this.location, Double.toString(this.postmile), |
|---|
| 180 | dotcolor.name()); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | private static enum XML_TAGS |
|---|
| 184 | { |
|---|
| 185 | |
|---|
| 186 | STATION("Station"), |
|---|
| 187 | LDS_ID("LDS_ID"), |
|---|
| 188 | LINE_NUM("Line_Num"), |
|---|
| 189 | DROP("Drop"), |
|---|
| 190 | LOOPS("Loops"), |
|---|
| 191 | LOCATION("Location"), |
|---|
| 192 | POST_MILE("Post_Mile"), |
|---|
| 193 | DIRECTION("Direction"), |
|---|
| 194 | FREEWAY("Freeway"), |
|---|
| 195 | ML_TOT_VOL("ML_Tot_Vol"), |
|---|
| 196 | OPP_TOT_VOL("Opp_Tot_Vol"); |
|---|
| 197 | |
|---|
| 198 | String tag; |
|---|
| 199 | |
|---|
| 200 | private XML_TAGS(String n) |
|---|
| 201 | { |
|---|
| 202 | tag = n; |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | public void toXML(Element currElem) |
|---|
| 207 | { |
|---|
| 208 | Document theDoc = currElem.getOwnerDocument(); |
|---|
| 209 | |
|---|
| 210 | Element stationElement = theDoc.createElement(XML_TAGS.STATION.tag); |
|---|
| 211 | currElem.appendChild(stationElement); |
|---|
| 212 | |
|---|
| 213 | Element ldsIDElement = theDoc.createElement(XML_TAGS.LDS_ID.tag); |
|---|
| 214 | ldsIDElement.appendChild(theDoc.createTextNode(String.valueOf(this.ldsID))); |
|---|
| 215 | stationElement.appendChild(ldsIDElement); |
|---|
| 216 | |
|---|
| 217 | Element lineNumElement = theDoc.createElement(XML_TAGS.LINE_NUM.tag); |
|---|
| 218 | lineNumElement.appendChild(theDoc.createTextNode(String.valueOf(this.lineNum))); |
|---|
| 219 | stationElement.appendChild(lineNumElement); |
|---|
| 220 | |
|---|
| 221 | Element dropElement = theDoc.createElement(XML_TAGS.DROP.tag); |
|---|
| 222 | dropElement.appendChild(theDoc.createTextNode(String.valueOf(this.drop))); |
|---|
| 223 | stationElement.appendChild(dropElement); |
|---|
| 224 | |
|---|
| 225 | Element locationElement = theDoc.createElement(XML_TAGS.LOCATION.tag); |
|---|
| 226 | locationElement.appendChild(theDoc.createTextNode(this.location)); |
|---|
| 227 | stationElement.appendChild(locationElement); |
|---|
| 228 | |
|---|
| 229 | Element postMileElement = theDoc.createElement(XML_TAGS.POST_MILE.tag); |
|---|
| 230 | postMileElement.appendChild(theDoc.createTextNode(String.valueOf(this.postmile))); |
|---|
| 231 | stationElement.appendChild(postMileElement); |
|---|
| 232 | |
|---|
| 233 | Element directionElement = theDoc.createElement(XML_TAGS.DIRECTION.tag); |
|---|
| 234 | directionElement.appendChild(theDoc.createTextNode("" + this.direction.getLetter())); |
|---|
| 235 | stationElement.appendChild(directionElement); |
|---|
| 236 | |
|---|
| 237 | Element freewayElement = theDoc.createElement(XML_TAGS.FREEWAY.tag); |
|---|
| 238 | freewayElement.appendChild(theDoc.createTextNode(String.valueOf(this.routeNumber))); |
|---|
| 239 | stationElement.appendChild(freewayElement); |
|---|
| 240 | |
|---|
| 241 | Element mlElement = theDoc.createElement(XML_TAGS.ML_TOT_VOL.tag); |
|---|
| 242 | mlElement.appendChild(theDoc.createTextNode(String.valueOf(this.MLTotVol))); |
|---|
| 243 | stationElement.appendChild(mlElement); |
|---|
| 244 | |
|---|
| 245 | Element oppElement = theDoc.createElement(XML_TAGS.OPP_TOT_VOL.tag); |
|---|
| 246 | oppElement.appendChild(theDoc.createTextNode(String.valueOf(this.OppTotVol))); |
|---|
| 247 | stationElement.appendChild(oppElement); |
|---|
| 248 | |
|---|
| 249 | Element loopsElement = theDoc.createElement(XML_TAGS.LOOPS.tag); |
|---|
| 250 | stationElement.appendChild(loopsElement); |
|---|
| 251 | |
|---|
| 252 | for (LoopDetector loop : loops) |
|---|
| 253 | { |
|---|
| 254 | loop.toXML(loopsElement); |
|---|
| 255 | } |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | /** |
|---|
| 259 | * Enum for freeway direction. |
|---|
| 260 | * |
|---|
| 261 | * @author John A. Torres |
|---|
| 262 | * @version 9/10/2017 |
|---|
| 263 | */ |
|---|
| 264 | public static enum DIRECTION |
|---|
| 265 | { |
|---|
| 266 | |
|---|
| 267 | NORTH, |
|---|
| 268 | SOUTH, |
|---|
| 269 | EAST, |
|---|
| 270 | WEST; |
|---|
| 271 | |
|---|
| 272 | // All the first letters of the values, in order. |
|---|
| 273 | private static String allLetters = "NSEW"; |
|---|
| 274 | |
|---|
| 275 | /** |
|---|
| 276 | * Return the first letter of this enum. |
|---|
| 277 | * |
|---|
| 278 | * @return String first letter of this enum. |
|---|
| 279 | */ |
|---|
| 280 | public String getLetter() |
|---|
| 281 | { |
|---|
| 282 | return this.toString().substring(0, 1); |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | /** |
|---|
| 286 | * Returns a direction given its first character. |
|---|
| 287 | * |
|---|
| 288 | * @param letter the first character of a direction |
|---|
| 289 | * @return direction corresponding to letter |
|---|
| 290 | * @pre letter must be one of allLetters |
|---|
| 291 | */ |
|---|
| 292 | public static DIRECTION toDirection(String letter) |
|---|
| 293 | { |
|---|
| 294 | return values()[allLetters.indexOf(letter.charAt(0))]; |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | } |
|---|
| 298 | } |
|---|