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 MSRailSignalConstraint.h
15 : /// @author Jakob Erdmann
16 : /// @date August 2020
17 : ///
18 : // A constraint on rail signal switching
19 : /****************************************************************************/
20 : #pragma once
21 : #include <config.h>
22 :
23 : #include <utils/common/Parameterised.h>
24 : #include <microsim/MSMoveReminder.h>
25 :
26 : // ===========================================================================
27 : // class declarations
28 : // ===========================================================================
29 : class MSRailSignal;
30 : class SUMOVehicle;
31 : class SUMOSAXAttributes;
32 :
33 :
34 : // ===========================================================================
35 : // class definitions
36 : // ===========================================================================
37 : /**
38 : * @class MSRailSignalConstraint
39 : * @brief A base class for constraints
40 : */
41 : class MSRailSignalConstraint : public Parameterised {
42 : public:
43 :
44 : enum ConstraintType {
45 : PREDECESSOR = 0, // swaps to PREDECESSOR
46 : INSERTION_PREDECESSOR = 1, // swaps to FOE_INSERTION
47 : FOE_INSERTION = 2, // swaps to INSERTION_PREDECESSOR
48 : INSERTION_ORDER = 3, // swaps to INSERTION_ORDER
49 : BIDI_PREDECESSOR = 4 // swaps to BIDI_PREDECESSOR
50 : };
51 :
52 : /** @brief Constructor
53 : */
54 881 : MSRailSignalConstraint(ConstraintType type) : myType(type) {};
55 :
56 : /// @brief Destructor
57 881 : virtual ~MSRailSignalConstraint() {};
58 :
59 : /// @brief whether the constraint has been met
60 : virtual bool cleared() const = 0;
61 :
62 : virtual void setActive(bool active) = 0;
63 :
64 : virtual bool isActive() const = 0;
65 :
66 0 : virtual std::string getDescription() const {
67 0 : return "RailSignalConstraint";
68 : }
69 :
70 0 : virtual const SUMOVehicle* getFoe() const {
71 0 : return nullptr;
72 : }
73 :
74 : virtual void write(OutputDevice& out, const std::string& tripId) const = 0;
75 :
76 : ConstraintType getType() const {
77 4595 : return myType;
78 : }
79 :
80 : SumoXMLTag getTag() const {
81 3151 : switch (myType) {
82 : case INSERTION_PREDECESSOR:
83 : return SUMO_TAG_INSERTION_PREDECESSOR;
84 : case FOE_INSERTION:
85 : return SUMO_TAG_FOE_INSERTION;
86 : case INSERTION_ORDER:
87 : return SUMO_TAG_INSERTION_ORDER;
88 : case BIDI_PREDECESSOR:
89 : return SUMO_TAG_BIDI_PREDECESSOR;
90 : default:
91 : return SUMO_TAG_PREDECESSOR;
92 : }
93 : }
94 :
95 : ConstraintType getSwappedType() const {
96 204 : switch (myType) {
97 : case INSERTION_PREDECESSOR:
98 : return FOE_INSERTION;
99 0 : case FOE_INSERTION:
100 0 : return INSERTION_PREDECESSOR;
101 204 : default:
102 204 : return myType;
103 : }
104 : }
105 :
106 : bool isInsertionConstraint() const {
107 8016512 : return myType == INSERTION_PREDECESSOR || myType == INSERTION_ORDER;
108 : }
109 :
110 : static void storeTripId(const std::string& tripId, const std::string& vehID);
111 :
112 : static const std::string& lookupVehId(const std::string& tripId);
113 :
114 : /// @brief clean up state
115 : static void cleanup();
116 :
117 : /** @brief Saves the current constraint states into the given stream */
118 : static void saveState(OutputDevice& out);
119 :
120 : /** @brief Clear all constraint states before quick-loading state */
121 : static void clearState();
122 :
123 : /** @brief Remove all constraints before quick-loading state */
124 : static void clearAll();
125 :
126 : protected:
127 : static const SUMOVehicle* getVeh(const std::string& tripID, bool checkID = false);
128 :
129 : ConstraintType myType;
130 :
131 : static std::map<std::string, std::string> myTripIdLookup;
132 : };
133 :
134 :
135 : class MSRailSignalConstraint_Predecessor : public MSRailSignalConstraint {
136 : public:
137 : /** @brief Constructor
138 : */
139 : MSRailSignalConstraint_Predecessor(ConstraintType type, const MSRailSignal* signal, const std::string& tripId, int limit, bool active);
140 :
141 : /// @brief Destructor
142 1762 : ~MSRailSignalConstraint_Predecessor() {};
143 :
144 : void write(OutputDevice& out, const std::string& tripId) const;
145 :
146 : /// @brief clean up state
147 : static void cleanup();
148 :
149 : /** @brief Saves the current constraint states into the given stream */
150 : static void saveState(OutputDevice& out);
151 :
152 : /** @brief loads the constraint state from the given attrs */
153 : static void loadState(const SUMOSAXAttributes& attrs);
154 :
155 : /** @brief Clear all constraint states before quick-loading state */
156 : static void clearState();
157 :
158 : bool cleared() const;
159 :
160 63 : void setActive(bool active) {
161 63 : myAmActive = active;
162 63 : }
163 :
164 3371 : bool isActive() const {
165 3371 : return myAmActive;
166 : }
167 :
168 : std::string getDescription() const;
169 :
170 : const SUMOVehicle* getFoe() const;
171 :
172 : class PassedTracker : public MSMoveReminder {
173 : public:
174 : PassedTracker(MSLane* lane);
175 :
176 : /// @name inherited from MSMoveReminder
177 : //@{
178 : /// @brief tracks vehicles that passed this link (entered the next lane)
179 : bool notifyEnter(SUMOTrafficObject& veh, MSMoveReminder::Notification reason, const MSLane* enteredLane = 0);
180 : //@}
181 :
182 : void raiseLimit(int limit);
183 :
184 : bool hasPassed(const std::string& tripId, int limit) const;
185 :
186 : /** @brief Clear all passed states before quick-loading state */
187 : void clearState();
188 :
189 : /** @brief Saves the current passed states into the given stream */
190 : void saveState(OutputDevice& out);
191 :
192 : /** @brief loads the current passed states into the given stream */
193 : void loadState(int index, const std::vector<std::string>& tripIDs);
194 :
195 : /// @brief passed tripIds
196 : std::vector<std::string> myPassed;
197 :
198 : /// @brief index of the last passed object
199 : int myLastIndex;
200 : };
201 :
202 : /// @brief the tracker object for this constraint
203 : std::vector<PassedTracker*> myTrackers;
204 :
205 : /// @brief id of the predecessor that must already have passed
206 : const std::string myTripId;
207 :
208 : /// @brief the number of passed vehicles within which tripId must have occured
209 : const int myLimit;
210 :
211 : /// @brief Whether this constraint is currently active
212 : bool myAmActive;
213 :
214 : /// @brief store the foe signal (for TraCI access)
215 : const MSRailSignal* myFoeSignal;
216 :
217 :
218 : static std::map<const MSLane*, PassedTracker*, ComparatorNumericalIdLess> myTrackerLookup;
219 :
220 : private:
221 : /// invalidated assignment operator
222 : MSRailSignalConstraint_Predecessor& operator=(const MSRailSignalConstraint_Predecessor& s) = delete;
223 : };
|