MSRouteProbe.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Writes route distributions at a certain edge
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 
00021 // ===========================================================================
00022 // included modules
00023 // ===========================================================================
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029 
00030 #include <string>
00031 #include <microsim/MSEdge.h>
00032 #include <microsim/MSLane.h>
00033 #include <microsim/MSGlobals.h>
00034 #include <microsim/MSRoute.h>
00035 #include <microsim/MSVehicle.h>
00036 #include <utils/common/ToString.h>
00037 #include <utils/iodevices/OutputDevice.h>
00038 #ifdef HAVE_MESOSIM
00039 #include <mesosim/MELoop.h>
00040 #include <mesosim/MESegment.h>
00041 #endif
00042 #include "MSRouteProbe.h"
00043 
00044 #ifdef CHECK_MEMORY_LEAKS
00045 #include <foreign/nvwa/debug_new.h>
00046 #endif // CHECK_MEMORY_LEAKS
00047 
00048 
00049 // ===========================================================================
00050 // method definitions
00051 // ===========================================================================
00052 MSRouteProbe::EntryReminder::EntryReminder(MSLane * const lane, MSRouteProbe& collector) throw()
00053         : MSMoveReminder(lane), myCollector(collector) {}
00054 
00055 
00056 bool
00057 MSRouteProbe::EntryReminder::notifyEnter(MSVehicle& veh, bool isEmit, bool isLaneChange) throw() {
00058     myCollector.addRoute(veh.getRoute());
00059     return false;
00060 }
00061 
00062 
00063 MSRouteProbe::MSRouteProbe(const std::string &id, const MSEdge *edge, SUMOTime begin) throw()
00064         : Named(id), myCurrentRouteDistribution(0) {
00065     const std::string distID = id + "_" + toString(begin);
00066     myCurrentRouteDistribution = MSRoute::distDictionary(distID);
00067     if (myCurrentRouteDistribution == 0) {
00068         myCurrentRouteDistribution = new RandomDistributor<const MSRoute*>();
00069         MSRoute::dictionary(distID, myCurrentRouteDistribution);
00070     }
00071 #ifdef HAVE_MESOSIM
00072     if (MSGlobals::gUseMesoSim) {
00073         MESegment *seg = MSGlobals::gMesoNet->getSegmentForEdge(*edge);
00074         while (seg!=0) {
00075             seg->setRouteProbe(this);
00076             seg = seg->getNextSegment();
00077         }
00078         return;
00079     }
00080 #endif
00081     std::vector<MSLane*>::const_iterator it = edge->getLanes().begin();
00082     myEntryReminder = new MSRouteProbe::EntryReminder(*it, *this);
00083     for (++it; it!=edge->getLanes().end(); ++it) {
00084         (*it)->addMoveReminder(myEntryReminder);
00085     }
00086 }
00087 
00088 
00089 MSRouteProbe::~MSRouteProbe() throw() {
00090 }
00091 
00092 
00093 void
00094 MSRouteProbe::writeXMLOutput(OutputDevice &dev,
00095                              SUMOTime startTime, SUMOTime stopTime) throw(IOError) {
00096     if (myCurrentRouteDistribution->getOverallProb() > 0) {
00097         const std::string indent("    ");
00098         dev << indent << "<routeDistribution id=\"" << getID() + "_" + time2string(startTime) << "\">\n";
00099         const std::vector<const MSRoute*> &routes = myCurrentRouteDistribution->getVals();
00100         const std::vector<SUMOReal> &probs = myCurrentRouteDistribution->getProbs();
00101         for (unsigned int j=0; j<routes.size(); ++j) {
00102             const MSRoute *r = routes[j];
00103             dev << indent << indent << "<route id=\"" << r->getID() << "_" <<time2string(startTime)<< "\" edges=\"";
00104             MSRouteIterator i = r->begin();
00105             for (; i!=r->end(); ++i) {
00106                 const MSEdge *e = *i;
00107                 dev << e->getID() << " ";
00108             }
00109             dev << "\" probability=\"" << probs[j] << "\"/>\n";
00110         }
00111         dev << indent << "</routeDistribution>\n";
00112         myCurrentRouteDistribution = new RandomDistributor<const MSRoute*>();
00113         MSRoute::dictionary(getID() + "_" + toString(stopTime), myCurrentRouteDistribution);
00114     }
00115 }
00116 
00117 
00118 void
00119 MSRouteProbe::writeXMLDetectorProlog(OutputDevice &dev) const throw(IOError) {
00120     dev.writeXMLHeader("route-probes");
00121 }
00122 
00123 
00124 void
00125 MSRouteProbe::addRoute(const MSRoute &route) const {
00126     if (myCurrentRouteDistribution != 0) {
00127         const MSRoute* routep = &route;
00128         if (!route.inFurtherUse()) {
00129             const std::string id = getID() + "_" + route.getID();
00130             routep = new MSRoute(id, route.getEdges(), true, route.getColor(), route.getStops());
00131             MSRoute::dictionary(id, routep);
00132         }
00133         myCurrentRouteDistribution->add(1., routep);
00134     }
00135 }

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