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

source: tmcsimulator/branches/ATMSDriver/src/atmsdriver/NetworkReader.java @ 75

Revision 75, 9.0 KB checked in by jtorres, 9 years ago (diff)

Renamed fep_client_cpp to ATMSCommunicator, and upgraded it to cpp from straight c. Converted/Renamed? fep_client_java to ATMSDriver NetBeans? project, and checked in initial code for ATMSDriver. It contains the skeleton network model and a NetworkReader? which loads static network data from network lookup files.

RevLine 
1package atmsdriver;
2
3import atmsdriver.network.model.FEPLine;
4import atmsdriver.network.model.LoopDetectorStation;
5import atmsdriver.network.model.LoopDetector;
6import atmsdriver.network.model.LoopDetectorStation.DIRECTION;
7import java.io.File;
8import java.io.FileNotFoundException;
9import java.util.ArrayList;
10import java.util.List;
11import java.util.Scanner;
12import java.util.logging.Level;
13import java.util.logging.Logger;
14
15/** The NetworkReader loads all static data for the traffic network through
16 *  network lookup files.
17 *
18 *  The public method getFEPLines() method can be used to get all the FEPLines
19 *  within a network. All other methods are private and are used to construct
20 *  the FEPLines.
21 *
22 *  The "LDSFile" contains LoopDetectorStation and FEPLine information.
23 *  ex.
24 *   < Insert file here >
25 *  The "loopFile" contains individual LoopDetector information.
26 *  ex.
27 *   < Insert file here >
28 *
29 * @author John A. Torres
30 * @version 09/10/2017
31 */
32public class NetworkReader {
33    // Two network config files
34    private final File LDSFile;
35    private final File loopFile;
36   
37    // The list of FEPLines
38    private List<FEPLine> lines;
39   
40    /**
41     * Constructor
42     * @param LDSFile contains FEPLine and Station static data
43     * @param loopFile contains individual LoopDetector static data
44     */
45    public NetworkReader(File LDSFile, File loopFile)
46    {
47        this.LDSFile = LDSFile;
48        this.loopFile = loopFile;
49        this.lines = new ArrayList<>();
50        constructFEPLines();
51    }
52   
53    /**
54     * Returns the list of FEPLines in the configured network
55    */
56    public List<FEPLine> getFEPLines()
57    {
58        return lines;
59    }
60   
61    // returns a list of the line numbers found in LDSFile
62    private ArrayList<Integer> getLineNums(Scanner sc)
63    {
64        ArrayList<Integer> lineNums = new ArrayList<>();
65       
66        System.out.println(sc.nextLine()); // skip first line
67       
68        while(sc.hasNext())
69        {
70            sc.nextInt(); // skip to line no
71            Integer lineNum = sc.nextInt(); // get the line number
72           
73            /* If the line is new, and has not been added, add it to lineNums */
74            if(!lineNums.contains(lineNum))
75            {
76                lineNums.add(lineNum);
77                System.out.println("Adding line: " + lineNum);
78            }
79       
80            sc.nextLine(); // skip to next line
81        }
82       
83        sc.close();
84       
85        return lineNums;
86    }
87   
88    // returns a list of station numbers for a given line
89    private ArrayList<Integer> getStationNums(Integer lineNum, Scanner sc)
90    {
91        ArrayList<Integer> stnNums = new ArrayList<>();
92       
93        sc.nextLine(); // skip first line
94       
95        while(sc.hasNext())
96        {
97            Integer stnNum = sc.nextInt();
98            Integer theLine = sc.nextInt();
99            if(theLine.equals(lineNum))
100            {
101                stnNums.add(stnNum);
102                System.out.println("Adding stn " + stnNum + " to line " + theLine);
103            }
104           
105            sc.nextLine();
106        }
107       
108        sc.close();
109       
110        return stnNums;
111    }
112   
113    // returns a list of loop detectors for a given station
114    private ArrayList<LoopDetector> getLoops(Integer stnNum, Scanner sc)
115    {
116        ArrayList<LoopDetector> loops = new ArrayList<>();
117       
118        sc.nextLine(); // skip first line
119       
120        while(sc.hasNext())
121        {
122            sc.next(); // skip FWY
123            sc.next(); // skip dir
124            sc.next(); // skip postmile
125            Integer ldsID = sc.nextInt(); // lds id
126            sc.next(); // skip vdsID
127            Integer loopID = sc.nextInt(); // loop id
128            sc.next(); // skip LOC
129            Integer laneNum = sc.nextInt(); // lane number
130            String loopLoc = sc.next();
131            if(stnNum.equals(ldsID))
132            {
133                LoopDetector loop = new LoopDetector(loopID, loopLoc, laneNum);
134                loops.add(loop);
135            }
136            sc.nextLine();
137        }
138       
139        sc.close();
140       
141        return loops;
142    }
143   
144    // method called to actually build the network from config files
145    private void constructFEPLines()
146    {
147        try {
148            System.out.println("Building network...");
149           
150            // Get FEPLine IDs
151            ArrayList<Integer> lineNums = getLineNums(new Scanner(LDSFile));
152           
153            // Create each FEPLine in the network
154            for(Integer lineNum : lineNums)
155            {
156                // Get Station IDs for the current FEPLine
157                ArrayList<Integer> stnNums = getStationNums(lineNum,
158                        new Scanner(LDSFile));
159               
160                // Create each Station for the current FEPLine
161                ArrayList<LoopDetectorStation> stns = new ArrayList<>();
162                for(Integer stnNum : stnNums)
163                {
164                    // Get Loops for the current Station
165                    ArrayList<LoopDetector> loops =
166                            getLoops(stnNum, new Scanner(loopFile));
167                   
168                    // Create the Station and add to list for curr FEPLine
169                    LoopDetectorStation stn =
170                            createStation(stnNum, new Scanner(LDSFile), loops);
171                    stns.add(stn);
172                }
173               
174                // Now that stations are created, create the actual FEPLine and
175                // and to FEPLines list
176                FEPLine line = createLine(lineNum, new Scanner(LDSFile), stns);
177                lines.add(line);
178            }
179        } catch (FileNotFoundException ex) {
180            Logger.getLogger(NetworkReader.class.getName()).log(Level.SEVERE, null, ex);
181        }
182    }
183   
184    // Creates  line
185    private FEPLine createLine(int lineNum, Scanner scLine, ArrayList<LoopDetectorStation> stns)
186    {
187        FEPLine line = null;
188       
189        scLine.nextLine();
190       
191        while(scLine.hasNext())
192        {
193            scLine.nextInt(); // skip ldsID
194            int currLineNum = scLine.nextInt(); // linenum
195            if(currLineNum == lineNum)
196            {
197                scLine.nextInt(); // skip drop
198                int sch = scLine.nextInt(); // schedule
199                int lineinfo = scLine.nextInt(); // lineinfo
200                int sysKey = scLine.nextInt(); // sysKey
201                int schSeq = scLine.nextInt(); // schSeq
202                int globSeq = scLine.nextInt(); // globSeq 
203                int count = scLine.nextInt(); // count
204               
205                line = new FEPLine(lineNum, stns, count, sch, lineinfo, sysKey, globSeq, schSeq);
206               
207                break;
208            }
209           
210            scLine.nextLine();
211        }
212       
213        scLine.close();
214       
215        return line;
216    }
217   
218    // Returns the loction given the whole line from the lookup file
219    private String getLocation(String line)
220    {
221        Scanner sc = new Scanner(line);
222        sc.nextInt(); // skip lds num
223        sc.nextInt(); // skip line num
224        int drop = sc.nextInt(); // drop num
225        sc.nextInt(); // skip schedule
226        sc.nextInt(); // skip lineinfo
227        sc.nextInt(); // skp systemkey
228        sc.nextInt(); // skip sch_seq
229        sc.nextInt(); // skip glob_seq
230        sc.nextInt(); // skip count
231        sc.nextInt(); // fwy
232        DIRECTION dir = DIRECTION.getEnum(sc.next()); // direction
233        sc.nextDouble();
234       
235        /** GRABS FROM CURRENT TO END OF LINE */
236        sc.useDelimiter("\\z"); 
237        String loc = sc.next();
238        sc.close();
239        return loc;
240    }
241   
242    // creates a Station
243    private LoopDetectorStation createStation(int stnNum, Scanner sc, ArrayList<LoopDetector> detectors)
244    {
245        LoopDetectorStation LDS = null;
246       
247        sc.nextLine();
248       
249        while(sc.hasNext())
250        {       
251            String strLine = sc.nextLine();
252            Scanner scLine = new Scanner(strLine);
253            int ldsID = scLine.nextInt(); // get curr lds id
254            if(ldsID == stnNum) // if we are on correct stn
255            {
256                scLine.nextInt(); // skip line num
257                int drop = scLine.nextInt(); // drop num
258                scLine.nextInt(); // skip schedule
259                scLine.nextInt(); // skip lineinfo
260                scLine.nextInt(); // skp systemkey
261                scLine.nextInt(); // skip sch_seq
262                scLine.nextInt(); // skip glob_seq
263                scLine.nextInt(); // skip count
264                int fwy = scLine.nextInt(); // fwy
265                DIRECTION dir = DIRECTION.getEnum(scLine.next()); // direction
266                double postmile = scLine.nextDouble();
267                String ldsName = getLocation(strLine); /************* DOESNT GRAB WHOLE???? */////
268                System.out.println("LDSNAME: " + ldsName);
269                LDS = new LoopDetectorStation(stnNum, ldsID, drop, ldsName, detectors, fwy, dir, postmile);
270               
271                break;
272            }
273            scLine.close();
274        }
275       
276        sc.close();
277       
278        return LDS;
279    }
280}
Note: See TracBrowser for help on using the repository browser.