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

Revision 103, 8.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 atmsdriver.ConsoleDriver;
4import atmsdriver.ConsoleDriver.DOTCOLOR;
5import java.util.List;
6import org.w3c.dom.Document;
7import org.w3c.dom.Element;
8
9/**
10 *A Station (LDS or Loop Detector Station) represents a group of lane detectors
11 * across all lanes at a particular point on a highway.  A station is
12 * identified by its highway number and postmile.  A station has an associated
13 * direction used to establish which direction is Main and which is Opposite.
14 * The MLTotVol and OppTotVol for a station can be dynamically updated.
15 * A station has other attributes: lineNum, ldsID, drop, and location used
16 * by the FEP.  A station can be compared to other stations by its postmile.
17 *
18 * @author John A. Torres
19 * @version 9/10/2017
20 */
21public class Station implements Comparable
22{
23
24    /* Static Station meta data */
25    final public int lineNum;
26    final public int ldsID; // double check
27    final public int drop;
28    final public String location;
29    final public List<LoopDetector> loops;
30    final public int routeNumber;
31    final public double postmile;
32    final public DIRECTION direction;
33
34    /* Dynamic Station data */
35    private int MLTotVol;
36    private int OppTotVol;
37
38    /* Constructor */
39    public Station(int lineNum, int ldsID, int drop,
40            String location, List<LoopDetector> loops, int hwy,
41            DIRECTION direction, double postmile)
42    {
43        this.lineNum = lineNum;
44        this.ldsID = ldsID;
45        this.drop = drop;
46        this.loops = loops;
47        this.location = location;
48        this.postmile = postmile;
49        this.direction = direction;
50        this.routeNumber = hwy;
51
52        this.MLTotVol = 0;
53        this.OppTotVol = 0;
54    }
55
56    /**
57     * Returns the station metadata in condensed form. This is just a quick
58     * script function to make a proper highway metadata configuration file, so
59     * that we can read the network faster.
60     *
61     * @return station metadata
62     */
63    public String getStationMeta()
64    {
65        StringBuilder build = new StringBuilder();
66        build.append(Integer.toString(this.ldsID));
67        build.append(" ");
68        build.append(Integer.toString(this.drop));
69        build.append(" ");
70        build.append(Integer.toString(this.routeNumber));
71        build.append(" ");
72        build.append(this.direction.getLetter());
73        build.append(" ");
74        build.append(Double.toString(this.postmile));
75        build.append(" ");
76        build.append(Integer.toString(loops.size()));
77        build.append(" ");
78        build.append(this.location);
79        build.append("\n");
80        for (LoopDetector loop : loops)
81        {
82            build.append(loop.getLoopMeta());
83        }
84        return build.toString();
85    }
86
87    /**
88     * Compare this Station to another by postmile. Note: This might be better
89     * as a Comparator since it checks only one field.
90     */
91    @Override
92    public int compareTo(Object otherStation)
93    {
94        // check for identity
95        if (this == otherStation)
96        {
97            return 0;
98        }
99        // check that Object is of type Station, if not throw exception
100        if (!(otherStation instanceof Station))
101        {
102            throw new ClassCastException("A Station object expected.");
103        }
104
105        // get difference of values
106        double otherStationPostmile = ((Station) otherStation).postmile;
107        double val = this.postmile - otherStationPostmile;
108
109        // set appropriate comparable return value
110        int retval = 0;
111        if (val > 0)
112        {
113            retval = 1;
114        }
115        else if (val < 0)
116        {
117            retval = -1;
118        }
119
120        return retval;
121    }
122
123    public void updateByDirection(DIRECTION direction, DOTCOLOR dotColor) {
124        if(direction.equals(this.direction))
125        {
126            updateML(dotColor);
127        }
128        else
129        {
130            updateOPP(dotColor);
131        }
132    }
133   
134    private void updateML(DOTCOLOR dotcolor)
135    {
136        outputUpdateMessage(dotcolor, "ML");
137       
138        for(LoopDetector loop : loops)
139        {
140            if(loop.loopLocation.startsWith("ML"))
141            {
142                // UPDATE LOOP WITH VALUES
143            }
144        }
145    }
146   
147    private void updateOPP(DOTCOLOR dotcolor)
148    {
149        outputUpdateMessage(dotcolor, "OPP");
150        for(LoopDetector loop : loops)
151        {
152            if(loop.loopLocation.startsWith("OP"))
153            {
154                // UPDATE LOOP WITH VALUES
155            }
156        }
157    }
158   
159    private void outputUpdateMessage(DOTCOLOR dotcolor, String OPP_ML)
160    {
161        System.out.printf("Updating route %-3.3s %-5.5s %-3.3s lanes\t %-12.12s "
162                + "at postmile %-6.6s to %-7.7s\n", 
163                Integer.toString(this.routeNumber), this.direction.name(), 
164                OPP_ML, this.location, Double.toString(this.postmile), 
165                dotcolor.name());
166    }
167   
168    private static enum XML_TAGS
169    {
170
171        STATION("Station"),
172        LDS_ID("LDS_ID"),
173        LINE_NUM("Line_Num"),
174        DROP("Drop"),
175        LOOPS("Loops"),
176        LOCATION("Location"),
177        POST_MILE("Post_Mile"),
178        DIRECTION("Direction"),
179        FREEWAY("Freeway"),
180        ML_TOT_VOL("ML_Tot_Vol"),
181        OPP_TOT_VOL("Opp_Tot_Vol");
182
183        String tag;
184
185        private XML_TAGS(String n)
186        {
187            tag = n;
188        }
189    }
190
191    public void toXML(Element currElem)
192    {
193        Document theDoc = currElem.getOwnerDocument();
194
195        Element stationElement = theDoc.createElement(XML_TAGS.STATION.tag);
196        currElem.appendChild(stationElement);
197
198        Element ldsIDElement = theDoc.createElement(XML_TAGS.LDS_ID.tag);
199        ldsIDElement.appendChild(theDoc.createTextNode(String.valueOf(this.ldsID)));
200        stationElement.appendChild(ldsIDElement);
201
202        Element lineNumElement = theDoc.createElement(XML_TAGS.LINE_NUM.tag);
203        lineNumElement.appendChild(theDoc.createTextNode(String.valueOf(this.lineNum)));
204        stationElement.appendChild(lineNumElement);
205
206        Element dropElement = theDoc.createElement(XML_TAGS.DROP.tag);
207        dropElement.appendChild(theDoc.createTextNode(String.valueOf(this.drop)));
208        stationElement.appendChild(dropElement);
209
210        Element locationElement = theDoc.createElement(XML_TAGS.LOCATION.tag);
211        locationElement.appendChild(theDoc.createTextNode(this.location));
212        stationElement.appendChild(locationElement);
213
214        Element postMileElement = theDoc.createElement(XML_TAGS.POST_MILE.tag);
215        postMileElement.appendChild(theDoc.createTextNode(String.valueOf(this.postmile)));
216        stationElement.appendChild(postMileElement);
217
218        Element directionElement = theDoc.createElement(XML_TAGS.DIRECTION.tag);
219        directionElement.appendChild(theDoc.createTextNode("" + this.direction.getLetter()));
220        stationElement.appendChild(directionElement);
221
222        Element freewayElement = theDoc.createElement(XML_TAGS.FREEWAY.tag);
223        freewayElement.appendChild(theDoc.createTextNode(String.valueOf(this.routeNumber)));
224        stationElement.appendChild(freewayElement);
225
226        Element mlElement = theDoc.createElement(XML_TAGS.ML_TOT_VOL.tag);
227        mlElement.appendChild(theDoc.createTextNode(String.valueOf(this.MLTotVol)));
228        stationElement.appendChild(mlElement);
229
230        Element oppElement = theDoc.createElement(XML_TAGS.OPP_TOT_VOL.tag);
231        oppElement.appendChild(theDoc.createTextNode(String.valueOf(this.OppTotVol)));
232        stationElement.appendChild(oppElement);
233
234        Element loopsElement = theDoc.createElement(XML_TAGS.LOOPS.tag);
235        stationElement.appendChild(loopsElement);
236
237        for (LoopDetector loop : loops)
238        {
239            loop.toXML(loopsElement);
240        }
241    }
242
243    /**
244     * Enum for freeway direction.
245     *
246     * @author John A. Torres
247     * @version 9/10/2017
248     */
249    public static enum DIRECTION
250    {
251
252        NORTH,
253        SOUTH,
254        EAST,
255        WEST;
256
257        // All the first letters of the values, in order.
258        private static String allLetters = "NSEW";
259
260        /**
261         * Return the first letter of this enum.
262         *
263         * @return String first letter of this enum.
264         */
265        public String getLetter()
266        {
267            return this.toString().substring(0, 1);
268        }
269
270        /**
271         * Returns a direction given its first character.
272         *
273         * @param letter the first character of a direction
274         * @return direction corresponding to letter
275         * @pre letter must be one of allLetters
276         */
277        public static DIRECTION toDirection(String letter)
278        {
279            return values()[allLetters.indexOf(letter.charAt(0))];
280        }
281
282    }
283}
Note: See TracBrowser for help on using the repository browser.