LCOV - code coverage report
Current view: top level - src/microsim/traffic_lights - MSRailSignal.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 75.0 % 8 6
Test Date: 2026-03-02 16:00:03 Functions: 50.0 % 2 1

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2002-2026 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    MSRailSignal.h
      15              : /// @author  Melanie Weber
      16              : /// @author  Andreas Kendziorra
      17              : /// @author  Jakob Erdmann
      18              : /// @date    Jan 2015
      19              : ///
      20              : // A rail signal logic
      21              : /****************************************************************************/
      22              : #pragma once
      23              : #include <config.h>
      24              : 
      25              : #include <vector>
      26              : #include <microsim/MSRoute.h>
      27              : #include <microsim/traffic_lights/MSTrafficLightLogic.h>
      28              : #include <microsim/traffic_lights/MSTLLogicControl.h>
      29              : 
      30              : 
      31              : // ===========================================================================
      32              : // class declarations
      33              : // ===========================================================================
      34              : class MSLink;
      35              : class MSPhaseDefinition;
      36              : class MSRailSignalConstraint;
      37              : class MSDriveWay;
      38              : 
      39              : 
      40              : // ===========================================================================
      41              : // class definitions
      42              : // ===========================================================================
      43              : /**
      44              :  * @class MSRailSignal
      45              :  * @brief A signal for rails
      46              :  */
      47              : class MSRailSignal : public MSTrafficLightLogic {
      48              : public:
      49              :     /** @brief Constructor
      50              :      * @param[in] tlcontrol The tls control responsible for this tls
      51              :      * @param[in] id This tls' id
      52              :      * @param[in] programID This tls' sub-id (program id)
      53              :      * @param[in] parameters This tls' parameters
      54              :      * @param[in] delay The time to wait before the first switch
      55              :      */
      56              :     MSRailSignal(MSTLLogicControl& tlcontrol,
      57              :                  const std::string& id, const std::string& programID, SUMOTime delay,
      58              :                  const Parameterised::Map& parameters);
      59              : 
      60              : 
      61              :     /** @brief Initialises the rail signal with information about adjacent rail signals
      62              :      * @param[in] nb The detector builder
      63              :      * @exception ProcessError If something fails on initialisation
      64              :      */
      65              :     void init(NLDetectorBuilder& nb) override;
      66              : 
      67              : 
      68              :     /// @brief Destructor
      69              :     ~MSRailSignal();
      70              : 
      71              :     inline int getNumericalID() const {
      72      2814094 :         return myNumericalID;
      73              :     }
      74              : 
      75              :     /**@brief Sets a parameter and updates internal constants */
      76              :     void setParameter(const std::string& key, const std::string& value) override;
      77              : 
      78              :     /** @brief Adds a link on building
      79              :      * @param[in] link The controlled link
      80              :      * @param[in] lane The lane this link starts at
      81              :      * @param[in] pos The link's index (signal group) within this program
      82              :      */
      83              :     void addLink(MSLink* link, MSLane* lane, int pos) override;
      84              : 
      85              :     /// @name Handling of controlled links
      86              :     /// @{
      87              : 
      88              :     /** @brief Applies information about controlled links and lanes from the given logic
      89              :      * @param[in] logic The logic to use the information about controlled links/lanes from
      90              :      * @see MSTrafficLightLogic::adaptLinkInformationFrom
      91              :      */
      92              :     void adaptLinkInformationFrom(const MSTrafficLightLogic& logic) override;
      93              :     /// @}
      94              : 
      95              : 
      96              :     /// @name Switching and setting current rows
      97              :     /// @{
      98              : 
      99              : 
     100              :     /** @brief returns the state of the signal that actually required
     101              :      *
     102              :      * Returns the state of the rail signal that is actually required based on the
     103              :      *  occupation of the adjoining blocks.
     104              :      *
     105              :      * @return The state actually required for this signal.
     106              :      */
     107              : 
     108              :     /// @brief updates the current phase of the signal and return whether it should be kept active
     109              :     bool updateCurrentPhase();
     110              : 
     111              :     /** @brief Switches to the next phase
     112              :     * @return The time of the next switch (always the next step)
     113              :     * @see MSTrafficLightLogic::trySwitch
     114              :     */
     115              :     SUMOTime trySwitch() override;
     116              : 
     117              :     /// @}
     118              : 
     119              : 
     120              :     /// @name Static Information Retrieval
     121              :     /// @{
     122              : 
     123              :     /** @brief Returns the number of phases
     124              :      * @return The number of this tls program's phases (always zero)
     125              :      * @see MSTrafficLightLogic::getPhaseNumber
     126              :      */
     127              :     int getPhaseNumber() const override;
     128              : 
     129              :     /** @brief Returns the phases of this tls program
     130              :      * @return The phases of this tls program
     131              :      * @see MSTrafficLightLogic::getPhases
     132              :      */
     133              :     const Phases& getPhases() const override;
     134              : 
     135              :     /** @brief Returns the definition of the phase from the given position within the plan
     136              :     *
     137              :     * Returns the current phase as there does not exist a plan of the phases.
     138              :     *
     139              :     * @param[in] givenstep The index of the phase within the plan
     140              :     * @return The definition of the phase at the given position
     141              :     * @see MSTrafficLightLogic::getPhase
     142              :     */
     143              :     const MSPhaseDefinition& getPhase(int givenstep) const override;
     144              : 
     145              :     /// @brief whether the given link index ever turns 'G'
     146          567 :     bool getsMajorGreen(int /*linkIndex*/) const override {
     147          567 :         return true;
     148              :     }
     149              :     /// @}
     150              : 
     151              : 
     152              :     /// @name Dynamic Information Retrieval
     153              :     /// @{
     154              : 
     155              :     /** @brief Returns the current index within the program
     156              :      * @return The index of the current phase within the tls (here, always zero will be returned)
     157              :      * @see MSTrafficLightLogic::getCurrentPhaseIndex
     158              :      */
     159              :     int getCurrentPhaseIndex() const override;
     160              : 
     161              :     /** @brief Returns the definition of the current phase
     162              :     * @return The current phase
     163              :     */
     164              :     const MSPhaseDefinition& getCurrentPhaseDef() const override;
     165              :     /// @}
     166              : 
     167              :     void resetLastSwitch(SUMOTime t); 
     168              : 
     169              :     /// @name Conversion between time and phase
     170              :     /// @{
     171              : 
     172              :     /** @brief Returns the index of the logic at the given simulation step
     173              :      * @return The (estimated) index of the tls at the given simulation time step (here, always zero will be returned)
     174              :      * @see MSTrafficLightLogic::getPhaseIndexAtTime
     175              :      */
     176              :     SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const override;
     177              : 
     178              :     /** @brief Returns the position (start of a phase during a cycle) from of a given step
     179              :      * @param[in] index The index of the phase to return the begin of
     180              :      * @return The begin time of the phase (here, always zero will be returned)
     181              :      * @see MSTrafficLightLogic::getOffsetFromIndex
     182              :      */
     183              :     SUMOTime getOffsetFromIndex(int index) const override;
     184              : 
     185              :     /** @brief Returns the step (the phasenumber) of a given position of the cycle
     186              :     * @param[in] offset The offset (time) for which the according phase shall be returned
     187              :     * @return The according phase (here, always zero will be returned)
     188              :     * @see MSTrafficLightLogic::getIndexFromOffset
     189              :     */
     190              :     int getIndexFromOffset(SUMOTime offset) const override;
     191              :     /// @}
     192              : 
     193              : 
     194              :     /// @name Changing phases and phase durations
     195              :     /// @{
     196              : 
     197              :     /** @brief Changes the current phase and her duration
     198              :      * @param[in] tlcontrol The responsible traffic lights control
     199              :      * @param[in] simStep The current simulation step
     200              :      * @param[in] step Index of the phase to use
     201              :      * @param[in] stepDuration The left duration of the phase
     202              :      * @see MSTrafficLightLogic::changeStepAndDuration
     203              :      */
     204            0 :     void changeStepAndDuration(MSTLLogicControl& tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration) override {
     205              :         UNUSED_PARAMETER(tlcontrol);
     206              :         UNUSED_PARAMETER(simStep);
     207              :         UNUSED_PARAMETER(step);
     208              :         UNUSED_PARAMETER(stepDuration);
     209            0 :     }
     210              :     /// @}
     211              : 
     212              :     /// @brief return vehicles that block the intersection/rail signal for vehicles that wish to pass the given linkIndex
     213              :     VehicleVector getBlockingVehicles(int linkIndex) override;
     214              :     std::string getBlockingVehicleIDs() const;
     215              :     /// @brief return vehicles that block the given driveWay
     216              :     static VehicleVector getBlockingVehicles(const MSDriveWay* dw);
     217              : 
     218              :     /// @brief return vehicles that approach the intersection/rail signal and are in conflict with vehicles that wish to pass the given linkIndex
     219              :     VehicleVector getRivalVehicles(int linkIndex) override;
     220              :     std::string getRivalVehicleIDs() const;
     221              : 
     222              :     /// @brief return vehicles that approach the intersection/rail signal and have priority over vehicles that wish to pass the given linkIndex
     223              :     VehicleVector getPriorityVehicles(int linkIndex) override;
     224              :     std::string getPriorityVehicleIDs() const;
     225              : 
     226              :     /// @brief return information regarding active rail signal constraints for the closest approaching vehicle
     227              :     std::string getConstraintInfo(int linkIndex);
     228              :     std::string getConstraintInfo() const;
     229              : 
     230              :     std::vector<const MSDriveWay*> getBlockingDriveWays(int linkIndex) override;
     231              :     std::string getBlockingDriveWayIDs() const;
     232              : 
     233              :     /// @brief return driveways that block the given driveWay
     234              :     static std::vector<const MSDriveWay*> getBlockingDriveWays(const MSDriveWay* dw);
     235              : 
     236              :     std::string getRequestedDriveWay(int linkIndex) override;
     237              :     std::string getRequestedDriveWay() const;
     238              : 
     239              :     /// @brief write rail signal block output for all links and driveways
     240              :     void writeBlocks(OutputDevice& od, bool writeVehicles) const;
     241              : 
     242              :     /// @brief register constraint for signal switching
     243              :     void addConstraint(const std::string& tripId, MSRailSignalConstraint* constraint);
     244              : 
     245              :     /// @name TraCI access to constraints
     246              :     /// @{
     247              :     const std::map<std::string, std::vector<MSRailSignalConstraint*> >&  getConstraints() const {
     248              :         return myConstraints;
     249              :     }
     250              : 
     251              :     /// @brief remove constraint for signal switching
     252              :     bool removeConstraint(const std::string& tripId, MSRailSignalConstraint* constraint);
     253              :     void removeConstraints();
     254              :     /// @}
     255              : 
     256              :     /// update driveway for extended deadlock protection
     257              :     void updateDriveway(int numericalID);
     258              : 
     259              :     /* @brief return whether vehicle insertion must be delayed for an oncoming train
     260              :      * @param[in] link The rail signal link before which the vehicle is being inserted
     261              :      * @param[in] veh The vehicle being inserted
     262              :      * @param[in] brakeBeforeSignal Whether the vehicle may brake before the signal,
     263              :      *                              Returns true if the vehicle has to brake before the signal
     264              :      */
     265              :     static bool hasInsertionConstraint(MSLink* link, const MSVehicle* veh, std::string& info, bool& isInsertionOrder);
     266              : 
     267              :     static void initDriveWays(const SUMOVehicle* ego, bool update);
     268              : 
     269              :     typedef std::pair<const SUMOVehicle* const, const MSLink::ApproachingVehicleInformation> Approaching;
     270              :     typedef std::map<const MSLane*, int, ComparatorNumericalIdLess> LaneVisitedMap;
     271              : 
     272              :     /* @brief retrieve driveway with the given numerical id
     273              :      * @note: throws exception if the driveway does not exist at this rail signal */
     274              :     const MSDriveWay& retrieveDriveWay(int numericalID) const;
     275              : 
     276              :     const MSDriveWay& retrieveDriveWayForVeh(int tlIndex, const SUMOVehicle* veh);
     277              :     const MSDriveWay& retrieveDriveWayForRoute(int tlIndex, MSRouteIterator first, MSRouteIterator end);
     278              : 
     279              :     const std::vector<MSDriveWay*> retrieveDriveWays(int tlIndex) const;
     280              : 
     281              :     /// @brief whether the given vehicle is free to drive
     282              :     bool constraintsAllow(const SUMOVehicle* veh, bool storeWaitRelation = false) const;
     283              : 
     284              :     bool isMovingBlock() const {
     285        10088 :         return myMovingBlock;
     286              :     }
     287              : 
     288              :     static bool storeVehicles() {
     289       907462 :         return myStoreVehicles;
     290              :     }
     291              : 
     292              :     static VehicleVector& blockingVehicles() {
     293              :         return myBlockingVehicles;
     294              :     }
     295              : 
     296              :     static std::vector<const MSDriveWay*>& blockingDriveWays() {
     297              :         return myBlockingDriveWays;
     298              :     }
     299              : 
     300              :     static VehicleVector& rivalVehicles() {
     301              :         return myRivalVehicles;
     302              :     }
     303              : 
     304              :     static VehicleVector& priorityVehicles() {
     305              :         return myPriorityVehicles;
     306              :     }
     307              : 
     308              :     /// @brief print link descriptions
     309              :     static std::string describeLinks(std::vector<MSLink*> links);
     310              : 
     311              : protected:
     312              : 
     313              :     const int myNumericalID;
     314              : 
     315              :     std::string getNewDrivewayID();
     316              : 
     317              :     /* The driveways for each link
     318              :      */
     319         7527 :     struct LinkInfo {
     320              :         /// @brief constructor
     321              :         LinkInfo(MSLink* link);
     322              : 
     323              :         /// @brief Destructor
     324              :         ~LinkInfo();
     325              : 
     326              :         MSLink* myLink;
     327              : 
     328              :         /// @brief all driveways immediately following this link
     329              :         std::vector<MSDriveWay*> myDriveways;
     330              : 
     331              :         /// @brief return id for this railsignal-link
     332              :         std::string getID() const;
     333              : 
     334              :         /// @brief retrieve an existing Driveway or construct a new driveway based on the vehicles route
     335              :         MSDriveWay& getDriveWay(const SUMOVehicle*, int searchStart = -1);
     336              :         MSDriveWay& getDriveWay(MSRouteIterator firstIt, MSRouteIterator endIt, const std::string& info = "");
     337              : 
     338              :         /// @brief try rerouting vehicle if reservation failed
     339              :         void reroute(SUMOVehicle* veh, const MSEdgeVector& occupied);
     340              : 
     341              :         /// @brief init LinkInfo
     342              :         void reset();
     343              : 
     344              :         SUMOTime myLastRerouteTime;
     345              :         SUMOVehicle* myLastRerouteVehicle;
     346              : 
     347              :         /// @brief whether this link is used for railway operations
     348              :         bool myControlled;
     349              :     };
     350              : 
     351              :     /// @brief data storage for every link at this node (more than one when directly guarding a switch)
     352              :     std::vector<LinkInfo> myLinkInfos;
     353              : 
     354              : protected:
     355              : 
     356              :     /** @brief The list of phases this logic uses
     357              :     *
     358              :     *   This vector is always empty and only constructed because the superclass MSTrafficLightLogic requires it.
     359              :     */
     360              :     Phases myPhases;
     361              : 
     362              :     /// @brief The current phase
     363              :     MSPhaseDefinition myCurrentPhase;
     364              : 
     365              :     /// @brief MSTrafficLightLogic requires that the phase index changes whenever signals change their state
     366              :     int myPhaseIndex;
     367              : 
     368              :     /// @brief whether the signal is in moving block mode (only protects from oncoming and flanking trains)
     369              :     bool myMovingBlock;
     370              : 
     371              :     /// @brief map from tripId to constraint list
     372              :     std::map<std::string, std::vector<MSRailSignalConstraint*> > myConstraints;
     373              : 
     374              :     /// @brief running number of driveways created for this signal
     375              :     int myDriveWayIndex;
     376              : 
     377              :     static int myRSIndex;
     378              : 
     379              : protected:
     380              :     /// @brief update vehicle lists for traci calls
     381              :     void storeTraCIVehicles(int linkIndex);
     382              :     static void storeTraCIVehicles(const MSDriveWay* dw);
     383              : 
     384              :     /// @brief reset temporary storage for injected conflict output
     385              :     static void resetStored();
     386              : 
     387              :     /// @name traci result storage
     388              :     //@{
     389              :     static bool myStoreVehicles;
     390              :     static VehicleVector myBlockingVehicles;
     391              :     static VehicleVector myRivalVehicles;
     392              :     static VehicleVector myPriorityVehicles;
     393              :     static std::string myConstraintInfo;
     394              :     static std::vector<const MSDriveWay*> myBlockingDriveWays;
     395              :     static std::string myRequestedDriveWay;
     396              :     //@}
     397              : 
     398              : 
     399              : };
        

Generated by: LCOV version 2.0-1