Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSRailSignal.h
Go to the documentation of this file.
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/****************************************************************************/
20// A rail signal logic
21/****************************************************************************/
22#pragma once
23#include <config.h>
24
25#include <vector>
26#include <microsim/MSRoute.h>
29
30
31// ===========================================================================
32// class declarations
33// ===========================================================================
34class MSLink;
37class MSDriveWay;
38
39
40// ===========================================================================
41// class definitions
42// ===========================================================================
48public:
57 const std::string& id, const std::string& programID, SUMOTime delay,
58 const Parameterised::Map& parameters);
59
60
65 void init(NLDetectorBuilder& nb) override;
66
67
70
71 inline int getNumericalID() const {
72 return myNumericalID;
73 }
74
76 void setParameter(const std::string& key, const std::string& value) override;
77
83 void addLink(MSLink* link, MSLane* lane, int pos) override;
84
87
92 void adaptLinkInformationFrom(const MSTrafficLightLogic& logic) override;
94
95
98
99
109 bool updateCurrentPhase();
110
115 SUMOTime trySwitch() override;
116
118
119
122
127 int getPhaseNumber() const override;
128
133 const Phases& getPhases() const override;
134
143 const MSPhaseDefinition& getPhase(int givenstep) const override;
144
146 bool getsMajorGreen(int /*linkIndex*/) const override {
147 return true;
148 }
150
151
154
159 int getCurrentPhaseIndex() const override;
160
164 const MSPhaseDefinition& getCurrentPhaseDef() const override;
166
167 void resetLastSwitch(SUMOTime t);
168
171
176 SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const override;
177
183 SUMOTime getOffsetFromIndex(int index) const override;
184
190 int getIndexFromOffset(SUMOTime offset) const override;
192
193
196
204 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 }
211
213 VehicleVector getBlockingVehicles(int linkIndex) override;
214 std::string getBlockingVehicleIDs() const;
217
219 VehicleVector getRivalVehicles(int linkIndex) override;
220 std::string getRivalVehicleIDs() const;
221
223 VehicleVector getPriorityVehicles(int linkIndex) override;
224 std::string getPriorityVehicleIDs() const;
225
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
234 static std::vector<const MSDriveWay*> getBlockingDriveWays(const MSDriveWay* dw);
235
236 std::string getRequestedDriveWay(int linkIndex) override;
237 std::string getRequestedDriveWay() const;
238
240 void writeBlocks(OutputDevice& od, bool writeVehicles) const;
241
243 void addConstraint(const std::string& tripId, MSRailSignalConstraint* constraint);
244
247 const std::map<std::string, std::vector<MSRailSignalConstraint*> >& getConstraints() const {
248 return myConstraints;
249 }
250
252 bool removeConstraint(const std::string& tripId, MSRailSignalConstraint* constraint);
253 void removeConstraints();
255
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);
278
279 const std::vector<MSDriveWay*> retrieveDriveWays(int tlIndex) const;
280
282 bool constraintsAllow(const SUMOVehicle* veh, bool storeWaitRelation = false) const;
283
284 bool isMovingBlock() const {
285 return myMovingBlock;
286 }
287
288 static bool storeVehicles() {
289 return myStoreVehicles;
290 }
291
293 return myBlockingVehicles;
294 }
295
296 static std::vector<const MSDriveWay*>& blockingDriveWays() {
297 return myBlockingDriveWays;
298 }
299
301 return myRivalVehicles;
302 }
303
305 return myPriorityVehicles;
306 }
307
309 static std::string describeLinks(std::vector<MSLink*> links);
310
311protected:
312
313 const int myNumericalID;
314
315 std::string getNewDrivewayID();
316
317 /* The driveways for each link
318 */
319 struct LinkInfo {
321 LinkInfo(MSLink* link);
322
324 ~LinkInfo();
325
327
329 std::vector<MSDriveWay*> myDriveways;
330
332 std::string getID() const;
333
335 MSDriveWay& getDriveWay(const SUMOVehicle*, int searchStart = -1);
336 MSDriveWay& getDriveWay(MSRouteIterator firstIt, MSRouteIterator endIt, const std::string& info = "");
337
339 void reroute(SUMOVehicle* veh, const MSEdgeVector& occupied);
340
342 void reset();
343
346
349 };
350
352 std::vector<LinkInfo> myLinkInfos;
353
354protected:
355
361
364
367
370
372 std::map<std::string, std::vector<MSRailSignalConstraint*> > myConstraints;
373
376
377 static int myRSIndex;
378
379protected:
381 void storeTraCIVehicles(int linkIndex);
382 static void storeTraCIVehicles(const MSDriveWay* dw);
383
385 static void resetStored();
386
388
389 static bool myStoreVehicles;
393 static std::string myConstraintInfo;
394 static std::vector<const MSDriveWay*> myBlockingDriveWays;
395 static std::string myRequestedDriveWay;
397
398
399};
long long int SUMOTime
Definition GUI.h:36
std::vector< MSEdge * > MSEdgeVector
Definition MSEdge.h:73
ConstMSEdgeVector::const_iterator MSRouteIterator
Definition MSRoute.h:57
Representation of a lane in the micro simulation.
Definition MSLane.h:84
The definition of a single phase of a tls logic.
A base class for constraints.
A signal for rails.
bool isMovingBlock() const
static VehicleVector myRivalVehicles
std::string getBlockingVehicleIDs() const
Phases myPhases
The list of phases this logic uses.
static VehicleVector myPriorityVehicles
std::string getRequestedDriveWay() const
int myPhaseIndex
MSTrafficLightLogic requires that the phase index changes whenever signals change their state.
SUMOTime getOffsetFromIndex(int index) const override
Returns the position (start of a phase during a cycle) from of a given step.
void setParameter(const std::string &key, const std::string &value) override
Sets a parameter and updates internal constants.
static std::string myConstraintInfo
MSPhaseDefinition myCurrentPhase
The current phase.
void addConstraint(const std::string &tripId, MSRailSignalConstraint *constraint)
register constraint for signal switching
std::vector< LinkInfo > myLinkInfos
data storage for every link at this node (more than one when directly guarding a switch)
const MSDriveWay & retrieveDriveWayForVeh(int tlIndex, const SUMOVehicle *veh)
SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const override
Returns the index of the logic at the given simulation step.
std::string getPriorityVehicleIDs() const
static std::string myRequestedDriveWay
const std::map< std::string, std::vector< MSRailSignalConstraint * > > & getConstraints() const
VehicleVector getBlockingVehicles(int linkIndex) override
return vehicles that block the intersection/rail signal for vehicles that wish to pass the given link...
void writeBlocks(OutputDevice &od, bool writeVehicles) const
write rail signal block output for all links and driveways
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration) override
Changes the current phase and her duration.
VehicleVector getRivalVehicles(int linkIndex) override
return vehicles that approach the intersection/rail signal and are in conflict with vehicles that wis...
int myDriveWayIndex
running number of driveways created for this signal
static std::string describeLinks(std::vector< MSLink * > links)
print link descriptions
bool getsMajorGreen(int) const override
whether the given link index ever turns 'G'
~MSRailSignal()
Destructor.
static void resetStored()
reset temporary storage for injected conflict output
void adaptLinkInformationFrom(const MSTrafficLightLogic &logic) override
Applies information about controlled links and lanes from the given logic.
static int myRSIndex
static VehicleVector myBlockingVehicles
bool constraintsAllow(const SUMOVehicle *veh, bool storeWaitRelation=false) const
whether the given vehicle is free to drive
void removeConstraints()
void storeTraCIVehicles(int linkIndex)
update vehicle lists for traci calls
int getIndexFromOffset(SUMOTime offset) const override
Returns the step (the phasenumber) of a given position of the cycle.
void init(NLDetectorBuilder &nb) override
Initialises the rail signal with information about adjacent rail signals.
std::map< std::string, std::vector< MSRailSignalConstraint * > > myConstraints
map from tripId to constraint list
std::pair< const SUMOVehicle *const, const MSLink::ApproachingVehicleInformation > Approaching
int getNumericalID() const
int getPhaseNumber() const override
Returns the number of phases.
const MSPhaseDefinition & getPhase(int givenstep) const override
Returns the definition of the phase from the given position within the plan.
static VehicleVector & rivalVehicles()
static bool storeVehicles()
SUMOTime trySwitch() override
Switches to the next phase.
std::string getBlockingDriveWayIDs() const
const std::vector< MSDriveWay * > retrieveDriveWays(int tlIndex) const
const int myNumericalID
const MSDriveWay & retrieveDriveWay(int numericalID) const
static bool myStoreVehicles
std::map< const MSLane *, int, ComparatorNumericalIdLess > LaneVisitedMap
bool myMovingBlock
whether the signal is in moving block mode (only protects from oncoming and flanking trains)
bool removeConstraint(const std::string &tripId, MSRailSignalConstraint *constraint)
remove constraint for signal switching
void resetLastSwitch(SUMOTime t)
static std::vector< const MSDriveWay * > myBlockingDriveWays
bool updateCurrentPhase()
returns the state of the signal that actually required
static VehicleVector & priorityVehicles()
const Phases & getPhases() const override
Returns the phases of this tls program.
const MSPhaseDefinition & getCurrentPhaseDef() const override
Returns the definition of the current phase.
void updateDriveway(int numericalID)
update driveway for extended deadlock protection
std::string getConstraintInfo() const
static std::vector< const MSDriveWay * > & blockingDriveWays()
static VehicleVector & blockingVehicles()
std::string getNewDrivewayID()
static void initDriveWays(const SUMOVehicle *ego, bool update)
void addLink(MSLink *link, MSLane *lane, int pos) override
Adds a link on building.
const MSDriveWay & retrieveDriveWayForRoute(int tlIndex, MSRouteIterator first, MSRouteIterator end)
std::string getRivalVehicleIDs() const
static bool hasInsertionConstraint(MSLink *link, const MSVehicle *veh, std::string &info, bool &isInsertionOrder)
VehicleVector getPriorityVehicles(int linkIndex) override
return vehicles that approach the intersection/rail signal and have priority over vehicles that wish ...
std::vector< const MSDriveWay * > getBlockingDriveWays(int linkIndex) override
return vehicles that approach the intersection/rail signal and have priority over vehicles that wish ...
int getCurrentPhaseIndex() const override
Returns the current index within the program.
A class that stores and controls tls and switching of their programs.
The parent class for traffic light logics.
std::vector< const SUMOVehicle * > VehicleVector
list of vehicles
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
Representation of a vehicle in the micro simulation.
Definition MSVehicle.h:77
Builds detectors for microsim.
Static storage of an output device and its base (abstract) implementation.
std::map< std::string, std::string > Map
parameters map
Representation of a vehicle.
Definition SUMOVehicle.h:63
#define UNUSED_PARAMETER(x)