Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2002-2024 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file MSRailSignalControl.h
15 : /// @author Jakob Erdmann
16 : /// @date Sept 2020
17 : ///
18 : // Centralized services for rail signal control (Singleton)
19 : // - monitors track usage for long-range deadlock prevention
20 : /****************************************************************************/
21 : #pragma once
22 : #include <config.h>
23 : #include <microsim/MSNet.h>
24 :
25 : // ===========================================================================
26 : // class declarations
27 : // ===========================================================================
28 : class MSRailSignal;
29 : class MSRailSignalConstraint;
30 : class MSEdge;
31 :
32 : // ===========================================================================
33 : // class definitions
34 : // ===========================================================================
35 : /**
36 : * @class MSRailSignalControl
37 : * @brief A signal for rails
38 : */
39 : class MSRailSignalControl : public MSNet::VehicleStateListener {
40 : public:
41 : ~MSRailSignalControl();
42 :
43 : static MSRailSignalControl& getInstance();
44 :
45 : static bool hasInstance() {
46 183847348 : return myInstance != nullptr;
47 : }
48 :
49 : static void cleanup();
50 :
51 : /** @brief Perform resets events when quick-loading state */
52 : static void clearState();
53 :
54 : /// @brief reset all waiting-for relationships at the start of the simulation step
55 7972581 : void resetWaitRelations() {
56 : myWaitRelations.clear();
57 : myWrittenDeadlocks.clear();
58 7972581 : }
59 :
60 : void addWaitRelation(const SUMOVehicle* waits, const MSRailSignal* rs, const SUMOVehicle* reason, MSRailSignalConstraint* constraint = nullptr);
61 :
62 : void addDrivewayFollower(const MSDriveWay* dw, const MSDriveWay* dw2);
63 :
64 : /// @brief check whether the given signal and driveway are part of a deadlock circle
65 : void addDWDeadlockChecks(const MSRailSignal* rs, MSDriveWay* dw);
66 :
67 : /// @brief whether there is a circle in the waiting-for relationships that contains the given vehicle
68 : bool haveDeadlock(const SUMOVehicle* veh) const;
69 :
70 : void addDeadlockCheck(std::vector<const MSRailSignal*> signals);
71 :
72 : /** @brief Called if a vehicle changes its state
73 : * @param[in] vehicle The vehicle which changed its state
74 : * @param[in] to The state the vehicle has changed to
75 : * @param[in] info Additional information on the state change
76 : */
77 : void vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to, const std::string& info = "");
78 :
79 : void addSignal(MSRailSignal* signal);
80 :
81 : const std::vector<MSRailSignal*>& getSignals() const {
82 : return mySignals;
83 : }
84 :
85 : const std::map<const MSRailSignal*, std::vector<const MSRailSignal*> >& getDeadlockChecks() const {
86 : return myDeadlockChecks;
87 : }
88 :
89 : /// switch rail signal to active
90 : void notifyApproach(const MSLink* link);
91 :
92 : /// @brief update active rail signals
93 : void updateSignals(SUMOTime t);
94 :
95 :
96 : protected:
97 :
98 : void findDeadlockFoes(const MSDriveWay* dw, const std::vector<const MSRailSignal*>& others, std::vector<const MSDriveWay*> deadlockFoes);
99 :
100 :
101 : private:
102 : /** @brief Constructor */
103 : MSRailSignalControl();
104 :
105 : /// @brief compute additioanl deadlock-check requirements for registered driveways
106 : void updateDriveways(const MSEdge* used);
107 :
108 : /// @brief all rail edges that are part of a known route
109 : std::set<const MSEdge*> myUsedEdges;
110 :
111 : struct WaitRelation {
112 94356 : WaitRelation(const MSRailSignal* _railSignal = nullptr, const SUMOVehicle* _foe = nullptr, MSRailSignalConstraint* _constraint = nullptr) :
113 94356 : railSignal(_railSignal), foe(_foe), constraint(_constraint) {}
114 : // indices along route
115 : const MSRailSignal* railSignal;
116 : const SUMOVehicle* foe;
117 : MSRailSignalConstraint* constraint;
118 : };
119 : std::map<const SUMOVehicle*, WaitRelation> myWaitRelations;
120 :
121 : mutable std::set<std::set<const SUMOVehicle*> > myWrittenDeadlocks;
122 :
123 : std::map<const MSRailSignal*, std::vector<const MSRailSignal*> > myDeadlockChecks;
124 : std::map<const MSDriveWay*, std::set<const MSDriveWay*>> myDriveWaySucc;
125 : std::map<const MSDriveWay*, std::set<const MSDriveWay*>> myDriveWayPred;
126 :
127 : /// @brief list of all rail signals
128 : std::vector<MSRailSignal*> mySignals;
129 :
130 : /// @brief list of signals that switched green along with driveway index
131 : std::vector<std::pair<MSLink*, int> > mySwitchedGreenFlanks;
132 : std::map<std::pair<int, int>, bool> myDriveWayCompatibility;
133 : std::set<MSRailSignal*, ComparatorNumericalIdLess> myActiveSignals;
134 :
135 : static MSRailSignalControl* myInstance;
136 :
137 :
138 : };
|