SUMO - Simulation of Urban MObility

MSDevice_Tripinfo.cpp

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