| 1 | package tmcsim.highwaymodel; |
|---|
| 2 | |
|---|
| 3 | import java.util.ArrayList; |
|---|
| 4 | import java.util.List; |
|---|
| 5 | import org.w3c.dom.Document; |
|---|
| 6 | import org.w3c.dom.Element; |
|---|
| 7 | |
|---|
| 8 | /** |
|---|
| 9 | * A LoopDetector represents a single detector for a single lane in a network. |
|---|
| 10 | * |
|---|
| 11 | * A LoopDetector contains static meta data, and three dynamic attributes: vol, |
|---|
| 12 | * occ, and spd. |
|---|
| 13 | * |
|---|
| 14 | * Lane Type A string indicating the type of lane. Possible values (and their meaning) are: |
|---|
| 15 | * |
|---|
| 16 | * CD (Coll/Dist) |
|---|
| 17 | * CH (Conventional Highway) |
|---|
| 18 | * FF (Fwy-Fwy connector) |
|---|
| 19 | * FR (Off Ramp) |
|---|
| 20 | * HV (HOV) |
|---|
| 21 | * ML (Mainline) |
|---|
| 22 | * OR (On Ramp) |
|---|
| 23 | * @author John A. Torres |
|---|
| 24 | * @version 09/10/2017 |
|---|
| 25 | */ |
|---|
| 26 | public class LoopDetector |
|---|
| 27 | { |
|---|
| 28 | /* static data */ |
|---|
| 29 | final public int loopID; |
|---|
| 30 | final public String loopLocation; |
|---|
| 31 | final public String loopLocationID; |
|---|
| 32 | |
|---|
| 33 | /* dynamic data */ |
|---|
| 34 | public int vol; |
|---|
| 35 | public float occ; |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * Constructs a LoopDetector from loopID, loopLocation, and laneNum |
|---|
| 39 | * with initially free flowing traffic. |
|---|
| 40 | * |
|---|
| 41 | * @param loopID |
|---|
| 42 | * @param loopLocation |
|---|
| 43 | * @param laneNum |
|---|
| 44 | */ |
|---|
| 45 | public LoopDetector(int loopID, String loopLocationID, String loopLocation) |
|---|
| 46 | { |
|---|
| 47 | /* Set static data */ |
|---|
| 48 | this.loopID = loopID; |
|---|
| 49 | this.loopLocation = loopLocation; |
|---|
| 50 | this.loopLocationID = loopLocationID; |
|---|
| 51 | /* Init dynamic data */ |
|---|
| 52 | this.vol = 0; |
|---|
| 53 | this.occ = 0; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | /** |
|---|
| 57 | * Setter for loop detector dynamic attributes. Reserved for future use |
|---|
| 58 | * with "live" highway data. |
|---|
| 59 | * @param vol volume |
|---|
| 60 | * @param occ occupancy |
|---|
| 61 | * @param spd speed not used |
|---|
| 62 | */ |
|---|
| 63 | public void setAttributes(int vol, float occ) |
|---|
| 64 | { |
|---|
| 65 | this.vol = vol; |
|---|
| 66 | this.occ = occ; |
|---|
| 67 | } |
|---|
| 68 | /** Set the attributes of this detector given a color. |
|---|
| 69 | * @param DOTCOLOR of the attributes to assign. |
|---|
| 70 | */ |
|---|
| 71 | public void setAttributes(DOTCOLOR color) |
|---|
| 72 | { |
|---|
| 73 | this.vol = color.volume(); |
|---|
| 74 | this.occ = color.occupancy(); |
|---|
| 75 | } |
|---|
| 76 | /** |
|---|
| 77 | * XML tags used for toXML() method. |
|---|
| 78 | */ |
|---|
| 79 | private static enum XML_TAGS |
|---|
| 80 | { |
|---|
| 81 | LOOP_ID("Loop_ID"), |
|---|
| 82 | LOOP_LOCATION("Loop_Location"), |
|---|
| 83 | VOL("Vol"), |
|---|
| 84 | OCC("Occ"), |
|---|
| 85 | LOOP("Loop"); |
|---|
| 86 | |
|---|
| 87 | String tag; |
|---|
| 88 | |
|---|
| 89 | private XML_TAGS(String n) |
|---|
| 90 | { |
|---|
| 91 | tag = n; |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | /** Returns a string of highways data. If MetaDataOnly is true, you get a full |
|---|
| 96 | * dump of the highways meta data, which does not include dynamic loop values, |
|---|
| 97 | * and does include the string location names. If MetaDataOnly is false, |
|---|
| 98 | * dynamic loop values are included, and unnecessary information like string |
|---|
| 99 | * location values are included. |
|---|
| 100 | * |
|---|
| 101 | * The FEPSimulator takes in the toCondensedFormat() output, with a MetaDataOnly |
|---|
| 102 | * value of false, over the socket. |
|---|
| 103 | * |
|---|
| 104 | * The MetaDataOnly flag should be used to get a full dump of the highways |
|---|
| 105 | * information. This was used to get the highways_fullmap.txt output. |
|---|
| 106 | * |
|---|
| 107 | * @param MetaDataOnly Whether you want meta data, or a full dump for FEPSim |
|---|
| 108 | * @return String, highways data in condensed format |
|---|
| 109 | */ |
|---|
| 110 | public String toCondensedFormat(boolean MetaDataOnly) |
|---|
| 111 | { |
|---|
| 112 | StringBuilder build = new StringBuilder(); |
|---|
| 113 | build.append(Integer.toString(this.loopID)); |
|---|
| 114 | build.append(" "); |
|---|
| 115 | if(!MetaDataOnly) |
|---|
| 116 | { |
|---|
| 117 | build.append(" "); |
|---|
| 118 | build.append(this.occ); |
|---|
| 119 | build.append(" "); |
|---|
| 120 | build.append(this.vol); |
|---|
| 121 | build.append(" "); |
|---|
| 122 | } |
|---|
| 123 | else |
|---|
| 124 | { |
|---|
| 125 | build.append(this.loopLocationID); |
|---|
| 126 | build.append(" "); |
|---|
| 127 | } |
|---|
| 128 | build.append(this.loopLocation); |
|---|
| 129 | build.append("\n"); |
|---|
| 130 | return build.toString(); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | /** |
|---|
| 134 | * Returns the LoopDetector data in XMLFormat |
|---|
| 135 | * |
|---|
| 136 | * @param currElem The current XML <LoopDetector> element |
|---|
| 137 | */ |
|---|
| 138 | public void toXML(Element currElem) |
|---|
| 139 | { |
|---|
| 140 | Document theDoc = currElem.getOwnerDocument(); |
|---|
| 141 | |
|---|
| 142 | Element loopElement = theDoc.createElement(XML_TAGS.LOOP.tag); |
|---|
| 143 | currElem.appendChild(loopElement); |
|---|
| 144 | |
|---|
| 145 | Element loopIDElement = theDoc.createElement(XML_TAGS.LOOP_ID.tag); |
|---|
| 146 | loopIDElement.appendChild(theDoc.createTextNode(String.valueOf(this.loopID))); |
|---|
| 147 | loopElement.appendChild(loopIDElement); |
|---|
| 148 | |
|---|
| 149 | Element loopLocElement = theDoc.createElement(XML_TAGS.LOOP_LOCATION.tag); |
|---|
| 150 | loopLocElement.appendChild(theDoc.createTextNode(this.loopLocation)); |
|---|
| 151 | loopElement.appendChild(loopLocElement); |
|---|
| 152 | |
|---|
| 153 | Element volElement = theDoc.createElement(XML_TAGS.VOL.tag); |
|---|
| 154 | volElement.appendChild(theDoc.createTextNode(String.valueOf(this.vol))); |
|---|
| 155 | loopElement.appendChild(volElement); |
|---|
| 156 | |
|---|
| 157 | Element occElement = theDoc.createElement(XML_TAGS.OCC.tag); |
|---|
| 158 | occElement.appendChild(theDoc.createTextNode(String.valueOf(this.occ))); |
|---|
| 159 | loopElement.appendChild(occElement); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | /** |
|---|
| 163 | * Enum for highway status dot colors. Each color has associated volume |
|---|
| 164 | * and occupancy constants, single character symbol, and hml color name. |
|---|
| 165 | * |
|---|
| 166 | * @author John A. Torres, jdalbey |
|---|
| 167 | * @version 10/11/2017 |
|---|
| 168 | */ |
|---|
| 169 | public static enum DOTCOLOR { |
|---|
| 170 | |
|---|
| 171 | RED(1, 0.06f,'@',"red"), // "Stopped" is less than 25mph |
|---|
| 172 | YELLOW(3,0.059f,'+',"yellow"), // speed = 26 |
|---|
| 173 | GREEN(0,0,'-',"lime"); // freeflowing |
|---|
| 174 | |
|---|
| 175 | // All the first letters of the values, in order. |
|---|
| 176 | public static String allLetters = "RYG"; |
|---|
| 177 | |
|---|
| 178 | private int vol; /* volume */ |
|---|
| 179 | private float occ; /* occupancy */ |
|---|
| 180 | private char symbol; /* symbolic representation of this color */ |
|---|
| 181 | private String htmlColor; /* html color name */ |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | private DOTCOLOR(int v, float o, char symbol, String htmlColor) |
|---|
| 185 | { |
|---|
| 186 | vol = v; |
|---|
| 187 | occ = o; |
|---|
| 188 | this.symbol = symbol; |
|---|
| 189 | this.htmlColor = htmlColor; |
|---|
| 190 | } |
|---|
| 191 | /** |
|---|
| 192 | * Return the first letter of this enum. |
|---|
| 193 | * |
|---|
| 194 | * @return String first letter of this enum. |
|---|
| 195 | */ |
|---|
| 196 | public String getLetter() { |
|---|
| 197 | return this.toString().substring(0, 1); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | public int volume() |
|---|
| 201 | { |
|---|
| 202 | return vol; |
|---|
| 203 | } |
|---|
| 204 | public float occupancy() |
|---|
| 205 | { |
|---|
| 206 | return occ; |
|---|
| 207 | } |
|---|
| 208 | public char symbol() |
|---|
| 209 | { |
|---|
| 210 | return symbol; |
|---|
| 211 | } |
|---|
| 212 | public String htmlColor() |
|---|
| 213 | { |
|---|
| 214 | return htmlColor; |
|---|
| 215 | } |
|---|
| 216 | /** |
|---|
| 217 | * Returns a DOTCOLOR given its first character. |
|---|
| 218 | * |
|---|
| 219 | * @param letter the first character of a DOTCOLOR |
|---|
| 220 | * @return DOTCOLOR corresponding to letter |
|---|
| 221 | * @pre letter must be one of allLetters |
|---|
| 222 | */ |
|---|
| 223 | public static DOTCOLOR toDotColor(String letter) { |
|---|
| 224 | return values()[allLetters.indexOf(letter.charAt(0))]; |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | } |
|---|