ns2::MobilityWriter Class Reference


Detailed Description

class for writing ns2 mobility files
Author:
Thimor Bohn <bohn@itm.uni-luebeck.de>

Definition at line 21 of file MobilityWriter.java.


Static Public Member Functions

static void write (String trace, String mobility, List< String > wantedVehicle, List< String > vehicleNewId, List< Edge > edges, double begin, double end, double penetration, boolean hasPenetration)

Member Function Documentation

static void ns2::MobilityWriter::write ( String  trace,
String  mobility,
List< String >  wantedVehicle,
List< String >  vehicleNewId,
List< Edge edges,
double  begin,
double  end,
double  penetration,
boolean  hasPenetration 
) [inline, static]

working method

Parameters:
trace name of sumo netstate-dump file
mobility name of ns2 mobility file
wantedVehicle list of vehicles to be selected for ns2
vehicleNewId list of vehicle ids for ns2
edges list of edges used in sumo simulation
begin sumo time at which ns2 should start to simulate
end sumo time at which ns2 should stop to simulate

Definition at line 32 of file MobilityWriter.java.

References ns2::Edge::lanes, ns2::Lane::length, ns2::Lane::xfrom, ns2::Lane::xto, ns2::Lane::yfrom, and ns2::Lane::yto.

00041                                     {
00042         try {
00043             InputStream in = new FileInputStream(trace);
00044             PrintWriter out = new PrintWriter(mobility);
00045             XMLInputFactory factory = XMLInputFactory.newInstance();
00046             XMLStreamReader parser = factory.createXMLStreamReader(in);
00047             String edgeid = "";
00048             String laneid = "";
00049             float time = -1;
00050             Map<String, Double> initialX = new HashMap<String, Double>();
00051             Map<String, Double> initialY = new HashMap<String, Double>();
00052             // parse trace file
00053             for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
00054                 if (event == XMLStreamConstants.START_ELEMENT) {
00055                     // get current time
00056                     if (parser.getLocalName().equals("timestep")) {
00057                         for (int attr = 0; attr < parser.getAttributeCount(); attr++) {
00058                             String attrName = parser.getAttributeLocalName(attr);
00059                             String value = parser.getAttributeValue(attr);
00060                             if ("time".equals(attrName)) {
00061                                 time = Float.parseFloat(value);
00062                             }
00063                         }
00064                     }
00065                     // wanted time
00066                     if (time >= begin && time <= end) {
00067                         // edge element found
00068                         if (parser.getLocalName().equals("edge")) {
00069                             for (int attr = 0; attr < parser.getAttributeCount(); attr++) {
00070                                 String attrName = parser.getAttributeLocalName(attr);
00071                                 String value = parser.getAttributeValue(attr);
00072                                 if ("id".equals(attrName)) {
00073                                     edgeid = value;
00074                                 }
00075                             }
00076                         }
00077                         // lane element found
00078                         if (parser.getLocalName().equals("lane")) {
00079                             for (int attr = 0; attr < parser.getAttributeCount(); attr++) {
00080                                 String attrName = parser.getAttributeLocalName(attr);
00081                                 String value = parser.getAttributeValue(attr);
00082                                 if ("id".equals(attrName)) {
00083                                     laneid = value;
00084                                 }
00085                             }
00086                         }
00087                         // vehicle element found
00088                         if (parser.getLocalName().equals("vehicle")) {
00089                             String id = "";
00090                             float pos = 0;
00091                             float x = 0;
00092                             float y = 0;
00093                             float v = 0;
00094                             for (int attr = 0; attr < parser.getAttributeCount(); attr++) {
00095                                 String attrName = parser.getAttributeLocalName(attr);
00096                                 String value = parser.getAttributeValue(attr);
00097                                 if ("id".equals(attrName)) {
00098                                     id = value;
00099                                 }
00100                                 if ("pos".equals(attrName)) {
00101                                     pos = Float.parseFloat(value);
00102                                 }
00103                                 if ("speed".equals(attrName)) {
00104                                     v = Float.parseFloat(value);
00105                                 }
00106                             }
00107 
00108                             // find corresponding edge object
00109                             Edge thisedge = null;
00110                             for (Edge edge : edges) {
00111                                 thisedge = edge;
00112                                 if (edge.id.equals(edgeid)) {
00113                                     break;
00114                                 }
00115                             }
00116 
00117                             // get lane of edge
00118                             Lane thislane = thisedge.lanes.get(laneid);
00119                             
00120                             // calculate positons of vehicle
00121                             x = thislane.xfrom + pos * (thislane.xto - thislane.xfrom) / thislane.length;
00122                             y = thislane.yfrom + pos * (thislane.yto - thislane.yfrom) / thislane.length;
00123                             
00124                             // add to inititial positions if not already inside
00125                             if (!initialX.containsKey(id)) {
00126                                 initialX.put(id, new Double(x));
00127                                 initialY.put(id, new Double(y));
00128                             }
00129                             
00130                             // write to mobility file
00131                             String newId =  vehicleNewId.get(wantedVehicle.indexOf(id));
00132                             double minP =  Double.parseDouble(newId)/wantedVehicle.size();
00133                             if (hasPenetration) {
00134                                 if (penetration > minP) {
00135                                     out.println("$ns_ at " + (time-begin) + " \"$node_(" + newId + ") " + "setdest " + x + " " + y + " " + v + "\"");
00136                                 }
00137                             } else {
00138                                 out.print("if { $opt(penetration) > " + minP  + " } { ");
00139                                 out.print("$ns_ at " + (time-begin) + " \"$node_(" + newId + ") " + "setdest " + x + " " + y + " " + v + "\"");
00140                                 out.println(" }");
00141                             }
00142                         }
00143                     }
00144                 }
00145             }
00146             parser.close();
00147 
00148             // write initial positions of vehicles to mobility file
00149             for (String id: wantedVehicle) {
00150                 String newId =  vehicleNewId.get(wantedVehicle.indexOf(id));
00151                 double minP =  Double.parseDouble(newId)/wantedVehicle.size();
00152                 if (hasPenetration) {
00153                     if (penetration > minP) {
00154                         out.print("  $node_(" + newId + ") ");
00155                         out.print("  set X_ " + initialX.get(id));
00156                         out.println("\t# SUMO-ID: " + id);
00157                         out.print("  $node_(" + newId + ") ");
00158                         out.println("  set Y_ " + initialY.get(id) + " ");
00159                         out.print("  $node_(" + newId + ") ");
00160                         out.println("  set Z_ 0.0 ");
00161                     }
00162                 } else {
00163                     out.println("if { $opt(penetration) > " + minP  + " } { ");
00164                     out.print("  $node_(" + newId + ") ");
00165                     out.println("  set X_ " + initialX.get(id));
00166                     out.print("  $node_(" + newId + ") ");
00167                     out.println("  set Y_ " + initialY.get(id) + " ");
00168                     out.print("  $node_(" + newId + ") ");
00169                     out.println("  set Z_ 0.0 ");
00170                     out.println("}");                   
00171                 }
00172             }
00173 
00174             out.flush();
00175             out.close();
00176         } catch (XMLStreamException ex) {
00177             System.err.println(ex);
00178         } catch (IOException ex) {
00179             System.err.println(ex);
00180         }
00181     }


The documentation for this class was generated from the following file:

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