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

Revision 92, 7.4 KB checked in by jdalbey, 9 years ago (diff)

Station.java: reformatted braces.

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. * A single LDS contains multiple
12 * LoopDetectors, which contain data for a single lane on one direction of the
13 * freeway. A LDS is specific to a single freeway, direction, and postmile.
14 *
15 * @author John A. Torres
16 * @version 9/10/2017
17 */
18public class Station implements Comparable
19{
20
21    /* Static Station meta data */
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 highwayNum;
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 hwy,
38            DIRECTION direction, double postmile)
39    {
40        this.lineNum = lineNum;
41        this.ldsID = ldsID;
42        this.drop = drop;
43        this.loops = loops;
44        this.location = location;
45        this.postmile = postmile;
46        this.direction = direction;
47        this.highwayNum = hwy;
48
49        this.MLTotVol = 0;
50        this.OppTotVol = 0;
51    }
52
53    public int getHighwayNumber()
54    {
55        return this.highwayNum;
56    }
57
58    public DIRECTION getDirection()
59    {
60        return this.direction;
61    }
62
63    /**
64     * Returns the station metadata in condensed form. This is just a quick
65     * script function to make a proper highway metadata configuration file, so
66     * that we can read the network faster.
67     *
68     * @return station metadata
69     */
70    public String getStationMeta()
71    {
72        StringBuilder build = new StringBuilder();
73        build.append(Integer.toString(this.ldsID));
74        build.append(" ");
75        build.append(Integer.toString(this.drop));
76        build.append(" ");
77        build.append(Integer.toString(this.highwayNum));
78        build.append(" ");
79        build.append(this.direction.getLetter());
80        build.append(" ");
81        build.append(Double.toString(this.postmile));
82        build.append(" ");
83        build.append(Integer.toString(loops.size()));
84        build.append(" ");
85        build.append(this.location);
86        build.append("\n");
87        for (LoopDetector loop : loops)
88        {
89            build.append(loop.getLoopMeta());
90        }
91        return build.toString();
92    }
93
94    public double getPostmile()
95    {
96        return postmile;
97    }
98
99    /**
100     * Compare this Station to another by postmile. Note: This might be better
101     * as a Comparator since it checks only one field.
102     */
103    @Override
104    public int compareTo(Object otherStation)
105    {
106        // check for identity
107        if (this == otherStation)
108        {
109            return 0;
110        }
111        // check that Object is of type Station, if not throw exception
112        if (!(otherStation instanceof Station))
113        {
114            throw new ClassCastException("A Station object expected.");
115        }
116
117        // get difference of values
118        double otherStationPostmile = ((Station) otherStation).getPostmile();
119        double val = this.postmile - otherStationPostmile;
120
121        // set appropriate comparable return value
122        int retval = 0;
123        if (val > 0)
124        {
125            retval = 1;
126        }
127        else if (val < 0)
128        {
129            retval = -1;
130        }
131
132        return retval;
133    }
134
135    private static enum XML_TAGS
136    {
137
138        STATION("Station"),
139        LDS_ID("LDS_ID"),
140        LINE_NUM("Line_Num"),
141        DROP("Drop"),
142        LOOPS("Loops"),
143        LOCATION("Location"),
144        POST_MILE("Post_Mile"),
145        DIRECTION("Direction"),
146        FREEWAY("Freeway"),
147        ML_TOT_VOL("ML_Tot_Vol"),
148        OPP_TOT_VOL("Opp_Tot_Vol");
149
150        String tag;
151
152        private XML_TAGS(String n)
153        {
154            tag = n;
155        }
156    }
157
158    public void toXML(Element currElem)
159    {
160        Document theDoc = currElem.getOwnerDocument();
161
162        Element stationElement = theDoc.createElement(XML_TAGS.STATION.tag);
163        currElem.appendChild(stationElement);
164
165        Element ldsIDElement = theDoc.createElement(XML_TAGS.LDS_ID.tag);
166        ldsIDElement.appendChild(theDoc.createTextNode(String.valueOf(this.ldsID)));
167        stationElement.appendChild(ldsIDElement);
168
169        Element lineNumElement = theDoc.createElement(XML_TAGS.LINE_NUM.tag);
170        lineNumElement.appendChild(theDoc.createTextNode(String.valueOf(this.lineNum)));
171        stationElement.appendChild(lineNumElement);
172
173        Element dropElement = theDoc.createElement(XML_TAGS.DROP.tag);
174        dropElement.appendChild(theDoc.createTextNode(String.valueOf(this.drop)));
175        stationElement.appendChild(dropElement);
176
177        Element locationElement = theDoc.createElement(XML_TAGS.LOCATION.tag);
178        locationElement.appendChild(theDoc.createTextNode(this.location));
179        stationElement.appendChild(locationElement);
180
181        Element postMileElement = theDoc.createElement(XML_TAGS.POST_MILE.tag);
182        postMileElement.appendChild(theDoc.createTextNode(String.valueOf(this.postmile)));
183        stationElement.appendChild(postMileElement);
184
185        Element directionElement = theDoc.createElement(XML_TAGS.DIRECTION.tag);
186        directionElement.appendChild(theDoc.createTextNode("" + this.direction.getLetter()));
187        stationElement.appendChild(directionElement);
188
189        Element freewayElement = theDoc.createElement(XML_TAGS.FREEWAY.tag);
190        freewayElement.appendChild(theDoc.createTextNode(String.valueOf(this.highwayNum)));
191        stationElement.appendChild(freewayElement);
192
193        Element mlElement = theDoc.createElement(XML_TAGS.ML_TOT_VOL.tag);
194        mlElement.appendChild(theDoc.createTextNode(String.valueOf(this.MLTotVol)));
195        stationElement.appendChild(mlElement);
196
197        Element oppElement = theDoc.createElement(XML_TAGS.OPP_TOT_VOL.tag);
198        oppElement.appendChild(theDoc.createTextNode(String.valueOf(this.OppTotVol)));
199        stationElement.appendChild(oppElement);
200
201        Element loopsElement = theDoc.createElement(XML_TAGS.LOOPS.tag);
202        stationElement.appendChild(loopsElement);
203
204        for (LoopDetector loop : loops)
205        {
206            loop.toXML(loopsElement);
207        }
208    }
209
210    /**
211     * Enum for freeway direction.
212     *
213     * @author John A. Torres
214     * @version 9/10/2017
215     */
216    public static enum DIRECTION
217    {
218
219        NORTH,
220        SOUTH,
221        EAST,
222        WEST;
223
224        // All the first letters of the values, in order.
225        private static String allLetters = "NSEW";
226
227        /**
228         * Return the first letter of this enum.
229         *
230         * @return String first letter of this enum.
231         */
232        public String getLetter()
233        {
234            return this.toString().substring(0, 1);
235        }
236
237        /**
238         * Returns a direction given its first character.
239         *
240         * @param letter the first character of a direction
241         * @return direction corresponding to letter
242         * @pre letter must be one of allLetters
243         */
244        public static DIRECTION toDirection(String letter)
245        {
246            return values()[allLetters.indexOf(letter.charAt(0))];
247        }
248
249    }
250}
Note: See TracBrowser for help on using the repository browser.