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 <microsim/MSVehicle.h>
00035 #include <utils/options/OptionsCont.h>
00036 #include <utils/iodevices/OutputDevice.h>
00037 #include "MSDevice_Tripinfo.h"
00038 
00039 #ifdef CHECK_MEMORY_LEAKS
00040 #include <foreign/nvwa/debug_new.h>
00041 #endif // CHECK_MEMORY_LEAKS
00042 
00043 
00044 // ===========================================================================
00045 // method definitions
00046 // ===========================================================================
00047 // ---------------------------------------------------------------------------
00048 // static initialisation methods
00049 // ---------------------------------------------------------------------------
00050 void
00051 MSDevice_Tripinfo::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*> &into) {
00052     if (OptionsCont::getOptions().isSet("tripinfo-output")) {
00053         MSDevice_Tripinfo* device = new MSDevice_Tripinfo(v, "tripinfo_" + v.getID());
00054         into.push_back(device);
00055     }
00056 }
00057 
00058 
00059 // ---------------------------------------------------------------------------
00060 // MSDevice_Tripinfo-methods
00061 // ---------------------------------------------------------------------------
00062 MSDevice_Tripinfo::MSDevice_Tripinfo(SUMOVehicle& holder, const std::string& id)
00063     : MSDevice(holder, id), myDepartLane(""), myDepartPos(-1), myDepartSpeed(-1),
00064       myWaitingSteps(0), myArrivalTime(-1), myArrivalLane(""), myArrivalPos(-1), myArrivalSpeed(-1) {
00065 }
00066 
00067 
00068 MSDevice_Tripinfo::~MSDevice_Tripinfo() {
00069 }
00070 
00071 
00072 bool
00073 MSDevice_Tripinfo::notifyMove(SUMOVehicle& /*veh*/, SUMOReal /*oldPos*/,
00074                               SUMOReal /*newPos*/, SUMOReal newSpeed) {
00075     if (newSpeed <= 0.1) {
00076         myWaitingSteps++;
00077     }
00078     return true;
00079 }
00080 
00081 
00082 bool
00083 MSDevice_Tripinfo::notifyEnter(SUMOVehicle& veh, MSMoveReminder::Notification reason) {
00084     if (reason == MSMoveReminder::NOTIFICATION_DEPARTED) {
00085         if (!MSGlobals::gUseMesoSim) {
00086             myDepartLane = static_cast<MSVehicle&>(veh).getLane()->getID();
00087         }
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) {
00098     if (reason >= MSMoveReminder::NOTIFICATION_ARRIVED) {
00099         myArrivalTime = MSNet::getInstance()->getCurrentTimeStep();
00100         if (!MSGlobals::gUseMesoSim) {
00101             myArrivalLane = static_cast<MSVehicle&>(veh).getLane()->getID();
00102         }
00103         myArrivalPos = myHolder.getPositionOnLane();
00104         myArrivalSpeed = veh.getSpeed();
00105     }
00106     return true;
00107 }
00108 
00109 
00110 void
00111 MSDevice_Tripinfo::generateOutput() const {
00112     SUMOReal routeLength = myHolder.getRoute().getLength();
00113     // write
00114     OutputDevice& os = OutputDevice::getDeviceByOption("tripinfo-output");
00115     os.openTag("tripinfo") << " id=\"" << myHolder.getID() << "\" ";
00116     routeLength -= myDepartPos;
00117     os << "depart=\"" << time2string(myHolder.getDeparture()) << "\" "
00118        << "departLane=\"" << myDepartLane << "\" "
00119        << "departPos=\"" << myDepartPos << "\" "
00120        << "departSpeed=\"" << myDepartSpeed << "\" "
00121        << "departDelay=\"" << time2string(myHolder.getDeparture() - myHolder.getParameter().depart) << "\" ";
00122     if (myArrivalLane != "") {
00123         routeLength -= MSLane::dictionary(myArrivalLane)->getLength() - myArrivalPos;
00124     }
00125     os << "arrival=\"" << time2string(myArrivalTime) << "\" "
00126        << "arrivalLane=\"" << myArrivalLane << "\" "
00127        << "arrivalPos=\"" << myArrivalPos << "\" "
00128        << "arrivalSpeed=\"" << myArrivalSpeed << "\" "
00129        << "duration=\"" << time2string(myArrivalTime - myHolder.getDeparture()) << "\" "
00130        << "routeLength=\"" << routeLength << "\" "
00131        << "waitSteps=\"" << myWaitingSteps << "\" "
00132        << "rerouteNo=\"" << myHolder.getNumberReroutes();
00133     const std::vector<MSDevice*> &devices = myHolder.getDevices();
00134     std::ostringstream str;
00135     for (std::vector<MSDevice*>::const_iterator i = devices.begin(); i != devices.end(); ++i) {
00136         if (i != devices.begin()) {
00137             str << ' ';
00138         }
00139         str << (*i)->getID();
00140     }
00141     os << "\" devices=\"" << str.str()
00142        << "\" vType=\"" << myHolder.getVehicleType().getID()
00143        << "\" vaporized=\"" << (myHolder.getEdge() == *(myHolder.getRoute().end() - 1) ? "" : "0")
00144        << "\"";
00145     if (devices.size() > 1) {
00146         os << ">\n";
00147     }
00148 }
00149 
00150 
00151 
00152 /****************************************************************************/
00153 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines