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-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/****************************************************************************/
18// A constraint on rail signal switching
19/****************************************************************************/
20#include <config.h>
21#include <cassert>
22#include <utility>
23
26#include <microsim/MSLane.h>
27#include <microsim/MSEdge.h>
28#include <microsim/MSLink.h>
29#include <microsim/MSNet.h>
31#include "MSRailSignal.h"
33#include "MSRailSignalControl.h"
34
35//#define DEBUG_PASSED
36//#define DEBUG_LANE
37
38// ===========================================================================
39// static value definitions
40// ===========================================================================
41std::map<const MSLane*, MSRailSignalConstraint_Predecessor::PassedTracker*, ComparatorNumericalIdLess> MSRailSignalConstraint_Predecessor::myTrackerLookup;
42std::map<std::string, std::string> MSRailSignalConstraint::myTripIdLookup;
43
44// ===========================================================================
45// MSRailSignalConstraint method definitions
46// ===========================================================================
47void
51
52void
54 if (OptionsCont::getOptions().getBool("save-state.constraints")) {
56 if (s->getConstraints().size() > 0) {
58 out.writeAttr(SUMO_ATTR_ID, s->getID());
59 for (auto item : s->getConstraints()) {
60 for (MSRailSignalConstraint* c : item.second) {
61 c->write(out, item.first);
62 }
63 }
64 out.closeTag();
65 }
66 }
67 }
69}
70
71void
76
77void
80 s->removeConstraints();
81 }
82 myTripIdLookup.clear();
83}
84
85
86const SUMOVehicle*
87MSRailSignalConstraint::getVeh(const std::string& tripID, bool checkID) {
89 const std::string& vehID = lookupVehId(tripID);
90 if (vehID != "") {
91 SUMOVehicle* veh = c.getVehicle(vehID);
92 if (veh != nullptr) {
93 return veh;
94 }
95 }
96 for (MSVehicleControl::constVehIt i = c.loadedVehBegin(); i != c.loadedVehEnd(); ++i) {
97 SUMOVehicle* veh = i->second;
98 if (veh->getParameter().getParameter("tripId") == tripID || (checkID && veh->getID() == tripID)) {
99 return veh;
100 }
101 }
102 return nullptr;
103}
104
105// ===========================================================================
106// MSRailSignalConstraint_Predecessor method definitions
107// ===========================================================================
108MSRailSignalConstraint_Predecessor::MSRailSignalConstraint_Predecessor(ConstraintType type, const MSRailSignal* signal, const std::string& tripId, int limit, bool active) :
110 myTripId(tripId),
111 myLimit(limit),
112 myAmActive(active),
113 myFoeSignal(signal) {
114 for (const auto& lv : signal->getLinks()) {
115 for (const MSLink* link : lv) {
116 MSLane* lane = link->getViaLaneOrLane();
117 PassedTracker* pt = nullptr;
118 if (myTrackerLookup.count(lane) == 0) {
119 pt = new PassedTracker(lane);
120 myTrackerLookup[lane] = pt;
121 } else {
122 pt = myTrackerLookup[lane];
123 }
124 pt->raiseLimit(limit);
125 myTrackers.push_back(pt);
126 }
127 }
128
129}
130
131void
133 for (auto item : myTrackerLookup) {
134 delete item.second;
135 }
136 myTrackerLookup.clear();
137}
138
139void
141 for (auto item : myTrackerLookup) {
142 item.second->saveState(out);
143 }
144}
145
146void
148 bool ok;
149 const std::string laneID = attrs.getString(SUMO_ATTR_LANE);
150 const int index = attrs.get<int>(SUMO_ATTR_INDEX, nullptr, ok);
151 const std::vector<std::string>& tripIDs = attrs.get<std::vector<std::string> >(SUMO_ATTR_STATE, nullptr, ok);
152 MSLane* lane = MSLane::dictionary(laneID);
153 if (lane == nullptr) {
154 throw ProcessError(TLF("Unknown lane '%' in loaded state.", laneID));
155 }
156 if (myTrackerLookup.count(lane) == 0) {
157 WRITE_WARNINGF(TL("Unknown tracker lane '%' in loaded state."), laneID);
158 return;
159 }
160 PassedTracker* tracker = myTrackerLookup[lane];
161 tracker->loadState(index, tripIDs);
162}
163
164
165void
167 for (auto item : myTrackerLookup) {
168 item.second->clearState();
169 }
170}
171
172
173bool
175 if (!myAmActive) {
176 return true;
177 }
178 for (PassedTracker* pt : myTrackers) {
179 if (pt->hasPassed(myTripId, myLimit)) {
180 return true;
181 }
182 }
183 return false;
184}
185
186std::string
188 // try to retrieve vehicle id that belongs to myTripId
189 // this may be slow so it should only be used for debugging
190 const SUMOVehicle* veh = getVeh(myTripId);
191 std::string vehID;
192 if (veh != nullptr) {
193 vehID = " (" + veh->getID() + ")";
194 }
195 std::vector<std::string> passedIDs;
196 for (const std::string& passedTripID : myTrackers.front()->myPassed) {
197 if (passedTripID == "") {
198 continue;
199 }
200 const SUMOVehicle* passedVeh = getVeh(passedTripID);
201 if (passedVeh != nullptr) {
202 passedIDs.push_back(passedVeh->getID());
203 }
204 }
205 std::string passedIDs2 = "";
206 if (passedIDs.size() > 0) {
207 passedIDs2 = " (" + toString(passedIDs) + ")";
208 }
209 std::string params = "";
210 for (auto item : getParametersMap()) {
211 params += ("\n key=" + item.first + " value=" + item.second);
212 }
213 return (toString(getTag()) + " " + myTripId + vehID + " at signal " + myTrackers.front()->getLane()->getEdge().getFromJunction()->getID()
214 + " passed=" + StringUtils::prune(toString(myTrackers.front()->myPassed)) + passedIDs2 + params);
215}
216
217const SUMOVehicle*
221
222void
223MSRailSignalConstraint::storeTripId(const std::string& tripId, const std::string& vehID) {
224 myTripIdLookup[tripId] = vehID;
225}
226
227const std::string&
228MSRailSignalConstraint::lookupVehId(const std::string& tripId) {
229 return myTripIdLookup[tripId];
230}
231
232// ===========================================================================
233// MSRailSignalConstraint_Predecessor::PassedTracker method definitions
234// ===========================================================================
235
237 MSMoveReminder("PassedTracker_" + lane->getID(), lane, true),
238 myPassed(1, ""),
239 myLastIndex(-1)
240{ }
241
242bool
244 myLastIndex = (myLastIndex + 1) % myPassed.size();
245 myPassed[myLastIndex] = veh.getParameter().getParameter("tripId", veh.getID());
246#ifdef DEBUG_PASSED
247 if (myLane->getID() == DEBUG_LANE) {
248 std::cout << SIMTIME << " hasPassed " << veh.getID() << " tripId=" << veh.getParameter().getParameter("tripId", veh.getID()) << " index=" << myLastIndex << "\n";
249 }
250#endif
251 return true;
252}
253
254void
256 while (limit > (int)myPassed.size()) {
257 myPassed.insert(myPassed.begin() + (myLastIndex + 1), "");
258 }
259#ifdef DEBUG_PASSED
260 if (myLane->getID() == DEBUG_LANE) {
261 std::cout << " raiseLimit=" << limit << "\n";
262 }
263#endif
264}
265
266bool
267MSRailSignalConstraint_Predecessor::PassedTracker::hasPassed(const std::string& tripId, int limit) const {
268 if (myLastIndex < 0) {
269 return false;
270 }
271 int i = myLastIndex;
272 while (limit > 0) {
273 if (myPassed[i] == tripId) {
274 return true;
275 }
276 if (i == 0) {
277 i = (int)myPassed.size() - 1;
278 } else {
279 i--;
280 }
281 limit--;
282 }
283 return false;
284}
285
286void
288 myPassed = std::vector<std::string>(myPassed.size());
289 myLastIndex = 0;
290}
291
292void
294 const std::string state = toString(myPassed.back() == ""
295 ? std::vector<std::string>(myPassed.begin(), myPassed.begin() + (myLastIndex + 1))
296 // wrapped around
297 : myPassed);
298 // no need to save state if no vehicles have passed this tracker
299 if (state != "") {
301 out.writeAttr(SUMO_ATTR_LANE, getLane()->getID());
302 out.writeAttr(SUMO_ATTR_INDEX, myLastIndex);
303 out.writeAttr(SUMO_ATTR_STATE, state);
304 out.closeTag();
305 }
306}
307
308void
309MSRailSignalConstraint_Predecessor::PassedTracker::loadState(int index, const std::vector<std::string>& tripIDs) {
310 raiseLimit((int)tripIDs.size());
311 for (int i = 0; i < (int)tripIDs.size(); i++) {
312 myPassed[i] = tripIDs[i];
313 }
314#ifdef DEBUG_PASSED
315 if (myLane->getID() == DEBUG_LANE) {
316 std::cout << " loadState limit=" << tripIDs.size() << " index=" << index << "\n";
317 for (int i = 0; i < (int)myPassed.size(); i++) {
318 std::cout << " i=" << i << " passed=" << myPassed[i] << "\n";
319 }
320 }
321#endif
322 myLastIndex = index;
323}
324
325
326void
327MSRailSignalConstraint_Predecessor::write(OutputDevice& out, const std::string& tripId) const {
328 out.openTag(getTag());
329 out.writeAttr(SUMO_ATTR_TRIP_ID, tripId);
332 if (myLimit > 1) {
334 }
335 if (!myAmActive) {
337 }
338 writeParams(out);
339 out.closeTag();
340}
341
342/****************************************************************************/
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:296
#define TL(string)
Definition MsgHandler.h:315
#define TLF(string,...)
Definition MsgHandler.h:317
#define SIMTIME
Definition SUMOTime.h:62
@ 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:46
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:2415
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:186
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition MSNet.h:378
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 occured
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:74
static OptionsCont & getOptions()
Retrieves the options.
Static storage of an output device and its base (abstract) implementation.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
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.
void writeParams(OutputDevice &device) const
write Params in the given outputdevice
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:62
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.