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

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

Revision 101, 9.9 KB checked in by jtorres, 9 years ago (diff)

ATMSDriver: added local configuration for ATMSDriver (for when the FEPSimulator is being run locally). This included adding a new config file: turnk/config/atms_driver_config_local.properties

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