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

Revision 87, 7.5 KB checked in by jtorres, 9 years ago (diff)

merged branches/trunk into regular tmcsim/trunk. Changed Network.java to Highways.java. Highways.java: added writeToFEP(), updateSequences(), loadHighways(), and writeHighwaysMeta(). writeToFEP() is a socket client that communicates with FEPSimulator socket server. updateSequences() updates global and schedule sequences per the existing UCI plugin code and may not be necessary. loadHighways() loads the ArrayList? of Highways used in highways class, in sorted order. Stations are now implementing comparable to sort by postmile. writeHighwaysMeta() is an attempt at writing the highways meta data in condensed form, because loading time in NetworkLoader? is super long. We will eventually get rid of NetworkLoader? and do it within the Highways class for good OOP practice. There are new properties in atms_driver_properties.properties in config to support all these changes. Highway.java: new class, represents a highway which is a sorted ArrayList? of stations. This is a good checkpoint, for persistent green dots and all is working within the triangle. There is much commented out code in Highways.java which does not quite work. This commented code is an attempt at writing the condensed form highway meta data to improve loading times as described above.

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