source: tmcsimulator/trunk/src/atmsdriver/model/LoopDetector.java @ 343

Revision 343, 5.7 KB checked in by jdalbey, 7 years ago (diff)

Fix defect #117. Update HighwaysTest?.java for new highway model.

Line 
1package atmsdriver.model;
2
3import java.util.ArrayList;
4import java.util.List;
5import org.w3c.dom.Document;
6import 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 * @author John A. Torres
15 * @version 09/10/2017
16 */
17public class LoopDetector 
18{
19    /* static data */
20    final public int loopID;
21    final public String loopLocation;
22    final public String loopLocationID;
23   
24    /* dynamic data */
25    public int vol;
26    public float occ;
27   
28    /**
29     * Constructs a LoopDetector from loopID, loopLocation, and laneNum
30     *
31     * @param loopID
32     * @param loopLocation
33     * @param laneNum
34     */
35    public LoopDetector(int loopID, String loopLocationID, String loopLocation)
36    {
37        /* Set static data */
38        this.loopID = loopID;
39        this.loopLocation = loopLocation;
40        this.loopLocationID = loopLocationID;
41        /* Init dynamic data */
42        this.vol = 0;
43        this.occ = 0;
44    }
45   
46    /**
47     * XML tags used for toXML() method.
48     */
49    private static enum XML_TAGS
50    {
51        LOOP_ID("Loop_ID"),
52        LOOP_LOCATION("Loop_Location"),
53        VOL("Vol"),
54        OCC("Occ"),
55        LOOP("Loop");
56       
57        String tag;
58       
59        private XML_TAGS(String n)
60        {
61            tag = n;
62        }
63    }
64   
65    /** Returns a string of highways data. If MetaDataOnly is true, you get a full
66     *  dump of the highways meta data, which does not include dynamic loop values,
67     *  and does include the string location names. If MetaDataOnly is false,
68     *  dynamic loop values are included, and unnecessary information like string
69     *  location values are included.
70     *
71     *  The FEPSimulator takes in the toCondensedFormat() output, with a MetaDataOnly
72     *  value of false, over the socket.
73     *
74     *  The MetaDataOnly flag should be used to get a full dump of the highways
75     *  information. This was used to get the highways_fullmap.txt output.
76     *
77     * @param MetaDataOnly Whether you want meta data, or a full dump for FEPSim
78     * @return String, highways data in condensed format
79     */
80    public String toCondensedFormat(boolean MetaDataOnly)
81    {
82        StringBuilder build = new StringBuilder();
83        build.append(Integer.toString(this.loopID));
84        build.append(" ");
85        if(!MetaDataOnly)
86        {
87            build.append(" ");
88            build.append(this.occ);
89            build.append(" ");
90            build.append(this.vol);
91            build.append(" ");
92        }
93        else
94        {
95            build.append(this.loopLocationID);
96            build.append(" ");
97        }
98        build.append(this.loopLocation);
99        build.append("\n");
100        return build.toString();
101    }
102   
103    /**
104     * Updates loop detector dynamic attributes.
105     * @param vol volume
106     * @param occ occupancy
107     * @param spd speed
108     */
109    public void updateLoop(int vol, float occ)
110    {
111        this.vol = vol;
112        this.occ = occ;
113    }
114
115    /**
116     * Returns the LoopDetector data in XMLFormat
117     *
118     * @param currElem The current XML <LoopDetector> element
119     */
120    public void toXML(Element currElem)
121    {
122        Document theDoc = currElem.getOwnerDocument();
123       
124        Element loopElement = theDoc.createElement(XML_TAGS.LOOP.tag);
125        currElem.appendChild(loopElement);
126       
127        Element loopIDElement = theDoc.createElement(XML_TAGS.LOOP_ID.tag);
128        loopIDElement.appendChild(theDoc.createTextNode(String.valueOf(this.loopID)));
129        loopElement.appendChild(loopIDElement);
130       
131        Element loopLocElement = theDoc.createElement(XML_TAGS.LOOP_LOCATION.tag);
132        loopLocElement.appendChild(theDoc.createTextNode(this.loopLocation));
133        loopElement.appendChild(loopLocElement);
134       
135        Element volElement = theDoc.createElement(XML_TAGS.VOL.tag);
136        volElement.appendChild(theDoc.createTextNode(String.valueOf(this.vol)));
137        loopElement.appendChild(volElement);
138       
139        Element occElement = theDoc.createElement(XML_TAGS.OCC.tag);
140        occElement.appendChild(theDoc.createTextNode(String.valueOf(this.occ)));
141        loopElement.appendChild(occElement);
142    }
143   
144    /**
145     * Enum for highway status dot colors. Each color has associated volume
146     * and occupancy constants.
147     *
148     * @author John A. Torres, jdalbey
149     * @version 10/11/2017
150     */
151    public static enum DOTCOLOR {
152
153        RED(1, 0.06f),    // "Stopped" is less than 25mph
154        YELLOW(3,0.059f), // speed = 26
155        GREEN(0,0);
156       
157        // All the first letters of the values, in order.
158        public static String allLetters = "RYG";
159       
160        private int vol;  /* volume */
161        private float occ;  /* occupancy */     
162       
163        private DOTCOLOR(int v, float o)
164        {
165            vol = v;
166            occ = o;
167        }
168        /**
169         * Return the first letter of this enum.
170         *
171         * @return String first letter of this enum.
172         */
173        public String getLetter() {
174            return this.toString().substring(0, 1);
175        }
176
177        public int volume()
178        {
179            return vol;
180        }
181        public float occupancy()
182        {
183            return occ;
184        }
185        /**
186         * Returns a dot color given its first character.
187         *
188         * @param letter the first character of a dot color
189         * @return dot color corresponding to letter
190         * @pre letter must be one of allLetters
191         */
192        public static DOTCOLOR toDotColor(String letter) {
193            return values()[allLetters.indexOf(letter.charAt(0))];
194        }
195    } 
196   
197}
Note: See TracBrowser for help on using the repository browser.