LCOV - code coverage report
Current view: top level - src/microsim/output - MSRouteProbe.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 60 65 92.3 %
Date: 2024-05-06 15:32:35 Functions: 8 9 88.9 %

          Line data    Source code
       1             : /****************************************************************************/
       2             : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3             : // Copyright (C) 2008-2024 German Aerospace Center (DLR) and others.
       4             : // This program and the accompanying materials are made available under the
       5             : // terms of the Eclipse Public License 2.0 which is available at
       6             : // https://www.eclipse.org/legal/epl-2.0/
       7             : // This Source Code may also be made available under the following Secondary
       8             : // Licenses when the conditions for such availability set forth in the Eclipse
       9             : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10             : // or later which is available at
      11             : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12             : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13             : /****************************************************************************/
      14             : /// @file    MSRouteProbe.cpp
      15             : /// @author  Michael Behrisch
      16             : /// @author  Daniel Krajzewicz
      17             : /// @author  Tino Morenz
      18             : /// @author  Jakob Erdmann
      19             : /// @date    Thu, 04.12.2008
      20             : ///
      21             : // Writes route distributions at a certain edge
      22             : /****************************************************************************/
      23             : #include <config.h>
      24             : 
      25             : #include <string>
      26             : #include <microsim/MSEdge.h>
      27             : #include <microsim/MSLane.h>
      28             : #include <microsim/MSGlobals.h>
      29             : #include <microsim/MSRoute.h>
      30             : #include <microsim/MSVehicle.h>
      31             : #include <utils/common/ToString.h>
      32             : #include <utils/iodevices/OutputDevice.h>
      33             : #include <mesosim/MELoop.h>
      34             : #include <mesosim/MESegment.h>
      35             : #include "MSRouteProbe.h"
      36             : 
      37             : // ===========================================================================
      38             : // static members
      39             : // ===========================================================================
      40             : 
      41             : // ===========================================================================
      42             : // method definitions
      43             : // ===========================================================================
      44         200 : MSRouteProbe::MSRouteProbe(const std::string& id, const MSEdge* edge, const std::string& distID, const std::string& lastID,
      45         200 :                            const std::string& vTypes) :
      46             :     MSDetectorFileOutput(id, vTypes), MSMoveReminder(id),
      47         200 :     myDistID(distID),
      48         200 :     myLastID(lastID),
      49         200 :     myLastRouteDistribution(nullptr),
      50         200 :     myCurrentRouteDistribution(nullptr),
      51         400 :     myEdge(edge) {
      52         200 :     if (MSGlobals::gUseMesoSim) {
      53          18 :         MESegment* seg = MSGlobals::gMesoNet->getSegmentForEdge(*edge);
      54          80 :         while (seg != nullptr) {
      55          62 :             seg->addDetector(this);
      56             :             seg = seg->getNextSegment();
      57             :         }
      58             :         return;
      59             :     }
      60         719 :     for (std::vector<MSLane*>::const_iterator it = edge->getLanes().begin(); it != edge->getLanes().end(); ++it) {
      61         537 :         (*it)->addMoveReminder(this);
      62             :     }
      63           0 : }
      64             : 
      65             : 
      66         400 : MSRouteProbe::~MSRouteProbe() {
      67         400 : }
      68             : 
      69             : void
      70           0 : MSRouteProbe::clearState(SUMOTime step) {
      71             :     UNUSED_PARAMETER(step);
      72           0 :     myCurrentRouteDistribution = nullptr;
      73           0 :     myLastRouteDistribution = nullptr;
      74           0 : }
      75             : 
      76             : void
      77       18112 : MSRouteProbe::initDistributions() {
      78       18112 :     if (myCurrentRouteDistribution == nullptr) {
      79         184 :         myCurrentRouteDistribution = MSRoute::distDictionary(myDistID);
      80         184 :         if (myCurrentRouteDistribution == 0) {
      81         184 :             myCurrentRouteDistribution = new RandomDistributor<ConstMSRoutePtr>();
      82         184 :             MSRoute::dictionary(myDistID, myCurrentRouteDistribution, false);
      83             :         }
      84         184 :         myLastRouteDistribution = MSRoute::distDictionary(myLastID);
      85             :     }
      86       18112 : }
      87             : 
      88             : bool
      89       18216 : MSRouteProbe::notifyEnter(SUMOTrafficObject& veh, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
      90       18216 :     if (!vehicleApplies(veh)) {
      91             :         return false;
      92             :     }
      93       18194 :     if (reason != MSMoveReminder::NOTIFICATION_SEGMENT && reason != MSMoveReminder::NOTIFICATION_LANE_CHANGE) {
      94       18112 :         SUMOVehicle* vehicle = dynamic_cast<SUMOVehicle*>(&veh);
      95       18112 :         if (vehicle != nullptr) {
      96       18112 :             initDistributions();
      97       36224 :             myCurrentRouteDistribution->add(vehicle->getRoutePtr(), 1.);
      98             :         }
      99             :     }
     100             :     return false;
     101             : }
     102             : 
     103             : 
     104             : void
     105        5064 : MSRouteProbe::writeXMLOutput(OutputDevice& dev,
     106             :                              SUMOTime startTime, SUMOTime stopTime) {
     107        5064 :     if (myCurrentRouteDistribution && myCurrentRouteDistribution->getOverallProb() > 0) {
     108        2554 :         dev.openTag("routeDistribution") << " id=\"" << getID() + "_" + time2string(startTime) << "\"";
     109        1277 :         const std::vector<ConstMSRoutePtr>& routes = myCurrentRouteDistribution->getVals();
     110             :         const std::vector<double>& probs = myCurrentRouteDistribution->getProbs();
     111        2632 :         for (int j = 0; j < (int)routes.size(); ++j) {
     112        1355 :             ConstMSRoutePtr r = routes[j];
     113        2710 :             dev.openTag("route") << " id=\"" << r->getID() + "_" + time2string(startTime) << "\" edges=\"";
     114       12087 :             for (MSRouteIterator i = r->begin(); i != r->end(); ++i) {
     115       10732 :                 if (i != r->begin()) {
     116        9377 :                     dev << " ";
     117             :                 }
     118       10732 :                 dev << (*i)->getID();
     119             :             }
     120        1355 :             dev << "\" probability=\"" << probs[j] << "\"";
     121        2710 :             dev.closeTag();
     122             :         }
     123        1277 :         dev.closeTag();
     124        1277 :         if (myLastRouteDistribution != 0) {
     125        1093 :             MSRoute::checkDist(myLastID);
     126             :         }
     127        1277 :         myLastRouteDistribution = myCurrentRouteDistribution;
     128        1277 :         myLastID = myDistID;
     129        2554 :         myDistID = getID() + "_" + toString(stopTime);
     130        1277 :         myCurrentRouteDistribution = new RandomDistributor<ConstMSRoutePtr>();
     131        1277 :         MSRoute::dictionary(myDistID, myCurrentRouteDistribution, false);
     132             :     }
     133        5064 : }
     134             : 
     135             : 
     136             : void
     137         200 : MSRouteProbe::writeXMLDetectorProlog(OutputDevice& dev) const {
     138         400 :     dev.writeXMLHeader("routes", "routes_file.xsd");
     139         200 : }
     140             : 
     141             : 
     142             : ConstMSRoutePtr
     143        3276 : MSRouteProbe::sampleRoute(bool last) const {
     144        3276 :     if (myLastRouteDistribution == 0 || !last) {
     145        1800 :         if (myCurrentRouteDistribution && myCurrentRouteDistribution->getOverallProb() > 0) {
     146        1038 :             return myCurrentRouteDistribution->get();
     147             :         }
     148             :         return nullptr;
     149             :     }
     150        1476 :     return myLastRouteDistribution->get();
     151             : }

Generated by: LCOV version 1.14