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