Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/netload/NLJunctionControlBuilder.h
169666 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 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 NLJunctionControlBuilder.h
15
/// @author Daniel Krajzewicz
16
/// @author Jakob Erdmann
17
/// @author Michael Behrisch
18
/// @date Mon, 9 Jul 2001
19
///
20
// Builder of microsim-junctions and tls
21
/****************************************************************************/
22
#pragma once
23
#include <config.h>
24
25
#include <string>
26
#include <vector>
27
#include <microsim/MSJunction.h>
28
#include <microsim/MSRightOfWayJunction.h>
29
#include <microsim/MSJunctionControl.h>
30
#include <utils/geom/Position.h>
31
#include <utils/geom/PositionVector.h>
32
#include <microsim/traffic_lights/MSSimpleTrafficLightLogic.h>
33
#include <microsim/traffic_lights/MSActuatedTrafficLightLogic.h>
34
#include <microsim/traffic_lights/MSRailSignal.h>
35
#include <microsim/MSBitSetLogic.h>
36
#include <microsim/traffic_lights/MSTLLogicControl.h>
37
#include <utils/common/UtilExceptions.h>
38
39
40
// ===========================================================================
41
// class declarations
42
// ===========================================================================
43
class OptionsCont;
44
class NLDetectorBuilder;
45
46
47
// ===========================================================================
48
// class definitions
49
// ===========================================================================
50
/**
51
* @class NLJunctionControlBuilder
52
* @brief Builder of microsim-junctions and tls
53
*
54
* NLJunctionControlBuilder is a factory for MSJunction, MSJunctionLogic, WAUT,
55
* and MSTRafficLightLogic-instances.
56
*
57
* @todo Refactor this class - it's too large
58
* @todo Resort method by one of the topics.
59
*/
60
class NLJunctionControlBuilder {
61
private:
62
/// @brief Definition of a lane vector
63
typedef std::vector<MSLane*> LaneVector;
64
65
public:
66
/** @brief Constructor
67
*
68
* Stores default values for extended tls reading them from the given
69
* options. Builds a MSTLLogicControl instance for myLogicControl.
70
* @param[in] net The network to fill
71
* @param[in] db The detector builder to use
72
*/
73
NLJunctionControlBuilder(MSNet& net, NLDetectorBuilder& db);
74
75
76
/** @brief Destructor
77
*
78
* Deletes previously allocated "myLogicControl" and "myJunctions" if
79
* they were not previously returned (this may happen if an error occurred).
80
*/
81
virtual ~NLJunctionControlBuilder();
82
83
84
/** @brief Begins the processing of the named junction
85
*
86
* @param[in] id The ID of the junction
87
* @param[in] key unused?!
88
* @param[in] type The type of the junction
89
* @param[in] x x-position of the junction
90
* @param[in] y y-position of the junction
91
* @param[in] shape The shape of the junction
92
* @param[in] incomingLanes List of lanes which end at this intersection
93
* @param[in] internalLanes List of internal lanes across this intersection
94
* @exception InvalidArgument If the junction type is not known
95
* @todo Check why "key" is given
96
*/
97
void openJunction(const std::string& id, const std::string& key,
98
const SumoXMLNodeType type,
99
const Position pos,
100
const PositionVector& shape,
101
const std::vector<MSLane*>& incomingLanes,
102
const std::vector<MSLane*>& internalLanes,
103
const std::string& name);
104
105
106
/** @brief Closes (ends) the processing of the current junction
107
*
108
* This method throws an InvalidArgument when a junction with the same id
109
* as the current was already added or if the junction type stored in "myType"
110
* is invalid. It throws a ProcessError if the container to store the
111
* junction in was not built before.
112
*
113
* @exception InvalidArgument If the current id is already used or the junction type was invalid
114
* @exception ProcessError If the container to store the junction in was not built before
115
* @todo Throwing ProcessError would get unneeded if the container would be built by default (see prepare)
116
* @todo The type of the junctions shoould probably be checked when supprted (in openJunction)
117
*/
118
void closeJunction(const std::string& basePath);
119
120
121
/** @brief Builds the MSJunctionControl which holds all of the simulations junctions
122
*
123
* Returns the previously built junction control ("myJunctions"). "myJunctions" is
124
* set to 0, so that it will not be destroyed by the destructor.
125
*
126
* @return The built junction control
127
*/
128
MSJunctionControl* build() const;
129
130
131
/** @brief Initialises a junction logic
132
* @param[in] id The id of the row-logic
133
*/
134
void initJunctionLogic(const std::string& id);
135
136
137
/** @brief Adds a logic item
138
*
139
* Adds a logic item to the current processed logic. Throws an InvalidArgument
140
* if the current request size (myRequestSize) is 0 or lower.
141
*
142
* @param[in] request The request (~link) index
143
* @param[in] response The response (list of higher priorised links)
144
* @param[in] foes List of foes to the request
145
* @param[in] cont Whether the request is followed by an internal end
146
* @todo Recheck "cont"; is the description correct?
147
*/
148
void addLogicItem(int request, const std::string& response,
149
const std::string& foes, bool cont);
150
151
/** @brief Begins the reading of a traffic lights logic
152
*
153
* @param[in] id The id of the tls
154
* @param[in] programID The id of the currently loaded program
155
* @param[in] type The type of the tls
156
* @param[in] offset The offset to start with
157
* @todo Why is the type not verified?
158
* @todo Recheck, describe usage of detectorOffset (where does the information come from?)
159
*/
160
void initTrafficLightLogic(const std::string& id, const std::string& programID,
161
TrafficLightType type, SUMOTime offset);
162
163
/** @brief Adds a phase to the currently built traffic lights logic
164
*
165
* @param[in] phase The new phase
166
*/
167
void addPhase(MSPhaseDefinition* phase);
168
169
/** @brief Adds a condition to the currently built traffic lights logic
170
*
171
* @param[in] id the condition id
172
* @param[in] value the condition expression
173
*/
174
bool addCondition(const std::string& id, const std::string& value);
175
176
/** @brief Adds an assignment to the currently built traffic lights logic
177
*
178
* @param[in] id the condition id
179
* @param[in] check the check condition that guards the assignment
180
* @param[in] value the assigned expression
181
*/
182
void addAssignment(const std::string& id, const std::string& check, const std::string& value);
183
184
/** @brief adds a switching condition function to the traffic lights logic currently build
185
*
186
* @param[in] id the function id
187
* @param[in] nArgs the number of arguments
188
*/
189
void addFunction(const std::string& id, int nArgs);
190
191
/// closes a switching condition function to the traffic lights logic currently build
192
void closeFunction();
193
194
/** @brief Returns a previously build tls logic
195
*
196
* @param[in] id The ID of the tls logic to return
197
* @return The named logic
198
* @exception InvalidArgument If the named tls logic was not built before
199
*/
200
MSTLLogicControl::TLSLogicVariants& getTLLogic(const std::string& id) const;
201
202
/** @brief Returns the built tls-logic control
203
*
204
* Returns the junction control ("myLogicControl") built using MSTLLogicControl::closeNetworkReading.
205
* "myLogicControl" is set to 0, so that it will not be destroyed by the destructor.
206
*
207
* @return The built tls-logic control
208
* @exception ProcessError If the traffic lights were not correct
209
* @see MSTLLogicControl::closeNetworkReading
210
*/
211
MSTLLogicControl* buildTLLogics();
212
213
/** @brief Ends the building of a traffic lights logic
214
*
215
* Builds the correct type of a MSTrafficLightLogic using the stored information.
216
* Tries to add it to the used tls control. Throws an InvalidArgument if
217
* this is not possible (another tls logic with the same name exists).
218
*
219
* @exception InvalidArgument If another tls logic with the same name as the currently built was loaded before
220
*/
221
virtual void closeTrafficLightLogic(const std::string& basePath);
222
223
/** @brief Adds a parameter
224
*
225
* @param[in] key The key of the parameter
226
* @param[in] value The value of the parameter
227
* @todo Where are these parameter used? Describe!
228
* @todo Can a parameter be overwritten?
229
*/
230
void addParam(const std::string& key, const std::string& value);
231
232
/** @brief Returns the active key
233
* @return The active key
234
*/
235
const std::string& getActiveKey() const;
236
237
/** @brief Returns the active sub key
238
* @return The active sub key
239
*/
240
const std::string& getActiveSubKey() const;
241
242
/** @brief Returns the used tls control
243
*
244
* This may be either the internal one ("myLogicControl"), or, if the network has
245
* been already loaded and we are loading additional tls programs, the net's logic control.
246
*
247
* @return The tls control to use
248
*/
249
MSTLLogicControl& getTLLogicControlToUse() const;
250
251
/// @brief initialize junctions after all connections have been loaded
252
void postLoadInitialization();
253
254
/// @brief try to retrieve junction by id
255
MSJunction* retrieve(const std::string id);
256
257
/// @brief return the phases loaded so far (for error reporting and cleanup)
258
const MSSimpleTrafficLightLogic::Phases& getLoadedPhases() const {
259
return myActivePhases;
260
}
261
262
void netIsLoaded() {
263
myNetIsLoaded = true;
264
}
265
266
protected:
267
/// @name Factory methods, virtual so that other versions of the structures can be built
268
/// @{
269
270
/** @brief Builds a junction that does not use a logic
271
*
272
* Builds a MSNoLogicJunction
273
*
274
* @return The built junction
275
*/
276
virtual MSJunction* buildNoLogicJunction();
277
278
/** @brief Builds a junction with a logic
279
*
280
* Builds a MSRightOfWayJunction. Throws an exception if the logic was not built.
281
*
282
* @return The built junction
283
* @exception InvalidArgument If the logic of the junction was not built before
284
*/
285
virtual MSJunction* buildLogicJunction(MSJunctionLogic* const logic);
286
287
/** @brief Builds an internal junction
288
*
289
* Builds a MSInternalJunction
290
*
291
* @return The built junction
292
*/
293
virtual MSJunction* buildInternalJunction();
294
/// @}
295
296
protected:
297
/// @brief The net to use
298
MSNet& myNet;
299
300
/// @brief The detector builder to use
301
NLDetectorBuilder& myDetectorBuilder;
302
303
/// @brief The switch offset within the tls
304
SUMOTime myOffset;
305
306
/// @brief The current logic type
307
TrafficLightType myLogicType;
308
309
/// @brief The right-of-way-logic of the currently chosen bitset-logic
310
MSBitsetLogic::Logic myActiveLogic;
311
312
/// @brief The description about which lanes disallow other passing the junction simultaneously
313
MSBitsetLogic::Foes myActiveFoes;
314
315
/// @brief The description about which lanes have an internal follower
316
std::bitset<SUMO_MAX_CONNECTIONS> myActiveConts;
317
318
/// @brief The current phase definitions for a simple traffic light
319
MSSimpleTrafficLightLogic::Phases myActivePhases;
320
321
/// @brief The current switching conditions for an actuated traffic light
322
MSActuatedTrafficLightLogic::ConditionMap myActiveConditions;
323
324
/// @brief The current assignments for an actuated traffic light
325
MSActuatedTrafficLightLogic::AssignmentMap myActiveAssignments;
326
327
/// @brief The current functions for an actuated traffic light
328
MSActuatedTrafficLightLogic::FunctionMap myActiveFunctions;
329
330
/// @brief The current function for an actuated traffic light
331
MSActuatedTrafficLightLogic::Function myActiveFunction;
332
333
/// @brief The size of the request
334
int myRequestSize;
335
336
/// @brief Counter for the inserted items
337
int myRequestItemNumber;
338
339
/// @brief The junctions controls
340
mutable MSJunctionControl* myJunctions;
341
342
/// @brief The list of the incoming lanes of the currently chosen junction
343
LaneVector myActiveIncomingLanes;
344
345
/// @brief The list of the internal lanes of the currently chosen junction
346
LaneVector myActiveInternalLanes;
347
348
/// @brief The id of the currently chosen junction
349
std::string myActiveID;
350
351
/// @brief The key of the currently chosen junction
352
std::string myActiveKey, myActiveProgram;
353
354
/// @brief The type of the currently chosen junction
355
SumoXMLNodeType myType;
356
357
/// @brief The position of the junction
358
Position myPosition;
359
360
/// @brief The absolute duration of a tls-control loop
361
SUMOTime myAbsDuration;
362
363
/// @brief The shape of the current junction
364
PositionVector myShape;
365
366
/// @brief the name of the current junction
367
std::string myActiveName;
368
369
/// @brief Definition of a parameter map (key->value)
370
typedef Parameterised::Map StringParameterMap;
371
372
/// @brief The container for information which junctions shall be initialised using which values
373
std::vector<MSTrafficLightLogic*> myNetworkLogics;
374
std::vector<MSTrafficLightLogic*> myAdditionalLogics;
375
std::vector<MSTrafficLightLogic*> myRailSignals;
376
std::map<MSTrafficLightLogic*, StringParameterMap> myLogicParams;
377
378
/// @brief The tls control to use (0 if net's tls control shall be used)
379
mutable MSTLLogicControl* myLogicControl;
380
381
/// @brief Parameter map (key->value)
382
StringParameterMap myAdditionalParameter;
383
384
/// @brief Information whether the current logic had an error
385
bool myCurrentHasError;
386
387
private:
388
/** @brief invalidated copy operator */
389
NLJunctionControlBuilder(const NLJunctionControlBuilder& s);
390
391
/** @brief invalidated assignment operator */
392
NLJunctionControlBuilder& operator=(const NLJunctionControlBuilder& s);
393
394
static const int NO_REQUEST_SIZE;
395
396
/// @brief whether the network has been loaded
397
bool myNetIsLoaded;
398
399
400
};
401
402