Warning: Can't use blame annotator:
svn blame failed on trunk/src/atmsdriver/model/LoopDetector.java: ("Can't find a temporary directory: Internal error", 20014)

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

Revision 180, 5.1 KB checked in by jdalbey, 9 years ago (diff)

ATMSDriver.java Refactored for cleaner design. DotColor? enum moved to LoopDetector? class. ConsoleDriver? renamed ConsoleTrafficDriver? (and associated project configurations updated), build.xml package-jars target updated, ConsoleTrafficDriver? logic improved to allow more user control, runtraffic.bash script created for automating traffic display tests.

RevLine 
1package atmsdriver.model;
2
3import java.util.ArrayList;
4import java.util.List;
5import org.w3c.dom.Document;
6import org.w3c.dom.Element;
7
8/** A LoopDetector represents a single detector for a single lane in a network.
9 *
10 *  A LoopDetector contains static meta data, and three dynamic attributes: vol,
11 *  occ, and spd.
12 *
13 * @author John A. Torres
14 * @version 09/10/2017
15 */
16public class LoopDetector 
17{
18    /* static data */
19    final public int loopID;
20    final public String loopLocation;
21    final public int laneNum;
22   
23    /* dynamic data */
24    public int vol;
25    public float occ;
26    private int spd;
27   
28    public LoopDetector(int loopID, String loopLocation, int laneNum)
29    {
30        /* Set static data */
31        this.loopID = loopID;
32        this.loopLocation = loopLocation;
33        this.laneNum = laneNum;
34       
35        /* Init dynamic data */
36        this.vol = 0;
37        this.spd = 0;
38        this.occ = 0;
39    }
40   
41    private static enum XML_TAGS
42    {
43        LOOP_ID("Loop_ID"),
44        LOOP_LOCATION("Loop_Location"),
45        LANE_NUM("Lane_Num"),
46        VOL("Vol"),
47        SPD("Spd"),
48        OCC("Occ"),
49        LOOP("Loop");
50       
51        String tag;
52       
53        private XML_TAGS(String n)
54        {
55            tag = n;
56        }
57    }
58   
59    /**
60     * Returns the loop metadata in condensed form.
61     * This is just a quick script function to make a proper highway
62     * metadata configuration file, so that we can read the network
63     * faster.
64     *
65     * @return loop metadata
66     */
67    public String getLoopMeta()
68    {
69        StringBuilder build = new StringBuilder();
70        build.append(Integer.toString(this.loopID));
71        build.append(" ");
72        build.append(Integer.toString(this.laneNum));
73        build.append(" ");
74        build.append(this.loopLocation);
75        build.append("\n");
76        return build.toString();
77    }
78   
79    /**
80     * Updates loop detector dynamic attributes.
81     * @param vol volume
82     * @param occ occupancy
83     * @param spd speed
84     */
85    public void updateLoop(int vol, float occ, int spd)
86    {
87        this.vol = vol;
88        this.occ = occ;
89        this.spd = spd;
90    }
91   
92    public void toXML(Element currElem)
93    {
94        Document theDoc = currElem.getOwnerDocument();
95       
96        Element loopElement = theDoc.createElement(XML_TAGS.LOOP.tag);
97        currElem.appendChild(loopElement);
98       
99        Element loopIDElement = theDoc.createElement(XML_TAGS.LOOP_ID.tag);
100        loopIDElement.appendChild(theDoc.createTextNode(String.valueOf(this.loopID)));
101        loopElement.appendChild(loopIDElement);
102       
103        Element loopLocElement = theDoc.createElement(XML_TAGS.LOOP_LOCATION.tag);
104        loopLocElement.appendChild(theDoc.createTextNode(this.loopLocation));
105        loopElement.appendChild(loopLocElement);
106       
107        Element laneNumElement = theDoc.createElement(XML_TAGS.LANE_NUM.tag);
108        laneNumElement.appendChild(theDoc.createTextNode(String.valueOf(this.laneNum)));
109        loopElement.appendChild(laneNumElement);
110       
111        Element volElement = theDoc.createElement(XML_TAGS.VOL.tag);
112        volElement.appendChild(theDoc.createTextNode(String.valueOf(this.vol)));
113        loopElement.appendChild(volElement);
114       
115        Element occElement = theDoc.createElement(XML_TAGS.OCC.tag);
116        occElement.appendChild(theDoc.createTextNode(String.valueOf(this.occ)));
117        loopElement.appendChild(occElement);
118       
119        Element spdElement = theDoc.createElement(XML_TAGS.SPD.tag);
120        spdElement.appendChild(theDoc.createTextNode(String.valueOf(this.spd)));
121        loopElement.appendChild(spdElement);
122    }
123   
124    /**
125     * Enum for highway status dot colors. Each color has associated volume
126     * and occupancy constants.
127     *
128     * @author John A. Torres, jdalbey
129     * @version 10/11/2017
130     */
131    public static enum DOTCOLOR {
132
133        RED(1, 0.06f),    // "Stopped" is less than 25mph
134        YELLOW(3,0.059f), // speed = 26
135        GREEN(0,0);
136       
137        // All the first letters of the values, in order.
138        public static String allLetters = "RYG";
139       
140        private int vol;  /* volume */
141        private float occ;  /* occupancy */     
142       
143        private DOTCOLOR(int v, float o)
144        {
145            vol = v;
146            occ = o;
147        }
148        /**
149         * Return the first letter of this enum.
150         *
151         * @return String first letter of this enum.
152         */
153        public String getLetter() {
154            return this.toString().substring(0, 1);
155        }
156
157        public int volume()
158        {
159            return vol;
160        }
161        public float occupancy()
162        {
163            return occ;
164        }
165        /**
166         * Returns a dot color given its first character.
167         *
168         * @param letter the first character of a dot color
169         * @return dot color corresponding to letter
170         * @pre letter must be one of allLetters
171         */
172        public static DOTCOLOR toDotColor(String letter) {
173            return values()[allLetters.indexOf(letter.charAt(0))];
174        }
175    } 
176   
177}
Note: See TracBrowser for help on using the repository browser.