1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
10 /// @file    NBTrafficLightLogic.h
11 /// @author  Daniel Krajzewicz
12 /// @author  Jakob Erdmann
13 /// @author  Michael Behrisch
14 /// @date    Sept 2002
15 /// @version $Id$
16 ///
17 // A SUMO-compliant built logic for a traffic light
18 /****************************************************************************/
19 #ifndef NBTrafficLightLogic_h
20 #define NBTrafficLightLogic_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <vector>
29 #include <string>
30 #include <bitset>
31 #include <utility>
32 #include <set>
33 #include "NBConnectionDefs.h"
34 #include <utils/common/SUMOTime.h>
35 #include <utils/common/Named.h>
36 #include <utils/common/Parameterised.h>
37 
38 
39 // ===========================================================================
40 // class declarations
41 // ===========================================================================
42 class OutputDevice;
43 
44 
45 // ===========================================================================
46 // class definitions
47 // ===========================================================================
48 /**
49  * @class NBTrafficLightLogic
50  * @brief A SUMO-compliant built logic for a traffic light
51  */
52 class NBTrafficLightLogic : public Named, public Parameterised {
53 public:
54     /**
55      * @class PhaseDefinition
56      * @brief The definition of a single phase of the logic
57      */
58     class PhaseDefinition {
59     public:
60         /// @brief The duration of the phase in s
61         SUMOTime duration;
62         SUMOTime minDur;
63         SUMOTime maxDur;
64 
65         /// @brief The state definition
66         std::string state;
67 
68         /// @brief next phase indices or empty list
69         std::vector<int> next;
70         /// @brief option phase name
71         std::string name;
72 
73         /** @brief Constructor
74          * @param[in] durationArg The duration of the phase
75          * @param[in] stateArg Signals per link
76          */
PhaseDefinition(SUMOTime durationArg,const std::string & stateArg,SUMOTime minDurArg,SUMOTime maxDurArg,const std::vector<int> & nextArg,const std::string & nameArg)77         PhaseDefinition(SUMOTime durationArg, const std::string& stateArg, SUMOTime minDurArg, SUMOTime maxDurArg, const std::vector<int>& nextArg, const std::string& nameArg) :
78             duration(durationArg),
79             minDur(minDurArg),
80             maxDur(maxDurArg),
81             state(stateArg),
82             next(nextArg),
83             name(nameArg)
84         { }
85 
86         /// @brief Destructor
~PhaseDefinition()87         ~PhaseDefinition() { }
88 
89         /** @brief Comparison operator
90          * @param[in] pd A second phase
91          * @return Whether this and the given phases are same
92          */
93         bool operator!=(const PhaseDefinition& pd) const {
94             return (pd.duration != duration
95                     || pd.minDur != minDur
96                     || pd.maxDur != maxDur
97                     || pd.state != state
98                     || pd.next != next
99                     || pd.name != name);
100         }
101 
102     };
103 
104 
105     /** @brief Constructor
106      * @param[in] id The id of the traffic light
107      * @param[in] subid The id of the program
108      * @param[in] noLinks Number of links that are controlled by this tls. 0 means the value is not known beforehand
109      * @param[in] offset The offset of the program (delay)
110      * @param[in] type The algorithm type for the computed traffic light
111      */
112     NBTrafficLightLogic(const std::string& id, const std::string& subid, int noLinks,
113                         SUMOTime offset = 0, TrafficLightType type = TLTYPE_STATIC);
114 
115 
116     /** @brief Copy Constructor
117      * @param[in] logic The logic to copy
118      */
119     NBTrafficLightLogic(const NBTrafficLightLogic* logic);
120 
121 
122     /// @brief Destructor
123     ~NBTrafficLightLogic();
124 
125 
126     /** @brief Adds a phase to the logic
127      *
128      * @param[in] duration The duration of the phase to add
129      * @param[in] state The state definition of a tls phase
130      * @param[in] minDur The minimum duration of the phase to add
131      * @param[in] maxDur The maximum duration of the phase to add
132      * @param[in] name The name of the phase
133      * @param[in] next The index of the next phase
134      * @param[in] index The index of the new phase (-1 means append to end)
135      * @note: the length of the state has to match the number of links
136      *        and the length given in previous calls to addStep (throws ProcessError)
137      */
138     void addStep(SUMOTime duration, const std::string& state,
139                  const std::vector<int>& next = std::vector<int>(), const std::string& name = "", int index = -1);
140     void addStep(SUMOTime duration, const std::string& state, SUMOTime minDur, SUMOTime maxDur,
141                  const std::vector<int>& next = std::vector<int>(), const std::string& name = "", int index = -1);
142 
143 
144     /** @brief Modifies the state for an existing phase (used by NETEDIT)
145      * @param[in] phaseIndex The index of the phase to modify
146      * @param[in] tlIndex The index at which to modify the state
147      * @param[in] linkState The new link state for the given index
148      */
149     void setPhaseState(int phaseIndex, int tlIndex, LinkState linkState);
150 
151     /** @brief Modifies the duration for an existing phase (used by NETEDIT)
152      * @param[in] phaseIndex The index of the phase to modify
153      * @param[in] duration The new duration for this phase
154      */
155     void setPhaseDuration(int phaseIndex, SUMOTime duration);
156     void setPhaseMinDuration(int phaseIndex, SUMOTime duration);
157     void setPhaseMaxDuration(int phaseIndex, SUMOTime duration);
158     void setPhaseNext(int phaseIndex, const std::vector<int>& next);
159     void setPhaseName(int phaseIndex, const std::string& name);
160 
161     /* @brief deletes the phase at the given index
162      * @note thhrows InvalidArgument on out-of range index
163     */
164     void deletePhase(int index);
165 
166     /* @brief changes state size either by cutting of at the end or by adding
167      * new states at the end
168     */
169     void setStateLength(int numLinks, LinkState fill = LINKSTATE_TL_RED);
170 
171     /* @brief deletes all phases and reset the expect number of links
172     */
173     void resetPhases();
174 
175     /** @brief closes the building process
176      *
177      * Joins equal steps.
178      */
179     void closeBuilding(bool checkVarDurations = true);
180 
181 
182     /** @brief Returns the duration of the complete cycle
183      * @return The duration of this logic's cycle
184      */
185     SUMOTime getDuration() const;
186 
187 
188     /** @brief Sets the offset of this tls
189      * @param[in] offset The offset of this cycle
190      */
setOffset(SUMOTime offset)191     void setOffset(SUMOTime offset) {
192         myOffset = offset;
193     }
194 
195 
196     /** @brief Returns the ProgramID
197      * @return The ID of the program (subID)
198      */
getProgramID()199     const std::string& getProgramID() const {
200         return mySubID;
201     };
202 
203 
204     /** @brief Returns the phases
205      * @return The phase list
206      */
getPhases()207     const std::vector<PhaseDefinition>& getPhases() const {
208         return myPhases;
209     }
210 
211 
212     /** @brief Returns the offset of first switch
213      * @return The switch offset
214      */
getOffset()215     SUMOTime getOffset() const {
216         return myOffset;
217     };
218 
219 
220     /** @brief Returns the number of participating links
221      */
getNumLinks()222     int getNumLinks() {
223         return myNumLinks;
224     }
225 
226     /// @brief get the algorithm type (static etc..)
getType()227     TrafficLightType getType() const {
228         return myType;
229     }
230 
231     /// @brief set the algorithm type (static etc..)
setType(TrafficLightType type)232     void setType(TrafficLightType type) {
233         myType = type;
234     }
235 
236 private:
237     /// @brief The number of participating links
238     int myNumLinks;
239 
240     /// @brief The tls program's subid
241     const std::string mySubID;
242 
243     /// @brief The tls program's offset
244     SUMOTime myOffset;
245 
246     /// @brief Definition of a vector of traffic light phases
247     typedef std::vector<PhaseDefinition> PhaseDefinitionVector;
248 
249     /// @brief The junction logic's storage for traffic light phase list
250     PhaseDefinitionVector myPhases;
251 
252     /// @brief The algorithm type for the traffic light
253     TrafficLightType myType;
254 
255 private:
256     /// @brief Invalidated assignment operator
257     NBTrafficLightLogic& operator=(const NBTrafficLightLogic& s);
258 
259 };
260 
261 
262 #endif
263 
264 /****************************************************************************/
265 
266