| 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 freeway; |
|---|
| 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 fwy, |
|---|
| 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.freeway = fwy; |
|---|
| 47 | |
|---|
| 48 | this.MLTotVol = 0; |
|---|
| 49 | this.OppTotVol = 0; |
|---|
| 50 | } |
|---|
| 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 | |
|---|
| 85 | @Override |
|---|
| 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 | |
|---|
| 109 | STATION("Station"), |
|---|
| 110 | LDS_ID("LDS_ID"), |
|---|
| 111 | LINE_NUM("Line_Num"), |
|---|
| 112 | DROP("Drop"), |
|---|
| 113 | LOOPS("Loops"), |
|---|
| 114 | LOCATION("Location"), |
|---|
| 115 | POST_MILE("Post_Mile"), |
|---|
| 116 | DIRECTION("Direction"), |
|---|
| 117 | FREEWAY("Freeway"), |
|---|
| 118 | ML_TOT_VOL("ML_Tot_Vol"), |
|---|
| 119 | OPP_TOT_VOL("Opp_Tot_Vol"); |
|---|
| 120 | |
|---|
| 121 | String tag; |
|---|
| 122 | |
|---|
| 123 | private XML_TAGS(String n) { |
|---|
| 124 | tag = n; |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | public void toXML(Element currElem) { |
|---|
| 129 | Document theDoc = currElem.getOwnerDocument(); |
|---|
| 130 | |
|---|
| 131 | Element stationElement = theDoc.createElement(XML_TAGS.STATION.tag); |
|---|
| 132 | currElem.appendChild(stationElement); |
|---|
| 133 | |
|---|
| 134 | Element ldsIDElement = theDoc.createElement(XML_TAGS.LDS_ID.tag); |
|---|
| 135 | ldsIDElement.appendChild(theDoc.createTextNode(String.valueOf(this.ldsID))); |
|---|
| 136 | stationElement.appendChild(ldsIDElement); |
|---|
| 137 | |
|---|
| 138 | Element lineNumElement = theDoc.createElement(XML_TAGS.LINE_NUM.tag); |
|---|
| 139 | lineNumElement.appendChild(theDoc.createTextNode(String.valueOf(this.lineNum))); |
|---|
| 140 | stationElement.appendChild(lineNumElement); |
|---|
| 141 | |
|---|
| 142 | Element dropElement = theDoc.createElement(XML_TAGS.DROP.tag); |
|---|
| 143 | dropElement.appendChild(theDoc.createTextNode(String.valueOf(this.drop))); |
|---|
| 144 | stationElement.appendChild(dropElement); |
|---|
| 145 | |
|---|
| 146 | Element locationElement = theDoc.createElement(XML_TAGS.LOCATION.tag); |
|---|
| 147 | locationElement.appendChild(theDoc.createTextNode(this.location)); |
|---|
| 148 | stationElement.appendChild(locationElement); |
|---|
| 149 | |
|---|
| 150 | Element postMileElement = theDoc.createElement(XML_TAGS.POST_MILE.tag); |
|---|
| 151 | postMileElement.appendChild(theDoc.createTextNode(String.valueOf(this.postmile))); |
|---|
| 152 | stationElement.appendChild(postMileElement); |
|---|
| 153 | |
|---|
| 154 | Element directionElement = theDoc.createElement(XML_TAGS.DIRECTION.tag); |
|---|
| 155 | directionElement.appendChild(theDoc.createTextNode(this.direction.name)); |
|---|
| 156 | stationElement.appendChild(directionElement); |
|---|
| 157 | |
|---|
| 158 | Element freewayElement = theDoc.createElement(XML_TAGS.FREEWAY.tag); |
|---|
| 159 | freewayElement.appendChild(theDoc.createTextNode(String.valueOf(this.freeway))); |
|---|
| 160 | stationElement.appendChild(freewayElement); |
|---|
| 161 | |
|---|
| 162 | Element mlElement = theDoc.createElement(XML_TAGS.ML_TOT_VOL.tag); |
|---|
| 163 | mlElement.appendChild(theDoc.createTextNode(String.valueOf(this.MLTotVol))); |
|---|
| 164 | stationElement.appendChild(mlElement); |
|---|
| 165 | |
|---|
| 166 | Element oppElement = theDoc.createElement(XML_TAGS.OPP_TOT_VOL.tag); |
|---|
| 167 | oppElement.appendChild(theDoc.createTextNode(String.valueOf(this.OppTotVol))); |
|---|
| 168 | stationElement.appendChild(oppElement); |
|---|
| 169 | |
|---|
| 170 | Element loopsElement = theDoc.createElement(XML_TAGS.LOOPS.tag); |
|---|
| 171 | stationElement.appendChild(loopsElement); |
|---|
| 172 | |
|---|
| 173 | for (LoopDetector loop : loops) { |
|---|
| 174 | loop.toXML(loopsElement); |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | /** |
|---|
| 179 | * Enum for freeway direction. |
|---|
| 180 | * |
|---|
| 181 | * @author John A. Torres |
|---|
| 182 | * @version 9/10/2017 |
|---|
| 183 | */ |
|---|
| 184 | public static enum DIRECTION { |
|---|
| 185 | |
|---|
| 186 | NORTH("N"), |
|---|
| 187 | SOUTH("S"), |
|---|
| 188 | EAST("E"), |
|---|
| 189 | WEST("W"); |
|---|
| 190 | |
|---|
| 191 | String name; |
|---|
| 192 | |
|---|
| 193 | DIRECTION(String name) { |
|---|
| 194 | this.name = name; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | /** |
|---|
| 198 | * Returns the direction enum, given a string value. |
|---|
| 199 | * |
|---|
| 200 | * @param name str value for enum |
|---|
| 201 | * @return enum for given str value |
|---|
| 202 | */ |
|---|
| 203 | public static DIRECTION getEnum(String name) { |
|---|
| 204 | switch (name) { |
|---|
| 205 | case "S": |
|---|
| 206 | return SOUTH; |
|---|
| 207 | case "N": |
|---|
| 208 | return NORTH; |
|---|
| 209 | case "E": |
|---|
| 210 | return EAST; |
|---|
| 211 | case "W": |
|---|
| 212 | return WEST; |
|---|
| 213 | default: |
|---|
| 214 | return null; |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | } |
|---|