Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSRailSignalConstraint.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-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/****************************************************************************/
18// A constraint on rail signal switching
19/****************************************************************************/
20#include <config.h>
21
22#include <cassert>
23#include <utility>
24
28#include <microsim/MSLane.h>
29#include <microsim/MSEdge.h>
30#include <microsim/MSLink.h>
31#include <microsim/MSNet.h>
33#include "MSRailSignal.h"
35#include "MSRailSignalControl.h"
36
37//#define DEBUG_PASSED
38//#define DEBUG_LANE
39
40// ===========================================================================
41// static value definitions
42// ===========================================================================
43std::map<const MSLane*, MSRailSignalConstraint_Predecessor::PassedTracker*, ComparatorNumericalIdLess> MSRailSignalConstraint_Predecessor::myTrackerLookup;
44std::map<std::string, std::string> MSRailSignalConstraint::myTripIdLookup;
45
46// ===========================================================================
47// MSRailSignalConstraint method definitions
48// ===========================================================================
49void
53
54void
56 if (OptionsCont::getOptions().getBool("save-state.constraints")) {
58 if (s->getConstraints().size() > 0) {
60 out.writeAttr(SUMO_ATTR_ID, s->getID());
61 for (auto item : s->getConstraints()) {
62 for (MSRailSignalConstraint* c : item.second) {
63 c->write(out, item.first);
64 }
65 }
66 out.closeTag();
67 }
68 }
69 }
71}
72
73void
78
79void
82 s->removeConstraints();
83 }
84 myTripIdLookup.clear();
85}
86
87
88const SUMOVehicle*
89MSRailSignalConstraint::getVeh(const std::string& tripID, bool checkID) {
91 const std::string& vehID = lookupVehId(tripID);
92 if (vehID != "") {
93 SUMOVehicle* veh = c.getVehicle(vehID);
94 if (veh != nullptr) {
95 return veh;
96 }
97 }
98 for (MSVehicleControl::constVehIt i = c.loadedVehBegin(); i != c.loadedVehEnd(); ++i) {
99 SUMOVehicle* veh = i->second;
100 if (veh->getParameter().getParameter("tripId") == tripID || (checkID && veh->getID() == tripID)) {
101 return veh;
102 }
103 }
104 return nullptr;
105}
106
107// ===========================================================================
108// MSRailSignalConstraint_Predecessor method definitions
109// ===========================================================================
110MSRailSignalConstraint_Predecessor::MSRailSignalConstraint_Predecessor(ConstraintType type, const MSRailSignal* signal, const std::string& tripId, int limit, bool active) :
112 myTripId(tripId),
113 myLimit(limit),
114 myAmActive(active),
115 myFoeSignal(signal) {
116 for (const auto& lv : signal->getLinks()) {
117 for (const MSLink* link : lv) {
118 MSLane* lane = link->getViaLaneOrLane();
119 PassedTracker* pt = nullptr;
120 if (myTrackerLookup.count(lane) == 0) {
121 pt = new PassedTracker(lane);
122 myTrackerLookup[lane] = pt;
123 } else {
124 pt = myTrackerLookup[lane];
125 }
126 pt->raiseLimit(limit);
127 myTrackers.push_back(pt);
128 }
129 }
130
131}
132
133void
135 for (auto item : myTrackerLookup) {
136 delete item.second;
137 }
138 myTrackerLookup.clear();
139}
140
141void
143 for (auto item : myTrackerLookup) {
144 item.second->saveState(out);
145 }
146}
147
148void
150 bool ok;
151 const std::string laneID = attrs.getString(SUMO_ATTR_LANE);
152 const int index = attrs.get<int>(SUMO_ATTR_INDEX, nullptr, ok);
153 const std::vector<std::string>& tripIDs = attrs.get<std::vector<std::string> >(SUMO_ATTR_STATE, nullptr, ok);
154 MSLane* lane = MSLane::dictionary(laneID);
155 if (lane == nullptr) {
156 throw ProcessError(TLF("Unknown lane '%' in loaded state.", laneID));
157 }
158 if (myTrackerLookup.count(lane) == 0) {
159 WRITE_WARNINGF(TL("Unknown tracker lane '%' in loaded state."), laneID);
160 return;
161 }
162 PassedTracker* tracker = myTrackerLookup[lane];
163 tracker->loadState(index, tripIDs);
164}
165
166
167void
169 for (auto item : myTrackerLookup) {
170 item.second->clearState();
171 }
172}
173
174
175bool
177 if (!myAmActive) {
178 return true;
179 }
180 for (PassedTracker* pt : myTrackers) {
181 if (pt->hasPassed(myTripId, myLimit)) {
182 return true;
183 }
184 }
185 return false;
186}
187
188std::string
190 // try to retrieve vehicle id that belongs to myTripId
191 // this may be slow so it should only be used for debugging
192 const SUMOVehicle* veh = getVeh(myTripId);
193 std::string vehID;
194 if (veh != nullptr) {
195 vehID = " (" + veh->getID() + ")";
196 }
197 std::vector<std::string> passedIDs;
198 for (const std::string& passedTripID : myTrackers.front()->myPassed) {
199 if (passedTripID == "") {
200 continue;
201 }
202 const SUMOVehicle* passedVeh = getVeh(passedTripID);
203 if (passedVeh != nullptr) {
204 passedIDs.push_back(passedVeh->getID());
205 }
206 }
207 std::string passedIDs2 = "";
208 if (passedIDs.size() > 0) {
209 passedIDs2 = " (" + toString(passedIDs) + ")";
210 }
211 std::string params = "";
212 for (auto item : getParametersMap()) {
213 params += ("\n key=" + item.first + " value=" + item.second);
214 }
215 return (toString(getTag()) + " " + myTripId + vehID + " at signal " + myTrackers.front()->getLane()->getEdge().getFromJunction()->getID()
216 + " passed=" + StringUtils::prune(toString(myTrackers.front()->myPassed)) + passedIDs2 + params);
217}
218
219const SUMOVehicle*
223
224void
225MSRailSignalConstraint::storeTripId(const std::string& tripId, const std::string& vehID) {
226 myTripIdLookup[tripId] = vehID;
227}
228
229const std::string&
230MSRailSignalConstraint::lookupVehId(const std::string& tripId) {
231 return myTripIdLookup[tripId];
232}
233
234// ===========================================================================
235// MSRailSignalConstraint_Predecessor::PassedTracker method definitions
236// ===========================================================================
237
239 MSMoveReminder("PassedTracker_" + lane->getID(), lane, true),
240 myPassed(1, ""),
241 myLastIndex(-1)
242{ }
243
244bool
246 myLastIndex = (myLastIndex + 1) % myPassed.size();
247 myPassed[myLastIndex] = veh.getParameter().getParameter("tripId", veh.getID());
248#ifdef DEBUG_PASSED
249 if (myLane->getID() == DEBUG_LANE) {
250 std::cout << SIMTIME << " hasPassed " << veh.getID() << " tripId=" << veh.getParameter().getParameter("tripId", veh.getID()) << " index=" << myLastIndex << "\n";
251 }
252#endif
253 return true;
254}
255
256void
258 while (limit > (int)myPassed.size()) {
259 myPassed.insert(myPassed.begin() + (myLastIndex + 1), "");
260 }
261#ifdef DEBUG_PASSED
262 if (myLane->getID() == DEBUG_LANE) {
263 std::cout << " raiseLimit=" << limit << "\n";
264 }
265#endif
266}
267
268bool
269MSRailSignalConstraint_Predecessor::PassedTracker::hasPassed(const std::string& tripId, int limit) const {
270 if (myLastIndex < 0) {
271 return false;
272 }
273 int i = myLastIndex;
274 while (limit > 0) {
275 if (myPassed[i] == tripId) {
276 return true;
277 }
278 if (i == 0) {
279 i = (int)myPassed.size() - 1;
280 } else {
281 i--;
282 }
283 limit--;
284 }
285 return false;
286}
287
288void
290 myPassed = std::vector<std::string>(myPassed.size());
291 myLastIndex = 0;
292}
293
294void
296 const std::string state = toString(myPassed.back() == ""
297 ? std::vector<std::string>(myPassed.begin(), myPassed.begin() + (myLastIndex + 1))
298 // wrapped around
299 : myPassed);
300 // no need to save state if no vehicles have passed this tracker
301 if (state != "") {
303 out.writeAttr(SUMO_ATTR_LANE, getLane()->getID());
304 out.writeAttr(SUMO_ATTR_INDEX, myLastIndex);
305 out.writeAttr(SUMO_ATTR_STATE, state);
306 out.closeTag();
307 }
308}
309
310void
311MSRailSignalConstraint_Predecessor::PassedTracker::loadState(int index, const std::vector<std::string>& tripIDs) {
312 raiseLimit((int)tripIDs.size());
313 for (int i = 0; i < (int)tripIDs.size(); i++) {
314 myPassed[i] = tripIDs[i];
315 }
316#ifdef DEBUG_PASSED
317 if (myLane->getID() == DEBUG_LANE) {
318 std::cout << " loadState limit=" << tripIDs.size() << " index=" << index << "\n";
319 for (int i = 0; i < (int)myPassed.size(); i++) {
320 std::cout << " i=" << i << " passed=" << myPassed[i] << "\n";
321 }
322 }
323#endif
324 myLastIndex = index;
325}
326
327
328void
329MSRailSignalConstraint_Predecessor::write(OutputDevice& out, const std::string& tripId) const {
330 out.openTag(getTag());
331 out.writeAttr(SUMO_ATTR_TRIP_ID, tripId);
334 if (myLimit > 1) {
336 }
337 if (!myAmActive) {
339 }
340 XMLHelpers::writeParams(out, *this);
341 out.closeTag();
342}
343
344/****************************************************************************/
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:299
#define TL(string)
Definition MsgHandler.h:316
#define TLF(string,...)
Definition MsgHandler.h:318
#define SIMTIME
Definition SUMOTime.h:70
@ SUMO_TAG_RAILSIGNAL_CONSTRAINTS
Constraints on switching a rail signal.
@ SUMO_TAG_RAILSIGNAL_CONSTRAINT_TRACKER
Saved state for constraint tracker.
@ SUMO_ATTR_LANE
@ SUMO_ATTR_LIMIT
@ SUMO_ATTR_INDEX
@ SUMO_ATTR_TRIP_ID
@ SUMO_ATTR_TLID
link,node: the traffic light id responsible for this link
@ SUMO_ATTR_FOES
@ SUMO_ATTR_ID
@ SUMO_ATTR_STATE
The state of a link.
@ SUMO_ATTR_ACTIVE
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:57
Representation of a lane in the micro simulation.
Definition MSLane.h:84
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition MSLane.cpp:2538
Something on a lane to be noticed about vehicle movement.
Notification
Definition of a vehicle state.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:201
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition MSNet.h:403
bool hasPassed(const std::string &tripId, int limit) const
bool notifyEnter(SUMOTrafficObject &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
tracks vehicles that passed this link (entered the next lane)
void loadState(int index, const std::vector< std::string > &tripIDs)
loads the current passed states into the given stream
void clearState()
Clear all passed states before quick-loading state.
void saveState(OutputDevice &out)
Saves the current passed states into the given stream.
static std::map< const MSLane *, PassedTracker *, ComparatorNumericalIdLess > myTrackerLookup
MSRailSignalConstraint_Predecessor(ConstraintType type, const MSRailSignal *signal, const std::string &tripId, int limit, bool active)
Constructor.
const MSRailSignal * myFoeSignal
store the foe signal (for TraCI access)
bool cleared() const
whether the constraint has been met
bool myAmActive
Whether this constraint is currently active.
static void loadState(const SUMOSAXAttributes &attrs)
loads the constraint state from the given attrs
static void saveState(OutputDevice &out)
Saves the current constraint states into the given stream.
const std::string myTripId
id of the predecessor that must already have passed
static void clearState()
Clear all constraint states before quick-loading state.
std::vector< PassedTracker * > myTrackers
the tracker object for this constraint
void write(OutputDevice &out, const std::string &tripId) const
const int myLimit
the number of passed vehicles within which tripId must have occurred
A base class for constraints.
static const SUMOVehicle * getVeh(const std::string &tripID, bool checkID=false)
static void saveState(OutputDevice &out)
Saves the current constraint states into the given stream.
static std::map< std::string, std::string > myTripIdLookup
static void clearState()
Clear all constraint states before quick-loading state.
static void clearAll()
Remove all constraints before quick-loading state.
static void cleanup()
clean up state
static void storeTripId(const std::string &tripId, const std::string &vehID)
static const std::string & lookupVehId(const std::string &tripId)
static MSRailSignalControl & getInstance()
const std::vector< MSRailSignal * > & getSignals() const
A signal for rails.
const LinkVectorVector & getLinks() const
Returns the list of lists of all affected links.
The class responsible for building and deletion of vehicles.
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
std::map< std::string, SUMOVehicle * >::const_iterator constVehIt
Definition of the internal vehicles map iterator.
constVehIt loadedVehBegin() const
Returns the begin of the internal vehicle map.
constVehIt loadedVehEnd() const
Returns the end of the internal vehicle map.
const std::string & getID() const
Returns the id.
Definition Named.h:73
static OptionsCont & getOptions()
Retrieves the options.
Static storage of an output device and its base (abstract) implementation.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const ATTR_TYPE &attr, const T &val, const bool isNull=false, const bool escape=false)
writes a named attribute
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
virtual const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
const Parameterised::Map & getParametersMap() const
Returns the inner key/value map.
Encapsulated SAX-Attributes.
virtual std::string getString(int id, bool *isPresent=nullptr) const =0
Returns the string-value of the named (by its enum-value) attribute.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
Representation of a vehicle, person, or container.
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle's parameter (including departure definition)
Representation of a vehicle.
Definition SUMOVehicle.h:63
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
static OutputDevice & writeParams(OutputDevice &device, const Parameterised &params)
write the parameters of the given object as param tags