source: tmcsimulator/trunk/src/atmsdriver/model/Highways.java @ 89

Revision 89, 9.4 KB checked in by jtorres, 9 years ago (diff)

trunk/test/atmsdriver/model/LoadHighwaysTest.java added test to ensure highways are loaded and sorted by postmile. Renamed NetworkTest?.java to HighwaysTest?.java

Line 
1package atmsdriver.model;
2
3import atmsdriver.NetworkLoader;
4import atmsdriver.model.Station.DIRECTION;
5import java.io.File;
6import java.io.FileNotFoundException;
7import java.io.FileWriter;
8import java.io.IOException;
9import java.io.PrintWriter;
10import java.io.StringWriter;
11import java.io.Writer;
12import java.net.Socket;
13import java.util.ArrayList;
14import java.util.Collections;
15import java.util.Enumeration;
16import java.util.Hashtable;
17import java.util.Map;
18import java.util.Scanner;
19import java.util.Set;
20import java.util.logging.Level;
21import java.util.logging.Logger;
22import javax.xml.parsers.DocumentBuilder;
23import javax.xml.parsers.DocumentBuilderFactory;
24import javax.xml.transform.OutputKeys;
25import javax.xml.transform.Transformer;
26import javax.xml.transform.TransformerFactory;
27import javax.xml.transform.dom.DOMSource;
28import javax.xml.transform.stream.StreamResult;
29import org.w3c.dom.Document;
30import org.w3c.dom.Element;
31
32/**
33 *
34 * @author John A. Torres
35 */
36public class Highways {
37
38    final private ArrayList<FEPLine> lines;
39    final private String FEPHostName;
40    final private int FEPPortNum;
41    final private ArrayList<Highway> highways;
42   
43    // NEED FINISH final private ArrayList<Highway> highways;
44    public Highways(String ldsFileName, String loopsFileName,
45            String highwayMetaFileName, String FEPHostName, int FEPPortNum) {
46        /*
47         lines = loadLines(highwayMetaFileName);
48         System.out.println("SIZE: " + toXML().toCharArray().length);
49         */
50
51        NetworkLoader ldr = new NetworkLoader(new File(ldsFileName), new File(loopsFileName));
52        this.lines = (ArrayList<FEPLine>) ldr.getFEPLines();
53        this.FEPHostName = FEPHostName;
54        this.FEPPortNum = FEPPortNum;
55        this.highways = loadHighways();
56        //writeHighwaysMeta("hard.txt");
57    }
58
59    public ArrayList<Highway> getHighways()
60    {
61        return this.highways;
62    }
63   
64    private ArrayList<Highway> loadHighways() {
65        System.out.println("Loading highways...");
66        // The list of highways to return
67        ArrayList<Highway> highways = new ArrayList<Highway>();
68        Hashtable<Hashtable<Integer, DIRECTION>, ArrayList<Station>>
69                highwayTable = new Hashtable<>();
70       
71        for (FEPLine line : lines)
72        {
73            ArrayList<Station> lineStations = (ArrayList<Station>) line.getStations();
74            for(Station station : lineStations)
75            {
76                Integer hwyNum = station.getHighwayNumber();
77                DIRECTION dir = station.getDirection();
78                Hashtable<Integer, DIRECTION> check = new Hashtable<>();
79                check.put(hwyNum, dir);
80                if(highwayTable.get(check) == null)
81                {
82                    ArrayList<Station> stnList = new ArrayList<>();
83                    stnList.add(station);
84                    Hashtable<Hashtable<Integer, DIRECTION>, ArrayList<Station>>
85                            newEntry = new Hashtable();
86                    newEntry.put(check, stnList);
87                    highwayTable.putAll(newEntry);
88                }
89                else
90                {
91                    highwayTable.get(check).add(station);
92                }
93            }
94        }
95       
96        Set<Hashtable<Integer, DIRECTION>> hwyKeys = highwayTable.keySet();
97        for(Hashtable<Integer, DIRECTION> hwyKey : hwyKeys)
98        {
99            Enumeration<Integer> hwyNumEnum = hwyKey.keys();
100            Integer hwyNum = hwyNumEnum.nextElement();
101            ArrayList<Station> hwyStations = highwayTable.get(hwyKey);
102            DIRECTION hwyDir = hwyKey.get(hwyNum);
103            Collections.sort(highwayTable.get(hwyKey));
104            System.out.println("Adding highway: " + hwyNum + " " + hwyDir.name);
105            highways.add(new Highway(hwyNum, 
106                    hwyDir, hwyStations));
107        }
108        return highways;
109    }
110    /*
111     private ArrayList<FEPLine> loadLines(String highwayMetaFileName) {
112     ArrayList<FEPLine> lines = new ArrayList<>();
113     try {
114     Scanner sc = new Scanner(new File(highwayMetaFileName));
115     String firstLine = sc.nextLine();
116
117     Scanner linesc = new Scanner(firstLine);
118     int numLines = linesc.nextInt();
119     linesc.close();
120
121     for (int i = 0; i < numLines; i++) {
122     System.out.println("CURR: " + i);
123     lines.add(loadLine(sc));
124     }
125     sc.close();
126
127     } catch (FileNotFoundException ex) {
128     Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex);
129     }
130     return lines;
131     }
132
133     private FEPLine loadLine(Scanner sc) {
134     String line = sc.nextLine();
135     System.out.println(line);
136     Scanner scline = new Scanner(line);
137
138     int lineNum = scline.nextInt();
139     int count = scline.nextInt();
140     int numStations = scline.nextInt();
141     ArrayList<Station> stations = new ArrayList<>();
142     for (int i = 0; i < numStations; i++) {
143     stations.add(loadStation(sc, lineNum));
144     }
145
146     return new FEPLine(lineNum, stations, count);
147     }
148
149     private Station loadStation(Scanner sc, int lineNum) {
150     String line = sc.nextLine();
151     System.out.println(line);
152     Scanner scline = new Scanner(line);
153     int ldsID = scline.nextInt();
154     int drop = scline.nextInt();
155     int fwy = scline.nextInt();
156     DIRECTION dir = DIRECTION.getEnum(scline.next());
157     double postmile = scline.nextDouble();
158     int numLoops = scline.nextInt();
159     String location = getStationLoc(line);
160     ArrayList<LoopDetector> loops = new ArrayList<>();
161     for (int i = 0; i < numLoops; i++) {
162     loops.add(loadLoop(sc));
163     }
164
165     return new Station(lineNum, ldsID, drop, location, loops, fwy, dir, postmile);
166     }
167
168     private LoopDetector loadLoop(Scanner sc) {
169     String line = sc.nextLine();
170     Scanner scline = new Scanner(line);
171
172     int loopID = scline.nextInt();
173     int laneNum = scline.nextInt();
174     String loopLoc = getLoopLoc(line); // NEED GET LOOPLOC
175     scline.close();
176     return new LoopDetector(loopID, loopLoc, laneNum);
177     }
178
179     private String getLoopLoc(String line) {
180     Scanner sc = new Scanner(line);
181     sc.nextInt();
182     sc.nextInt();
183
184     // GRABS FROM CURRENT TO END OF LINE
185         
186     sc.useDelimiter("\\z");
187     String loc = sc.next().trim();
188     sc.close();
189     return loc;
190     }
191
192     // Returns the loction given the whole line from the lookup file
193     private String getStationLoc(String line) {
194     Scanner scline = new Scanner(line);
195     scline.nextInt();
196     scline.nextInt();
197     scline.nextInt();
198     scline.next();
199     scline.nextDouble();
200     scline.nextInt();
201
202     // GRABS FROM CURRENT TO END OF LINE
203     scline.useDelimiter("\\z");
204     String loc = scline.next().trim();
205     scline.close();
206     return loc;
207     }
208     */
209
210    public void writeToFEP() {
211        try {
212            Socket sock = new Socket(FEPHostName, FEPPortNum);
213            PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
214            out.println(this.toXML());
215            sock.close();
216        } catch (IOException ex) {
217            Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex);
218        }
219    }
220
221    // CHECK: DO WE EVEN NEED TO DO THIS?
222    private void updateSequences() {
223        for (FEPLine line : lines) {
224            line.updateSequences();
225        }
226    }
227
228    /**
229     * Returns the network metadata in condensed form. This is just a quick
230     * script function to make a proper highway metadata configuration file, so
231     * that we can read the network faster.
232     *
233     * @return Network metadata
234     */
235    public void writeHighwaysMeta(String fileName) {
236
237        try {
238            FileWriter fw = new FileWriter(new File(fileName));
239            StringBuilder build = new StringBuilder();
240            build.append(lines.size());
241            build.append("\n");
242            System.out.println(lines.size());
243            fw.write(build.toString());
244            int count = 1;
245            for (FEPLine line : lines) {
246                System.out.println("Writing num: " + count);
247                count++;
248                fw.write(line.getLineMeta());
249
250            }
251        } catch (IOException ex) {
252            Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex);
253        }
254    }
255
256    public String toXML() {
257        String xml = null;
258        try {
259            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
260            DocumentBuilder builder = factory.newDocumentBuilder();
261            Document theDoc = builder.newDocument();
262
263            Element networkElement = theDoc.createElement(XML_TAGS.NETWORK.tag);
264            theDoc.appendChild(networkElement);
265
266            for (FEPLine line : lines) {
267                line.toXML(networkElement);
268            }
269
270            Transformer tf = TransformerFactory.newInstance().newTransformer();
271
272            tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
273            tf.setOutputProperty(OutputKeys.INDENT, "yes");
274            tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
275
276            Writer out = new StringWriter();
277            tf.transform(new DOMSource(theDoc), new StreamResult(out));
278            xml = out.toString();
279            out.close();
280        } catch (Exception ex) {
281            Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex);
282        }
283        return xml;
284
285    }
286
287    private static enum XML_TAGS {
288
289        NETWORK("Network");
290
291        String tag;
292
293        private XML_TAGS(String n) {
294            tag = n;
295        }
296    }
297}
Note: See TracBrowser for help on using the repository browser.