1 | /************************************************************************************ |
---|
2 | * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University |
---|
3 | * All rights reserved. |
---|
4 | * Refer to LICENSE for terms and conditions of use. |
---|
5 | ***********************************************************************************/ |
---|
6 | package vnsim.vehicular.scenarios; |
---|
7 | |
---|
8 | |
---|
9 | import java.io.*; |
---|
10 | import java.util.StringTokenizer; |
---|
11 | import java.util.ArrayList; |
---|
12 | import java.util.logging.*; |
---|
13 | |
---|
14 | import javax.swing.JOptionPane; |
---|
15 | |
---|
16 | import vnsim.map.object.Map; |
---|
17 | import vnsim.vehicular.simulator.Location; |
---|
18 | import vnsim.vehicular.simulator.RouteSegment; |
---|
19 | import vnsim.gui.*; |
---|
20 | |
---|
21 | |
---|
22 | |
---|
23 | public class MainSMFCreator { |
---|
24 | |
---|
25 | /** Logger used by this class */ |
---|
26 | private static final transient Logger logger = Logger.getLogger("scenarios.MainSMFCreator"); |
---|
27 | |
---|
28 | public static String mapsDir = "/maps/map/"; |
---|
29 | public static String routeDir = "/maps/routefiles/"; |
---|
30 | |
---|
31 | public static void main(String args[]) throws Exception { |
---|
32 | //generates .smf file, using files from the "maps/map" directory |
---|
33 | //checks all "map" files, for each of them looks for a corresponding route file |
---|
34 | |
---|
35 | ScenarioMap scen=null; |
---|
36 | ArrayList<String> driverTypes=new ArrayList<String> (); |
---|
37 | driverTypes.add("very calm"); |
---|
38 | driverTypes.add("regular"); |
---|
39 | driverTypes.add("aggresive"); |
---|
40 | |
---|
41 | String[] allMaps = MapLoader.readSerializedMaps(); |
---|
42 | |
---|
43 | for(int i=0;i<allMaps.length;i++) { |
---|
44 | try { |
---|
45 | String name = allMaps[i].substring(0, allMaps[i].length()-4); |
---|
46 | String routeFile = name+".txt"; |
---|
47 | scen = new ScenarioMap(); |
---|
48 | scen.name = name; |
---|
49 | scen.imageFileName = "/images/none.png"; |
---|
50 | scen.driverTypes = driverTypes; |
---|
51 | scen.mapFileName = mapsDir + name + ".map"; |
---|
52 | try { |
---|
53 | // loadScenarioData(routeDir+routeFile,scen); |
---|
54 | } catch (Exception ex) { |
---|
55 | ex.printStackTrace(); |
---|
56 | logger.severe("Got exception loading scenario data "+ex.getLocalizedMessage()); |
---|
57 | } |
---|
58 | if(scen.entries==null || scen.entries.size()==0) { |
---|
59 | scen.entries=new ArrayList<Location>(); |
---|
60 | // scen.entries.add(new Location(0,0)); |
---|
61 | } |
---|
62 | if(scen.exits==null || scen.exits.size()==0) { |
---|
63 | scen.exits=new ArrayList<Location>(); |
---|
64 | // scen.exits.add(new Location(0,0)); |
---|
65 | } |
---|
66 | if(scen.allRoutes==null || scen.allRoutes.size()==0) { |
---|
67 | scen.allRoutes=new ArrayList<Route> (); |
---|
68 | /* Route r=new Route(); |
---|
69 | r.entry=new Location(0,0); |
---|
70 | r.exit=new Location(0,0); |
---|
71 | r.route=new ArrayList<RouteSegment>(); |
---|
72 | r.route.add(new RouteSegment((short)0,(short)0,(short)0)); |
---|
73 | scen.allRoutes.add(r);*/ |
---|
74 | } |
---|
75 | // create the dirs |
---|
76 | final String dir = System.getProperty("user.home")+File.separatorChar+"maps"+File.separatorChar+"smf"; |
---|
77 | (new File(dir)).mkdirs(); |
---|
78 | ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(dir+File.separatorChar+name+".smf")); |
---|
79 | oos.writeObject(scen); |
---|
80 | oos.close(); |
---|
81 | } catch (Exception ex) { |
---|
82 | logger.log(Level.SEVERE, "Could not create SMF file for "+allMaps[i], ex); |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | |
---|
89 | public static void loadScenarioData(String fileName, ScenarioMap scen) throws Exception { |
---|
90 | logger.info("Loading scenario:"+fileName+"..."); |
---|
91 | |
---|
92 | // text file format: |
---|
93 | // NEXITS EXIT1 ... EXITn |
---|
94 | // NENTRIES ENTRY1 ... ENTRYn |
---|
95 | // where : EXITi = ROAD POINT |
---|
96 | // and : ENTRYi = ROAD POINT NPOSSIBLEROUTES ROUTE1 ... ROUTEn |
---|
97 | // where : ROUTEi = ROADEXIT POINTEXIT NSEGS SEG1 ... SEGn |
---|
98 | // where: SEGi = ROAD POINTFROM POINTTO |
---|
99 | |
---|
100 | BufferedReader fin = null; |
---|
101 | // first try to load it from the disk... |
---|
102 | String localFile = System.getProperty("user.home")+fileName.replace('/', File.separatorChar).replace('\\', File.separatorChar); |
---|
103 | File f = new File(localFile); |
---|
104 | if (f.exists() && f.canRead() && f.isFile()) { |
---|
105 | fin = new BufferedReader(new InputStreamReader(new FileInputStream(f))); |
---|
106 | } else { // then read from loaded resources |
---|
107 | fin = new BufferedReader(new InputStreamReader(Utils.getInstance().openStream(fileName))); |
---|
108 | } |
---|
109 | |
---|
110 | String wholeFile="", line; |
---|
111 | while((line=fin.readLine())!=null) { |
---|
112 | wholeFile=wholeFile+"\n"+line; |
---|
113 | } |
---|
114 | fin.close(); |
---|
115 | |
---|
116 | String delim="\n \t\r"; |
---|
117 | for(char c='a';c<'z';c++) |
---|
118 | delim=delim+c; |
---|
119 | for(char c='A';c<'Z';c++) |
---|
120 | delim=delim+c; |
---|
121 | StringTokenizer st=new StringTokenizer(wholeFile,delim); |
---|
122 | scen.exits=new ArrayList<Location> (); |
---|
123 | int nexits=new Integer(st.nextToken()); |
---|
124 | logger.info("NEXITS="+nexits); |
---|
125 | for(int i=0;i<nexits;i++) { |
---|
126 | //for each exit |
---|
127 | scen.exits.add(new Location(new Integer(st.nextToken()), new Integer(st.nextToken()))); |
---|
128 | logger.info("EXIT:"+scen.exits.get(scen.exits.size()-1)); |
---|
129 | }; |
---|
130 | scen.entries=new ArrayList<Location> (); |
---|
131 | scen.allRoutes=new ArrayList<Route> (); |
---|
132 | int nentries=new Integer(st.nextToken()); |
---|
133 | logger.info("NENTRIES:"+nentries); |
---|
134 | for(int i=0;i<nentries;i++) { |
---|
135 | //for each entry |
---|
136 | int rdEntry=new Integer(st.nextToken()); |
---|
137 | int ptEntry=new Integer(st.nextToken()); |
---|
138 | scen.entries.add(new Location(rdEntry, ptEntry)); |
---|
139 | logger.info("ENTRY:"+rdEntry+";"+ptEntry); |
---|
140 | int nroutes=new Integer(st.nextToken()); |
---|
141 | logger.info("ROUTES:"+nroutes); |
---|
142 | for(int j=0;j<nroutes;j++) { |
---|
143 | Route r=new Route(); |
---|
144 | r.entry=new Location(rdEntry, ptEntry); |
---|
145 | r.exit=new Location(new Integer(st.nextToken()), new Integer(st.nextToken())); |
---|
146 | r.route=new ArrayList<RouteSegment>(); |
---|
147 | int nsegs=new Integer(st.nextToken()); |
---|
148 | logger.info("ROUTE SEGMENTS:"+nsegs); |
---|
149 | for(int k=0;k<nsegs;k++) { |
---|
150 | r.route.add(new RouteSegment(new Short(st.nextToken()), new Short(st.nextToken()), new Short(st.nextToken()))); |
---|
151 | } |
---|
152 | scen.allRoutes.add(r); |
---|
153 | } |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | |
---|
159 | public static void mainRouteInfo(String args[]) { |
---|
160 | //generates .smf file, using files from the "maps/map" directory |
---|
161 | |
---|
162 | ScenarioMap scen=null; |
---|
163 | |
---|
164 | ArrayList<String> driverTypes=new ArrayList<String> (); |
---|
165 | driverTypes.add("very calm"); |
---|
166 | driverTypes.add("regular"); |
---|
167 | driverTypes.add("aggresive"); |
---|
168 | |
---|
169 | try { |
---|
170 | // create the dirs |
---|
171 | final String dir = System.getProperty("user.home")+File.separatorChar+"maps"+File.separatorChar+"smf"; |
---|
172 | (new File(dir)).mkdirs(); |
---|
173 | ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(dir+File.separatorChar+"DowntownRouting.smf")); |
---|
174 | Route r1; |
---|
175 | scen = new ScenarioMap(); |
---|
176 | scen.name = "DowntownRouting"; |
---|
177 | scen.imageFileName = "/images/scen4.jpg"; |
---|
178 | scen.driverTypes = driverTypes; |
---|
179 | scen.mapFileName = mapsDir+"DowntownRouting.map"; |
---|
180 | |
---|
181 | scen.entries = new ArrayList<Location>(); |
---|
182 | scen.exits = new ArrayList<Location>(); |
---|
183 | scen.allRoutes = new ArrayList<Route>(); |
---|
184 | |
---|
185 | //Road 2 |
---|
186 | scen.entries.add(new Location(2,240)); |
---|
187 | scen.exits.add(new Location(2,0)); |
---|
188 | |
---|
189 | r1 = new Route(); |
---|
190 | r1.entry = new Location(2,240); |
---|
191 | r1.exit = new Location(2,0); |
---|
192 | r1.route = new ArrayList<RouteSegment> (); |
---|
193 | r1.route.add(new RouteSegment((short)2,(short)240,(short)0)); |
---|
194 | scen.allRoutes.add(r1); |
---|
195 | |
---|
196 | //Road 3 |
---|
197 | scen.entries.add(new Location(3,240)); |
---|
198 | scen.exits.add(new Location(3,0)); |
---|
199 | |
---|
200 | r1 = new Route(); |
---|
201 | r1.entry = new Location(3,240); |
---|
202 | r1.exit = new Location(3,0); |
---|
203 | r1.route = new ArrayList<RouteSegment> (); |
---|
204 | r1.route.add(new RouteSegment((short)3,(short)240,(short)0)); |
---|
205 | scen.allRoutes.add(r1); |
---|
206 | |
---|
207 | //Road 4 |
---|
208 | scen.entries.add(new Location(4,220)); |
---|
209 | scen.exits.add(new Location(4,0)); |
---|
210 | |
---|
211 | r1 = new Route(); |
---|
212 | r1.entry = new Location(4,220); |
---|
213 | r1.exit = new Location(4,0); |
---|
214 | r1.route = new ArrayList<RouteSegment> (); |
---|
215 | r1.route.add(new RouteSegment((short)4,(short)220,(short)0)); |
---|
216 | scen.allRoutes.add(r1); |
---|
217 | |
---|
218 | // Road 5 |
---|
219 | scen.entries.add(new Location(5,0)); |
---|
220 | scen.exits.add(new Location(5,250)); |
---|
221 | |
---|
222 | r1 = new Route(); |
---|
223 | r1.entry = new Location(5,0); |
---|
224 | r1.exit = new Location(5,250); |
---|
225 | r1.route = new ArrayList<RouteSegment> (); |
---|
226 | r1.route.add(new RouteSegment((short)5,(short)0,(short)250)); |
---|
227 | scen.allRoutes.add(r1); |
---|
228 | |
---|
229 | // Road 10 |
---|
230 | scen.entries.add(new Location(10,0)); |
---|
231 | scen.exits.add(new Location(10,220)); |
---|
232 | |
---|
233 | r1 = new Route(); |
---|
234 | r1.entry = new Location(10,0); |
---|
235 | r1.exit = new Location(10,220); |
---|
236 | r1.route = new ArrayList<RouteSegment> (); |
---|
237 | r1.route.add(new RouteSegment((short)10,(short)0,(short)220)); |
---|
238 | scen.allRoutes.add(r1); |
---|
239 | |
---|
240 | // Road 11 |
---|
241 | scen.entries.add(new Location(11,0)); |
---|
242 | scen.exits.add(new Location(11,208)); |
---|
243 | |
---|
244 | r1 = new Route(); |
---|
245 | r1.entry = new Location(11,0); |
---|
246 | r1.exit = new Location(11,208); |
---|
247 | r1.route = new ArrayList<RouteSegment> (); |
---|
248 | r1.route.add(new RouteSegment((short)11,(short)0,(short)208)); |
---|
249 | scen.allRoutes.add(r1); |
---|
250 | // Road 15 |
---|
251 | scen.entries.add(new Location(15,208)); |
---|
252 | scen.exits.add(new Location(15,0)); |
---|
253 | |
---|
254 | r1 = new Route(); |
---|
255 | r1.entry = new Location(15,208); |
---|
256 | r1.exit = new Location(15,0); |
---|
257 | r1.route = new ArrayList<RouteSegment> (); |
---|
258 | r1.route.add(new RouteSegment((short)15,(short)208,(short)8)); |
---|
259 | scen.allRoutes.add(r1); |
---|
260 | |
---|
261 | // Road 17 |
---|
262 | scen.entries.add(new Location(17,2)); |
---|
263 | scen.exits.add(new Location(17,200)); |
---|
264 | |
---|
265 | r1 = new Route(); |
---|
266 | r1.entry = new Location(17,2); |
---|
267 | r1.exit = new Location(17,200); |
---|
268 | r1.route = new ArrayList<RouteSegment> (); |
---|
269 | r1.route.add(new RouteSegment((short)17,(short)2,(short)200)); |
---|
270 | scen.allRoutes.add(r1); |
---|
271 | |
---|
272 | // Big Route |
---|
273 | scen.entries.add(new Location(22,83)); |
---|
274 | scen.exits.add(new Location(14,226)); |
---|
275 | //Route 1 |
---|
276 | r1 = new Route(); |
---|
277 | r1.entry = new Location(22,83); |
---|
278 | r1.exit = new Location(14,226); |
---|
279 | r1.route = new ArrayList<RouteSegment> (); |
---|
280 | r1.route.add(new RouteSegment((short)22,(short)83,(short)52)); |
---|
281 | r1.route.add(new RouteSegment((short)18,(short)16,(short)192)); |
---|
282 | r1.route.add(new RouteSegment((short)14,(short)32,(short)226)); |
---|
283 | |
---|
284 | scen.allRoutes.add(r1); |
---|
285 | // Route 2 |
---|
286 | r1 = new Route(); |
---|
287 | r1.entry = new Location(22,83); |
---|
288 | r1.exit = new Location(14,226); |
---|
289 | r1.route = new ArrayList<RouteSegment> (); |
---|
290 | r1.route.add(new RouteSegment((short)22,(short)83,(short)0)); |
---|
291 | r1.route.add(new RouteSegment((short)19,(short)8,(short)0)); |
---|
292 | r1.route.add(new RouteSegment((short)7,(short)0,(short)64)); |
---|
293 | r1.route.add(new RouteSegment((short)11,(short)16,(short)192)); |
---|
294 | r1.route.add(new RouteSegment((short)14,(short)160,(short)226)); |
---|
295 | scen.allRoutes.add(r1); |
---|
296 | |
---|
297 | // Route 3 |
---|
298 | r1 = new Route(); |
---|
299 | r1.entry = new Location(22,83); |
---|
300 | r1.exit = new Location(14,226); |
---|
301 | r1.route = new ArrayList<RouteSegment> (); |
---|
302 | r1.route.add(new RouteSegment((short)22,(short)83,(short)20)); |
---|
303 | r1.route.add(new RouteSegment((short)17,(short)0,(short)136)); |
---|
304 | r1.route.add(new RouteSegment((short)0,(short)64,(short)176)); |
---|
305 | r1.route.add(new RouteSegment((short)11,(short)144,(short)192)); |
---|
306 | r1.route.add(new RouteSegment((short)14,(short)160,(short)226)); |
---|
307 | |
---|
308 | scen.allRoutes.add(r1); |
---|
309 | // Route 4 |
---|
310 | r1 = new Route(); |
---|
311 | r1.entry = new Location(22,83); |
---|
312 | r1.exit = new Location(14,226); |
---|
313 | r1.route = new ArrayList<RouteSegment> (); |
---|
314 | r1.route.add(new RouteSegment((short)22,(short)83,(short)0)); |
---|
315 | r1.route.add(new RouteSegment((short)13,(short)16,(short)208)); |
---|
316 | r1.route.add(new RouteSegment((short)14,(short)164,(short)226)); |
---|
317 | |
---|
318 | scen.allRoutes.add(r1); |
---|
319 | |
---|
320 | ////////////Testing routes ////// |
---|
321 | //TODO complete |
---|
322 | |
---|
323 | scen.entries.add(new Location(7,121)); |
---|
324 | scen.exits.add(new Location(17,199)); |
---|
325 | |
---|
326 | r1 = new Route(); |
---|
327 | r1.entry = new Location(7,121); |
---|
328 | r1.exit = new Location(17,199); |
---|
329 | r1.route = new ArrayList<RouteSegment> (); |
---|
330 | r1.route.add(new RouteSegment((short)17,(short)121,(short)121)); |
---|
331 | scen.allRoutes.add(r1); |
---|
332 | |
---|
333 | scen.entries.add(new Location(16,33)); |
---|
334 | scen.exits.add(new Location(11,56)); |
---|
335 | |
---|
336 | r1 = new Route(); |
---|
337 | r1.entry = new Location(16,33); |
---|
338 | r1.exit = new Location(11,56); |
---|
339 | r1.route = new ArrayList<RouteSegment> (); |
---|
340 | r1.route.add(new RouteSegment((short)16,(short)33,(short)33)); |
---|
341 | scen.allRoutes.add(r1); |
---|
342 | |
---|
343 | oos.writeObject(scen); |
---|
344 | oos.close(); |
---|
345 | } catch (Exception ex) { |
---|
346 | logger.log(Level.SEVERE, "EXCEPTION Creating DowntownRoutin.smf", ex); |
---|
347 | } |
---|
348 | } |
---|
349 | |
---|
350 | |
---|
351 | |
---|
352 | } |
---|
353 | |
---|