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

Revision 95, 9.5 KB checked in by jdalbey, 9 years ago (diff)

Highways.java: revised to compile with revised Highway.java class.

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