/************************************************************************************ * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University * All rights reserved. * Refer to LICENSE for terms and conditions of use. ***********************************************************************************/ package vnsim.vehicular.scenarios; import java.io.*; import java.util.StringTokenizer; import java.util.ArrayList; import java.util.logging.*; import javax.swing.JOptionPane; import vnsim.map.object.Map; import vnsim.vehicular.simulator.Location; import vnsim.vehicular.simulator.RouteSegment; import vnsim.gui.*; public class MainSMFCreator { /** Logger used by this class */ private static final transient Logger logger = Logger.getLogger("scenarios.MainSMFCreator"); public static String mapsDir = "/maps/map/"; public static String routeDir = "/maps/routefiles/"; public static void main(String args[]) throws Exception { //generates .smf file, using files from the "maps/map" directory //checks all "map" files, for each of them looks for a corresponding route file ScenarioMap scen=null; ArrayList driverTypes=new ArrayList (); driverTypes.add("very calm"); driverTypes.add("regular"); driverTypes.add("aggresive"); String[] allMaps = MapLoader.readSerializedMaps(); for(int i=0;i(); // scen.entries.add(new Location(0,0)); } if(scen.exits==null || scen.exits.size()==0) { scen.exits=new ArrayList(); // scen.exits.add(new Location(0,0)); } if(scen.allRoutes==null || scen.allRoutes.size()==0) { scen.allRoutes=new ArrayList (); /* Route r=new Route(); r.entry=new Location(0,0); r.exit=new Location(0,0); r.route=new ArrayList(); r.route.add(new RouteSegment((short)0,(short)0,(short)0)); scen.allRoutes.add(r);*/ } // create the dirs final String dir = System.getProperty("user.home")+File.separatorChar+"maps"+File.separatorChar+"smf"; (new File(dir)).mkdirs(); ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(dir+File.separatorChar+name+".smf")); oos.writeObject(scen); oos.close(); } catch (Exception ex) { logger.log(Level.SEVERE, "Could not create SMF file for "+allMaps[i], ex); } } } public static void loadScenarioData(String fileName, ScenarioMap scen) throws Exception { logger.info("Loading scenario:"+fileName+"..."); // text file format: // NEXITS EXIT1 ... EXITn // NENTRIES ENTRY1 ... ENTRYn // where : EXITi = ROAD POINT // and : ENTRYi = ROAD POINT NPOSSIBLEROUTES ROUTE1 ... ROUTEn // where : ROUTEi = ROADEXIT POINTEXIT NSEGS SEG1 ... SEGn // where: SEGi = ROAD POINTFROM POINTTO BufferedReader fin = null; // first try to load it from the disk... String localFile = System.getProperty("user.home")+fileName.replace('/', File.separatorChar).replace('\\', File.separatorChar); File f = new File(localFile); if (f.exists() && f.canRead() && f.isFile()) { fin = new BufferedReader(new InputStreamReader(new FileInputStream(f))); } else { // then read from loaded resources fin = new BufferedReader(new InputStreamReader(Utils.getInstance().openStream(fileName))); } String wholeFile="", line; while((line=fin.readLine())!=null) { wholeFile=wholeFile+"\n"+line; } fin.close(); String delim="\n \t\r"; for(char c='a';c<'z';c++) delim=delim+c; for(char c='A';c<'Z';c++) delim=delim+c; StringTokenizer st=new StringTokenizer(wholeFile,delim); scen.exits=new ArrayList (); int nexits=new Integer(st.nextToken()); logger.info("NEXITS="+nexits); for(int i=0;i (); scen.allRoutes=new ArrayList (); int nentries=new Integer(st.nextToken()); logger.info("NENTRIES:"+nentries); for(int i=0;i(); int nsegs=new Integer(st.nextToken()); logger.info("ROUTE SEGMENTS:"+nsegs); for(int k=0;k driverTypes=new ArrayList (); driverTypes.add("very calm"); driverTypes.add("regular"); driverTypes.add("aggresive"); try { // create the dirs final String dir = System.getProperty("user.home")+File.separatorChar+"maps"+File.separatorChar+"smf"; (new File(dir)).mkdirs(); ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(dir+File.separatorChar+"DowntownRouting.smf")); Route r1; scen = new ScenarioMap(); scen.name = "DowntownRouting"; scen.imageFileName = "/images/scen4.jpg"; scen.driverTypes = driverTypes; scen.mapFileName = mapsDir+"DowntownRouting.map"; scen.entries = new ArrayList(); scen.exits = new ArrayList(); scen.allRoutes = new ArrayList(); //Road 2 scen.entries.add(new Location(2,240)); scen.exits.add(new Location(2,0)); r1 = new Route(); r1.entry = new Location(2,240); r1.exit = new Location(2,0); r1.route = new ArrayList (); r1.route.add(new RouteSegment((short)2,(short)240,(short)0)); scen.allRoutes.add(r1); //Road 3 scen.entries.add(new Location(3,240)); scen.exits.add(new Location(3,0)); r1 = new Route(); r1.entry = new Location(3,240); r1.exit = new Location(3,0); r1.route = new ArrayList (); r1.route.add(new RouteSegment((short)3,(short)240,(short)0)); scen.allRoutes.add(r1); //Road 4 scen.entries.add(new Location(4,220)); scen.exits.add(new Location(4,0)); r1 = new Route(); r1.entry = new Location(4,220); r1.exit = new Location(4,0); r1.route = new ArrayList (); r1.route.add(new RouteSegment((short)4,(short)220,(short)0)); scen.allRoutes.add(r1); // Road 5 scen.entries.add(new Location(5,0)); scen.exits.add(new Location(5,250)); r1 = new Route(); r1.entry = new Location(5,0); r1.exit = new Location(5,250); r1.route = new ArrayList (); r1.route.add(new RouteSegment((short)5,(short)0,(short)250)); scen.allRoutes.add(r1); // Road 10 scen.entries.add(new Location(10,0)); scen.exits.add(new Location(10,220)); r1 = new Route(); r1.entry = new Location(10,0); r1.exit = new Location(10,220); r1.route = new ArrayList (); r1.route.add(new RouteSegment((short)10,(short)0,(short)220)); scen.allRoutes.add(r1); // Road 11 scen.entries.add(new Location(11,0)); scen.exits.add(new Location(11,208)); r1 = new Route(); r1.entry = new Location(11,0); r1.exit = new Location(11,208); r1.route = new ArrayList (); r1.route.add(new RouteSegment((short)11,(short)0,(short)208)); scen.allRoutes.add(r1); // Road 15 scen.entries.add(new Location(15,208)); scen.exits.add(new Location(15,0)); r1 = new Route(); r1.entry = new Location(15,208); r1.exit = new Location(15,0); r1.route = new ArrayList (); r1.route.add(new RouteSegment((short)15,(short)208,(short)8)); scen.allRoutes.add(r1); // Road 17 scen.entries.add(new Location(17,2)); scen.exits.add(new Location(17,200)); r1 = new Route(); r1.entry = new Location(17,2); r1.exit = new Location(17,200); r1.route = new ArrayList (); r1.route.add(new RouteSegment((short)17,(short)2,(short)200)); scen.allRoutes.add(r1); // Big Route scen.entries.add(new Location(22,83)); scen.exits.add(new Location(14,226)); //Route 1 r1 = new Route(); r1.entry = new Location(22,83); r1.exit = new Location(14,226); r1.route = new ArrayList (); r1.route.add(new RouteSegment((short)22,(short)83,(short)52)); r1.route.add(new RouteSegment((short)18,(short)16,(short)192)); r1.route.add(new RouteSegment((short)14,(short)32,(short)226)); scen.allRoutes.add(r1); // Route 2 r1 = new Route(); r1.entry = new Location(22,83); r1.exit = new Location(14,226); r1.route = new ArrayList (); r1.route.add(new RouteSegment((short)22,(short)83,(short)0)); r1.route.add(new RouteSegment((short)19,(short)8,(short)0)); r1.route.add(new RouteSegment((short)7,(short)0,(short)64)); r1.route.add(new RouteSegment((short)11,(short)16,(short)192)); r1.route.add(new RouteSegment((short)14,(short)160,(short)226)); scen.allRoutes.add(r1); // Route 3 r1 = new Route(); r1.entry = new Location(22,83); r1.exit = new Location(14,226); r1.route = new ArrayList (); r1.route.add(new RouteSegment((short)22,(short)83,(short)20)); r1.route.add(new RouteSegment((short)17,(short)0,(short)136)); r1.route.add(new RouteSegment((short)0,(short)64,(short)176)); r1.route.add(new RouteSegment((short)11,(short)144,(short)192)); r1.route.add(new RouteSegment((short)14,(short)160,(short)226)); scen.allRoutes.add(r1); // Route 4 r1 = new Route(); r1.entry = new Location(22,83); r1.exit = new Location(14,226); r1.route = new ArrayList (); r1.route.add(new RouteSegment((short)22,(short)83,(short)0)); r1.route.add(new RouteSegment((short)13,(short)16,(short)208)); r1.route.add(new RouteSegment((short)14,(short)164,(short)226)); scen.allRoutes.add(r1); ////////////Testing routes ////// //TODO complete scen.entries.add(new Location(7,121)); scen.exits.add(new Location(17,199)); r1 = new Route(); r1.entry = new Location(7,121); r1.exit = new Location(17,199); r1.route = new ArrayList (); r1.route.add(new RouteSegment((short)17,(short)121,(short)121)); scen.allRoutes.add(r1); scen.entries.add(new Location(16,33)); scen.exits.add(new Location(11,56)); r1 = new Route(); r1.entry = new Location(16,33); r1.exit = new Location(11,56); r1.route = new ArrayList (); r1.route.add(new RouteSegment((short)16,(short)33,(short)33)); scen.allRoutes.add(r1); oos.writeObject(scen); oos.close(); } catch (Exception ex) { logger.log(Level.SEVERE, "EXCEPTION Creating DowntownRoutin.smf", ex); } } }