Eclipse SUMO - Simulation of Urban MObility
NLJunctionControlBuilder.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2020 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 /****************************************************************************/
21 // Builder of microsim-junctions and tls
22 /****************************************************************************/
23 #include <config.h>
24 
25 #include <map>
26 #include <string>
27 #include <vector>
28 #include <list>
29 #include <algorithm>
32 #include <utils/common/ToString.h>
33 #include <microsim/MSGlobals.h>
34 #include <microsim/MSNet.h>
56 #include "NLBuilder.h"
58 
59 
60 // ===========================================================================
61 // static members
62 // ===========================================================================
64 
65 // ===========================================================================
66 // method definitions
67 // ===========================================================================
69  myNet(net),
70  myDetectorBuilder(db),
71  myOffset(0),
72  myJunctions(nullptr),
73  myNetIsLoaded(false) {
76 }
77 
78 
80  delete myLogicControl;
81  delete myJunctions;
82 }
83 
84 
85 void
87  const std::string& key,
88  const SumoXMLNodeType type,
89  const Position pos,
90  const PositionVector& shape,
91  const std::vector<MSLane*>& incomingLanes,
92  const std::vector<MSLane*>& internalLanes,
93  const std::string& name) {
94  myActiveInternalLanes = internalLanes;
95  myActiveIncomingLanes = incomingLanes;
96  myActiveID = id;
97  myActiveKey = key;
98  myType = type;
99  myPosition.set(pos);
100  myShape = shape;
101  myActiveName = name;
102  myAdditionalParameter.clear();
103 }
104 
105 
106 void
107 NLJunctionControlBuilder::closeJunction(const std::string& basePath) {
108  if (myJunctions == nullptr) {
109  throw ProcessError("Information about the number of nodes was missing.");
110  }
111  MSJunction* junction = nullptr;
112  switch (myType) {
118  junction = buildNoLogicJunction();
119  break;
127  junction = buildLogicJunction();
128  break;
131  junction = buildInternalJunction();
132  }
133  break;
136  myOffset = 0;
138  myActiveProgram = "0";
140  closeTrafficLightLogic(basePath);
141  junction = buildLogicJunction();
142  break;
143  default:
144  throw InvalidArgument("False junction logic type.");
145  }
146  if (junction != nullptr) {
147  if (!myJunctions->add(myActiveID, junction)) {
148  throw InvalidArgument("Another junction with the id '" + myActiveID + "' exists.");
149  }
150  }
152 }
153 
154 
158  myJunctions = nullptr;
159  return js;
160 }
161 
162 
163 MSJunction*
167 }
168 
169 
170 MSJunction*
173  // build the junction
177  jtype);
178 }
179 
180 
181 MSJunction*
183  // build the junction
186 }
187 
188 
191  // get and check the junction logic
192  if (myLogics.find(myActiveID) == myLogics.end()) {
193  throw InvalidArgument("Missing junction logic '" + myActiveID + "'.");
194  }
195  return myLogics[myActiveID];
196 }
197 
198 
200 NLJunctionControlBuilder::getTLLogic(const std::string& id) const {
201  return getTLLogicControlToUse().get(id);
202 }
203 
204 
205 void
207  if (myActiveProgram == "off") {
208  if (myAbsDuration > 0) {
209  throw InvalidArgument("The off program for TLS '" + myActiveKey + "' has phases.");
210  }
213  throw InvalidArgument("Another logic with id '" + myActiveKey + "' and programID '" + myActiveProgram + "' exists.");
214  }
215  return;
216  }
217  SUMOTime firstEventOffset = 0;
218  int step = 0;
219  MSTrafficLightLogic* existing = nullptr;
220  MSSimpleTrafficLightLogic::Phases::const_iterator i = myActivePhases.begin();
222  if (myAbsDuration == 0) {
224  if (existing == nullptr) {
225  throw InvalidArgument("TLS program '" + myActiveProgram + "' for TLS '" + myActiveKey + "' has a duration of 0.");
226  } else {
227  // only modify the offset of an existing logic
228  myAbsDuration = existing->getDefaultCycleTime();
229  i = existing->getPhases().begin();
230  }
231  }
232  // compute the initial step and first switch time of the tls-logic
233  // a positive offset delays all phases by x (advance by absDuration - x) while a negative offset advances all phases by x seconds
234  // @note The implementation of % for negative values is implementation defined in ISO1998
235  SUMOTime offset; // the time to run the traffic light in advance
236  if (myOffset >= 0) {
238  } else {
240  }
241  while (offset >= (*i)->duration) {
242  step++;
243  offset -= (*i)->duration;
244  ++i;
245  }
246  firstEventOffset = (*i)->duration - offset + myNet.getCurrentTimeStep();
247  if (existing != nullptr) {
249  myNet.getCurrentTimeStep(), step, (*i)->duration - offset);
250  return;
251  }
252  }
253 
254  if (myActiveProgram == "") {
255  myActiveProgram = "default";
256  }
257  MSTrafficLightLogic* tlLogic = nullptr;
258  // build the tls-logic in dependance to its type
259  switch (myLogicType) {
261  firstEventOffset = DELTA_T; //this is needed because swarm needs to update the pheromone on the lanes at every step
263  break;
266  break;
269  break;
272  break;
275  break;
278  break;
281  break;
283  // @note it is unclear how to apply the given offset in the context
284  // of variable-length phases
287  myActivePhases, step, (*i)->minDuration + myNet.getCurrentTimeStep(),
288  myAdditionalParameter, basePath);
289  break;
293  myActivePhases, step, (*i)->minDuration + myNet.getCurrentTimeStep(),
294  myAdditionalParameter, basePath);
295  break;
299  myActivePhases, step, firstEventOffset,
301  break;
303  tlLogic = new MSRailSignal(getTLLogicControlToUse(),
306  break;
308  tlLogic = new MSRailCrossing(getTLLogicControlToUse(),
311  break;
314  break;
316  throw ProcessError("Invalid traffic light type '" + toString(myLogicType) + "'");
317  }
318  myActivePhases.clear();
319  if (tlLogic != nullptr) {
320  if (getTLLogicControlToUse().add(myActiveKey, myActiveProgram, tlLogic)) {
321  if (myNetIsLoaded) {
322  tlLogic->init(myDetectorBuilder);
323  } else {
324  myLogics2PostLoadInit.push_back(tlLogic);
325  }
326  } else {
327  WRITE_ERROR("Another logic with id '" + myActiveKey + "' and programID '" + myActiveProgram + "' exists.");
328  delete tlLogic;
329  }
330  }
331 }
332 
333 
334 void
336  myActiveKey = id;
337  myActiveProgram = "";
338  myActiveLogic.clear();
339  myActiveFoes.clear();
340  myActiveConts.reset();
341  myRequestSize = NO_REQUEST_SIZE; // seems not to be used
343  myCurrentHasError = false;
344 }
345 
346 
347 void
349  const std::string& response,
350  const std::string& foes,
351  bool cont) {
352  if (myCurrentHasError) {
353  // had an error
354  return;
355  }
356  if (request >= SUMO_MAX_CONNECTIONS) {
357  // bad request
358  myCurrentHasError = true;
359  throw InvalidArgument("Junction logic '" + myActiveKey + "' is larger than allowed; recheck the network.");
360  }
362  // initialize
363  myRequestSize = (int)response.size();
364  }
365  if (static_cast<int>(response.size()) != myRequestSize) {
366  myCurrentHasError = true;
367  throw InvalidArgument("Invalid response size " + toString(response.size()) +
368  " in Junction logic '" + myActiveKey + "' (expected " + toString(myRequestSize) + ")");
369  }
370  if (static_cast<int>(foes.size()) != myRequestSize) {
371  myCurrentHasError = true;
372  throw InvalidArgument("Invalid foes size " + toString(foes.size()) +
373  " in Junction logic '" + myActiveKey + "' (expected " + toString(myRequestSize) + ")");
374  }
375  // assert that the logicitems come ordered by their request index
376  assert((int)myActiveLogic.size() == request);
377  assert((int)myActiveFoes.size() == request);
378  // add the read response for the given request index
379  myActiveLogic.push_back(std::bitset<SUMO_MAX_CONNECTIONS>(response));
380  // add the read junction-internal foes for the given request index
381  myActiveFoes.push_back(std::bitset<SUMO_MAX_CONNECTIONS>(foes));
382  // add whether the vehicle may drive a little bit further
383  myActiveConts.set(request, cont);
384  // increse number of set information
386 }
387 
388 
389 void
390 NLJunctionControlBuilder::initTrafficLightLogic(const std::string& id, const std::string& programID,
391  TrafficLightType type, SUMOTime offset) {
392  myActiveKey = id;
393  myActiveProgram = programID;
394  myActivePhases.clear();
395  myAbsDuration = 0;
397  myLogicType = type;
398  myOffset = offset;
399  myAdditionalParameter.clear();
400 }
401 
402 
403 void
404 NLJunctionControlBuilder::addPhase(SUMOTime duration, const std::string& state, const std::vector<int>& nextPhases, SUMOTime minDuration, SUMOTime maxDuration, const std::string& name, bool transient_notdecisional, bool commit, MSPhaseDefinition::LaneIdVector* targetLanes) {
405  // build and add the phase definition to the list
406  myActivePhases.push_back(new MSPhaseDefinition(duration, state, minDuration, maxDuration, nextPhases, name, transient_notdecisional, commit, targetLanes));
407  // add phase duration to the absolute duration
408  myAbsDuration += duration;
409 }
410 
411 
412 void
413 NLJunctionControlBuilder::addPhase(SUMOTime duration, const std::string& state, const std::vector<int>& nextPhases,
414  SUMOTime minDuration, SUMOTime maxDuration, const std::string& name) {
415  // build and add the phase definition to the list
416  myActivePhases.push_back(new MSPhaseDefinition(duration, state, minDuration, maxDuration, nextPhases, name));
417  // add phase duration to the absolute duration
418  myAbsDuration += duration;
419 }
420 
421 
422 void
425  // We have a legacy network. junction element did not contain logicitems; read the logic later
426  return;
427  }
428  if (myCurrentHasError) {
429  // had an error before...
430  return;
431  }
433  throw InvalidArgument("The description for the junction logic '" + myActiveKey + "' is malicious.");
434  }
435  if (myLogics.count(myActiveKey) > 0) {
436  throw InvalidArgument("Junction logic '" + myActiveKey + "' was defined twice.");
437  }
441  myActiveConts);
442  myLogics[myActiveKey] = logic;
443 }
444 
445 
448  postLoadInitialization(); // must happen after edgeBuilder is finished
450  throw ProcessError("Traffic lights could not be built.");
451  }
453  myLogicControl = nullptr;
454  return ret;
455 }
456 
457 
458 void
459 NLJunctionControlBuilder::addParam(const std::string& key,
460  const std::string& value) {
461  myAdditionalParameter[key] = value;
462 }
463 
464 
467  if (myLogicControl != nullptr) {
468  return *myLogicControl;
469  }
470  return myNet.getTLSControl();
471 }
472 
473 
474 const std::string&
476  return myActiveKey;
477 }
478 
479 
480 const std::string&
482  return myActiveProgram;
483 }
484 
485 
486 void
488  for (MSTrafficLightLogic* const logic : myLogics2PostLoadInit) {
489  logic->init(myDetectorBuilder);
490  }
491  myNetIsLoaded = true;
492 }
493 
494 
495 MSJunction*
496 NLJunctionControlBuilder::retrieve(const std::string id) {
497  if (myJunctions != nullptr) {
498  return myJunctions->get(id);
499  } else {
500  return nullptr;
501  }
502 }
503 
504 
505 /****************************************************************************/
MSBitSetLogic< SUMO_MAX_CONNECTIONS > MSBitsetLogic
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:284
SUMOTime DELTA_T
Definition: SUMOTime.cpp:37
long long int SUMOTime
Definition: SUMOTime.h:31
TrafficLightType
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
#define SUMO_MAX_CONNECTIONS
the maximum number of connections across an intersection
Definition: StdDefs.h:40
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
An actuated (adaptive) traffic light logic.
std::vector< std::bitset< N > > Foes
Container holding the information which internal lanes prohibt which links Build the same way as Logi...
Definition: MSBitSetLogic.h:53
std::vector< std::bitset< N > > Logic
Container that holds the right of way bitsets. Each link has it's own bitset. The bits in the bitsets...
Definition: MSBitSetLogic.h:49
An actuated traffic light logic based on time delay of approaching vehicles.
static bool gUsingInternalLanes
Information whether the simulation regards internal lanes.
Definition: MSGlobals.h:66
Container for junctions; performs operations on all stored junctions.
The base class for an intersection.
Definition: MSJunction.h:58
The simulated network and simulation perfomer.
Definition: MSNet.h:89
MSTLLogicControl & getTLSControl()
Returns the tls logics control.
Definition: MSNet.h:444
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:313
A traffic lights logic which represents a tls in an off-mode.
The definition of a single phase of a tls logic.
std::vector< std::string > LaneIdVector
A signal for rails.
A signal for rails.
Definition: MSRailSignal.h:46
A junction with right-of-way - rules.
Class for low-level marching policy.
Class for low-level phase policy.
Class for low-level platoon policy.
A self-organizing traffic light logic based on a particular policy.
Class for low-level request policy.
A fixed traffic light logic.
Storage for all programs of a single tls.
A class that stores and controls tls and switching of their programs.
bool closeNetworkReading()
Lets MSTLLogicControl know that the network has been loaded.
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
The parent class for traffic light logics.
virtual void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)=0
Changes the current phase and her duration.
SUMOTime getDefaultCycleTime() const
Returns the cycle time (in ms)
virtual const Phases & getPhases() const =0
Returns the phases of this tls program.
virtual void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
Builds detectors for microsim.
PositionVector myShape
The shape of the current junction.
NLJunctionControlBuilder(MSNet &net, NLDetectorBuilder &db)
Constructor.
void closeJunctionLogic()
Ends the building of a junction logic (row-logic)
int myRequestItemNumber
Counter for the inserted items.
SumoXMLNodeType myType
The type of the currently chosen junction.
void addPhase(SUMOTime duration, const std::string &state, const std::vector< int > &nextPhases, SUMOTime min, SUMOTime max, const std::string &name)
Adds a phase to the currently built traffic lights logic.
MSTLLogicControl::TLSLogicVariants & getTLLogic(const std::string &id) const
Returns a previously build tls logic.
std::string myActiveName
the name of the current junction
virtual ~NLJunctionControlBuilder()
Destructor.
MSJunction * retrieve(const std::string id)
try to retrieve junction by id
MSBitsetLogic::Foes myActiveFoes
The description about which lanes disallow other passing the junction simultaneously.
std::string myActiveID
The id of the currently chosen junction.
SUMOTime myOffset
The switch offset within the tls.
MSTLLogicControl * myLogicControl
The tls control to use (0 if net's tls control shall be used)
std::string myActiveKey
The key of the currently chosen junction.
TrafficLightType myLogicType
The current logic type.
bool myCurrentHasError
Information whether the current logic had an error.
MSTLLogicControl * buildTLLogics()
Returns the built tls-logic control.
MSJunctionControl * build() const
Builds the MSJunctionControl which holds all of the simulations junctions.
int myRequestSize
The size of the request.
MSTLLogicControl & getTLLogicControlToUse() const
Returns the used tls control.
LaneVector myActiveIncomingLanes
The list of the incoming lanes of the currently chosen junction.
std::map< std::string, MSJunctionLogic * > myLogics
Map of loaded junction logics.
const std::string & getActiveSubKey() const
Returns the active sub key.
StringParameterMap myAdditionalParameter
Parameter map (key->value)
void openJunction(const std::string &id, const std::string &key, const SumoXMLNodeType type, const Position pos, const PositionVector &shape, const std::vector< MSLane * > &incomingLanes, const std::vector< MSLane * > &internalLanes, const std::string &name)
Begins the processing of the named junction.
void addParam(const std::string &key, const std::string &value)
Adds a parameter.
LaneVector myActiveInternalLanes
The list of the internal lanes of the currently chosen junction.
void initJunctionLogic(const std::string &id)
Initialises a junction logic.
void initTrafficLightLogic(const std::string &id, const std::string &programID, TrafficLightType type, SUMOTime offset)
Begins the reading of a traffic lights logic.
virtual MSJunction * buildLogicJunction()
Builds a junction with a logic.
NLDetectorBuilder & myDetectorBuilder
The detector builder to use.
virtual MSJunction * buildNoLogicJunction()
Builds a junction that does not use a logic.
virtual MSJunction * buildInternalJunction()
Builds an internal junction.
MSJunctionControl * myJunctions
The junctions controls.
const std::string & getActiveKey() const
Returns the active key.
Position myPosition
The position of the junction.
MSJunctionLogic * getJunctionLogicSecure()
Returns the current junction logic.
void postLoadInitialization()
initialize junctions after all connections have been loaded
void addLogicItem(int request, const std::string &response, const std::string &foes, bool cont)
Adds a logic item.
std::vector< MSTrafficLightLogic * > myLogics2PostLoadInit
The container for information which junctions shall be initialised using which values.
MSBitsetLogic::Logic myActiveLogic
The right-of-way-logic of the currently chosen bitset-logic.
SUMOTime myAbsDuration
The absolute duration of a tls-control loop.
void closeJunction(const std::string &basePath)
Closes (ends) the processing of the current junction.
MSSimpleTrafficLightLogic::Phases myActivePhases
The current phase definitions for a simple traffic light.
bool myNetIsLoaded
whether the network has been loaded
virtual void closeTrafficLightLogic(const std::string &basePath)
Ends the building of a traffic lights logic.
std::bitset< SUMO_MAX_CONNECTIONS > myActiveConts
The description about which lanes have an internal follower.
T get(const std::string &id) const
Retrieves an item.
bool add(const std::string &id, T item)
Adds an item.
void updateParameters(const std::map< std::string, std::string > &mapArg)
Adds or updates all given parameters from the map.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:36
void set(double x, double y)
set positions x and y
Definition: Position.h:84
A list of positions.