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