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

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

Revision 109, 8.8 KB checked in by jdalbey, 9 years ago (diff)

ATMSDriverClient.java Enhanced to call ConsoleDriver? methods to update highway colors. Removed redundant code in Station..java.

RevLine 
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    /** Determine which lane fields to update based on given direction
124     * and update all the loop detectors with the given color.
125     * @param direction desired highway direction
126     * @param dotColor desired dot color
127     */
128    public void updateByDirection(DIRECTION direction, DOTCOLOR dotColor) 
129    {
130        String laneDir = "OPP";
131        if(direction.equals(this.direction))
132        {
133            laneDir = "ML";
134        }
135        outputUpdateMessage(dotColor, laneDir);
136       
137        for(LoopDetector loop : loops)
138        {
139            if(loop.loopLocation.startsWith(laneDir))
140            {
141                // UPDATE LOOP WITH VALUES
142                // TODO: Perhaps enhance DOTCOLOR enum to include constants for
143                // vol and occ along with each color.  Then the updateLoop call
144                // below could be update(dotColor.volume, dotColor.occ, speed)
145                int volume = 0;
146                int occ = 0;
147                int speed = 0;
148                loop.updateLoop(volume, occ, speed);
149            }
150        }
151    }
152   
153    private void outputUpdateMessage(DOTCOLOR dotcolor, String OPP_ML)
154    {
155        System.out.printf("Updating route %-3.3s %-5.5s %-3.3s lanes\t %-12.12s "
156                + "at postmile %-6.6s to %-7.7s\n", 
157                Integer.toString(this.routeNumber), this.direction.name(), 
158                OPP_ML, this.location, Double.toString(this.postmile), 
159                dotcolor.name());
160    }
161   
162    private static enum XML_TAGS
163    {
164
165        STATION("Station"),
166        LDS_ID("LDS_ID"),
167        LINE_NUM("Line_Num"),
168        DROP("Drop"),
169        LOOPS("Loops"),
170        LOCATION("Location"),
171        POST_MILE("Post_Mile"),
172        DIRECTION("Direction"),
173        FREEWAY("Freeway"),
174        ML_TOT_VOL("ML_Tot_Vol"),
175        OPP_TOT_VOL("Opp_Tot_Vol");
176
177        String tag;
178
179        private XML_TAGS(String n)
180        {
181            tag = n;
182        }
183    }
184
185    public void toXML(Element currElem)
186    {
187        Document theDoc = currElem.getOwnerDocument();
188
189        Element stationElement = theDoc.createElement(XML_TAGS.STATION.tag);
190        currElem.appendChild(stationElement);
191
192        Element ldsIDElement = theDoc.createElement(XML_TAGS.LDS_ID.tag);
193        ldsIDElement.appendChild(theDoc.createTextNode(String.valueOf(this.ldsID)));
194        stationElement.appendChild(ldsIDElement);
195
196        Element lineNumElement = theDoc.createElement(XML_TAGS.LINE_NUM.tag);
197        lineNumElement.appendChild(theDoc.createTextNode(String.valueOf(this.lineNum)));
198        stationElement.appendChild(lineNumElement);
199
200        Element dropElement = theDoc.createElement(XML_TAGS.DROP.tag);
201        dropElement.appendChild(theDoc.createTextNode(String.valueOf(this.drop)));
202        stationElement.appendChild(dropElement);
203
204        Element locationElement = theDoc.createElement(XML_TAGS.LOCATION.tag);
205        locationElement.appendChild(theDoc.createTextNode(this.location));
206        stationElement.appendChild(locationElement);
207
208        Element postMileElement = theDoc.createElement(XML_TAGS.POST_MILE.tag);
209        postMileElement.appendChild(theDoc.createTextNode(String.valueOf(this.postmile)));
210        stationElement.appendChild(postMileElement);
211
212        Element directionElement = theDoc.createElement(XML_TAGS.DIRECTION.tag);
213        directionElement.appendChild(theDoc.createTextNode("" + this.direction.getLetter()));
214        stationElement.appendChild(directionElement);
215
216        Element freewayElement = theDoc.createElement(XML_TAGS.FREEWAY.tag);
217        freewayElement.appendChild(theDoc.createTextNode(String.valueOf(this.routeNumber)));
218        stationElement.appendChild(freewayElement);
219
220        Element mlElement = theDoc.createElement(XML_TAGS.ML_TOT_VOL.tag);
221        mlElement.appendChild(theDoc.createTextNode(String.valueOf(this.MLTotVol)));
222        stationElement.appendChild(mlElement);
223
224        Element oppElement = theDoc.createElement(XML_TAGS.OPP_TOT_VOL.tag);
225        oppElement.appendChild(theDoc.createTextNode(String.valueOf(this.OppTotVol)));
226        stationElement.appendChild(oppElement);
227
228        Element loopsElement = theDoc.createElement(XML_TAGS.LOOPS.tag);
229        stationElement.appendChild(loopsElement);
230
231        for (LoopDetector loop : loops)
232        {
233            loop.toXML(loopsElement);
234        }
235    }
236
237    /**
238     * Enum for freeway direction.
239     *
240     * @author John A. Torres
241     * @version 9/10/2017
242     */
243    public static enum DIRECTION
244    {
245
246        NORTH,
247        SOUTH,
248        EAST,
249        WEST;
250
251        // All the first letters of the values, in order.
252        private static String allLetters = "NSEW";
253
254        /**
255         * Return the first letter of this enum.
256         *
257         * @return String first letter of this enum.
258         */
259        public String getLetter()
260        {
261            return this.toString().substring(0, 1);
262        }
263
264        /**
265         * Returns a direction given its first character.
266         *
267         * @param letter the first character of a direction
268         * @return direction corresponding to letter
269         * @pre letter must be one of allLetters
270         */
271        public static DIRECTION toDirection(String letter)
272        {
273            return values()[allLetters.indexOf(letter.charAt(0))];
274        }
275
276    }
277}
Note: See TracBrowser for help on using the repository browser.