Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSDriveWay.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 sequende of rail tracks (lanes) that may be used as a "set route" (Fahrstraße)
19/****************************************************************************/
20#include <config.h>
21#include <cassert>
22#include <utility>
23
26#include <microsim/MSStop.h>
27#include <microsim/MSLane.h>
28#include <microsim/MSEdge.h>
29#include <microsim/MSLink.h>
30#include <microsim/MSNet.h>
33#include <mesosim/MELoop.h>
34#include "MSRailSignal.h"
35#include "MSDriveWay.h"
36#include "MSRailSignalControl.h"
37
38#define DRIVEWAY_SANITY_CHECK
39//#define SUBDRIVEWAY_WARN_NOCONFLICT
40
41//#define DEBUG_BUILD_DRIVEWAY
42//#define DEBUG_BUILD_SUBDRIVEWAY
43//#define DEBUG_ADD_FOES
44//#define DEBUG_BUILD_SIDINGS
45//#define DEBUG_DRIVEWAY_BUILDROUTE
46//#define DEBUG_CHECK_FLANKS
47//#define DEBUG_SIGNALSTATE_PRIORITY
48//#define DEBUG_SIGNALSTATE
49//#define DEBUG_FIND_PROTECTION
50//#define DEBUG_MOVEREMINDER
51//#define DEBUG_MATCH
52
53#define DEBUG_HELPER(obj) ((obj) != nullptr && (obj)->isSelected())
54//#define DEBUG_HELPER(obj) ((obj)->getID() == "")
55//#define DEBUG_HELPER(obj) (true)
56
57#define DEBUG_DW_ID ""
58#define DEBUG_COND_DW (dw->getID() == DEBUG_DW_ID || DEBUG_DW_ID == std::string("ALL"))
59#define DEBUG_COND_DW2 (getID() == DEBUG_DW_ID || DEBUG_DW_ID == std::string("ALL"))
60
61// ===========================================================================
62// static value definitions
63// ===========================================================================
65std::set<const MSEdge*> MSDriveWay::myBlockLengthWarnings;
68std::map<const MSLink*, std::vector<MSDriveWay*> > MSDriveWay::mySwitchDriveWays;
69std::map<const MSEdge*, std::vector<MSDriveWay*> > MSDriveWay::myReversalDriveWays;
70std::map<const MSEdge*, std::vector<MSDriveWay*>, ComparatorNumericalIdLess> MSDriveWay::myDepartureDriveways;
71std::map<const MSJunction*, int> MSDriveWay::myDepartDrivewayIndex;
72std::map<const MSEdge*, std::vector<MSDriveWay*> > MSDriveWay::myDepartureDrivewaysEnds;
73std::map<const MSEdge*, std::vector<MSDriveWay*>, ComparatorNumericalIdLess> MSDriveWay::myEndingDriveways;
74std::map<ConstMSEdgeVector, MSDriveWay*> MSDriveWay::myDriveWayRouteLookup;
75std::map<std::string, MSDriveWay*> MSDriveWay::myDriveWayLookup;
76
77// ---------------------------------------------------------------------------
78// static initialisation methods
79// ---------------------------------------------------------------------------
80void
82 myWriteVehicles = OptionsCont::getOptions().isSet("railsignal-vehicle-output");
83 myMovingBlockMaxDist = OptionsCont::getOptions().getFloat("railsignal.moving-block.max-dist");
84}
85
86// ===========================================================================
87// MSDriveWay method definitions
88// ===========================================================================
89
90
91MSDriveWay::MSDriveWay(const MSLink* origin, const std::string& id, bool temporary) :
92 MSMoveReminder("DriveWay_" + (temporary ? "tmp" : id)),
93 Named(id),
94 myNumericalID(temporary ? -1 : myGlobalDriveWayIndex++),
95 myOrigin(origin),
96 myActive(nullptr),
97 myCoreSize(0),
98 myForwardEdgeCount(0),
99 myFoundSignal(false),
100 myFoundJump(false),
101 myTerminateRoute(false),
102 myAbortedBuild(false),
103 myBidiEnded(false),
104 myIsSubDriveway(false)
105{}
106
107
109 for (const MSDriveWay* sub : mySubDriveWays) {
110 delete sub;
111 }
112 mySubDriveWays.clear();
113}
114
115void
118 myBlockLengthWarnings.clear();
119 myWriteVehicles = false;
120
121 for (auto item : myDepartureDriveways) {
122 for (MSDriveWay* dw : item.second) {
123 delete dw;
124 }
125 }
132}
133
134void
136 for (auto item : myEndingDriveways) {
137 for (MSDriveWay* dw : item.second) {
138 dw->myTrains.clear();
139 }
140 }
141}
142
143
144bool
146#ifdef DEBUG_MOVEREMINDER
147 std::cout << SIMTIME << " notifyEnter " << getDescription() << " veh=" << veh.getID() << " lane=" << (MSGlobals::gUseMesoSim ? veh.getEdge()->getID() : Named::getIDSecure(enteredLane)) << " reason=" << reason << "\n";
148#endif
149 if (veh.isVehicle() && (enteredLane == myLane || (MSGlobals::gUseMesoSim && veh.getEdge() == &myLane->getEdge()))
150 && (reason == NOTIFICATION_DEPARTED || reason == NOTIFICATION_JUNCTION || reason == NOTIFICATION_PARKING)) {
151 SUMOVehicle& sveh = dynamic_cast<SUMOVehicle&>(veh);
152 MSRouteIterator firstIt = std::find(sveh.getCurrentRouteEdge(), sveh.getRoute().end(), myLane->getNextNormal());
153 if (match(firstIt, sveh.getRoute().end())) {
154 if (myTrains.count(&sveh) == 0) {
155 enterDriveWay(sveh, reason);
156 }
157 return true;
158 }
159 } else if (reason == NOTIFICATION_REROUTE) {
160 assert(veh.isVehicle());
161 SUMOVehicle& sveh = dynamic_cast<SUMOVehicle&>(veh);
162 assert(myTrains.count(&sveh) == 0);
163 int movedPast = matchesPastRoute(sveh);
164 // vehicle must still be one the drivway
165 if (movedPast >= 0 && movedPast < myForwardEdgeCount) {
166 enterDriveWay(sveh, reason);
167 return true;
168 }
169 }
170 return false;
171}
172
173
174bool
175MSDriveWay::notifyLeave(SUMOTrafficObject& veh, double /*lastPos*/, Notification reason, const MSLane* enteredLane) {
176 UNUSED_PARAMETER(enteredLane);
177#ifdef DEBUG_MOVEREMINDER
178 std::cout << SIMTIME << " notifyLeave " << getDescription() << " veh=" << veh.getID() << " lane=" << Named::getIDSecure(enteredLane) << " reason=" << toString(reason) << "\n";
179#endif
180 if (veh.isVehicle()) {
181 // leaving network with departure, teleport etc
183 myTrains.erase(&dynamic_cast<SUMOVehicle&>(veh));
184 if (myWriteVehicles) {
185 myVehicleEvents.push_back(VehicleEvent(SIMSTEP, false, veh.getID(), reason));
186 }
187 return false;
189 // notifyLeave is called before moving the route iterator
190 const MSLane* leftLane = (*(dynamic_cast<SUMOVehicle&>(veh).getCurrentRouteEdge()))->getLanes().front();
191 return notifyLeaveBack(veh, reason, leftLane);
192 } else {
193 return true;
194 }
195 } else {
196 return false;
197 }
198}
199
200
201bool
203#ifdef DEBUG_MOVEREMINDER
204 std::cout << SIMTIME << " notifyLeaveBack " << getDescription() << " veh=" << veh.getID() << " lane=" << Named::getIDSecure(leftLane) << " reason=" << toString(reason) << "\n";
205#endif
206 if (veh.isVehicle()) {
207 if (leftLane == myForward.back() && (veh.getBackLane() != leftLane->getBidiLane() || MSGlobals::gUseMesoSim)) {
208 myTrains.erase(&dynamic_cast<SUMOVehicle&>(veh));
209 if (myWriteVehicles) {
210 myVehicleEvents.push_back(VehicleEvent(SIMSTEP, false, veh.getID(), reason));
211 }
212 return false;
213 } else {
214 return true;
215 }
216 } else {
217 return false;
218 }
219}
220
221
222bool
224#ifdef DEBUG_MOVEREMINDER
225 std::cout << SIMTIME << " notifyReroute " << getDescription() << " veh=" << veh.getID() << "\n";
226#endif
227 assert(veh.isVehicle());
228 SUMOVehicle* sveh = dynamic_cast<SUMOVehicle*>(&veh);
229 assert(myTrains.count(sveh) != 0);
230 if (matchesPastRoute(*sveh) >= 0) {
231 //std::cout << SIMTIME << " notifyReroute " << getDescription() << " veh=" << veh.getID() << " valid\n";
232 return true;
233 }
234 // no match found, remove
235 myTrains.erase(sveh);
236 if (myWriteVehicles) {
238 }
239 //std::cout << SIMTIME << " notifyReroute " << getDescription() << " veh=" << veh.getID() << " invalid\n";
240 return false;
241}
242
243
244int
246 // look backwards along the route to find the driveway lane
247 const ConstMSEdgeVector& routeEdges = sveh.getRoute().getEdges();
248 for (int i = sveh.getRoutePosition(); i >= 0; i--) {
249 if (routeEdges[i] == myLane->getNextNormal()) {
250 MSRouteIterator firstIt = routeEdges.begin() + i;
251 if (match(firstIt, sveh.getRoute().end())) {
252 // driveway is still valid after rerouting
253 //std::cout << SIMTIME << " notifyReroute " << getDescription() << " veh=" << veh.getID() << " valid\n";
254 return sveh.getRoutePosition() - i;
255 }
256 break;
257 }
258 }
259 return -1;
260}
261
262
263void
265 myTrains.insert(&sveh);
266 if (myOrigin != nullptr) {
268 }
269 for (const MSDriveWay* foe : myFoes) {
270 if (foe->myOrigin != nullptr) {
272 }
273 }
274 if (myWriteVehicles) {
275 myVehicleEvents.push_back(VehicleEvent(SIMSTEP, true, sveh.getID(), reason));
276 }
277}
278
279bool
280MSDriveWay::reserve(const Approaching& closest, MSEdgeVector& occupied) {
281 if (foeDriveWayOccupied(true, closest.first, occupied)) {
282 return false;
283 }
284 for (MSLink* foeLink : myConflictLinks) {
285 if (hasLinkConflict(closest, foeLink)) {
286#ifdef DEBUG_SIGNALSTATE
287 if (gDebugFlag4 || DEBUG_HELPER(closest.first)) {
288 std::cout << getID() << " linkConflict with " << getTLLinkID(foeLink) << "\n";
289 }
290#endif
291 return false;
292 }
293 }
294 myActive = closest.first;
295 return true;
296}
297
298
299bool
300MSDriveWay::hasLinkConflict(const Approaching& veh, const MSLink* foeLink) const {
301#ifdef DEBUG_SIGNALSTATE_PRIORITY
302 if (gDebugFlag4) {
303 std::cout << " checkLinkConflict foeLink=" << getTLLinkID(foeLink) << " ego=" << Named::getIDSecure(veh.first) << "\n";
304 }
305#endif
306 if (foeLink->getApproaching().size() > 0) {
307 Approaching foe = foeLink->getClosest();
308#ifdef DEBUG_SIGNALSTATE_PRIORITY
309 if (gDebugFlag4) {
310 std::cout << " approaching foe=" << foe.first->getID() << "\n";
311 }
312#endif
313 if (foe.first == veh.first) {
314 return false;
315 }
316 const MSTrafficLightLogic* foeTLL = foeLink->getTLLogic();
317 assert(foeTLL != nullptr);
318 const MSRailSignal* constFoeRS = dynamic_cast<const MSRailSignal*>(foeTLL);
319 MSRailSignal* foeRS = const_cast<MSRailSignal*>(constFoeRS);
320 if (foeRS != nullptr) {
321 const MSDriveWay& foeDriveWay = foeRS->retrieveDriveWayForVeh(foeLink->getTLIndex(), foe.first);
322 MSEdgeVector occupied;
323 if (foeDriveWay.foeDriveWayOccupied(false, foe.first, occupied) ||
324 !foeRS->constraintsAllow(foe.first) ||
325 !overlap(foeDriveWay) ||
326 !isFoeOrSubFoe(&foeDriveWay) ||
327 canUseSiding(veh.first, &foeDriveWay).first) {
328#ifdef DEBUG_SIGNALSTATE_PRIORITY
329 if (gDebugFlag4) {
330 if (foeDriveWay.foeDriveWayOccupied(false, foe.first, occupied)) {
331 std::cout << " foe blocked\n";
332 } else if (!foeRS->constraintsAllow(foe.first)) {
333 std::cout << " foe constrained\n";
334 } else if (!overlap(foeDriveWay)) {
335 std::cout << " no overlap\n";
336 } else if (!isFoeOrSubFoe(&foeDriveWay)) {
337 std::cout << " foeDW=" << foeDriveWay.getID() << " is not a foe to " << getID() << "\n";
338 } else if (canUseSiding(veh.first, &foeDriveWay).first) {
339 std::cout << " use siding\n";
340 }
341 }
342#endif
343 return false;
344 }
345#ifdef DEBUG_SIGNALSTATE_PRIORITY
346 if (gDebugFlag4) {
347 std::cout
348 << " aSB=" << veh.second.arrivalSpeedBraking << " foeASB=" << foe.second.arrivalSpeedBraking
349 << " aT=" << veh.second.arrivalTime << " foeAT=" << foe.second.arrivalTime
350 << " aS=" << veh.first->getSpeed() << " foeS=" << foe.first->getSpeed()
351 << " aD=" << veh.second.dist << " foeD=" << foe.second.dist
352 << " aW=" << veh.first->getWaitingTime() << " foeW=" << foe.first->getWaitingTime()
353 << " aN=" << veh.first->getNumericalID() << " foeN=" << foe.first->getNumericalID()
354 << "\n";
355 }
356#endif
357 const bool yield = mustYield(veh, foe);
359 MSRailSignal::rivalVehicles().push_back(foe.first);
360 if (yield) {
361 MSRailSignal::priorityVehicles().push_back(foe.first);
362 }
363 }
364 return yield;
365 }
366 }
367 return false;
368}
369
370
371bool
373 if (std::find(myFoes.begin(), myFoes.end(), foe) != myFoes.end()) {
374 return true;
375 }
376 for (const MSDriveWay* sub : foe->mySubDriveWays) {
377 if (isFoeOrSubFoe(sub)) {
378 return true;
379 }
380 }
381 return false;
382}
383
384
385bool
387 if (foe.second.arrivalSpeedBraking == veh.second.arrivalSpeedBraking) {
388 if (foe.second.arrivalTime == veh.second.arrivalTime) {
389 if (foe.first->getSpeed() == veh.first->getSpeed()) {
390 if (foe.second.dist == veh.second.dist) {
391 if (foe.first->getWaitingTime() == veh.first->getWaitingTime()) {
392 return foe.first->getNumericalID() < veh.first->getNumericalID();
393 } else {
394 return foe.first->getWaitingTime() > veh.first->getWaitingTime();
395 }
396 } else {
397 return foe.second.dist < veh.second.dist;
398 }
399 } else {
400 return foe.first->getSpeed() > veh.first->getSpeed();
401 }
402 } else {
403 return foe.second.arrivalTime < veh.second.arrivalTime;
404 }
405 } else {
406 return foe.second.arrivalSpeedBraking > veh.second.arrivalSpeedBraking;
407 }
408}
409
410
411bool
412MSDriveWay::conflictLaneOccupied(bool store, const SUMOVehicle* ego) const {
413 for (const MSLane* lane : myConflictLanes) {
414 if (!lane->isEmpty()) {
415 std::string joinVehicle = "";
416 if (ego != nullptr && !MSGlobals::gUseMesoSim) {
418 if (stop != nullptr) {
419 joinVehicle = stop->join;
420 }
421 }
422#ifdef DEBUG_SIGNALSTATE
423 if (gDebugFlag4) {
424 std::cout << SIMTIME << " conflictLane " << lane->getID() << " occupied ego=" << Named::getIDSecure(ego) << " vehNumber=" << lane->getVehicleNumber() << "\n";
425 if (joinVehicle != "") {
426 std::cout << " joinVehicle=" << joinVehicle << " occupant=" << toString(lane->getVehiclesSecure()) << "\n";
427 lane->releaseVehicles();
428 }
429 }
430#endif
431 if (lane->getVehicleNumberWithPartials() == 1) {
432 MSVehicle* foe = lane->getLastAnyVehicle();
433 if (joinVehicle != "") {
434 if (foe->getID() == joinVehicle && foe->isStopped()) {
435#ifdef DEBUG_SIGNALSTATE
436 if (gDebugFlag4) {
437 std::cout << " ignore join-target '" << joinVehicle << "\n";
438 }
439#endif
440 continue;
441 }
442 }
443 if (ego != nullptr) {
444 if (foe == ego && std::find(myForward.begin(), myForward.end(), lane) == myForward.end()) {
445#ifdef DEBUG_SIGNALSTATE
446 if (gDebugFlag4) {
447 std::cout << " ignore ego as oncoming '" << ego->getID() << "\n";
448 }
449#endif
450 continue;
451 }
452 if (foe->isStopped() && foe->getNextStopParameter()->join == ego->getID()) {
453#ifdef DEBUG_SIGNALSTATE
454 if (gDebugFlag4) {
455 std::cout << " ignore " << foe->getID() << " for which ego is join-target\n";
456 }
457#endif
458 continue;
459 }
460 }
461 }
462 if (MSRailSignal::storeVehicles() && store) {
463 MSRailSignal::blockingVehicles().push_back(lane->getLastAnyVehicle());
464 }
465 return true;
466 }
467 }
468 return false;
469}
470
471
472bool
474 for (const MSDriveWay* foeDW : myFoes) {
475 if (foeDW->myOrigin != nullptr && foeDW->myOrigin->getApproaching().size() > 0) {
476#ifdef DEBUG_SIGNALSTATE
477 if (gDebugFlag4) {
478 std::cout << SIMTIME << " foeLink=" << foeDW->myOrigin->getDescription() << " approachedBy=" << foeDW->myOrigin->getApproaching().begin()->first->getID() << "\n";
479 }
480#endif
481 return true;
482 }
483 }
484 return false;
485}
486
487
488bool
489MSDriveWay::foeDriveWayOccupied(bool store, const SUMOVehicle* ego, MSEdgeVector& occupied) const {
490 for (const MSDriveWay* foeDW : myFoes) {
491 if (!foeDW->myTrains.empty()) {
492#ifdef DEBUG_SIGNALSTATE
493 if (gDebugFlag4 || DEBUG_COND_DW || DEBUG_HELPER(ego)) {
494 std::cout << SIMTIME << " " << getID() << " foeDriveWay " << foeDW->getID() << " occupied ego=" << Named::getIDSecure(ego) << " foeVeh=" << toString(foeDW->myTrains) << "\n";
495 }
496#endif
497 if (foeDW->myTrains.size() == 1) {
498 SUMOVehicle* foe = *foeDW->myTrains.begin();
499 if (foe == ego) {
500#ifdef DEBUG_SIGNALSTATE
501 if (gDebugFlag4 || DEBUG_HELPER(ego)) {
502 std::cout << " ignore ego as foe '" << Named::getIDSecure(ego) << "\n";
503 }
504#endif
505 continue;
506 }
507 if (hasJoin(ego, foe)) {
508 continue;
509 }
510 }
511 std::pair<bool, const MSDriveWay*> useSiding = canUseSiding(ego, foeDW);
512#ifdef DEBUG_SIGNALSTATE
513 if (gDebugFlag4 || DEBUG_COND_DW || DEBUG_HELPER(ego)) {
514 auto it = mySidings.find(foeDW);
515 int numSidings = 0;
516 if (it != mySidings.end()) {
517 numSidings = it->second.size();
518 }
519 std::cout << " useSiding=" << useSiding.first << " sidingFoe=" << Named::getIDSecure(useSiding.second) << " numSidings=" << numSidings << "\n";
520 }
521#endif
522 if (useSiding.first) {
523 continue;
524 } else {
525 if (MSRailSignal::storeVehicles() && store) {
526 for (SUMOVehicle* foe : foeDW->myTrains) {
527 MSRailSignal::blockingVehicles().push_back(foe);
528 }
529 MSRailSignal::blockingDriveWays().push_back(foeDW);
530 }
531 for (const SUMOVehicle* foe : foeDW->myTrains) {
532 occupied.push_back(const_cast<MSEdge*>(foe->getEdge()));
533 MSEdge* bidi = const_cast<MSEdge*>(foe->getEdge()->getBidiEdge());
534 if (bidi != nullptr) {
535 occupied.push_back(bidi);
536 }
538 }
539 if (ego != nullptr && MSGlobals::gTimeToTeleportRSDeadlock > 0
540 && (ego->getWaitingTime() > ego->getVehicleType().getCarFollowModel().getStartupDelay() || !ego->isOnRoad())) {
541 // if there is an occupied siding, it becomes part of the waitRelation
542 SUMOVehicle* foe = *(useSiding.second == nullptr ? foeDW : useSiding.second)->myTrains.begin();
543 const MSRailSignal* rs = myOrigin != nullptr ? dynamic_cast<const MSRailSignal*>(myOrigin->getTLLogic()) : nullptr;
545 }
546 return true;
547 }
548 } else if (foeDW != this && isDepartDriveway() && !foeDW->isDepartDriveway()) {
549 if (foeDW->myOrigin->getApproaching().size() > 0) {
550 Approaching foeA = foeDW->myOrigin->getClosest();
551 const SUMOVehicle* foe = foeA.first;
552 if (foeA.second.dist < foe->getBrakeGap(true)) {
553 MSRouteIterator firstIt = std::find(foe->getCurrentRouteEdge(), foe->getRoute().end(), foeDW->myRoute.front());
554 if (firstIt != foe->getRoute().end()) {
555 if (foeDW->match(firstIt, foe->getRoute().end())) {
556 bool useSiding = canUseSiding(ego, foeDW).first;
557#ifdef DEBUG_SIGNALSTATE
558 if (gDebugFlag4 || DEBUG_COND_DW || DEBUG_HELPER(ego)) {
559 std::cout << SIMTIME << " " << getID() << " blocked by " << foeDW->getID() << " (approached by " << foe->getID() << ") useSiding=" << useSiding << "\n";
560 }
561#endif
562 if (useSiding) {
563 //std::cout << SIMTIME << " " << getID() << " ego=" << ego->getID() << " foeDW=" << foeDW->getID() << " myFoes=" << toString(myFoes) << "\n";
564 continue;
565 } else {
566 return true;
567 }
568 }
569 }
570 }
571 }
572 }
573 }
574 for (const std::set<const MSDriveWay*>& dlFoes : myDeadlocks) {
575 bool allOccupied = true;
576 for (const MSDriveWay* dlFoe : dlFoes) {
577 if (dlFoe->myTrains.empty()) {
578 allOccupied = false;
579 //std::cout << SIMTIME << " " << getID() << " ego=" << Named::getIDSecure(ego) << " deadlockCheck clear " << dlFoe->getID() << "\n";
580 break;
581 }
582 }
583 if (allOccupied) {
584#ifdef DEBUG_SIGNALSTATE
585 if (gDebugFlag4 || DEBUG_COND_DW || DEBUG_HELPER(ego)) {
586 std::cout << SIMTIME << " " << getID() << " ego=" << Named::getIDSecure(ego) << " deadlockCheck " << joinNamedToString(dlFoes, " ") << "\n";
587 }
588#endif
589 for (const MSDriveWay* dlFoe : dlFoes) {
590 MSRailSignal::blockingDriveWays().push_back(dlFoe);
591 }
592 return true;
593 }
594 }
595 return false;
596}
597
598
599bool
601 if (ego != nullptr && !MSGlobals::gUseMesoSim) {
602 std::string joinVehicle = "";
604 if (stop != nullptr) {
605 joinVehicle = stop->join;
606 }
607 if (joinVehicle == "" && !ego->hasDeparted() && ego->getStops().size() > 1) {
608 // check one more stop
609 auto it = ego->getStops().begin();
610 std::advance(it, 1);
611 joinVehicle = it->pars.join;
612 }
613 if (joinVehicle != "") {
614#ifdef DEBUG_SIGNALSTATE
615 if (gDebugFlag4 || DEBUG_COND_DW) {
616 std::cout << " joinVehicle=" << joinVehicle << "\n";
617 }
618#endif
619 if (foe->getID() == joinVehicle && foe->isStopped()) {
620#ifdef DEBUG_SIGNALSTATE
621 if (gDebugFlag4 || DEBUG_COND_DW) {
622 std::cout << " ignore join-target '" << joinVehicle << "\n";
623 }
624#endif
625 return true;
626 }
627 }
628
629 if (foe->isStopped() && foe->getNextStopParameter()->join == ego->getID()) {
630#ifdef DEBUG_SIGNALSTATE
631 if (gDebugFlag4 || DEBUG_COND_DW) {
632 std::cout << " ignore " << foe->getID() << " for which ego is join-target\n";
633 }
634#endif
635 return true;
636 }
637 }
638 return false;
639}
640
641
642std::pair<bool, const MSDriveWay*>
643MSDriveWay::canUseSiding(const SUMOVehicle* ego, const MSDriveWay* foe, bool recurse) const {
644 auto it = mySidings.find(foe);
645 if (it != mySidings.end()) {
646 for (auto siding : it->second) {
647 // assume siding is usuable when computing state for unapproached signal (ego == nullptr)
648 if (ego == nullptr || siding.length >= ego->getLength()) {
649 // if the siding is already "reserved" by another vehicle we cannot use it here
650 const MSEdge* sidingEnd = myRoute[siding.end];
651 for (MSDriveWay* sidingApproach : myEndingDriveways[sidingEnd]) {
652 if (!sidingApproach->myTrains.empty()) {
653 // possibly the foe vehicle can use the other part of the siding
654 if (recurse) {
655 const SUMOVehicle* foeVeh = nullptr;
656 if (!foe->myTrains.empty()) {
657 foeVeh = *foe->myTrains.begin();
658 } else if (foe->myOrigin != nullptr && foe->myOrigin->getApproaching().size() > 0) {
659 foeVeh = foe->myOrigin->getClosest().first;
660 }
661 if (foeVeh == nullptr) {
662 WRITE_WARNINGF("Invalid call to canUseSiding dw=% foe=% ego=% time=%", getID(), foe->getID(), Named::getIDSecure(ego), time2string(SIMSTEP));
663 continue;
664 }
665 if (foe->canUseSiding(foeVeh, this, false).first) {
666 continue;
667 }
668 }
669 // possibly the foe vehicle
670 // @todo: in principle it might still be possible to continue if vehicle that approaches the siding can safely leave the situation
671#ifdef DEBUG_SIGNALSTATE
672 if (gDebugFlag4 || DEBUG_COND_DW || DEBUG_HELPER(ego)) {
673 std::cout << SIMTIME << " " << getID() << " ego=" << Named::getIDSecure(ego) << " foe=" << foe->getID()
674 << " foeVeh=" << toString(foe->myTrains)
675 << " sidingEnd=" << sidingEnd->getID() << " sidingApproach=" << sidingApproach->getID() << " approaching=" << toString(sidingApproach->myTrains) << "\n";
676 }
677#endif
678 return std::make_pair(false, sidingApproach);
679 }
680 }
681 //std::cout << SIMTIME << " " << getID() << " ego=" << Named::getIDSecure(ego) << " foe=" << foe->getID()
682 // << " foeVeh=" << toString(foe->myTrains)
683 // << " sidingEnd=" << sidingEnd->getID() << "usable\n";
684 return std::make_pair(true, nullptr);
685 }
686 }
687 }
688 return std::make_pair(false, nullptr);
689}
690
691bool
692MSDriveWay::overlap(const MSDriveWay& other) const {
693 for (int i = 0; i < myCoreSize; i++) {
694 for (int j = 0; j < other.myCoreSize; j++) {
695 const MSEdge* edge = myRoute[i];
696 const MSEdge* edge2 = other.myRoute[j];
697 if (edge->getToJunction() == edge2->getToJunction()
698 || edge->getToJunction() == edge2->getFromJunction()) {
699 // XXX might be rail_crossing with parallel tracks
700 return true;
701 }
702 }
703 }
704 return false;
705}
706
707
708bool
710 for (const MSLane* lane : myForward) {
711 for (const MSLane* lane2 : other.myForward) {
712 if (lane == lane2) {
713 return true;
714 }
715 }
716 for (const MSLane* lane2 : other.myBidi) {
717 if (lane == lane2) {
718 if (bidiBlockedBy(other)) {
719 // it's only a deadlock if both trains block symmetrically
720 return true;
721 }
722 }
723 }
724 for (const MSLane* lane2 : other.myBidiExtended) {
725 if (lane == lane2) {
726 if (bidiBlockedBy(other)) {
727 // it's only a deadlock if both trains block symmetrically
728 return true;
729 }
730 }
731 }
732 }
733 return false;
734}
735
736
737bool
739 for (const MSLane* lane : myForward) {
740 for (const MSLane* lane2 : other.myForward) {
741 if (lane->isNormal() && lane2->isNormal() && lane->getEdge().getToJunction() == lane2->getEdge().getToJunction()) {
742 return true;
743 }
744 }
745 }
746 return false;
747}
748
749
750bool
752 for (const MSLane* lane : myBidi) {
753 for (const MSLane* lane2 : other.myForward) {
754 if (lane == lane2) {
755 return true;
756 }
757 }
758 }
759 for (const MSLane* lane : myBidiExtended) {
760 for (const MSLane* lane2 : other.myForward) {
761 if (lane == lane2) {
762 if (overlap(other)) {
763 return true;
764 }
765 }
766 }
767 }
768 return false;
769}
770
771
772bool
774 const MSLane* end = other.myForward.back();
775 for (const MSLane* lane : myBidi) {
776 if (lane == end) {
777 return true;
778 }
779 }
780 for (const MSLane* lane : myBidiExtended) {
781 if (lane == end) {
782 if (overlap(other)) {
783 return true;
784 }
785 }
786 }
787 return false;
788}
789
790bool
791MSDriveWay::forwardRouteConflict(std::set<const MSEdge*> forward, const MSDriveWay& other, bool secondCheck) {
792 int i = 0;
793 for (const MSEdge* edge2 : other.myRoute) {
794 if (i == other.myCoreSize) {
795 return false;
796 }
797 i++;
798 if (edge2 == myForward.front()->getNextNormal() && !secondCheck) {
799 // foe should not pass from behind through our own forward section
800 return false;
801 }
802 if (forward.count(edge2->getBidiEdge()) != 0) {
803 return true;
804 }
805 }
806 return false;
807}
808
809void
815 if (myCoreSize != (int)myRoute.size()) {
816 od.writeAttr("core", myCoreSize);
817 }
818 od.openTag("forward");
820 od.closeTag();
821 if (!myIsSubDriveway) {
822 od.openTag("bidi");
824 if (myBidiExtended.size() > 0) {
825 od.lf();
826 od << " ";
827 od.writeAttr("deadlockCheck", toString(myBidiExtended));
828 }
829 od.closeTag();
830 od.openTag("flank");
832 od.closeTag();
833
834 od.openTag("conflictLinks");
835
836 std::vector<std::string> signals;
837 for (MSLink* link : myConflictLinks) {
838 signals.push_back(getTLLinkID(link));
839 }
840 od.writeAttr("signals", joinToStringSorting(signals, " "));
841 od.closeTag();
842
843 std::vector<std::string> foes;
844 for (MSDriveWay* dw : myFoes) {
845 foes.push_back(dw->myID);
846 }
847 if (foes.size() > 0) {
848 od.openTag("foes");
849 od.writeAttr("driveWays", joinToStringSorting(foes, " "));
850 od.closeTag();
851 }
852 for (auto item : mySidings) {
853 od.openTag("sidings");
854 od.writeAttr("foe", item.first->getID());
855 for (auto siding : item.second) {
856 od.openTag("siding");
857 od.writeAttr("start", myRoute[siding.start]->getID());
858 od.writeAttr("end", myRoute[siding.end]->getID());
859 od.writeAttr("length", siding.length);
860 od.closeTag();
861 }
862 od.closeTag();
863 }
864 for (auto item : myDeadlocks) {
865 od.openTag("deadlock");
866 od.writeAttr("foes", joinNamedToStringSorting(item, " "));
867 od.closeTag();
868 }
869 }
870 od.closeTag(); // driveWay
871
872 for (const MSDriveWay* sub : mySubDriveWays) {
873 sub->writeBlocks(od);
874 }
875#ifdef DRIVEWAY_SANITY_CHECK
876 std::set<MSDriveWay*> uFoes(myFoes.begin(), myFoes.end());
877 if (uFoes.size() != myFoes.size()) {
878 WRITE_WARNINGF("Duplicate foes in driveway '%'", getID());
879
880 }
881#endif
882}
883
884
885void
887 od.openTag(myIsSubDriveway ? "subDriveWay" : "driveWay");
889 for (const VehicleEvent& ve : myVehicleEvents) {
890 od.openTag(ve.isEntry ? "entry" : "exit");
891 od.writeAttr(SUMO_ATTR_ID, ve.id);
893 od.writeAttr("reason", Notifications.getString(ve.reason));
894 od.closeTag(); // event
895 }
896 od.closeTag(); // driveWay
897
898 for (const MSDriveWay* sub : mySubDriveWays) {
899 sub->writeBlockVehicles(od);
900 }
901}
902
903
904void
907 LaneVisitedMap& visited,
908 std::set<MSLink*>& flankSwitches) {
909 double length = 0;
910 bool seekForwardSignal = true;
911 bool seekBidiSwitch = true;
912 bool foundUnsafeSwitch = false;
913 MSLane* toLane = origin ? origin->getViaLaneOrLane() : (*next)->getLanes()[0];
914 const std::string warnID = origin ? "rail signal " + getClickableTLLinkID(origin) : "insertion lane '" + toLane->getID() + "'";
915#ifdef DEBUG_DRIVEWAY_BUILDROUTE
917 if (gDebugFlag4) std::cout << "buildRoute origin=" << warnID << " vehRoute=" << toString(ConstMSEdgeVector(next, end))
918 << " visited=" << formatVisitedMap(visited) << "\n";
919#endif
920 while (true) {
922 // typical block length in germany on main lines is 3-5km on branch lines up to 7km
923 // special branches that are used by one train exclusively could also be up to 20km in length
924 // minimum block size in germany is 37.5m (LZB)
925 // larger countries (USA, Russia) might see blocks beyond 20km)
926 if (seekForwardSignal && myBlockLengthWarnings.count(myRoute.front()) == 0) {
927 WRITE_WARNINGF("Block after % exceeds maximum length (stopped searching after edge '%' (length=%m).",
928 warnID, toLane->getEdge().getID(), length);
929 myBlockLengthWarnings.insert(myRoute.front());
930 }
931 myAbortedBuild = true;
932 // length exceeded
933#ifdef DEBUG_DRIVEWAY_BUILDROUTE
934 if (gDebugFlag4) {
935 std::cout << " abort: length=" << length << "\n";
936 }
937#endif
938 return;
939 }
940#ifdef DEBUG_DRIVEWAY_BUILDROUTE
941 if (gDebugFlag4) {
942 std::cout << " toLane=" << toLane->getID() << " visited=" << formatVisitedMap(visited) << "\n";
943 }
944#endif
945 const MSEdge* current = &toLane->getEdge();
946 if (current->isNormal()) {
947 myRoute.push_back(current);
948 if (next != end) {
949 next++;
950 }
951 }
952 appendMapIndex(visited, toLane);
953 length += toLane->getLength();
954 MSLane* bidi = toLane->getBidiLane();
955 if (seekForwardSignal) {
956 if (!foundUnsafeSwitch) {
957 myForward.push_back(toLane);
958 if (toLane->isNormal()) {
960 }
961 if (myForward.size() == 1) {
962 myLane = toLane;
965 s->addDetector(this, myLane->getIndex());
966 } else {
967 toLane->addMoveReminder(this, false);
968 }
969 }
970 }
971 } else if (bidi == nullptr) {
972 if (toLane->isInternal() && toLane->getIncomingLanes().front().viaLink->isTurnaround()) {
973#ifdef DEBUG_DRIVEWAY_BUILDROUTE
974 if (gDebugFlag4) {
975 std::cout << " continue bidiSearch beyond turnaround\n";
976 }
977#endif
978 } else {
979 seekBidiSwitch = false;
980#ifdef DEBUG_DRIVEWAY_BUILDROUTE
981 if (gDebugFlag4) {
982 std::cout << " noBidi, abort search for bidiSwitch\n";
983 }
984#endif
985 }
986 }
987 if (bidi != nullptr) {
988 if (!seekForwardSignal && !foundUnsafeSwitch && bidi->isNormal()) {
989 // look for switch that could protect from oncoming vehicles
990 for (const MSLink* const link : bidi->getLinkCont()) {
991 if (link->getDirection() == LinkDirection::TURN) {
992 continue;
993 }
994 if (!myBidi.empty() && link->getViaLaneOrLane() != myBidi.back()) {
995 myCoreSize = (int)myRoute.size() - 1;
996 MSLink* used = const_cast<MSLink*>(bidi->getLinkTo(myBidi.back()));
997#ifdef DEBUG_DRIVEWAY_BUILDROUTE
998 if (gDebugFlag4) {
999 std::cout << " found unsafe switch " << link->getDescription() << " (used=" << (used == nullptr ? "NULL" : used->getDescription()) << ")\n";
1000 }
1001#endif
1002 // trains along our route beyond this switch might create deadlock
1003 foundUnsafeSwitch = true;
1004 // the switch itself must still be guarded to ensure safety
1005 if (used != nullptr) {
1006 // possibly nullptr if there was an intermediate section of unidirectional edges
1007 flankSwitches.insert(used);
1008 }
1009 break;
1010 }
1011 }
1012 }
1013 if (foundUnsafeSwitch) {
1014 myBidiExtended.push_back(bidi);
1015 } else {
1016 myBidi.push_back(bidi);
1017 }
1018 }
1019 const std::vector<MSLink*>& links = toLane->getLinkCont();
1020 toLane = nullptr;
1021 for (const MSLink* const link : links) {
1022 if ((next != end && &link->getLane()->getEdge() == *next)
1023 && isRailwayOrShared(link->getViaLaneOrLane()->getPermissions())) {
1024 toLane = link->getViaLaneOrLane();
1025 if (link->getTLLogic() != nullptr && link->getTLIndex() >= 0 && link->getTLLogic()->getLogicType() == TrafficLightType::RAIL_SIGNAL) {
1026 if (link == origin) {
1027 if (seekForwardSignal) {
1028 WRITE_WARNINGF(TL("Found circular block after % (% edges, length %)"), warnID, toString(myRoute.size()), toString(length));
1029 }
1030 //std::cout << getClickableTLLinkID(origin) << " circularBlock2=" << toString(myRoute) << "\n";
1031 myAbortedBuild = true;
1032#ifdef DEBUG_DRIVEWAY_BUILDROUTE
1033 if (gDebugFlag4) {
1034 std::cout << " abort: found circle\n";
1035 }
1036#endif
1037 return;
1038 }
1039 seekForwardSignal = false;
1040 myFoundSignal = true;
1041 seekBidiSwitch = bidi != nullptr;
1042#ifdef DEBUG_DRIVEWAY_BUILDROUTE
1043 if (gDebugFlag4) {
1044 std::cout << " found forwardSignal " << link->getTLLogic()->getID() << " seekBidiSwitch=" << seekBidiSwitch << "\n";
1045 }
1046#endif
1047 }
1048 //if (links.size() > 1 && !foundUnsafeSwitch) {
1049 if (isSwitch(link)) {
1050 // switch on driveway
1051 //std::cout << "mySwitchDriveWays " << getID() << " link=" << link->getDescription() << "\n";
1052 mySwitchDriveWays[link].push_back(this);
1053 }
1054 if (link->getLane()->getBidiLane() != nullptr && &link->getLane()->getEdge() == current->getBidiEdge()) {
1055 // reversal on driveway
1056 myReversalDriveWays[current].push_back(this);
1057 myReversals.push_back(current);
1058 }
1059 break;
1060 }
1061 }
1062 if (toLane == nullptr) {
1063 if (next != end) {
1064 // no connection found, jump to next route edge
1065 toLane = (*next)->getLanes()[0];
1066#ifdef DEBUG_DRIVEWAY_BUILDROUTE
1067 if (gDebugFlag4) {
1068 std::cout << " abort: turn-around or jump\n";
1069 }
1070#endif
1071 myFoundJump = true;
1072 return;
1073 } else {
1074#ifdef DEBUG_DRIVEWAY_BUILDROUTE
1075 if (gDebugFlag4) {
1076 std::cout << " abort: no next lane available\n";
1077 }
1078#endif
1079 myTerminateRoute = true;
1080 return;
1081 }
1082 }
1083 }
1084 myBidiEnded = !seekBidiSwitch;
1085#ifdef DEBUG_DRIVEWAY_BUILDROUTE
1086 if (gDebugFlag4) {
1087 std::cout << " normalEnd myBidiEnded=" << myBidiEnded << "\n";
1088 }
1089#endif
1090}
1091
1092
1093bool
1095 for (const MSLink* other : link->getLaneBefore()->getNormalPredecessorLane()->getLinkCont()) {
1096 if (other->getLane() != link->getLane() && !other->isTurnaround()) {
1097 return true;
1098 }
1099 }
1100 for (auto ili : link->getLane()->getIncomingLanes()) {
1101 if (ili.viaLink != link && !ili.viaLink->isTurnaround()) {
1102 return true;
1103 }
1104 }
1105 const MSLane* bidi = link->getLane()->getBidiLane();
1106 if (bidi != nullptr) {
1107 for (const MSLink* other : bidi->getLinkCont()) {
1108 if (other->getLane() != link->getLaneBefore()->getNormalPredecessorLane()->getBidiLane() && !other->isTurnaround()) {
1109 return true;
1110 }
1111 }
1112 }
1113 return false;
1114}
1115
1116
1117void
1118MSDriveWay::checkFlanks(const MSLink* originLink, const std::vector<const MSLane*>& lanes, const LaneVisitedMap& visited, bool allFoes, std::set<MSLink*>& flankSwitches) const {
1119#ifdef DEBUG_CHECK_FLANKS
1120 std::cout << " checkFlanks lanes=" << toString(lanes) << " allFoes=" << allFoes << "\n";
1121#endif
1122 const MSLink* reverseOriginLink = originLink != nullptr && originLink->getLane()->getBidiLane() != nullptr && originLink->getLaneBefore()->getBidiLane() != nullptr
1123 ? originLink->getLane()->getBidiLane()->getLinkTo(originLink->getLaneBefore()->getBidiLane())
1124 : nullptr;
1125 //std::cout << " originLink=" << originLink->getDescription() << "\n";
1126 if (reverseOriginLink != nullptr) {
1127 reverseOriginLink = reverseOriginLink->getCorrespondingExitLink();
1128 //std::cout << " reverseOriginLink=" << reverseOriginLink->getDescription() << "\n";
1129 }
1130 for (int i = 0; i < (int)lanes.size(); i++) {
1131 const MSLane* lane = lanes[i];
1132 const MSLane* prev = i > 0 ? lanes[i - 1] : nullptr;
1133 const MSLane* next = i + 1 < (int)lanes.size() ? lanes[i + 1] : nullptr;
1134 if (lane->isInternal()) {
1135 continue;
1136 }
1137 for (auto ili : lane->getIncomingLanes()) {
1138 if (ili.viaLink == originLink
1139 || ili.viaLink == reverseOriginLink
1140 || ili.viaLink->getDirection() == LinkDirection::TURN
1141 || ili.viaLink->getDirection() == LinkDirection::TURN_LEFTHAND) {
1142 continue;
1143 }
1144 if (ili.lane != prev && ili.lane != next) {
1145#ifdef DEBUG_CHECK_FLANKS
1146 std::cout << " add flankSwitch junction=" << ili.viaLink->getJunction()->getID() << " index=" << ili.viaLink->getIndex() << " iLane=" << ili.lane->getID() << " prev=" << Named::getIDSecure(prev) << " targetLane=" << lane->getID() << " next=" << Named::getIDSecure(next) << "\n";
1147#endif
1148 flankSwitches.insert(ili.viaLink);
1149 } else if (allFoes) {
1150 // link is part of the driveway, find foes that cross the driveway without entering
1151 checkCrossingFlanks(ili.viaLink, visited, flankSwitches);
1152 }
1153 }
1154 }
1155}
1156
1157
1158void
1159MSDriveWay::checkCrossingFlanks(MSLink* dwLink, const LaneVisitedMap& visited, std::set<MSLink*>& flankSwitches) const {
1160#ifdef DEBUG_CHECK_FLANKS
1161 std::cout << " checkCrossingFlanks dwLink=" << dwLink->getDescription() << " visited=" << formatVisitedMap(visited) << "\n";
1162#endif
1163 const MSJunction* junction = dwLink->getJunction();
1164 if (junction == nullptr) {
1165 return; // unregulated junction;
1166 }
1167 const MSJunctionLogic* logic = junction->getLogic();
1168 if (logic == nullptr) {
1169 return; // unregulated junction;
1170 }
1171 for (const MSEdge* in : junction->getIncoming()) {
1172 if (in->isInternal()) {
1173 continue;
1174 }
1175 for (MSLane* inLane : in->getLanes()) {
1176 const MSLane* inBidi = inLane->getBidiLane();
1177 if (isRailwayOrShared(inLane->getPermissions()) && visited.count(inLane) == 0 && (inBidi == nullptr || visited.count(inBidi) == 0)) {
1178 for (MSLink* link : inLane->getLinkCont()) {
1179 if (link->getIndex() >= 0 && logic->getFoesFor(dwLink->getIndex()).test(link->getIndex())
1180 && visited.count(link->getLane()) == 0) {
1181#ifdef DEBUG_CHECK_FLANKS
1182 std::cout << " add crossing flankSwitch junction=" << junction->getID() << " index=" << link->getIndex() << "\n";
1183#endif
1184 if (link->getViaLane() == nullptr) {
1185 flankSwitches.insert(link);
1186 } else {
1187 flankSwitches.insert(link->getViaLane()->getLinkCont().front());
1188 }
1189 }
1190 }
1191 }
1192 }
1193 }
1194}
1195
1196void
1197MSDriveWay::findFlankProtection(MSLink* link, MSLink* origLink, std::vector<const MSLane*>& flank) {
1198#ifdef DEBUG_CHECK_FLANKS
1199 std::cout << " findFlankProtection link=" << link->getDescription() << " origLink=" << origLink->getDescription() << "\n";
1200#endif
1201 if (link->getCorrespondingEntryLink()->getTLLogic() != nullptr && link->getJunction()->getType() == SumoXMLNodeType::RAIL_SIGNAL) {
1202 MSLink* entry = const_cast<MSLink*>(link->getCorrespondingEntryLink());
1203 // guarded by signal
1204#ifdef DEBUG_CHECK_FLANKS
1205 std::cout << " flank guarded by " << entry->getTLLogic()->getID() << "\n";
1206#endif
1207 // @note, technically it's enough to collect links from foe driveways
1208 // but this also adds "unused" conflict links which may aid comprehension
1209 myConflictLinks.push_back(entry);
1210 addFoes(entry);
1211 } else {
1212 const MSLane* lane = link->getLaneBefore();
1213 std::vector<MSLink*> predLinks;
1214 for (auto ili : lane->getIncomingLanes()) {
1215 if (!ili.viaLink->isTurnaround()) {
1216 predLinks.push_back(ili.viaLink);
1217 }
1218 }
1219 if (predLinks.size() > 1) {
1220 // this is a switch
1221#ifdef DEBUG_ADD_FOES
1222 std::cout << " predecessors of " << link->getDescription() << " isSwitch\n";
1223#endif
1224 for (MSLink* pred : predLinks) {
1225 addSwitchFoes(pred);
1226 }
1227 } else if (predLinks.size() == 1) {
1228 if (isSwitch(link)) {
1229 addSwitchFoes(link);
1230 } else {
1231 // continue upstream via single predecessor
1232 findFlankProtection(predLinks.front(), origLink, flank);
1233 }
1234 }
1235 // check for insertions
1236 if (myDepartureDriveways.count(&lane->getEdge()) != 0) {
1237 for (MSDriveWay* foe : myDepartureDriveways[&lane->getEdge()]) {
1238 if (flankConflict(*foe) || crossingConflict(*foe)) {
1239#ifdef DEBUG_ADD_FOES
1240 std::cout << " foe " << foe->getID() << " departs on flank=" << lane->getID() << "\n";
1241#endif
1242 myFoes.push_back(foe);
1243 } else {
1244#ifdef DEBUG_ADD_FOES
1245 std::cout << " cand foe " << foe->getID() << " departs on flank=" << lane->getID() << " rejected\n";
1246#endif
1247 }
1248 }
1249 }
1250 }
1251}
1252
1253
1254void
1256 auto it = mySwitchDriveWays.find(link);
1257 if (it != mySwitchDriveWays.end()) {
1258#ifdef DEBUG_ADD_FOES
1259 std::cout << " driveway " << myID << " addSwitchFoes for link " << link->getDescription() << "\n";
1260#endif
1261 for (MSDriveWay* foe : it->second) {
1262 if (foe != this && (flankConflict(*foe) || foe->flankConflict(*this) || crossingConflict(*foe) || foe->crossingConflict(*this))) {
1263#ifdef DEBUG_ADD_FOES
1264 std::cout << " foe=" << foe->myID
1265 << " fc1=" << flankConflict(*foe) << " fc2=" << foe->flankConflict(*this)
1266 << " cc1=" << crossingConflict(*foe) << " cc2=" << foe->crossingConflict(*this) << "\n";
1267#endif
1268 myFoes.push_back(foe);
1269 } else {
1270#ifdef DEBUG_ADD_FOES
1271 std::cout << " cand=" << foe->myID << "\n";
1272#endif
1273 }
1274 }
1275 }
1276}
1277
1278
1280MSDriveWay::buildDriveWay(const std::string& id, const MSLink* link, MSRouteIterator first, MSRouteIterator end) {
1281 // collect lanes and links that are relevant for setting this signal for the current driveWay
1282 // For each driveway we collect
1283 // - conflictLanes (signal must be red if any conflict lane is occupied)
1284 // - conflictLinks (signal must be red if any conflict link is approached by a vehicle
1285 // - that cannot break in time (arrivalSpeedBraking > 0)
1286 // - approached by a vehicle with higher switching priority (see #3941)
1287 // These objects are construct in steps:
1288 //
1289 // forwardBlock
1290 // - search forward recursive from outgoing lane until controlled railSignal link found
1291 // -> add all found lanes to conflictLanes
1292 //
1293 // bidiBlock (if any forwardBlock edge has bidi edge)
1294 // - search bidi backward recursive until first switch
1295 // - from switch search backward recursive all other incoming until controlled rail signal link
1296 // -> add final links to conflictLinks
1297 //
1298 // flanks
1299 // - search backward recursive from flanking switches
1300 // until controlled railSignal link or protecting switch is found
1301 // -> add all found lanes to conflictLanes
1302 // -> add final links to conflictLinks
1303 MSDriveWay* dw = new MSDriveWay(link, id);
1304 LaneVisitedMap visited;
1305 std::vector<const MSLane*> before;
1306 MSLane* fromBidi = nullptr;
1307 if (link != nullptr) {
1308 appendMapIndex(visited, link->getLaneBefore());
1309 fromBidi = link->getLaneBefore()->getBidiLane();
1310 }
1311 std::set<MSLink*> flankSwitches; // list of switches that threaten the driveway and for which protection must be found
1312
1313 if (fromBidi != nullptr) {
1314 before.push_back(fromBidi);
1315 }
1316 dw->buildRoute(link, first, end, visited, flankSwitches);
1317 dw->myCoreSize = (int)dw->myRoute.size();
1318 dw->checkFlanks(link, dw->myForward, visited, true, flankSwitches);
1319 dw->checkFlanks(link, dw->myBidi, visited, false, flankSwitches);
1320 dw->checkFlanks(link, before, visited, true, flankSwitches);
1321 for (MSLink* fsLink : flankSwitches) {
1322#ifdef DEBUG_ADD_FOES
1323 if (DEBUG_COND_DW) {
1324 std::cout << " fsLink=" << fsLink->getDescription() << "\n";
1325 }
1326#endif
1327 dw->findFlankProtection(fsLink, fsLink, dw->myFlank);
1328 }
1329 std::set<MSLink*> flankSwitchesBidiExtended;
1330 dw->checkFlanks(link, dw->myBidiExtended, visited, false, flankSwitchesBidiExtended);
1331 for (MSLink* const flink : flankSwitchesBidiExtended) {
1332#ifdef DEBUG_ADD_FOES
1333 if (DEBUG_COND_DW) {
1334 std::cout << " fsLinkExtended=" << flink->getDescription() << "\n";
1335 }
1336#endif
1337 dw->findFlankProtection(flink, flink, dw->myBidiExtended);
1338 }
1339 MSRailSignal* rs = link ? const_cast<MSRailSignal*>(static_cast<const MSRailSignal*>(link->getTLLogic())) : nullptr;
1340 const bool movingBlock = (rs && rs->isMovingBlock()) || (!rs &&
1341 (OptionsCont::getOptions().getBool("railsignal-moving-block")
1342 || MSRailSignalControl::isMovingBlock((*first)->getPermissions())));
1343#ifdef DEBUG_BUILD_DRIVEWAY
1344 if (DEBUG_COND_DW) {
1345 std::cout << SIMTIME << " buildDriveWay " << dw->myID << " link=" << (link == nullptr ? "NULL" : link->getDescription())
1346 << "\n route=" << toString(dw->myRoute)
1347 << "\n forward=" << toString(dw->myForward)
1348 << "\n bidi=" << toString(dw->myBidi)
1349 << "\n bidiEx=" << toString(dw->myBidiExtended)
1350 << "\n flank=" << toString(dw->myFlank)
1351 << "\n flankSwitch=" << MSRailSignal::describeLinks(std::vector<MSLink*>(flankSwitches.begin(), flankSwitches.end()))
1352 << "\n coreSize=" << dw->myCoreSize
1353 << "\n";
1354 }
1355#endif
1356 if (!rs || !rs->isMovingBlock()) {
1357 dw->myConflictLanes.insert(dw->myConflictLanes.end(), dw->myForward.begin(), dw->myForward.end());
1358 }
1359 dw->myConflictLanes.insert(dw->myConflictLanes.end(), dw->myBidi.begin(), dw->myBidi.end());
1360 dw->myConflictLanes.insert(dw->myConflictLanes.end(), dw->myFlank.begin(), dw->myFlank.end());
1361 dw->addBidiFoes(rs, false);
1362 dw->addBidiFoes(rs, true);
1363 // add driveways that start on the same signal / lane
1364 dw->addParallelFoes(link, *first);
1365 // add driveways that reverse along this driveways route
1366 dw->addReversalFoes(movingBlock);
1367 // make foes unique and symmetrical
1368 std::set<MSDriveWay*, ComparatorNumericalIdLess> uniqueFoes(dw->myFoes.begin(), dw->myFoes.end());
1369 dw->myFoes.clear();
1370 // check for self-intersecting forward-section in movingBlock mode
1371 if (movingBlock && uniqueFoes.count(dw) == 0) {
1372 std::set<const MSJunction*> forwardJunctions;
1373 for (const MSLane* fw : dw->myForward) {
1374 if (fw->isNormal()) {
1375 const MSJunction* fwTo = fw->getEdge().getToJunction();
1376 if (forwardJunctions.count(fwTo) == 1) {
1377 dw->myFoes.push_back(dw);
1378#ifdef DEBUG_ADD_FOES
1379 if (DEBUG_COND_DW) {
1380 std::cout << " self-intersecting movingBlock for dw=" << dw->getID() << "\n";
1381 }
1382#endif
1383 break;
1384 }
1385 forwardJunctions.insert(fwTo);
1386 }
1387 }
1388 }
1389 std::set<MSLink*> uniqueCLink(dw->myConflictLinks.begin(), dw->myConflictLinks.end());
1390 const MSEdge* lastEdge = &dw->myForward.back()->getEdge();
1391 for (MSDriveWay* foe : uniqueFoes) {
1392 const MSEdge* foeLastEdge = &foe->myForward.back()->getEdge();
1393 const bool sameLast = foeLastEdge == lastEdge;
1394 if (sameLast && !movingBlock) {
1395 dw->myFoes.push_back(foe);
1396 if (foe != dw) {
1397 foe->myFoes.push_back(dw);
1398 }
1399 } else {
1400 if (foe->bidiBlockedByEnd(*dw)) {
1401#ifdef DEBUG_ADD_FOES
1402 if (DEBUG_COND_DW) {
1403 std::cout << " setting " << dw->getID() << " as foe of " << foe->getID() << "\n";
1404 }
1405#endif
1406 foe->myFoes.push_back(dw);
1407 foe->addSidings(dw);
1408 } else {
1409 dw->buildSubFoe(foe, movingBlock);
1410 }
1411 if (foe != dw) { // check for movingBlock
1412 if (dw->bidiBlockedByEnd(*foe)) {
1413#ifdef DEBUG_ADD_FOES
1414 if (DEBUG_COND_DW) {
1415 std::cout << " addFoeCheckSiding " << foe->getID() << "\n";
1416 }
1417#endif
1418 dw->myFoes.push_back(foe);
1419 dw->addSidings(foe);
1420 } else {
1421 foe->buildSubFoe(dw, movingBlock);
1422 }
1423 }
1424 }
1425 if (link) {
1426 foe->addConflictLink(link);
1427 }
1428 // ignore links that have the same start junction
1429 if (foe->myRoute.front()->getFromJunction() != dw->myRoute.front()->getFromJunction()) {
1430 for (auto ili : foe->myForward.front()->getIncomingLanes()) {
1431 if (ili.viaLink->getTLLogic() != nullptr) {
1432 // ignore links that originate on myBidi
1433 const MSLane* origin = ili.viaLink->getLaneBefore();
1434 if (std::find(dw->myBidi.begin(), dw->myBidi.end(), origin) == dw->myBidi.end()) {
1435 uniqueCLink.insert(ili.viaLink);
1436 }
1437 }
1438 }
1439 }
1440 }
1441 dw->myConflictLinks.clear();
1442 dw->myConflictLinks.insert(dw->myConflictLinks.begin(), uniqueCLink.begin(), uniqueCLink.end());
1443 myEndingDriveways[lastEdge].push_back(dw);
1444 if (!movingBlock) {
1445 // every driveway is it's own foe (also all driveways that depart in the same block)
1446 for (MSDriveWay* sameEnd : myEndingDriveways[lastEdge]) {
1447 if (uniqueFoes.count(sameEnd) == 0) {
1448 dw->myFoes.push_back(sameEnd);
1449 if (sameEnd != dw) {
1450 sameEnd->myFoes.push_back(dw);
1451 }
1452 }
1453 }
1454 }
1455#ifdef DEBUG_BUILD_DRIVEWAY
1456 if (DEBUG_COND_DW) {
1457 std::cout << dw->myID << " finalFoes " << toString(dw->myFoes) << "\n";
1458 }
1459#endif
1460 return dw;
1461}
1462
1463std::string
1465 return link->getTLLogic()->getID() + "_" + toString(link->getTLIndex());
1466}
1467
1468std::string
1470 return link->getJunction()->getID() + "_" + toString(link->getIndex());
1471}
1472
1473std::string
1475 return "junction '" + link->getTLLogic()->getID() + "', link " + toString(link->getTLIndex());
1476}
1477
1478std::string
1480 UNUSED_PARAMETER(visited);
1481 /*
1482 std::vector<const MSLane*> lanes(visited.size(), nullptr);
1483 for (auto item : visited) {
1484 lanes[item.second] = item.first;
1485 }
1486 for (auto it = lanes.begin(); it != lanes.end();) {
1487 if (*it == nullptr) {
1488 it = lanes.erase(it);
1489 } else {
1490 it++;
1491 }
1492 }
1493 return toString(lanes);
1494 */
1495 return "dummy";
1496}
1497
1498
1499void
1501 // avoid undefined behavior from evaluation order
1502 const int tmp = (int)map.size();
1503 map[lane] = tmp;
1504}
1505
1506bool
1508 // @todo optimize: it is sufficient to check for specific edges (after each switch)
1509 auto itRoute = firstIt;
1510 auto itDwRoute = myRoute.begin();
1511 bool match = true;
1512 while (itRoute != endIt && itDwRoute != myRoute.end()) {
1513 if (*itRoute != *itDwRoute) {
1514 match = false;
1515#ifdef DEBUG_MATCH
1516 std::cout << " check dw=" << getID() << " match failed at vehEdge=" << (*itRoute)->getID() << " dwEdge=" << (*itDwRoute)->getID() << "\n";
1517#endif
1518 break;
1519 }
1520 itRoute++;
1521 itDwRoute++;
1522 }
1523 // if the vehicle arrives before the end of this driveway,
1524 // we'd rather build a new driveway to avoid superfluous restrictions
1525 if (match && itDwRoute == myRoute.end()
1526 && (itRoute == endIt || myAbortedBuild || myBidiEnded || myFoundJump || myIsSubDriveway)) {
1527 //std::cout << " using dw=" << "\n";
1528 if (itRoute != endIt) {
1529 // check whether the current route requires an extended driveway
1530 const MSEdge* next = *itRoute;
1531 const MSEdge* prev = myRoute.back();
1532 if (myFoundJump && prev->getBidiEdge() != next && prev->getBidiEdge() != nullptr
1533 && prev->isConnectedTo(*next, (SUMOVehicleClass)(SVC_RAIL_CLASSES & prev->getPermissions()))) {
1534#ifdef DEBUG_MATCH
1535 std::cout << " check dw=" << getID() << " prev=" << prev->getID() << " next=" << next->getID() << "\n";
1536#endif
1537 return false;
1538 }
1539 if (!myFoundJump && prev->getBidiEdge() == next && prev == &myForward.back()->getEdge()) {
1540 assert(myIsSubDriveway || myBidiEnded);
1541 // must not leave driveway via reversal
1542#ifdef DEBUG_MATCH
1543 std::cout << getID() << " back=" << myForward.back()->getID() << " noMatch route " << toString(ConstMSEdgeVector(firstIt, endIt)) << "\n";
1544#endif
1545 return false;
1546 }
1547 }
1548 return true;
1549 }
1550 return false;
1551}
1552
1553void
1555#ifdef DEBUG_ADD_FOES
1556 std::cout << "driveway " << myID << " addFoes for link " << link->getDescription() << "\n";
1557#endif
1558 const MSRailSignal* rs = dynamic_cast<const MSRailSignal*>(link->getTLLogic());
1559 if (rs != nullptr) {
1560 for (MSDriveWay* foe : rs->retrieveDriveWays(link->getTLIndex())) {
1561#ifdef DEBUG_ADD_FOES
1562 std::cout << " cand foe=" << foe->myID << " fc1=" << flankConflict(*foe) << " fc2=" << foe->flankConflict(*this) << " cc1=" << crossingConflict(*foe) << " cc2=" << foe->crossingConflict(*this) << "\n";
1563#endif
1564 if (foe != this && (flankConflict(*foe) || foe->flankConflict(*this) || crossingConflict(*foe) || foe->crossingConflict(*this))) {
1565#ifdef DEBUG_ADD_FOES
1566 std::cout << " foe=" << foe->myID << "\n";
1567#endif
1568 myFoes.push_back(foe);
1569 }
1570 }
1571 }
1572}
1573
1574
1575void
1576MSDriveWay::addBidiFoes(const MSRailSignal* ownSignal, bool extended) {
1577#ifdef DEBUG_ADD_FOES
1578 std::cout << "driveway " << myID << " addBidiFoes extended=" << extended << "\n";
1579#endif
1580 const std::vector<const MSLane*>& bidiLanes = extended ? myBidiExtended : myBidi;
1581 for (const MSLane* bidi : bidiLanes) {
1582 for (auto ili : bidi->getIncomingLanes()) {
1583 const MSRailSignal* rs = dynamic_cast<const MSRailSignal*>(ili.viaLink->getTLLogic());
1584 if (rs != nullptr && rs != ownSignal &&
1585 std::find(bidiLanes.begin(), bidiLanes.end(), ili.lane) != bidiLanes.end()) {
1586 addFoes(ili.viaLink);
1587 }
1588 }
1589 const MSEdge* bidiEdge = &bidi->getEdge();
1590 if (myDepartureDriveways.count(bidiEdge) != 0) {
1591 for (MSDriveWay* foe : myDepartureDriveways[bidiEdge]) {
1592 if (flankConflict(*foe)) {
1593#ifdef DEBUG_ADD_FOES
1594 std::cout << " foe " << foe->getID() << " departs on bidi=" << bidiEdge->getID() << "\n";
1595#endif
1596 myFoes.push_back(foe);
1597 } else {
1598#ifdef DEBUG_ADD_FOES
1599 std::cout << " cand foe " << foe->getID() << " departs on bidi=" << bidiEdge->getID() << " rejected\n";
1600#endif
1601 }
1602 }
1603 }
1604 if (myDepartureDrivewaysEnds.count(bidiEdge) != 0) {
1605 for (MSDriveWay* foe : myDepartureDrivewaysEnds[bidiEdge]) {
1606 if (flankConflict(*foe)) {
1607#ifdef DEBUG_ADD_FOES
1608 std::cout << " foe " << foe->getID() << " ends on bidi=" << bidiEdge->getID() << "\n";
1609#endif
1610 myFoes.push_back(foe);
1611 } else {
1612#ifdef DEBUG_ADD_FOES
1613 std::cout << " cand foe " << foe->getID() << " ends on bidi=" << bidiEdge->getID() << " rejected\n";
1614#endif
1615 }
1616 }
1617 }
1618 }
1619}
1620
1621
1622void
1623MSDriveWay::addParallelFoes(const MSLink* link, const MSEdge* first) {
1624#ifdef DEBUG_ADD_FOES
1625 std::cout << "driveway " << myID << " addParallelFoes\n";
1626#endif
1627 if (link) {
1628 addFoes(link);
1629 } else {
1630 auto it = myDepartureDriveways.find(first);
1631 if (it != myDepartureDriveways.end()) {
1632 for (MSDriveWay* foe : it->second) {
1633#ifdef DEBUG_ADD_FOES
1634 std::cout << " foe " << foe->getID() << " departs on first=" << first->getID() << "\n";
1635#endif
1636 myFoes.push_back(foe);
1637 }
1638 }
1639 }
1640}
1641
1642
1643void
1645#ifdef DEBUG_ADD_FOES
1646 std::cout << "driveway " << myID << " addReversalFoes\n";
1647#endif
1648 std::set<const MSEdge*> forward;
1649 for (const MSLane* lane : myForward) {
1650 if (lane->isNormal()) {
1651 forward.insert(&lane->getEdge());
1652 }
1653 }
1654 int i = 0;
1655 for (const MSEdge* e : myRoute) {
1656 if (forward.count(e) != 0 && !movingBlock) {
1657 // reversals in our own forward can be ignored because each driveway
1658 // is automatically a foe of itself by default
1659 continue;
1660 }
1661 if (i == myCoreSize) {
1662 break;
1663 }
1664 i++;
1665 auto it = myReversalDriveWays.find(e);
1666 if (it != myReversalDriveWays.end()) {
1667 for (MSDriveWay* foe : it->second) {
1668 // check whether the foe reverses into our own forward section
1669 // (it might reverse again or disappear via arrival)
1670#ifdef DEBUG_ADD_FOES
1671 //std::cout << " candidate foe " << foe->getID() << " reverses on edge=" << e->getID() << " forward=" << joinNamedToString(forward, " ") << " foeRoute=" << toString(foe->myRoute) << "\n";
1672#endif
1673 if (forwardRouteConflict(forward, *foe)) {
1674 std::set<const MSEdge*> foeForward;
1675 for (const MSLane* lane : foe->myForward) {
1676 if (lane->isNormal()) {
1677 foeForward.insert(&lane->getEdge());
1678 if (lane->getBidiLane() != nullptr) {
1679 foeForward.insert(lane->getEdge().getBidiEdge());
1680 }
1681 }
1682 }
1683#ifdef DEBUG_ADD_FOES
1684 std::cout << " reversal cand=" << foe->getID() << " foeForward " << toString(foeForward) << "\n";
1685#endif
1686 if (foe->forwardRouteConflict(foeForward, *this, true)) {
1687#ifdef DEBUG_ADD_FOES
1688 std::cout << " foe " << foe->getID() << " reverses on edge=" << e->getID() << "\n";
1689#endif
1690 myFoes.push_back(foe);
1691 }
1692 } else if (movingBlock && foe == this) {
1693#ifdef DEBUG_ADD_FOES
1694 std::cout << " dw " << getID() << " reverses on forward edge=" << e->getID() << " (movingBlock)\n";
1695#endif
1696 myFoes.push_back(foe);
1697 }
1698 }
1699 }
1700 }
1701}
1702
1703
1704bool
1705MSDriveWay::buildSubFoe(MSDriveWay* foe, bool movingBlock) {
1706 // Subdriveways (Teilfahrstraße) model the resolution of a driving conflict
1707 // before a vehicle has left the driveway. This is possible when the driveway diverges from the foe
1708 // driveway at an earlier point (switch or crossing).
1709 //
1710 // We already know that the last edge of this driveway doesn't impact the foe (unless the driveway ends within the block).
1711 // Remove further edges from the end of the driveway (myForward) until the point of conflict is found.
1712 //
1713 // For movingBlock the logic is changed:
1714 // We remove the conflict-free part as before but then keep removing the conflict part until another non-conconflit part is found
1715 if (myForward.size() < foe->myForward.size() &&
1716 myForward == std::vector<const MSLane*>(foe->myForward.begin(), foe->myForward.begin() + myForward.size())) {
1717#ifdef DEBUG_BUILD_SUBDRIVEWAY
1718 std::cout << SIMTIME << " buildSubFoe dw=" << getID() << " is subpart of foe=" << foe->getID() << "\n";
1719#endif
1720 foe->myFoes.push_back(this);
1721 return true;
1722 }
1723 int subLast = (int)myForward.size() - 2;
1724 if (movingBlock && myForward.back() == foe->myForward.back()) {
1725 subLast++;
1726 }
1727#ifdef DEBUG_BUILD_SUBDRIVEWAY
1728 if (subLast < 0) {
1729 std::cout << " " << getID() << " cannot build subDriveWay for foe " << foe->getID() << " because myForward has only a single lane\n";
1730 }
1731#endif
1732 bool foundConflict = false;
1733 bool flankC = false;
1734 bool zipperC = false;
1735 while (subLast >= 0) {
1736 const MSLane* lane = myForward[subLast];
1737 MSDriveWay tmp(myOrigin, "tmp", true);
1738 tmp.myForward.push_back(lane);
1739 flankC = tmp.flankConflict(*foe);
1740 const bool bidiConflict = std::find(foe->myBidi.begin(), foe->myBidi.end(), lane) != foe->myBidi.end();
1741 const bool crossC = tmp.crossingConflict(*foe);
1742#ifdef DEBUG_BUILD_SUBDRIVEWAY
1743 std::cout << " subLast=" << subLast << " lane=" << lane->getID() << " fc=" << flankC << " cc=" << crossC << " bc=" << bidiConflict << "\n";
1744#endif
1745 if (flankC || crossC || bidiConflict) {
1746 foundConflict = true;
1747 if (!movingBlock || bidiConflict) {
1748 break;
1749 }
1750 if (((flankC && lane->getFromJunction()->getType() == SumoXMLNodeType::ZIPPER)
1751 || (!flankC && lane->getToJunction()->getType() == SumoXMLNodeType::ZIPPER))
1752 && (isDepartDriveway()
1753 || getForwardDistance(flankC ? subLast - 1 : subLast) > myMovingBlockMaxDist)) {
1754 zipperC = true;
1755 foundConflict = false;
1756#ifdef DEBUG_BUILD_SUBDRIVEWAY
1757 std::cout << " ignored movingBlock zipperConflict\n";
1758#endif
1759 if (!flankC && crossC) {
1760#ifdef DEBUG_BUILD_SUBDRIVEWAY
1761 std::cout << SIMTIME << " buildSubFoe dw=" << getID() << " foe=" << foe->getID() << " movingBlock-save\n";
1762#endif
1763 return false;
1764 }
1765 }
1766 if (!flankC && crossC) {
1767 break;
1768 }
1769 } else if (foundConflict) {
1770 break;
1771 }
1772 subLast--;
1773 }
1774#ifdef DEBUG_BUILD_SUBDRIVEWAY
1775 std::cout << " subLastFina=" << subLast << " movingBlock=" << movingBlock << " zipperC=" << zipperC << "\n";
1776#endif
1777 if (subLast < 0) {
1778 if (movingBlock && zipperC) {
1779#ifdef DEBUG_BUILD_SUBDRIVEWAY
1780 std::cout << SIMTIME << " buildSubFoe dw=" << getID() << " foe=" << foe->getID() << " movingBlock-save\n";
1781#endif
1782 return false;
1783 } else if (&myForward.back()->getEdge() == myRoute.back() && foe->forwardEndOnRoute(this)) {
1784 // driveway ends in the middle of the block and only the final edge overlaps with the foe driveWay
1785 foe->myFoes.push_back(this);
1786#ifdef DEBUG_BUILD_SUBDRIVEWAY
1787 std::cout << SIMTIME << " buildSubFoe dw=" << getID() << " foe=" << foe->getID() << " foe endsOnForward\n";
1788#endif
1789 } else if (foe->myTerminateRoute) {
1790 if (bidiBlockedByEnd(*foe) && bidiBlockedBy(*this) && foe->forwardEndOnRoute(this)) {
1791 foe->myFoes.push_back(this);
1792 // foe will get the sidings
1793 addSidings(foe, true);
1794 }
1795#ifdef DEBUG_BUILD_SUBDRIVEWAY
1796 std::cout << SIMTIME << " buildSubFoe dw=" << getID() << " foe=" << foe->getID() << " terminates\n";
1797#endif
1798 } else if (myTerminateRoute && myBidi.size() <= myForward.size()) {
1799 foe->myFoes.push_back(this);
1800#ifdef DEBUG_BUILD_SUBDRIVEWAY
1801 std::cout << SIMTIME << " buildSubFoe dw=" << getID() << " terminates, foe=" << foe->getID() << "\n";
1802#endif
1803 return true;
1804 } else if (foe->myReversals.size() % 2 == 1) {
1805#ifdef DEBUG_BUILD_SUBDRIVEWAY
1806 std::cout << SIMTIME << " buildSubFoe dw=" << getID() << " foe=" << foe->getID() << " has " << foe->myReversals.size() << " reversals\n";
1807#endif
1808 } else {
1809#ifdef DEBUG_BUILD_SUBDRIVEWAY
1810 std::cout << SIMTIME << " buildSubFoe dw=" << getID() << " foe=" << foe->getID() << " failed\n";
1811#endif
1812#ifdef SUBDRIVEWAY_WARN_NOCONFLICT
1813 WRITE_WARNINGF("No point of conflict found between driveway '%' and driveway '%' when creating sub-driveway", getID(), foe->getID());
1814#endif
1815 }
1816 return false;
1817 }
1818 int subSize = subLast + 1;
1819 for (MSDriveWay* cand : mySubDriveWays) {
1820 if ((int)cand->myForward.size() == subSize) {
1821 // can re-use existing sub-driveway
1822 foe->myFoes.push_back(cand);
1823 cand->myFoes.push_back(foe);
1824#ifdef DEBUG_BUILD_SUBDRIVEWAY
1825 std::cout << SIMTIME << " buildSubFoe dw=" << getID() << " foe=" << foe->getID() << " useExisting=" << cand->getID() << "\n";
1826#endif
1827 return true;
1828 }
1829 }
1830 std::vector<const MSLane*> forward(myForward.begin(), myForward.begin() + subSize);
1831 std::vector<const MSEdge*> route;
1832 for (const MSLane* lane : forward) {
1833 if (lane->isNormal()) {
1834 route.push_back(&lane->getEdge());
1835 }
1836 }
1837 if (route.empty()) {
1838#ifdef DEBUG_BUILD_SUBDRIVEWAY
1839 std::cout << SIMTIME << " abort subFoe dw=" << getID() << " foe=" << foe->getID() << " empty subRoute\n";
1840#endif
1841 return false;
1842 }
1843 if (myRoute.size() > route.size()) {
1844 // route continues. make sure the subDriveway does not end with a reversal
1845 const MSEdge* lastNormal = route.back();
1846 const MSEdge* nextNormal = myRoute[route.size()];
1847 if (lastNormal->getBidiEdge() == nextNormal) {
1848#ifdef DEBUG_BUILD_SUBDRIVEWAY
1849 std::cout << SIMTIME << " abort subFoe dw=" << getID() << " foe=" << foe->getID()
1850 << " lastNormal=" << lastNormal->getID() << " nextNormal=" << nextNormal->getID() << " endWithReversal\n";
1851#endif
1852 return false;
1853 }
1854 }
1855 MSDriveWay* sub = new MSDriveWay(myOrigin, getID() + "." + toString(mySubDriveWays.size()));
1856 sub->myLane = myLane;
1857 sub->myIsSubDriveway = true;
1858 sub->myForward = forward;
1859 sub->myRoute = route;
1860 sub->myCoreSize = (int)sub->myRoute.size();
1861 myLane->addMoveReminder(sub, false);
1862
1863 // copy trains that are currently on this driveway (and associated entry events)
1864 for (SUMOVehicle* veh : myTrains) {
1865 auto itOnSub = std::find(sub->myRoute.begin(), sub->myRoute.end(), veh->getEdge());
1866 if (itOnSub != sub->myRoute.end()) {
1867 sub->myTrains.insert(veh);
1868 // non-zero is enough to avoid superfluous activation via activateReminders (and removal)
1869 const double pos = sub->myRoute.front()->getLength();
1870 dynamic_cast<MSBaseVehicle*>(veh)->addReminder(sub, pos);
1871 for (const VehicleEvent& ve : myVehicleEvents) {
1872 if (ve.id == veh->getID()) {
1873 sub->myVehicleEvents.push_back(ve);
1874 }
1875 }
1876 }
1877 }
1878
1879 foe->myFoes.push_back(sub);
1880 sub->myFoes.push_back(foe);
1881 mySubDriveWays.push_back(sub);
1882#ifdef DEBUG_BUILD_SUBDRIVEWAY
1883 std::cout << SIMTIME << " buildSubFoe dw=" << getID() << " foe=" << foe->getID() << " sub=" << sub->getID() << " route=" << toString(sub->myRoute) << "\n";
1884#endif
1885 return true;
1886}
1887
1888
1889double
1891 assert(lastIndex < (int)myForward.size());
1892 double result = 0;
1893 for (int i = 0; i <= lastIndex; i++) {
1894 result += myForward[i]->getLength();
1895 }
1896 return result;
1897}
1898
1899
1900void
1902 const MSEdge* foeEndBidi = foe->myForward.back()->getEdge().getBidiEdge();
1903 int forwardNormals = 0;
1904 for (auto lane : foe->myForward) {
1905 if (lane->isNormal()) {
1906 forwardNormals++;
1907 }
1908 }
1909 if (forwardNormals == (int)foe->myRoute.size()) {
1910#ifdef DEBUG_BUILD_SIDINGS
1911 std::cout << "checkSiding " << getID() << " foe=" << foe->getID() << " forwardNormals=" << forwardNormals << " frSize=" << foe->myRoute.size() << " aborted\n";
1912#endif
1913 return;
1914 }
1915 auto foeSearchBeg = foe->myRoute.begin() + forwardNormals;
1916 auto foeSearchEnd = foe->myRoute.end();
1917 if (foeEndBidi == nullptr) {
1918 throw ProcessError("checkSiding " + getID() + " foe=" + foe->getID() + " noBidi\n");
1919 }
1920 int i;
1921 std::vector<int> start;
1922 std::vector<double> length;
1923 for (i = 0; i < (int)myRoute.size(); i++) {
1924 if (myRoute[i] == foeEndBidi) {
1925 break;
1926 }
1927 }
1928 if (i == (int)myRoute.size()) {
1929 throw ProcessError("checkSiding " + getID() + " foe=" + foe->getID() + " foeEndBidi=" + foeEndBidi->getID() + " not on route\n");
1930 }
1931 const MSEdge* next = myRoute[i];
1932#ifdef DEBUG_BUILD_SIDINGS
1933 std::cout << "checkSiding " << getID() << " foe=" << foe->getID() << " i=" << i << " next=" << next->getID() << " forwardNormals=" << forwardNormals << " frSize=" << foe->myRoute.size() << " foeSearchBeg=" << (*foeSearchBeg)->getID() << "\n";
1934#endif
1935 i--;
1936 for (; i >= 0; i--) {
1937 const MSEdge* cur = myRoute[i];
1938 if (hasRS(cur, next)) {
1939 if (std::find(foeSearchBeg, foeSearchEnd, cur->getBidiEdge()) == foeSearchEnd) {
1940 start.push_back(i);
1941 length.push_back(0);
1942 }
1943 }
1944 if (!start.empty()) {
1945 auto itFind = std::find(foeSearchBeg, foeSearchEnd, cur->getBidiEdge());
1946 if (itFind != foeSearchEnd) {
1947#ifdef DEBUG_BUILD_SIDINGS
1948 std::cout << "endSiding " << getID() << " foe=" << foe->getID() << " i=" << i << " curBidi=" << Named::getIDSecure(cur->getBidiEdge()) << " length=" << toString(length) << "\n";
1949#endif
1950 const int firstIndex = i + 1;
1951 if (addToFoe) {
1952 auto& foeSidings = foe->mySidings[this];
1953 // indices must be mapped onto foe route;
1954 const MSEdge* first = myRoute[firstIndex];
1955 auto itFirst = std::find(foe->myRoute.begin(), foe->myRoute.end(), first);
1956 if (itFirst != foe->myRoute.end()) {
1957 for (int j = 0; j < (int)length.size(); j++) {
1958 const MSEdge* last = myRoute[start[j]];
1959 auto itLast = std::find(itFirst, foe->myRoute.end(), last);
1960 if (itLast != foe->myRoute.end()) {
1961 foeSidings.insert(foeSidings.begin(), Siding((int)(itFirst - foe->myRoute.begin()), (int)(itLast - foe->myRoute.begin()), length[j]));
1962 }
1963 }
1964 }
1965 } else {
1966 auto& foeSidings = mySidings[foe];
1967 for (int j = 0; j < (int)length.size(); j++) {
1968 foeSidings.insert(foeSidings.begin(), Siding(firstIndex, start[j], length[j]));
1969 }
1970 }
1971 start.clear();
1972 length.clear();
1973 foeSearchBeg = itFind;
1974 } else {
1975 for (int j = 0; j < (int)length.size(); j++) {
1976 length[j] += cur->getLength();
1977 }
1978 }
1979 }
1980 next = cur;
1981 }
1982}
1983
1984
1985bool
1986MSDriveWay::hasRS(const MSEdge* cur, const MSEdge* next) {
1988 // check if there is a controlled link between cur and next
1989 for (auto lane : cur->getLanes()) {
1990 for (const MSLink* link : lane->getLinkCont()) {
1991 if (&link->getLane()->getEdge() == next && link->getTLLogic() != nullptr) {
1992 return true;
1993 }
1994 }
1995 }
1996 }
1997 return false;
1998}
1999
2000
2001bool
2003 const MSEdge* foeForwardEnd = &foe->myForward.back()->getNormalPredecessorLane()->getEdge();
2004 return std::find(myRoute.begin(), myRoute.end(), foeForwardEnd) != myRoute.end();
2005}
2006
2007void
2009 if (link->getTLLogic() != nullptr) {
2010 // ignore links that originate on myBidi
2011 // and also links from the same junction as my own link
2012 const MSLane* origin = link->getLaneBefore();
2013 if (std::find(myBidi.begin(), myBidi.end(), origin) == myBidi.end()) {
2014 if (link->getJunction() != myRoute.front()->getFromJunction()) {
2015 if (std::find(myConflictLinks.begin(), myConflictLinks.end(), link) == myConflictLinks.end()) {
2016 myConflictLinks.push_back(const_cast<MSLink*>(link));
2017 }
2018 }
2019 }
2020 }
2021}
2022
2023void
2024MSDriveWay::addDWDeadlock(const std::vector<const MSDriveWay*>& deadlockFoes) {
2025 std::set<const MSDriveWay*> filtered;
2026 for (const MSDriveWay* foe : deadlockFoes) {
2027 if (std::find(myFoes.begin(), myFoes.end(), foe) == myFoes.end()) {
2028 filtered.insert(foe);
2029 }
2030 }
2031 if (std::find(myDeadlocks.begin(), myDeadlocks.end(), filtered) == myDeadlocks.end()) {
2032 myDeadlocks.push_back(filtered);
2033 //std::cout << getID() << " deadlockFoes=" << toString(deadlockFoes) << "\n";
2034 }
2035}
2036
2037const MSDriveWay*
2039 const MSEdge* edge = init ? veh->getRoute().getEdges()[veh->getDepartEdge()] : veh->getEdge();
2040 assert(isRailwayOrShared(edge->getPermissions()));
2042 for (const MSLane* lane : edge->getLanes()) {
2043 for (auto ili : lane->getIncomingLanes()) {
2044 const MSLink* entry = ili.viaLink->getCorrespondingEntryLink();
2045 const MSRailSignal* rs = dynamic_cast<const MSRailSignal*>(entry->getTLLogic());
2046 if (rs != nullptr) {
2047 const MSDriveWay* dw = &const_cast<MSRailSignal*>(rs)->retrieveDriveWayForVeh(entry->getTLIndex(), veh);
2048 if (&dw->myForward.front()->getEdge() == edge) {
2049 return dw;
2050 }
2051 }
2052 }
2053 }
2054 }
2055 for (MSDriveWay* dw : myDepartureDriveways[edge]) {
2056 auto matchStart = init ? veh->getRoute().begin() + veh->getDepartEdge() : veh->getCurrentRouteEdge();
2057 if (dw->match(matchStart, veh->getRoute().end())) {
2058 return dw;
2059 }
2060 }
2061 const std::string id = edge->getFromJunction()->getID() + ".d" + toString(myDepartDrivewayIndex[edge->getFromJunction()]++);
2062 MSDriveWay* dw = buildDriveWay(id, nullptr, veh->getCurrentRouteEdge(), veh->getRoute().end());
2063 myDepartureDriveways[edge].push_back(dw);
2064 myDepartureDrivewaysEnds[&dw->myForward.back()->getEdge()].push_back(dw);
2065 dw->setVehicle(veh->getID());
2066 return dw;
2067}
2068
2069
2070void
2072 for (auto item : myDepartureDriveways) {
2073 const MSEdge* edge = item.first;
2074 if (item.second.size() > 0) {
2075 od.openTag("departJunction");
2077 for (const MSDriveWay* dw : item.second) {
2078 if (writeVehicles) {
2079 dw->writeBlockVehicles(od);
2080 } else {
2081 dw->writeBlocks(od);
2082 }
2083 }
2084 od.closeTag(); // departJunction
2085 }
2086 }
2087}
2088
2089void
2091 // all driveways are in myEndingDriveways which makes it convenient
2092 for (auto item : myEndingDriveways) {
2093 for (MSDriveWay* dw : item.second) {
2094 dw->_saveState(out);
2095 for (MSDriveWay* sub : dw->mySubDriveWays) {
2096 sub->_saveState(out);
2097 }
2098 }
2099 }
2100}
2101
2102void
2104 if (!myTrains.empty() || haveSubTrains()) {
2108 if (!myTrains.empty()) {
2109 std::vector<std::string> trainIDs;
2110 for (SUMOVehicle* veh : myTrains) {
2111 trainIDs.push_back(veh->getID());
2112 }
2113 out.writeAttr(SUMO_ATTR_VEHICLES, toString(trainIDs));
2114 }
2115 out.closeTag();
2116 }
2117}
2118
2119
2120bool
2122 for (MSDriveWay* sub : mySubDriveWays) {
2123 if (!sub->myTrains.empty()) {
2124 return true;
2125 }
2126 }
2127 return false;
2128}
2129
2130void
2132 if ((int)myDriveWayRouteLookup.size() < myGlobalDriveWayIndex) {
2133 for (auto item : myEndingDriveways) {
2134 for (MSDriveWay* dw : item.second) {
2135 myDriveWayRouteLookup[dw->myRoute] = dw;
2136 }
2137 }
2138 }
2140 bool ok;
2141 const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
2142 const std::string edges = attrs.get<std::string>(SUMO_ATTR_EDGES, id.c_str(), ok);
2143 ConstMSEdgeVector route;
2144 if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
2145 MSEdge::parseEdgesList(edges, route, id);
2146 }
2147 MSDriveWay* dw = nullptr;
2148 if (tag == SUMO_TAG_DRIVEWAY) {
2149 auto it = myDriveWayRouteLookup.find(route);
2150 if (it == myDriveWayRouteLookup.end()) {
2151 //WRITE_WARNING(TLF("Unknown driveWay '%' with route '%'", id, edges));
2152 //return;
2153 throw ProcessError(TLF("Unknown driveWay '%' with route '%'", id, edges));
2154 }
2155 dw = it->second;
2156 myDriveWayLookup[id] = dw;
2157 } else {
2158 std::string parentID = id.substr(0, id.rfind('.'));
2159 auto it = myDriveWayLookup.find(parentID);
2160 if (it == myDriveWayLookup.end()) {
2161 //WRITE_WARNING(TLF("Unknown parent driveway '%' for subDriveWay '%'", parentID, id));
2162 //return;
2163 throw ProcessError(TLF("Unknown parent driveway '%' for subDriveWay '%'", parentID, id));
2164 }
2165 MSDriveWay* parent = it->second;
2166 for (MSDriveWay* sub : parent->mySubDriveWays) {
2167 if (sub->myRoute == route) {
2168 dw = sub;
2169 break;
2170 }
2171 }
2172 if (dw == nullptr) {
2173 // missing subdriveways can be ignored. They may have been created
2174 // as foes for driveways that are not relevant at state loading time
2175 return;
2176 }
2177 }
2178 const std::string vehicles = attrs.getOpt<std::string>(SUMO_ATTR_VEHICLES, id.c_str(), ok, "");
2179 for (const std::string& vehID : StringTokenizer(vehicles).getVector()) {
2180 MSBaseVehicle* veh = dynamic_cast<MSBaseVehicle*>(c.getVehicle(vehID));
2181 if (veh == nullptr) {
2182 throw ProcessError(TLF("Unknown vehicle '%' in driveway '%'", vehID, id));
2183 }
2184 if (!dw->hasTrain(veh)) {
2185 dw->myTrains.insert(veh);
2186 veh->addReminder(dw);
2187 }
2188 }
2189}
2190
2191const MSDriveWay*
2192MSDriveWay::retrieveDepartDriveWay(const MSEdge* edge, const std::string& id) {
2193 for (MSDriveWay* dw : myDepartureDriveways[edge]) {
2194 if (dw->getID() == id) {
2195 return dw;
2196 }
2197 }
2198 return nullptr;
2199}
2200
2201
2202bool
2204 return myTrains.count(veh) != 0;
2205}
2206
2207/****************************************************************************/
#define DEBUG_HELPER(obj)
#define DEBUG_COND_DW2
#define DEBUG_COND_DW
std::vector< const MSEdge * > ConstMSEdgeVector
Definition MSEdge.h:74
std::vector< MSEdge * > MSEdgeVector
Definition MSEdge.h:73
ConstMSEdgeVector::const_iterator MSRouteIterator
Definition MSRoute.h:57
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:287
#define TL(string)
Definition MsgHandler.h:304
#define TLF(string,...)
Definition MsgHandler.h:306
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:91
#define SIMSTEP
Definition SUMOTime.h:61
#define SIMTIME
Definition SUMOTime.h:62
bool isRailwayOrShared(SVCPermissions permissions)
Returns whether an edge with the given permissions is a railway edge or a shared road/rail edge.
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
@ SVC_RAIL_CLASSES
classes which drive on tracks
@ SUMO_TAG_SUBDRIVEWAY
@ SUMO_TAG_DRIVEWAY
Saved driveway information.
@ TURN
The link is a 180 degree turn.
@ TURN_LEFTHAND
The link is a 180 degree turn (left-hand network)
@ SUMO_ATTR_EDGES
the edges of a route
@ SUMO_ATTR_LANES
@ SUMO_ATTR_VEHICLES
@ SUMO_ATTR_VEHICLE
@ SUMO_ATTR_ID
@ SUMO_ATTR_TIME
trigger: the time of the step
bool gDebugFlag4
Definition StdDefs.cpp:46
std::string joinNamedToString(const std::set< T *, C > &ns, const T_BETWEEN &between)
Definition ToString.h:332
std::string joinToStringSorting(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition ToString.h:305
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
std::string joinNamedToStringSorting(const std::set< T * > &ns, const T_BETWEEN &between)
Definition ToString.h:313
MESegment * getSegmentForEdge(const MSEdge &e, double pos=0)
Get the segment for a given edge at a given position.
Definition MELoop.cpp:340
A single mesoscopic segment (cell)
Definition MESegment.h:50
void addDetector(MSMoveReminder *data, int queueIndex=-1)
Adds a data collector for a detector to this segment.
The base class for microscopic and mesoscopic vehicles.
const SUMOVehicleParameter::Stop * getNextStopParameter() const
return parameters for the next stop (SUMOVehicle Interface)
void addReminder(MSMoveReminder *rem, double pos=0)
Adds a MoveReminder dynamically.
bool isStopped() const
Returns whether the vehicle is at a stop.
SUMOTime getStartupDelay() const
Get the vehicle type's startupDelay.
Definition MSCFModel.h:293
void addFoes(const MSLink *link)
add all driveWays that start at the given link as foes
std::vector< MSLink * > myConflictLinks
Definition MSDriveWay.h:252
const SUMOVehicle * myActive
whether the current signal is switched green for a train approaching this block
Definition MSDriveWay.h:208
bool crossingConflict(const MSDriveWay &other) const
Wether there is a crossing conflict with the given driveway.
static std::string getJunctionLinkID(const MSLink *link)
return junctionID_junctionLinkIndex
static void clearState()
bool haveSubTrains() const
bool myIsSubDriveway
Definition MSDriveWay.h:227
std::vector< MSDriveWay * > mySubDriveWays
Definition MSDriveWay.h:370
std::vector< std::set< const MSDriveWay * > > myDeadlocks
Definition MSDriveWay.h:365
void checkFlanks(const MSLink *originLink, const std::vector< const MSLane * > &lanes, const LaneVisitedMap &visited, bool allFoes, std::set< MSLink * > &flankSwitches) const
bool myAbortedBuild
whether driveway building was aborted due to MAX_BLOCK_LENGTH
Definition MSDriveWay.h:224
bool buildSubFoe(MSDriveWay *foe, bool movingBlock)
std::set< SUMOVehicle *, ComparatorNumericalIdLess > myTrains
Definition MSDriveWay.h:360
static std::map< const MSJunction *, int > myDepartDrivewayIndex
Definition MSDriveWay.h:391
static bool hasJoin(const SUMOVehicle *ego, const SUMOVehicle *foe)
bool isDepartDriveway() const
Definition MSDriveWay.h:159
static std::string getClickableTLLinkID(const MSLink *link)
return logicID_linkIndex in a way that allows clicking in sumo-gui
const MSLink * myOrigin
the link that enters this driveway or nullptr for a departure driveWay
Definition MSDriveWay.h:205
bool myTerminateRoute
Definition MSDriveWay.h:222
static double myMovingBlockMaxDist
Definition MSDriveWay.h:380
std::vector< const MSLane * > myBidi
Definition MSDriveWay.h:236
static bool myWriteVehicles
Definition MSDriveWay.h:379
void checkCrossingFlanks(MSLink *dwLink, const LaneVisitedMap &visited, std::set< MSLink * > &flankSwitches) const
bool notifyReroute(SUMOTrafficObject &veh)
Called if the vehicle change it's route.
bool hasLinkConflict(const Approaching &closest, const MSLink *foeLink) const
Whether the approaching vehicle is prevent from driving by another vehicle approaching the given link...
void findFlankProtection(MSLink *link, MSLink *origLink, std::vector< const MSLane * > &flank)
std::vector< const MSLane * > myBidiExtended
Definition MSDriveWay.h:240
MSDriveWay(const MSLink *origin, const std::string &id, bool temporary=false)
static bool hasRS(const MSEdge *cur, const MSEdge *next)
void addBidiFoes(const MSRailSignal *ownSignal, bool extended)
derive foe driveways based on myBidi or myBidiExtended
static const MSDriveWay * retrieveDepartDriveWay(const MSEdge *edge, const std::string &id)
void writeBlocks(OutputDevice &od) const
Write block items for this driveway.
bool notifyLeave(SUMOTrafficObject &veh, double lastPos, Notification reason, const MSLane *enteredLane=0)
Called if the vehicle leaves the reminder's lane.
std::vector< const MSLane * > myForward
Definition MSDriveWay.h:231
std::vector< const MSEdge * > myReversals
track own occurences in myReversalDriveWays for cleanup in destructor
Definition MSDriveWay.h:373
std::map< const MSDriveWay *, std::vector< Siding >, ComparatorIdLess > mySidings
Definition MSDriveWay.h:364
void writeBlockVehicles(OutputDevice &od) const
virtual ~MSDriveWay()
Destructor.
bool match(MSRouteIterator firstIt, MSRouteIterator endIt) const
whether the give route matches this driveway
static void appendMapIndex(LaneVisitedMap &map, const MSLane *lane)
append to map by map index and avoid undefined behavior
bool overlap(const MSDriveWay &other) const
Wether this driveway (route) overlaps with the given one.
void buildRoute(const MSLink *origin, MSRouteIterator next, MSRouteIterator end, LaneVisitedMap &visited, std::set< MSLink * > &)
std::vector< const MSLane * > myFlank
Definition MSDriveWay.h:244
static std::string getTLLinkID(const MSLink *link)
return logicID_linkIndex
std::vector< const MSEdge * > myRoute
list of edges for matching against train routes
Definition MSDriveWay.h:211
std::map< const MSLane *, int, ComparatorNumericalIdLess > LaneVisitedMap
Definition MSDriveWay.h:45
static void writeDepatureBlocks(OutputDevice &od, bool writeVehicles)
static std::map< ConstMSEdgeVector, MSDriveWay * > myDriveWayRouteLookup
lookup table for state loading
Definition MSDriveWay.h:399
static std::set< const MSEdge * > myBlockLengthWarnings
Definition MSDriveWay.h:381
static std::map< const MSEdge *, std::vector< MSDriveWay * > > myReversalDriveWays
all driveways reversing on the given switch (used to look up flank foes)
Definition MSDriveWay.h:387
static void init()
void _saveState(OutputDevice &out) const
static std::string formatVisitedMap(const LaneVisitedMap &visited)
print link descriptions
static std::map< std::string, MSDriveWay * > myDriveWayLookup
Definition MSDriveWay.h:400
int matchesPastRoute(SUMOVehicle &sveh) const
bool flankConflict(const MSDriveWay &other) const
Wether there is a flank conflict with the given driveway.
bool conflictLaneOccupied(bool store=true, const SUMOVehicle *ego=nullptr) const
whether any of myConflictLanes is occupied (vehicles that are the target of a join must be ignored)
void addSwitchFoes(MSLink *link)
bool hasTrain(SUMOVehicle *veh) const
whether the given train is on this driveway
static std::map< const MSEdge *, std::vector< MSDriveWay * > > myDepartureDrivewaysEnds
all driveways that do not start at a rail signal (and are only used at departure) by end edge
Definition MSDriveWay.h:393
int myCoreSize
number of edges in myRoute where overlap with other driveways is forbidden
Definition MSDriveWay.h:214
double getForwardDistance(int lastIndex) const
compute distance along the forward section up to lastIndex
std::pair< const SUMOVehicle *const, const MSLink::ApproachingVehicleInformation > Approaching
Definition MSDriveWay.h:43
void addConflictLink(const MSLink *link)
add symmetical conflict link for foes when building a new driveway
static void cleanup()
bool notifyLeaveBack(SUMOTrafficObject &veh, Notification reason, const MSLane *leftLane)
Called if the vehicle's back leaves the reminder's lane.
std::string myFirstVehicle
the first vehicle using this driveway
Definition MSDriveWay.h:376
bool forwardEndOnRoute(const MSDriveWay *foe) const
bool myFoundJump
Definition MSDriveWay.h:221
static void saveState(OutputDevice &out)
Save driveway occupancy into the given stream.
static std::map< const MSEdge *, std::vector< MSDriveWay * >, ComparatorNumericalIdLess > myDepartureDriveways
all driveways that do not start at a rail signal (and are only used at departure)
Definition MSDriveWay.h:390
void setVehicle(const std::string &vehID)
Definition MSDriveWay.h:142
bool myBidiEnded
whether driveway building was aborted when no further bidi edge was found
Definition MSDriveWay.h:226
static bool mustYield(const Approaching &veh, const Approaching &foe)
Whether veh must yield to the foe train.
void addSidings(MSDriveWay *foe, bool addToFoe=false)
add sidings for the given foe
void enterDriveWay(SUMOVehicle &sveh, Notification reason)
helper method for notifyEnter
bool bidiBlockedByEnd(const MSDriveWay &other) const
Wether there is a bidi conflict with the end of the given driveway.
static int myGlobalDriveWayIndex
Definition MSDriveWay.h:378
static MSDriveWay * buildDriveWay(const std::string &id, const MSLink *link, MSRouteIterator first, MSRouteIterator end)
construct a new driveway by searching along the given route until all block structures are found
bool myFoundSignal
whether this driveway ends its forward section with a rail signal (and thus comprises a full block)
Definition MSDriveWay.h:220
static std::map< const MSLink *, std::vector< MSDriveWay * > > mySwitchDriveWays
all driveways passing the given switch (used to look up flank foes)
Definition MSDriveWay.h:384
bool isFoeOrSubFoe(const MSDriveWay *foe) const
void addReversalFoes(bool movingBlock)
derive foe driveways that enter the bidi section by reversing
int myForwardEdgeCount
number of normal edges in myForward
Definition MSDriveWay.h:217
void addDWDeadlock(const std::vector< const MSDriveWay * > &deadlockFoes)
static std::map< const MSEdge *, std::vector< MSDriveWay * >, ComparatorNumericalIdLess > myEndingDriveways
all driveways that end on the given edge
Definition MSDriveWay.h:396
bool foeDriveWayApproached() const
whether any of my Foes is being approached
static void loadState(const SUMOSAXAttributes &attrs, int tag)
bool forwardRouteConflict(std::set< const MSEdge * > forward, const MSDriveWay &other, bool secondCheck=false)
Wether the route of other passes into the forward section of this driveway.
std::pair< bool, const MSDriveWay * > canUseSiding(const SUMOVehicle *ego, const MSDriveWay *foe, bool recurse=true) const
return whether a siding can be used. If a siding exist but is occupied, also return the occupied driv...
std::vector< const MSLane * > myConflictLanes
the lanes that must be clear of trains before this signal can switch to green
Definition MSDriveWay.h:247
static const MSDriveWay * getDepartureDriveway(const SUMOVehicle *veh, bool init=false)
std::vector< MSDriveWay * > myFoes
Definition MSDriveWay.h:363
std::vector< VehicleEvent > myVehicleEvents
Definition MSDriveWay.h:362
static bool isSwitch(const MSLink *link)
bool notifyEnter(SUMOTrafficObject &veh, Notification reason, const MSLane *enteredLane)
Checks whether the reminder is activated by a vehicle entering the lane.
bool bidiBlockedBy(const MSDriveWay &other) const
Wether there is a bidi conflict with the given driveway.
bool reserve(const Approaching &closest, MSEdgeVector &occupied)
attempt reserve this driveway for the given vehicle
void addParallelFoes(const MSLink *link, const MSEdge *first)
derive foe driveways that start at the same signal
bool foeDriveWayOccupied(bool store, const SUMOVehicle *ego, MSEdgeVector &occupied) const
whether any of myFoes is occupied (vehicles that are the target of a join must be ignored)
A road/street connecting two junctions.
Definition MSEdge.h:77
SVCPermissions getPermissions() const
Returns the combined permissions of all lanes of this edge.
Definition MSEdge.h:658
const std::vector< MSLane * > & getLanes() const
Returns this edge's lanes.
Definition MSEdge.h:168
static void parseEdgesList(const std::string &desc, ConstMSEdgeVector &into, const std::string &rid)
Parses the given string assuming it contains a list of edge ids divided by spaces.
Definition MSEdge.cpp:1146
const MSEdge * getBidiEdge() const
return opposite superposable/congruent edge, if it exist and 0 else
Definition MSEdge.h:283
bool isNormal() const
return whether this edge is an internal edge
Definition MSEdge.h:264
const MSJunction * getToJunction() const
Definition MSEdge.h:427
double getLength() const
return the length of the edge
Definition MSEdge.h:694
const MSJunction * getFromJunction() const
Definition MSEdge.h:423
bool isConnectedTo(const MSEdge &destination, SUMOVehicleClass vclass, bool ignoreTransientPermissions=false) const
Definition MSEdge.h:244
static bool gUseMesoSim
Definition MSGlobals.h:106
static MELoop * gMesoNet
mesoscopic simulation infrastructure
Definition MSGlobals.h:112
static SUMOTime gTimeToTeleportRSDeadlock
Definition MSGlobals.h:72
static double gMaxRailSignalBlockLength
The maximum length of a rail signal block.
Definition MSGlobals.h:189
The base class for an intersection.
Definition MSJunction.h:58
SumoXMLNodeType getType() const
return the type of this Junction
Definition MSJunction.h:133
virtual const MSJunctionLogic * getLogic() const
Definition MSJunction.h:141
const ConstMSEdgeVector & getIncoming() const
Definition MSJunction.h:108
virtual const MSLogicJunction::LinkBits & getFoesFor(int linkIndex) const
Returns the foes for the given link.
Representation of a lane in the micro simulation.
Definition MSLane.h:84
const MSLink * getLinkTo(const MSLane *const) const
returns the link to the given lane or nullptr, if it is not connected
Definition MSLane.cpp:2759
const MSEdge * getNextNormal() const
Returns the lane's follower if it is an internal lane, the edge of the lane otherwise.
Definition MSLane.cpp:2472
const std::vector< IncomingLaneInfo > & getIncomingLanes() const
Definition MSLane.h:960
virtual void addMoveReminder(MSMoveReminder *rem, bool addToVehicles=true)
Add a move-reminder to move-reminder container.
Definition MSLane.cpp:360
const MSJunction * getToJunction() const
Definition MSLane.cpp:4744
double getLength() const
Returns the lane's length.
Definition MSLane.h:611
const MSJunction * getFromJunction() const
Definition MSLane.cpp:4738
int getIndex() const
Returns the lane's index.
Definition MSLane.h:647
bool isNormal() const
Definition MSLane.cpp:2633
bool isInternal() const
Definition MSLane.cpp:2627
MSLane * getBidiLane() const
retrieve bidirectional lane or nullptr
Definition MSLane.cpp:4693
MSEdge & getEdge() const
Returns the lane's edge.
Definition MSLane.h:769
const MSLane * getNormalPredecessorLane() const
get normal lane leading to this internal lane, for normal lanes, the lane itself is returned
Definition MSLane.cpp:3255
const std::vector< MSLink * > & getLinkCont() const
returns the container with all links !!!
Definition MSLane.h:729
Something on a lane to be noticed about vehicle movement.
const std::string & getDescription() const
Notification
Definition of a vehicle state.
@ NOTIFICATION_DEPARTED
The vehicle has departed (was inserted into the network)
@ NOTIFICATION_REROUTE
The vehicle changed it's route.
@ NOTIFICATION_SEGMENT
The vehicle changes the segment (meso only)
@ NOTIFICATION_JUNCTION
The vehicle arrived at a junction.
@ NOTIFICATION_PARKING
The vehicle starts or ends parking.
static StringBijection< Notification > Notifications
MSLane * myLane
Lane on which the reminder works.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:187
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition MSNet.h:392
void notifyApproach(const MSLink *link)
switch rail signal to active
void addWaitRelation(const SUMOVehicle *waits, const MSRailSignal *rs, const SUMOVehicle *reason, MSRailSignalConstraint *constraint=nullptr)
static MSRailSignalControl & getInstance()
static bool isMovingBlock(SVCPermissions svc)
A signal for rails.
bool isMovingBlock() const
const MSDriveWay & retrieveDriveWayForVeh(int tlIndex, const SUMOVehicle *veh)
static std::string describeLinks(std::vector< MSLink * > links)
print link descriptions
bool constraintsAllow(const SUMOVehicle *veh, bool storeWaitRelation=false) const
whether the given vehicle is free to drive
static VehicleVector & rivalVehicles()
static bool storeVehicles()
const std::vector< MSDriveWay * > retrieveDriveWays(int tlIndex) const
static VehicleVector & priorityVehicles()
static std::vector< const MSDriveWay * > & blockingDriveWays()
static VehicleVector & blockingVehicles()
const ConstMSEdgeVector & getEdges() const
Definition MSRoute.h:128
MSRouteIterator end() const
Returns the end of the list of edges to pass.
Definition MSRoute.cpp:79
MSRouteIterator begin() const
Returns the begin of the list of edges to pass.
Definition MSRoute.cpp:73
The parent class for traffic light logics.
The class responsible for building and deletion of vehicles.
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
Representation of a vehicle in the micro simulation.
Definition MSVehicle.h:77
const MSCFModel & getCarFollowModel() const
Returns the vehicle type's car following model definition (const version)
Base class for objects which have an id.
Definition Named.h:54
std::string myID
The name of the object.
Definition Named.h:125
static std::string getIDSecure(const T *obj, const std::string &fallBack="NULL")
get an identifier for Named-like object which may be Null
Definition Named.h:67
const std::string & getID() const
Returns the id.
Definition Named.h:74
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
static OptionsCont & getOptions()
Retrieves the options.
Static storage of an output device and its base (abstract) implementation.
void lf()
writes a line feed if applicable
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.
Encapsulated SAX-Attributes.
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue=T(), bool report=true) const
Tries to read given attribute assuming it is an int.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
Representation of a vehicle, person, or container.
virtual bool isVehicle() const
Whether it is a vehicle.
virtual const MSVehicleType & getVehicleType() const =0
Returns the object's "vehicle" type.
virtual const MSLane * getBackLane() const =0
Returns the lane the where the rear of the object is currently at.
virtual SUMOTime getWaitingTime(const bool accumulated=false) const =0
virtual int getRoutePosition() const =0
return index of edge within route
virtual const MSEdge * getEdge() const =0
Returns the edge the object is currently at.
Representation of a vehicle.
Definition SUMOVehicle.h:62
virtual bool isStopped() const =0
Returns whether the vehicle is at a stop and waiting for a person or container to continue.
virtual int getDepartEdge() const =0
Returns the edge on which this vehicle shall depart.
virtual const std::list< MSStop > & getStops() const =0
virtual bool hasDeparted() const =0
Returns whether this vehicle has departed.
virtual double getLength() const =0
Returns the vehicles's length.
virtual bool isOnRoad() const =0
Returns the information whether the vehicle is on a road (is simulated)
virtual double getBrakeGap(bool delayed=false) const =0
get distance for coming to a stop (used for rerouting checks)
virtual const ConstMSEdgeVector::const_iterator & getCurrentRouteEdge() const =0
Returns an iterator pointing to the current edge in this vehicles route.
virtual const SUMOVehicleParameter::Stop * getNextStopParameter() const =0
Returns parameters of the next stop or nullptr.
virtual const MSRoute & getRoute() const =0
Returns the current route.
Definition of vehicle stop (position and duration)
std::string join
the id of the vehicle (train portion) to which this vehicle shall be joined
const std::string & getString(const T key) const
get string
std::vector< std::string > getVector()
return vector of strings
#define UNUSED_PARAMETER(x)
Function-object for stable sorting of objects with numerical ids.
Definition Named.h:39