MobilityWriter.java

Go to the documentation of this file.
00001 package ns2;
00002 
00003 import java.io.FileInputStream;
00004 import java.io.IOException;
00005 import java.io.InputStream;
00006 import java.io.PrintWriter;
00007 import java.util.HashMap;
00008 import java.util.List;
00009 import java.util.Map;
00010 
00011 import javax.xml.stream.XMLInputFactory;
00012 import javax.xml.stream.XMLStreamConstants;
00013 import javax.xml.stream.XMLStreamException;
00014 import javax.xml.stream.XMLStreamReader;
00015 
00021 public class MobilityWriter {
00032     public static void write(
00033             String trace, 
00034             String mobility,
00035             List<String> wantedVehicle, 
00036             List<String> vehicleNewId,
00037             List<Edge> edges, 
00038             double begin, 
00039             double end,
00040             double penetration,
00041             boolean hasPenetration) {
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     }
00182 }

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