LCOV - code coverage report
Current view: top level - src/microsim/devices - MSDevice_Vehroutes.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 11 11
Test Date: 2024-11-22 15:46:21 Functions: 80.0 % 5 4

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2009-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    MSDevice_Vehroutes.h
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Michael Behrisch
      17              : /// @author  Jakob Erdmann
      18              : /// @date    Fri, 30.01.2009
      19              : ///
      20              : // A device which collects info on the vehicle trip
      21              : /****************************************************************************/
      22              : #pragma once
      23              : #include <config.h>
      24              : 
      25              : #include "MSVehicleDevice.h"
      26              : #include <microsim/MSNet.h>
      27              : #include <microsim/MSVehicle.h>
      28              : #include <utils/common/SUMOTime.h>
      29              : #include <utils/iodevices/OutputDevice_String.h>
      30              : 
      31              : 
      32              : // ===========================================================================
      33              : // class declarations
      34              : // ===========================================================================
      35              : class MSEdge;
      36              : class MSRoute;
      37              : 
      38              : 
      39              : // ===========================================================================
      40              : // class definitions
      41              : // ===========================================================================
      42              : /**
      43              :  * @class MSDevice_Vehroutes
      44              :  * @brief A device which collects info on the vehicle trip (mainly on departure and arrival)
      45              :  *
      46              :  * Each device collects departure time, lane and speed and the same for arrival.
      47              :  *
      48              :  * @see MSDevice
      49              :  */
      50              : class MSDevice_Vehroutes : public MSVehicleDevice {
      51              : public:
      52              :     /** @brief Static intialization
      53              :      */
      54              :     static void init();
      55              : 
      56              :     /** @brief Inserts MSDevice_FCD-options
      57              :      * @param[filled] oc The options container to add the options to
      58              :      */
      59              :     static void insertOptions(OptionsCont& oc);
      60              : 
      61              :     /** @brief Build devices for the given vehicle, if needed
      62              :      *
      63              :      * The options are read and evaluated whether a vehroutes-device shall be built
      64              :      *  for the given vehicle.
      65              :      *
      66              :      * The built device is stored in the given vector.
      67              :      *
      68              :      * @param[in] v The vehicle for which a device may be built
      69              :      * @param[filled] into The vector to store the built device in
      70              :      */
      71              :     static MSDevice_Vehroutes* buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into, int maxRoutes = std::numeric_limits<int>::max());
      72              : 
      73              : 
      74              :     /// @brief generate vehroute output for pending vehicles at sim end, either due to sorting or because they are still in the net
      75              :     static void writePendingOutput(const bool includeUnfinished);
      76              : 
      77              : 
      78              : public:
      79              :     /// @brief Destructor.
      80              :     ~MSDevice_Vehroutes();
      81              : 
      82              : 
      83              : 
      84              :     /// @name Methods called on vehicle movement / state change, overwriting MSDevice
      85              :     /// @{
      86              : 
      87              :     /** @brief Does nothing, returns true only if exit times should be collected
      88              :      *
      89              :      * @param[in] veh The entering vehicle.
      90              :      * @param[in] reason how the vehicle enters the lane
      91              :      * @return True, if exit times are to be collected.
      92              :      * @see MSMoveReminder::notifyEnter
      93              :      * @see MSMoveReminder::Notification
      94              :      */
      95              :     bool notifyEnter(SUMOTrafficObject& veh, MSMoveReminder::Notification reason, const MSLane* enteredLane = 0);
      96              : 
      97              : 
      98              :     /** @brief Saves exit times if needed
      99              :      *
     100              :      * The exit time is collected on all occasions except for lane change.
     101              :      *
     102              :      * @param[in] veh The leaving vehicle.
     103              :      * @param[in] lastPos Position on the lane when leaving.
     104              :      * @param[in] reason how the vehicle leaves the lane
     105              :      * @see MSMoveReminder::notifyLeave
     106              :      *
     107              :      * @return True, if exit times are to be collected.
     108              :      */
     109              :     bool notifyLeave(SUMOTrafficObject& veh, double lastPos, Notification reason, const MSLane* enteredLane = 0);
     110              :     /// @}
     111              : 
     112              :     /// @brief return the name for this type of device
     113         1048 :     const std::string deviceName() const {
     114         1048 :         return "vehroute";
     115              :     }
     116              : 
     117              :     void notifyStopEnded();
     118              : 
     119              :     /** @brief Called on writing vehroutes output
     120              :      *
     121              :      * @exception IOError not yet implemented
     122              :      */
     123              :     void generateOutput(OutputDevice* tripinfoOut) const;
     124              : 
     125              : 
     126              :     /** @brief Called on route retrieval
     127              :      *
     128              :      * @param[in] index The index of the route to retrieve
     129              :      * @return the route at the index
     130              :      */
     131              :     ConstMSRoutePtr getRoute(int index) const;
     132              : 
     133              : 
     134              :     /** @brief Saves the state of the device
     135              :     *
     136              :     * @param[in] out The OutputDevice to write the information into
     137              :     */
     138              :     void saveState(OutputDevice& out) const;
     139              : 
     140              :     /** @brief Loads the state of the device from the given description
     141              :     *
     142              :     * @param[in] attrs XML attributes describing the current state
     143              :     */
     144              :     void loadState(const SUMOSAXAttributes& attrs);
     145              : 
     146              :     /// @brief Information needed to sort vehicle / transportable output by departure time
     147         7703 :     struct SortedRouteInfo {
     148              :         /// @brief route output device
     149              :         OutputDevice* routeOut = nullptr;
     150              : 
     151              :         /// @brief Map needed to sort vehicles by departure time
     152              :         std::map<const SUMOTime, int> departureCounts;
     153              : 
     154              :         /// @brief pregenerated route output sorted by time
     155              :         std::map<const SUMOTime, std::map<const std::string, std::string> > routeXML;
     156              :     };
     157              : 
     158              :     static void registerTransportableDepart(SUMOTime depart);
     159              :     static void writeSortedOutput(SortedRouteInfo* routeInfo, SUMOTime depart, const std::string& id, const std::string& xmlOutput);
     160              : 
     161              : private:
     162              :     /** @brief Constructor
     163              :      *
     164              :      * @param[in] holder The vehicle that holds this device
     165              :      * @param[in] id The ID of the device
     166              :      */
     167              :     MSDevice_Vehroutes(SUMOVehicle& holder, const std::string& id, int maxRoutes);
     168              : 
     169              : 
     170              :     /** @brief Called on route output
     171              :      *
     172              :      * @param[in] os the device to write into
     173              :      * @param[in] index The index of the route to write (-1 writes the current route)
     174              :      */
     175              :     void writeXMLRoute(OutputDevice& os, int index = -1) const;
     176              : 
     177              : 
     178              :     /** @brief Called on writing vehroutes output
     179              :     *
     180              :     * @param[in] hasArrived whether the vehicle has valid arrival information
     181              :     * @exception IOError not yet implemented
     182              :     */
     183              :     void writeOutput(const bool hasArrived) const;
     184              : 
     185              : 
     186              :     /** @brief Called on route change
     187              :      */
     188              :     void addRoute(const std::string& info);
     189              : 
     190              : private:
     191              :     /// @brief A shortcut for the Option "vehroute-output.exit-times"
     192              :     static bool mySaveExits;
     193              : 
     194              :     /// @brief A shortcut for the Option "vehroute-output.last-route"
     195              :     static bool myLastRouteOnly;
     196              : 
     197              :     /// @brief A shortcut for the Option "vehroute-output.dua"
     198              :     static bool myDUAStyle;
     199              : 
     200              :     /// @brief A shortcut for the Option "vehroute-output.costs"
     201              :     static bool myWriteCosts;
     202              : 
     203              :     /// @brief A shortcut for the Option "vehroute-output.sorted"
     204              :     static bool mySorted;
     205              : 
     206              :     /// @brief A shortcut for the Option "vehroute-output.intended-depart"
     207              :     static bool myIntendedDepart;
     208              : 
     209              :     /// @brief A shortcut for the Option "vehroute-output.route-length"
     210              :     static bool myRouteLength;
     211              : 
     212              :     /// @brief A shortcut for the Option "vehroute-output.skip-ptlines"
     213              :     static bool mySkipPTLines;
     214              : 
     215              :     /// @brief A shortcut for the Option "vehroute-output.incomplete"
     216              :     static bool myIncludeIncomplete;
     217              : 
     218              :     /// @brief A shortcut for the Option "vehroute-output.stop-edges"
     219              :     static bool myWriteStopPriorEdges;
     220              : 
     221              :     /// @brief A shortcut for the Option "vehroute-output.internal"
     222              :     static bool myWriteInternal;
     223              : 
     224              :     /** @class StateListener
     225              :      * @brief A class that is notified about reroutings
     226              :      */
     227              :     class StateListener : public MSNet::VehicleStateListener {
     228              :     public:
     229              :         /// @brief Destructor
     230        43604 :         ~StateListener() {}
     231              : 
     232              :         /** @brief Called if a vehicle changes its state
     233              :          * @param[in] vehicle The vehicle which changed its state
     234              :          * @param[in] to The state the vehicle has changed to
     235              :          */
     236              :         void vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to, const std::string& info = "");
     237              : 
     238              :         /// @brief A map for internal notification
     239              :         std::map<const SUMOVehicle*, MSDevice_Vehroutes*, ComparatorNumericalIdLess> myDevices;
     240              : 
     241              :     };
     242              : 
     243              : 
     244              :     /// @brief A class that is notified about reroutings
     245              :     static StateListener myStateListener;
     246              : 
     247              :     /// @brief Information needed to sort vehicles by departure time
     248              :     static SortedRouteInfo myRouteInfos;
     249              : 
     250              : 
     251              :     /**
     252              :      * @class RouteReplaceInfo
     253              :      * @brief Information about a replaced route
     254              :      *
     255              :      * Generated optionally and stored in a vector within the device
     256              :      *  this structure contains information about a replaced route: the edge
     257              :      *  the route was replaced at by a new one, the time this was done, and
     258              :      *  the previous route.
     259              :      */
     260              :     class RouteReplaceInfo {
     261              :     public:
     262              :         /** @brief Constructor
     263              :          * @param[in] edge_ The edge the route was replaced at
     264              :          * @param[in] time_ The time the route was replaced
     265              :          * @param[in] route_ The prior route
     266              :          */
     267        68274 :         RouteReplaceInfo(const MSEdge* const edge_, const SUMOTime time_, ConstMSRoutePtr const route_,
     268        68274 :                          const std::string& info_, int lastRouteIndex_, int newRouteIndex_) :
     269        68274 :             edge(edge_), time(time_), route(route_), info(info_),
     270        68274 :             lastRouteIndex(lastRouteIndex_),
     271        68274 :             newRouteIndex(newRouteIndex_)
     272        68274 :         {}
     273              : 
     274              :         /// @brief Destructor
     275       169581 :         ~RouteReplaceInfo() { }
     276              : 
     277              :         /// @brief The edge the vehicle was on when the route was replaced
     278              :         const MSEdge* edge;
     279              : 
     280              :         /// @brief The time the route was replaced
     281              :         SUMOTime time;
     282              : 
     283              :         /// @brief The prior route
     284              :         ConstMSRoutePtr route;
     285              : 
     286              :         /// @brief Information regarding rerouting
     287              :         std::string info;
     288              : 
     289              :         /// @brief The last index in the replaced route
     290              :         // (vehicle may or may not have driven to the end of it)
     291              :         int lastRouteIndex;
     292              : 
     293              :         /// @brief The current index in the replacement route
     294              :         // (new route may or may not include prior driven route edges)
     295              :         int newRouteIndex;
     296              : 
     297              :     };
     298              : 
     299              :     /// @brief The currently used route
     300              :     ConstMSRoutePtr myCurrentRoute;
     301              : 
     302              :     /// @brief Prior routes
     303              :     std::vector<RouteReplaceInfo> myReplacedRoutes;
     304              : 
     305              :     /// @brief The times at which the vehicle exits an edge
     306              :     std::vector<SUMOTime> myExits;
     307              : 
     308              :     /// @brief The maximum number of routes to report
     309              :     const int myMaxRoutes;
     310              : 
     311              :     /// @brief The last edge the exit time was saved for
     312              :     const MSEdge* myLastSavedAt;
     313              : 
     314              :     /// @brief The route index of the last edge that the vehicle left
     315              :     int myLastRouteIndex;
     316              : 
     317              :     /// @brief The lane the vehicle departed at
     318              :     int myDepartLane;
     319              : 
     320              :     /// @brief The lane the vehicle departed at
     321              :     double myDepartPos;
     322              : 
     323              :     /// @brief The speed on departure
     324              :     double myDepartSpeed;
     325              : 
     326              :     /// @brief The lateral depart position
     327              :     double myDepartPosLat;
     328              : 
     329              :     /// @brief the edges that were passed before the current stop
     330              :     std::vector<const MSEdge*> myPriorEdges;
     331              : 
     332              :     OutputDevice_String myStopOut;
     333              : 
     334              : private:
     335              :     /// @brief Invalidated copy constructor.
     336              :     MSDevice_Vehroutes(const MSDevice_Vehroutes&);
     337              : 
     338              :     /// @brief Invalidated assignment operator.
     339              :     MSDevice_Vehroutes& operator=(const MSDevice_Vehroutes&);
     340              : 
     341              : 
     342              : };
        

Generated by: LCOV version 2.0-1