ConfigWriter.java

Go to the documentation of this file.
00001 package ns2;
00002 
00003 import java.io.FileNotFoundException;
00004 import java.io.PrintWriter;
00005 import java.util.List;
00006 import java.util.Map;
00007 
00014 public class ConfigWriter {
00026     public static void write(
00027             String config,
00028             String activity,
00029             String mobility,
00030             List<Edge> edges,
00031             List<String> wantedVehicle,
00032             Map<String, Double> vehicleFirstOcc, 
00033             Map<String, Double> vehicleLastOcc,
00034             double begin,
00035             double penetration,
00036             boolean hasPenetration) {
00037         float xmin = 0, xmax = 0, ymin = 0, ymax = 0;
00038         float end = 0;
00039 
00040         // extend of map
00041         boolean first = true;
00042         for (Edge edge: edges) {
00043             for (Lane lane: edge.lanes.values() ) {
00044                 if (first) {
00045                     first = false;
00046                     xmin = Math.min(lane.xfrom, lane.xto);
00047                     xmax = Math.max(lane.xfrom, lane.xto);
00048                     ymin = Math.min(lane.yfrom, lane.yto);
00049                     ymax = Math.max(lane.yfrom, lane.yto);
00050                 } else {
00051                     xmin = Math.min(Math.min(lane.xfrom, lane.xto), xmin);
00052                     xmax = Math.max(Math.max(lane.xfrom, lane.xto), xmax);
00053                     ymin = Math.min(Math.min(lane.yfrom, lane.yto), ymin);
00054                     ymax = Math.max(Math.max(lane.yfrom, lane.yto), ymax);
00055                 }
00056             }
00057         }
00058         xmin = (float) Math.floor(xmin);
00059         xmax = (float) Math.ceil(xmax);
00060         ymin = (float) Math.floor(ymin);
00061         ymax = (float) Math.ceil(ymax);
00062         
00063 
00064         // duration of simulation
00065         first = true;
00066         for (String id: wantedVehicle) {
00067             if (first) {
00068                 first = false;
00069                 end   = vehicleLastOcc.get(id).floatValue();
00070             } else {
00071                 end   = Math.max(vehicleLastOcc.get(id).floatValue(), end);
00072             }
00073         }
00074 
00075         // write!
00076         try {
00077             PrintWriter out = new PrintWriter(config);
00078             
00079             out.println("# set number of nodes");
00080             if (hasPenetration) {
00081                 out.println("set opt(nn) " + Math.floor(penetration*wantedVehicle.size()));
00082             } else {
00083                 int N = wantedVehicle.size();
00084                 for (int i = 0; i <= N-1; i++) {
00085                     out.print("if { $opt(penetration) > " + ((double) i/N)  + " } { ");
00086                     out.print("set opt(nn) " + (i+1));
00087                     out.println(" }");
00088                 }
00089             }
00090             out.println();
00091             out.println("# set activity file");
00092             out.println("set opt(af) $opt(config-path)");
00093             out.println("append opt(af) /" + activity);
00094             out.println();
00095             out.println("# set mobility file");
00096             out.println("set opt(mf) $opt(config-path)");
00097             out.println("append opt(mf) /" + mobility);
00098             out.println();
00099             out.println("# set start/stop time");
00100             out.println("set opt(start) 0.0");
00101             out.println("set opt(stop) " + (end-begin));
00102             out.println();
00103             out.println("# set floor size");
00104             out.println("set opt(x) " + (int) xmax);
00105             out.println("set opt(y) " + (int) ymax);
00106             out.println("set opt(min-x) " + (int) xmin);
00107             out.println("set opt(min-y) " + (int) ymin);
00108             
00109             out.flush();
00110             out.close();
00111         } catch (FileNotFoundException ex) {
00112             System.err.println(ex);
00113         }
00114     }
00115 }

Generated on Wed May 5 00:06:28 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6