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

Revision 103, 3.6 KB checked in by jtorres, 9 years ago (diff)

trunk/src/atmsdriver/ConsoleDriver.java: Created console driver for ATMSDriver, completed and very nice. Still need to apply correct vol/occ values to actually change the colors. Still need to write a JUnit test for it. trunk/src/atmsdriver/ATMSDriver.java: Refactored to run the console driver. ATMSDriver runs in thread, console driver runs, and they share the highways instance. Renamed NetworkLoader?.java to FEPLineLoader.java. trunk/src/atmsdriver/model/Highways.java: refactored loadHighways() method to conform to new undirected highway abstraction. trunk/src/atmsdriver/model/Station.java: added updateByDirection(DIRECTION dir) method and supporting utility methods. trunk/test/atmsdriver/model/LoadHighwaysTest.java: Conformed LoadHighways? test to new undirected highway abstraction. trunk/test/atmsdriver/model/StationTest.java: Conformed StationTest?.java to new changes - very minor stuff. Went through all model classes and changed any final privates with a getter method to final public, for good OOP practice and simplicity. Went through ALL FILES and commented everything very well. minor application custom configuration changes. Removed all .class or .o.d files from svn repository

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/** 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    private int vol;
25    private int 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, int 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}
Note: See TracBrowser for help on using the repository browser.