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

Revision 234, 5.6 KB checked in by jtorres, 8 years ago (diff)

config/vds_data/highways_fullmap.txt: added loopLocationID. LoopDetector?.java: added loopLocationID field. Highways.java: reading in loopLocationID

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        build.append(this.loopLocation);
94        build.append("\n");
95        return build.toString();
96    }
97   
98    /**
99     * Updates loop detector dynamic attributes.
100     * @param vol volume
101     * @param occ occupancy
102     * @param spd speed
103     */
104    public void updateLoop(int vol, float occ)
105    {
106        this.vol = vol;
107        this.occ = occ;
108    }
109
110    /**
111     * Returns the LoopDetector data in XMLFormat
112     *
113     * @param currElem The current XML <LoopDetector> element
114     */
115    public void toXML(Element currElem)
116    {
117        Document theDoc = currElem.getOwnerDocument();
118       
119        Element loopElement = theDoc.createElement(XML_TAGS.LOOP.tag);
120        currElem.appendChild(loopElement);
121       
122        Element loopIDElement = theDoc.createElement(XML_TAGS.LOOP_ID.tag);
123        loopIDElement.appendChild(theDoc.createTextNode(String.valueOf(this.loopID)));
124        loopElement.appendChild(loopIDElement);
125       
126        Element loopLocElement = theDoc.createElement(XML_TAGS.LOOP_LOCATION.tag);
127        loopLocElement.appendChild(theDoc.createTextNode(this.loopLocation));
128        loopElement.appendChild(loopLocElement);
129       
130        Element volElement = theDoc.createElement(XML_TAGS.VOL.tag);
131        volElement.appendChild(theDoc.createTextNode(String.valueOf(this.vol)));
132        loopElement.appendChild(volElement);
133       
134        Element occElement = theDoc.createElement(XML_TAGS.OCC.tag);
135        occElement.appendChild(theDoc.createTextNode(String.valueOf(this.occ)));
136        loopElement.appendChild(occElement);
137    }
138   
139    /**
140     * Enum for highway status dot colors. Each color has associated volume
141     * and occupancy constants.
142     *
143     * @author John A. Torres, jdalbey
144     * @version 10/11/2017
145     */
146    public static enum DOTCOLOR {
147
148        RED(1, 0.06f),    // "Stopped" is less than 25mph
149        YELLOW(3,0.059f), // speed = 26
150        GREEN(0,0);
151       
152        // All the first letters of the values, in order.
153        public static String allLetters = "RYG";
154       
155        private int vol;  /* volume */
156        private float occ;  /* occupancy */     
157       
158        private DOTCOLOR(int v, float o)
159        {
160            vol = v;
161            occ = o;
162        }
163        /**
164         * Return the first letter of this enum.
165         *
166         * @return String first letter of this enum.
167         */
168        public String getLetter() {
169            return this.toString().substring(0, 1);
170        }
171
172        public int volume()
173        {
174            return vol;
175        }
176        public float occupancy()
177        {
178            return occ;
179        }
180        /**
181         * Returns a dot color given its first character.
182         *
183         * @param letter the first character of a dot color
184         * @return dot color corresponding to letter
185         * @pre letter must be one of allLetters
186         */
187        public static DOTCOLOR toDotColor(String letter) {
188            return values()[allLetters.indexOf(letter.charAt(0))];
189        }
190    } 
191   
192}
Note: See TracBrowser for help on using the repository browser.