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    NBTrafficLightLogicCont.h
11 /// @author  Daniel Krajzewicz
12 /// @author  Jakob Erdmann
13 /// @author  Michael Behrisch
14 /// @date    Sept 2002
15 /// @version $Id$
16 ///
17 // A container for traffic light definitions and built programs
18 /****************************************************************************/
19 #ifndef NBTrafficLightLogicCont_h
20 #define NBTrafficLightLogicCont_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <map>
29 #include <string>
30 #include "NBTrafficLightDefinition.h"
31 
32 
33 // ===========================================================================
34 // class declarations
35 // ===========================================================================
36 class OptionsCont;
37 class NBEdgeCont;
38 class OutputDevice;
39 
40 
41 // ===========================================================================
42 // class definitions
43 // ===========================================================================
44 /**
45  * @class NBTrafficLightLogicCont
46  * @brief A container for traffic light definitions and built programs
47  *
48  * This container class holds definitions of traffic light logics during
49  *  the loading of the network. After all information has been loaded, these
50  *  definitions are used to build the traffic light logics.
51  *
52  * The built traffic light logics are kept stored within this container during
53  *  their building and written to the network file at the end.
54  *
55  * @see NBTrafficLightDefinition
56  * @see NBTrafficLightLogic
57  */
58 class NBTrafficLightLogicCont {
59 public:
60     /// @brief Constructor
61     NBTrafficLightLogicCont();
62 
63     /// @brief Destructor
64     ~NBTrafficLightLogicCont();
65 
66     /** @brief Initialises the storage by applying given options
67      *
68      * Options, mainly setting offsets, are parsed
69      *  and the according internal variables are set.
70      *
71      * @param[in] oc The options container to read options from
72      * @todo Recheck exceptions
73      */
74     void applyOptions(OptionsCont& oc);
75 
76 
77     /** @brief Adds a logic definition to the dictionary
78      *
79      * "true" is returned if the logic is accepted - no logic with the same
80      *  name and programID exists within this container.
81      *
82      * @param[in] logic The logic to add
83      * @param[in] forceInsert If true, rename the program to make insertion succeed
84      * @return Whether the logic was valid (no logic with the same id and programID is already known)
85      */
86     bool insert(NBTrafficLightDefinition* logic, bool forceInsert = false);
87 
88 
89     /** @brief Removes a logic definition (and all programs) from the dictionary
90      *
91      * "true" is returned if the logic existed in the dictionary,
92      *  otherwise "false".
93      *
94      * @param[in] id The id of the logic to remove
95      * @return Whether the named logic was within the dictionary
96      */
97     bool removeFully(const std::string id);
98 
99 
100     /** @brief Removes a program of a logic definition from the dictionary
101      *
102      * "true" is returned if the program existed in the dictionary,
103      *  otherwise "false".
104      *
105      * @param[in] id The id of the logic
106      * @param[in] programID The id of the program to remove
107      * @param[in] del Whether the definition shall be deleted
108      * @return Whether the program was within the dictionary
109      */
110     bool removeProgram(const std::string id, const std::string programID, bool del = true);
111 
112 
113     /** @brief Extracts a traffic light definition from myDefinitions
114      * but keeps it in myExtracted for eventual * deletion (used by NETEDIT)
115      */
116     void extract(NBTrafficLightDefinition* definition);
117 
118 
119     /// @brief return the number of extracted traffic light definitions
getNumExtracted()120     int getNumExtracted() const {
121         return (int)myExtracted.size();
122     }
123 
124 
125     /// @brief Returns a list of all computed logics
126     std::vector<NBTrafficLightLogic*> getComputed() const;
127 
128 
129     /** @brief Computes the traffic light logics using the stored definitions and stores the results
130      *
131      * Goes through all stored definitions and calls "NBTrafficLightDefinition::compute"
132      *  for each. Stores the result using "insert".
133      *
134      * @param[in] oc Options used during the computation
135      * @return The number of computed tls and programs
136      * @see NBTrafficLightDefinition::compute
137      */
138     std::pair<int, int> computeLogics(OptionsCont& oc);
139 
140 
141     /** @brief Computes a specific traffic light logic (using by NETEDIT)
142      *
143      * @param[in] oc Options used during the computation
144      * @return whether the logic was computed successfully
145      * @see NBTrafficLightDefinition::compute
146      */
147     bool computeSingleLogic(OptionsCont& oc, NBTrafficLightDefinition* def);
148 
149 
150     /** @brief Replaces occurences of the removed edge in incoming/outgoing edges of all definitions
151      *
152      * @param[in] removed The removed edge
153      * @param[in] incoming The edges to use instead if an incoming edge was removed
154      * @param[in] outgoing The edges to use instead if an outgoing edge was removed
155      * @todo Recheck usage
156      */
157     void remapRemoved(NBEdge* removed,
158                       const EdgeVector& incoming, const EdgeVector& outgoing);
159 
160 
161     /** @brief Replaces occurences of the removed edge/lane in all definitions by the given edge
162      *
163      * @param[in] removed The removed edge
164      * @param[in] removed The removed lane
165      * @param[in] by The edge to use instead
166      * @param[in] byLane The lane to use instead
167      * @todo Recheck usage
168      */
169     void replaceRemoved(NBEdge* removed, int removedLane,
170                         NBEdge* by, int byLane);
171 
172 
173     /** @brief Returns the named definition
174      *
175      * @param[in] id The id of the definition to return
176      * @param[in] programID The id of the program to return
177      * @return The named definition, 0 if it is not known
178      */
179     NBTrafficLightDefinition* getDefinition(const std::string& id, const std::string& programID) const;
180 
181 
182     /** @brief Returns all programs for the given tl-id
183      *
184      * @param[in] id The tl-id for which to return all programs
185      * @return The map of programIDs to definitions
186      */
187     const std::map<std::string, NBTrafficLightDefinition*>& getPrograms(const std::string& id) const;
188 
189 
190     /** @brief Returns the computed logic for the given name
191      *
192      * @param[in] id The id of the logic to return
193      * @param[in] programID The id of the program to return
194      * @return The named definition, 0 if it is not known
195      */
196     NBTrafficLightLogic* getLogic(const std::string& id, const std::string& programID) const;
197 
198 
199     /** @brief Informs the edges about being controlled by a tls
200      *
201      * Goes through all definition, calling eachs "setParticipantsInformation" method.
202      * Goes through all definition, calling eachs "setTLControllingInformation" method.
203      *
204      * @param[in] ec The ede control to set information into
205      * @see NBTrafficLightDefinition::setParticipantsInformation
206      * @see NBTrafficLightDefinition::setTLControllingInformation
207      */
208     void setTLControllingInformation(const NBEdgeCont& ec, const NBNodeCont& nc);
209 
210     /// @brief Returns a list of all definitions (convenience for easier iteration)
211     typedef std::vector<NBTrafficLightDefinition*> Definitions;
212     Definitions getDefinitions() const;
213 
214 private:
215     /// @brief Definition of internal the container types
216     typedef std::map<std::string, NBTrafficLightLogic*> Program2Logic;
217     typedef std::map<std::string, Program2Logic> Id2Logics;
218     typedef std::map<std::string, NBTrafficLightDefinition*> Program2Def;
219     typedef std::map<std::string, Program2Def> Id2Defs;
220     typedef std::vector<NBTrafficLightLogic*> Logics;
221 
222     /// @brief The container for previously computed tl-logics
223     Id2Logics myComputed;
224 
225     /// @brief The container for tl-ids to their definitions
226     Id2Defs myDefinitions;
227 
228     /// @brief The container for extracted definitions
229     std::set<NBTrafficLightDefinition*> myExtracted;
230 
231     /// @brief List of tls which shall have an offset of T/2
232     std::set<std::string> myHalfOffsetTLS;
233 
234     /// @brief List of tls which shall have an offset of T/2
235     std::set<std::string> myQuarterOffsetTLS;
236 
237     static const Program2Def EmptyDefinitions;
238 
239 private:
240 
241     /** @brief Destroys all stored definitions and logics
242      */
243     void clear();
244 
245 
246 };
247 
248 
249 #endif
250 
251 /****************************************************************************/
252 
253