/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file NBLoadedTLDef.h14/// @author Daniel Krajzewicz15/// @author Sascha Krieg16/// @date Fri, 29.04.200517///18// A loaded (complete) traffic light logic19/****************************************************************************/20#pragma once21#include <config.h>2223#include <vector>24#include <string>25#include <set>26#include "NBTrafficLightDefinition.h"27#include "NBNode.h"28#include <utils/common/SUMOTime.h>293031// ===========================================================================32// class definitions33// ===========================================================================34/**35* @class NBLoadedTLDef36* @brief A loaded (complete) traffic light logic37*/38class NBLoadedTLDef : public NBTrafficLightDefinition {39public:40/** @class SignalGroup41* @brief A single signal group, may control several connections42*/43class SignalGroup : public Named {44public:45/** @brief Constructor46* @param[in] id The id of the signal group47*/48SignalGroup(const std::string& id);4950/// @brief Destructor51~SignalGroup();5253/** @brief Inserts a controlled connection54* @param[in] c The connection to be controlled by this signal group55*/56void addConnection(const NBConnection& c);5758/** @brief Sets the begin of a phase59* @param[in] time The time at which the phase starts60* @param[in] color The color of this signal starting at the given time61*/62void addPhaseBegin(SUMOTime time, TLColor color);6364/** @brief Sets the times for redyellow and yellow65* @param[in] tRedYellowe The duration of the redyellow phase66* @param[in] tYellow The duration of the yellow phase67*/68void setYellowTimes(SUMOTime tRedYellowe, SUMOTime tYellow);6970/** @brief Returns the times at which the signal switches71* @param[in] cycleDuration The duration of the complete cycle72* @return The switch times of this signal73*/74std::vector<SUMOTime> getTimes(SUMOTime cycleDuration) const;7576/** @brief Sorts the phases */77void sortPhases();7879/** @brief Returns the number of links (connection) controlled by this signal80* @return The number of links controlled by this signal81*/82int getLinkNo() const;8384/** @brief Returns whether vehicles on controlled links may drive at the given time85* @param[in] time The regarded time86* @return Whether vehicles may drive at this time87*/88bool mayDrive(SUMOTime time) const;8990/** @brief Returns whether controlled links have yellow at the given time91* @param[in] time The regarded time92* @return Whether controlled links are yellow at this time93*/94bool hasYellow(SUMOTime time) const;9596/** @brief Returns whether this signal controls the given edge97* @param[in] from The incoming edge98* @return Whether this edge is controlled by this signal99*/100bool containsIncoming(NBEdge* from) const;101102/** @brief Replaces the given incoming edge by the others given103* @param[in] which The edge to replace104* @param[in] by The replacements105*/106void remapIncoming(NBEdge* which, const EdgeVector& by);107108/** @brief Returns whether this signal controls a connection where the given edge is the destination109* @param[in] from The outgoing edge110* @return Whether this edge's predecessing edge is controlled by this signal111*/112bool containsOutgoing(NBEdge* to) const;113114/** @brief Replaces the given outgoing edge by the others given115* @param[in] which The edge to replace116* @param[in] by The replacements117*/118void remapOutgoing(NBEdge* which, const EdgeVector& by);119120/** @brief Returns the connection at the given index121* @param[in] pos The position within this signal122* @return The connection at the given index123*/124const NBConnection& getConnection(int pos) const;125126/** @brief Sets the yellow time127* @param[in] tyellow The yellow time to set in seconds128* @param[in] forced Whether resetting tyellow was forced by the user by setting "tls.yellow.patch-small"129*/130void patchTYellow(SUMOTime tyellow, bool forced);131132/** @brief Replaces a removed edge/lane133* @param[in] removed The edge to replace134* @param[in] removedLane The lane of this edge to replace135* @param[in] by The edge to insert instead136* @param[in] byLane This edge's lane to insert instead137*/138void remap(NBEdge* removed, int removedLane, NBEdge* by, int byLane);139140/** @class PhaseDef141* @brief Definition of a single, loaded phase142*/143class PhaseDef {144public:145/** @brief Constructor146* @param[in] time The begin time of this phase147* @param[in] color A signal's color from this time148*/149PhaseDef(SUMOTime time, TLColor color)150: myTime(time), myColor(color) { }151152/// @brief The begin time of this phase153SUMOTime myTime;154/// @brief A signal's color from this time155TLColor myColor;156};157158private:159/// @brief Connections controlled by this signal160NBConnectionVector myConnections;161/// @brief The phases of this signal162std::vector<PhaseDef> myPhases;163/// @brief The times of redyellow and yellow164SUMOTime myTRedYellow, myTYellow;165};166167168169/// @brief Definition of the container for signal groups170typedef std::map<std::string, SignalGroup*> SignalGroupCont;171172173/** @brief Constructor174* @param[in] id The id of the tls175* @param[in] junctions Junctions controlled by this tls176* @param[in] offset The offset of the plan177* @param[in] type The algorithm type for the computed traffic light178*/179NBLoadedTLDef(const NBEdgeCont& ec, const std::string& id,180const std::vector<NBNode*>& junctions, SUMOTime offset,181TrafficLightType type);182183184/** @brief Constructor185* @param[in] id The id of the tls186* @param[in] junction The junction controlled by this tls187* @param[in] offset The offset of the plan188* @param[in] type The algorithm type for the computed traffic light189*/190NBLoadedTLDef(const NBEdgeCont& ec, const std::string& id, NBNode* junction, SUMOTime offset,191TrafficLightType type);192193194/** @brief Constructor195* @param[in] id The id of the tls196* @param[in] offset The offset of the plan197* @param[in] type The algorithm type for the computed traffic light198*/199NBLoadedTLDef(const NBEdgeCont& ec, const std::string& id, SUMOTime offset,200TrafficLightType type);201202203/// @brief Destructor204~NBLoadedTLDef();205206207/** @brief Sets the duration of a cycle208* @param[in] cycleDur The duration of the cycle209*/210void setCycleDuration(SUMOTime cycleDur);211212213/** @brief Adds a signal group214* @param[in] id The id of the signal group215*/216void addSignalGroup(const std::string& id);217218219/** @brief Adds a connection to a signal group220* @param[in] groupid The id of the signal group to add the connection to221* @param[in] connection The connection to add222*/223bool addToSignalGroup(const std::string& groupid,224const NBConnection& connection);225226227/** @brief Adds a list of connections to a signal group228* @param[in] groupid The id of the signal group to add the connections to229* @param[in] connections The connections to add230*/231bool addToSignalGroup(const std::string& groupid,232const NBConnectionVector& connections);233234235/** @brief Sets the information about the begin of a phase236* @param[in] groupid The id of the signal group to add the phase to237* @param[in] time The time the phase starts at238* @param[in] color The color of the signal during this phase239*/240void addSignalGroupPhaseBegin(const std::string& groupid,241SUMOTime time, TLColor color);242243244/** @brief Sets the times the light is yellow or red/yellow245* @param[in] groupid The id of the signal group to add the phase to246* @param[in] tRedYellow The duration of redyellow247* @param[in] tYellow The duration of yellow248*/249void setSignalYellowTimes(const std::string& groupid,250SUMOTime tRedYellow, SUMOTime tYellow);251252253/// @name Public methods from NBTrafficLightDefinition-interface254/// @{255256/** @brief Informs edges about being controlled by a tls257* @param[in] ec The container of edges258* @see NBTrafficLightDefinition::setTLControllingInformation259*/260void setTLControllingInformation() const;261262263/** @brief Replaces occurrences of the removed edge in incoming/outgoing edges of all definitions264* @param[in] removed The removed edge265* @param[in] incoming The edges to use instead if an incoming edge was removed266* @param[in] outgoing The edges to use instead if an outgoing edge was removed267* @see NBTrafficLightDefinition::remapRemoved268*/269void remapRemoved(NBEdge* removed,270const EdgeVector& incoming, const EdgeVector& outgoing);271272/* initialize myNeedsContRelation and set myNeedsContRelationReady to true */273void initNeedsContRelation() const;274275///@brief Returns the maximum index controlled by this traffic light276int getMaxIndex();277278/// @}279280281protected:282/// @name Protected methods from NBTrafficLightDefinition-interface283/// @{284285/** @brief Computes the traffic light logic finally in dependence to the type286* @param[in] brakingTime Duration a vehicle needs for braking in front of the tls in seconds287* @return The computed logic288* @see NBTrafficLightDefinition::myCompute289*/290NBTrafficLightLogic* myCompute(int brakingTimeSeconds);291292/** @brief Builds the list of participating nodes/edges/links293* @see NBTrafficLightDefinition::setParticipantsInformation294*/295void setParticipantsInformation();296297298/** @brief Collects the nodes participating in this traffic light299* @see NBTrafficLightDefinition::collectNodes300*/301void collectNodes();302303304/** @brief Collects the links participating in this traffic light305* @exception ProcessError If a link could not be found306* @see NBTrafficLightDefinition::collectLinks307*/308void collectLinks();309310311/** @brief Returns the information whether a connection must brake, given a phase312* @param[in] possProhibited The connection to investigate313* @param[in] state The state314* @param[in] strmpos The index of this connection within the masks315* @return Whether the given connection must brake316*/317bool mustBrake(const NBConnection& possProhibited,318const std::string& state,319int strmpos) const;320321322/** @brief Replaces a removed edge/lane323* @param[in] removed The edge to replace324* @param[in] removedLane The lane of this edge to replace325* @param[in] by The edge to insert instead326* @param[in] byLane This edge's lane to insert instead327* @see NBTrafficLightDefinition::replaceRemoved328*/329void replaceRemoved(NBEdge* removed, int removedLane,330NBEdge* by, int byLane, bool incoming);331/// @}332333private:334/** @brief Builds the phase for a given time335* @param[in] ec The edge control to use336* @param[in] time The time to build the phase for337* @return The phase of this tls for the given time338*/339std::string buildPhaseState(const SUMOTime time) const;340341// pointer to the NBEdgeCont for checking edges342const NBEdgeCont* myEdgeCont;343344345private:346/// @brief Controlled signal groups347SignalGroupCont mySignalGroups;348349/// @brief The duration of a single cycle350SUMOTime myCycleDuration;351352353};354355356