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