SUMO - Simulation of Urban MObility

MSDevice_Tripinfo.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // A device which collects info on the vehicle trip
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright (C) 2001-2011 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 // included modules
00022 // ===========================================================================
00023 #ifdef _MSC_VER
00024 #include <windows_config.h>
00025 #else
00026 #include <config.h>
00027 #endif
00028 
00029 #include <microsim/MSNet.h>
00030 #include <microsim/MSLane.h>
00031 #include <utils/options/OptionsCont.h>
00032 #include <utils/iodevices/OutputDevice.h>
00033 #include "MSDevice_Tripinfo.h"
00034 
00035 #ifdef CHECK_MEMORY_LEAKS
00036 #include <foreign/nvwa/debug_new.h>
00037 #endif // CHECK_MEMORY_LEAKS
00038 
00039 
00040 // ===========================================================================
00041 // method definitions
00042 // ===========================================================================
00043 // ---------------------------------------------------------------------------
00044 // static initialisation methods
00045 // ---------------------------------------------------------------------------
00046 void
00047 MSDevice_Tripinfo::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*> &into) throw() {
00048     if (OptionsCont::getOptions().isSet("tripinfo-output")) {
00049         MSDevice_Tripinfo* device = new MSDevice_Tripinfo(v, "tripinfo_" + v.getID());
00050         into.push_back(device);
00051     }
00052 }
00053 
00054 
00055 // ---------------------------------------------------------------------------
00056 // MSDevice_Tripinfo-methods
00057 // ---------------------------------------------------------------------------
00058 MSDevice_Tripinfo::MSDevice_Tripinfo(SUMOVehicle& holder, const std::string& id) throw()
00059     : MSDevice(holder, id), myDepartLane(""), myDepartPos(-1), myDepartSpeed(-1),
00060       myWaitingSteps(0), myArrivalTime(-1), myArrivalLane(""), myArrivalPos(-1), myArrivalSpeed(-1) {
00061 }
00062 
00063 
00064 MSDevice_Tripinfo::~MSDevice_Tripinfo() throw() {
00065 }
00066 
00067 
00068 bool
00069 MSDevice_Tripinfo::notifyMove(SUMOVehicle& /*veh*/, SUMOReal /*oldPos*/,
00070                               SUMOReal /*newPos*/, SUMOReal newSpeed) throw() {
00071     if (newSpeed<=0.1) {
00072         myWaitingSteps++;
00073     }
00074     return true;
00075 }
00076 
00077 
00078 bool
00079 MSDevice_Tripinfo::notifyEnter(SUMOVehicle& veh, MSMoveReminder::Notification reason) throw() {
00080     if (reason == MSMoveReminder::NOTIFICATION_DEPARTED) {
00081 #ifdef HAVE_MESOSIM
00082         if (!MSGlobals::gUseMesoSim) {
00083 #endif
00084             myDepartLane = static_cast<MSVehicle&>(veh).getLane()->getID();
00085 #ifdef HAVE_MESOSIM
00086         }
00087 #endif
00088         myDepartPos = veh.getPositionOnLane();
00089         myDepartSpeed = veh.getSpeed();
00090     }
00091     return true;
00092 }
00093 
00094 
00095 bool
00096 MSDevice_Tripinfo::notifyLeave(SUMOVehicle& veh, SUMOReal /*lastPos*/,
00097                                MSMoveReminder::Notification reason) throw() {
00098     if (reason >= MSMoveReminder::NOTIFICATION_ARRIVED) {
00099         myArrivalTime = MSNet::getInstance()->getCurrentTimeStep();
00100 #ifdef HAVE_MESOSIM
00101         if (!MSGlobals::gUseMesoSim) {
00102 #endif
00103             myArrivalLane = static_cast<MSVehicle&>(veh).getLane()->getID();
00104 #ifdef HAVE_MESOSIM
00105         }
00106 #endif
00107         myArrivalPos = myHolder.getPositionOnLane();
00108         myArrivalSpeed = veh.getSpeed();
00109     }
00110     return true;
00111 }
00112 
00113 
00114 void
00115 MSDevice_Tripinfo::generateOutput() const throw(IOError) {
00116     SUMOReal routeLength = myHolder.getRoute().getLength();
00117     // write
00118     OutputDevice& os = OutputDevice::getDeviceByOption("tripinfo-output");
00119     os.openTag("tripinfo") << " id=\"" << myHolder.getID() << "\" ";
00120     routeLength -= myDepartPos;
00121     os << "depart=\"" << time2string(myHolder.getDeparture()) << "\" "
00122        << "departLane=\"" << myDepartLane << "\" "
00123        << "departPos=\"" << myDepartPos << "\" "
00124        << "departSpeed=\"" << myDepartSpeed << "\" "
00125        << "departDelay=\"" << time2string(myHolder.getDeparture() - myHolder.getParameter().depart) << "\" ";
00126     if (myArrivalLane != "") {
00127         routeLength -= MSLane::dictionary(myArrivalLane)->getLength() - myArrivalPos;
00128     }
00129     os << "arrival=\"" << time2string(myArrivalTime) << "\" "
00130        << "arrivalLane=\"" << myArrivalLane << "\" "
00131        << "arrivalPos=\"" << myArrivalPos << "\" "
00132        << "arrivalSpeed=\"" << myArrivalSpeed << "\" "
00133        << "duration=\"" << time2string(myArrivalTime - myHolder.getDeparture()) << "\" "
00134        << "routeLength=\"" << routeLength << "\" "
00135        << "waitSteps=\"" << myWaitingSteps << "\" "
00136        << "rerouteNo=\"" << myHolder.getNumberReroutes();
00137     const std::vector<MSDevice*> &devices = myHolder.getDevices();
00138     std::ostringstream str;
00139     for (std::vector<MSDevice*>::const_iterator i=devices.begin(); i!=devices.end(); ++i) {
00140         if (i!=devices.begin()) {
00141             str << ' ';
00142         }
00143         str << (*i)->getID();
00144     }
00145     os << "\" devices=\"" << str.str()
00146        << "\" vType=\"" << myHolder.getVehicleType().getID()
00147        << "\" vaporized=\"" << (myHolder.getEdge() == *(myHolder.getRoute().end() - 1) ? "" : "0")
00148        << "\"";
00149     if (devices.size() > 1) {
00150         os << ">\n";
00151     }
00152 }
00153 
00154 
00155 
00156 /****************************************************************************/
00157 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines