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