source: tmcsimulator/trunk/src/atmsdriver/model/Station.java @ 88

Revision 88, 7.0 KB checked in by jtorres, 9 years ago (diff)

Created a Highway.java class with list of sorted stations (by postmile). Highways.java: now has a loadHighways method which returns the List of highways with sorted stations. Highways.java handles loading and sorting, Highway.java just holds the data.

Line 
1package atmsdriver.model;
2
3import java.util.List;
4import org.w3c.dom.Document;
5import org.w3c.dom.Element;
6
7/**
8 * A Station is a simulation of a station in a traffic network.
9 *
10 * A Station (LDS) contains static meta data about the station, and two dynamic
11 * attributes, MLTotVol and OppTotVol.  *
12 * A single LDS contains multiple LoopDetectors, which contain data for a single
13 * lane on one direction of the freeway. A LDS is specific to a single freeway,
14 * direction, and postmile.
15 *
16 * @author John A. Torres
17 * @version 9/10/2017
18 */
19public class Station implements Comparable {
20    /* Static Station meta data */
21
22    final private int lineNum;
23    final private int ldsID; // double check
24    final private int drop;
25    final private String location;
26    final private List<LoopDetector> loops;
27    final private int freeway;
28    final private double postmile;
29    final private DIRECTION direction;
30
31    /* Dynamic Station data */
32    private int MLTotVol;
33    private int OppTotVol;
34
35    /* Constructor */
36    public Station(int lineNum, int ldsID, int drop,
37            String location, List<LoopDetector> loops, int fwy,
38            DIRECTION direction, double postmile) {
39        this.lineNum = lineNum;
40        this.ldsID = ldsID;
41        this.drop = drop;
42        this.loops = loops;
43        this.location = location;
44        this.postmile = postmile;
45        this.direction = direction;
46        this.freeway = fwy;
47
48        this.MLTotVol = 0;
49        this.OppTotVol = 0;
50    }
51
52    public int getHighwayNumber()
53    {
54        return this.freeway;
55    }
56   
57    public DIRECTION getDirection()
58    {
59        return this.direction;
60    }
61   
62    /**
63     * Returns the station metadata in condensed form. This is just a quick
64     * script function to make a proper highway metadata configuration file, so
65     * that we can read the network faster.
66     *
67     * @return station metadata
68     */
69    public String getStationMeta() {
70        StringBuilder build = new StringBuilder();
71        build.append(Integer.toString(this.ldsID));
72        build.append(" ");
73        build.append(Integer.toString(this.drop));
74        build.append(" ");
75        build.append(Integer.toString(this.freeway));
76        build.append(" ");
77        build.append(this.direction.name);
78        build.append(" ");
79        build.append(Double.toString(this.postmile));
80        build.append(" ");
81        build.append(Integer.toString(loops.size()));
82        build.append(" ");
83        build.append(this.location);
84        build.append("\n");
85        for (LoopDetector loop : loops) {
86            build.append(loop.getLoopMeta());
87        }
88        return build.toString();
89    }
90
91    public double getPostmile() {
92        return postmile;
93    }
94
95    @Override
96    public int compareTo(Object otherStation) {
97        // check that Object is of type Station, if not throw exception
98        if (!(otherStation instanceof Station)) {
99            throw new ClassCastException("A Station object expected.");
100        }
101
102        // get difference of values
103        double otherStationPostmile = ((Station) otherStation).getPostmile();
104        double val = this.postmile - otherStationPostmile;
105
106        // set appropriate comparable return value
107        int retval = 0;
108        if (val > 0) {
109            retval = 1;
110        } else if (val < 0) {
111            retval = -1;
112        }
113
114        return retval;
115    }
116
117    private static enum XML_TAGS {
118
119        STATION("Station"),
120        LDS_ID("LDS_ID"),
121        LINE_NUM("Line_Num"),
122        DROP("Drop"),
123        LOOPS("Loops"),
124        LOCATION("Location"),
125        POST_MILE("Post_Mile"),
126        DIRECTION("Direction"),
127        FREEWAY("Freeway"),
128        ML_TOT_VOL("ML_Tot_Vol"),
129        OPP_TOT_VOL("Opp_Tot_Vol");
130
131        String tag;
132
133        private XML_TAGS(String n) {
134            tag = n;
135        }
136    }
137
138    public void toXML(Element currElem) {
139        Document theDoc = currElem.getOwnerDocument();
140
141        Element stationElement = theDoc.createElement(XML_TAGS.STATION.tag);
142        currElem.appendChild(stationElement);
143
144        Element ldsIDElement = theDoc.createElement(XML_TAGS.LDS_ID.tag);
145        ldsIDElement.appendChild(theDoc.createTextNode(String.valueOf(this.ldsID)));
146        stationElement.appendChild(ldsIDElement);
147
148        Element lineNumElement = theDoc.createElement(XML_TAGS.LINE_NUM.tag);
149        lineNumElement.appendChild(theDoc.createTextNode(String.valueOf(this.lineNum)));
150        stationElement.appendChild(lineNumElement);
151
152        Element dropElement = theDoc.createElement(XML_TAGS.DROP.tag);
153        dropElement.appendChild(theDoc.createTextNode(String.valueOf(this.drop)));
154        stationElement.appendChild(dropElement);
155
156        Element locationElement = theDoc.createElement(XML_TAGS.LOCATION.tag);
157        locationElement.appendChild(theDoc.createTextNode(this.location));
158        stationElement.appendChild(locationElement);
159
160        Element postMileElement = theDoc.createElement(XML_TAGS.POST_MILE.tag);
161        postMileElement.appendChild(theDoc.createTextNode(String.valueOf(this.postmile)));
162        stationElement.appendChild(postMileElement);
163
164        Element directionElement = theDoc.createElement(XML_TAGS.DIRECTION.tag);
165        directionElement.appendChild(theDoc.createTextNode(this.direction.name));
166        stationElement.appendChild(directionElement);
167
168        Element freewayElement = theDoc.createElement(XML_TAGS.FREEWAY.tag);
169        freewayElement.appendChild(theDoc.createTextNode(String.valueOf(this.freeway)));
170        stationElement.appendChild(freewayElement);
171
172        Element mlElement = theDoc.createElement(XML_TAGS.ML_TOT_VOL.tag);
173        mlElement.appendChild(theDoc.createTextNode(String.valueOf(this.MLTotVol)));
174        stationElement.appendChild(mlElement);
175
176        Element oppElement = theDoc.createElement(XML_TAGS.OPP_TOT_VOL.tag);
177        oppElement.appendChild(theDoc.createTextNode(String.valueOf(this.OppTotVol)));
178        stationElement.appendChild(oppElement);
179
180        Element loopsElement = theDoc.createElement(XML_TAGS.LOOPS.tag);
181        stationElement.appendChild(loopsElement);
182
183        for (LoopDetector loop : loops) {
184            loop.toXML(loopsElement);
185        }
186    }
187
188    /**
189     * Enum for freeway direction.
190     *
191     * @author John A. Torres
192     * @version 9/10/2017
193     */
194    public static enum DIRECTION {
195
196        NORTH("N"),
197        SOUTH("S"),
198        EAST("E"),
199        WEST("W");
200
201        String name;
202
203        DIRECTION(String name) {
204            this.name = name;
205        }
206
207        /**
208         * Returns the direction enum, given a string value.
209         *
210         * @param name str value for enum
211         * @return enum for given str value
212         */
213        public static DIRECTION getEnum(String name) {
214            switch (name) {
215                case "S":
216                    return SOUTH;
217                case "N":
218                    return NORTH;
219                case "E":
220                    return EAST;
221                case "W":
222                    return WEST;
223                default:
224                    return null;
225            }
226        }
227    }
228}
Note: See TracBrowser for help on using the repository browser.