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

Revision 190, 5.5 KB checked in by jtorres, 9 years ago (diff)

vds_data/highways_fullmap.txt: removed lane num field, not necessary. ATMSDriver.java: removed System.out statement. FEPLine.java: renamed lineNum member to lineID. Highways.java: added example output for toCondensedFormat(boolean) method in method comments. LoopDetector?.java: removed spd member, not necessary, as we calculate speed form occ and vol. Station.java: conformed to above changes.

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