SUMO - Simulation of Urban MObility
NLJunctionControlBuilder.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Builder of microsim-junctions and tls
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <map>
35 #include <string>
36 #include <vector>
37 #include <list>
38 #include <algorithm>
58 #include <microsim/MSGlobals.h>
59 #include <microsim/MSNet.h>
64 #include <utils/common/ToString.h>
65 #include <netbuild/NBNode.h>
66 #include "NLBuilder.h"
68 
69 #ifdef CHECK_MEMORY_LEAKS
70 #include <foreign/nvwa/debug_new.h>
71 #endif // CHECK_MEMORY_LEAKS
72 
73 
74 // ===========================================================================
75 // static members
76 // ===========================================================================
78 
79 // ===========================================================================
80 // method definitions
81 // ===========================================================================
83  myNet(net),
84  myDetectorBuilder(db),
85  myOffset(0),
86  myJunctions(0),
87  myNetIsLoaded(false) {
90 }
91 
92 
94  delete myLogicControl;
95  delete myJunctions;
96 }
97 
98 
99 void
101  const std::string& key,
102  const SumoXMLNodeType type,
103  SUMOReal x, SUMOReal y,
104  const PositionVector& shape,
105  const std::vector<MSLane*>& incomingLanes,
106  const std::vector<MSLane*>& internalLanes) {
107 #ifdef HAVE_INTERNAL_LANES
108  myActiveInternalLanes = internalLanes;
109 #else
110  UNUSED_PARAMETER(internalLanes);
111 #endif
112  myActiveIncomingLanes = incomingLanes;
113  myActiveID = id;
114  myActiveKey = key;
115  myType = type;
116  myPosition.set(x, y);
117  myShape = shape;
118 }
119 
120 
121 void
122 NLJunctionControlBuilder::closeJunction(const std::string& basePath) {
123  if (myJunctions == 0) {
124  throw ProcessError("Information about the number of nodes was missing.");
125  }
126  MSJunction* junction = 0;
127  switch (myType) {
128  case NODETYPE_NOJUNCTION:
129  case NODETYPE_DEAD_END:
131  case NODETYPE_DISTRICT:
133  junction = buildNoLogicJunction();
134  break;
138  case NODETYPE_PRIORITY:
141  case NODETYPE_ZIPPER:
142  junction = buildLogicJunction();
143  break;
144  case NODETYPE_INTERNAL:
145 #ifdef HAVE_INTERNAL_LANES
147  junction = buildInternalJunction();
148  }
149 #endif
150  break;
153  myOffset = 0;
155  myActiveProgram = "0";
157  closeTrafficLightLogic(basePath);
158  junction = buildLogicJunction();
159  break;
160  default:
161  throw InvalidArgument("False junction logic type.");
162  }
163  if (junction != 0) {
164  if (!myJunctions->add(myActiveID, junction)) {
165  throw InvalidArgument("Another junction with the id '" + myActiveID + "' exists.");
166  }
167  }
168 }
169 
170 
174  myJunctions = 0;
175  return js;
176 }
177 
178 
179 MSJunction*
182 #ifdef HAVE_INTERNAL_LANES
183  , myActiveInternalLanes
184 #endif
185  );
186 }
187 
188 
189 MSJunction*
192  // build the junction
194 #ifdef HAVE_INTERNAL_LANES
195  myActiveInternalLanes,
196 #endif
197  jtype);
198 }
199 
200 
201 #ifdef HAVE_INTERNAL_LANES
202 MSJunction*
203 NLJunctionControlBuilder::buildInternalJunction() {
204  // build the junction
206  myActiveInternalLanes);
207 }
208 #endif
209 
210 
213  // get and check the junction logic
214  if (myLogics.find(myActiveID) == myLogics.end()) {
215  throw InvalidArgument("Missing junction logic '" + myActiveID + "'.");
216  }
217  return myLogics[myActiveID];
218 }
219 
220 
222 NLJunctionControlBuilder::getTLLogic(const std::string& id) const {
223  return getTLLogicControlToUse().get(id);
224 }
225 
226 
227 void
229  if (myActiveProgram == "off") {
230  if (myAbsDuration > 0) {
231  throw InvalidArgument("The off program for TLS '" + myActiveKey + "' has phases.");
232  }
235  throw InvalidArgument("Another logic with id '" + myActiveKey + "' and subid '" + myActiveProgram + "' exists.");
236  }
237  return;
238  }
239  SUMOTime firstEventOffset = 0;
240  int step = 0;
241  MSTrafficLightLogic* existing = 0;
242  MSSimpleTrafficLightLogic::Phases::const_iterator i = myActivePhases.begin();
243  if (myLogicType != TLTYPE_RAIL) {
244  if (myAbsDuration == 0) {
246  if (existing == 0) {
247  throw InvalidArgument("TLS program '" + myActiveProgram + "' for TLS '" + myActiveKey + "' has a duration of 0.");
248  } else {
249  // only modify the offset of an existing logic
250  myAbsDuration = existing->getDefaultCycleTime();
251  i = existing->getPhases().begin();
252  }
253  }
254  // compute the initial step and first switch time of the tls-logic
255  // a positive offset delays all phases by x (advance by absDuration - x) while a negative offset advances all phases by x seconds
256  // @note The implementation of % for negative values is implementation defined in ISO1998
257  SUMOTime offset; // the time to run the traffic light in advance
258  if (myOffset >= 0) {
260  } else {
262  }
263  while (offset >= (*i)->duration) {
264  step++;
265  offset -= (*i)->duration;
266  ++i;
267  }
268  firstEventOffset = (*i)->duration - offset + myNet.getCurrentTimeStep();
269  if (existing != 0) {
271  myNet.getCurrentTimeStep(), step, (*i)->duration - offset);
272  return;
273  }
274  }
275 
276  if (myActiveProgram == "") {
277  myActiveProgram = "default";
278  }
279  MSTrafficLightLogic* tlLogic = 0;
280  // build the tls-logic in dependance to its type
281  switch (myLogicType) {
282  case TLTYPE_SWARM_BASED:
283  firstEventOffset = DELTA_T; //this is needed because swarm needs to update the pheromone on the lanes at every step
285  break;
288  break;
289  case TLTYPE_SOTL_REQUEST:
291  break;
292  case TLTYPE_SOTL_PLATOON:
294  break;
295  case TLTYPE_SOTL_WAVE:
297  break;
298  case TLTYPE_SOTL_PHASE:
300  break;
303  break;
304  case TLTYPE_ACTUATED:
305  // @note it is unclear how to apply the given offset in the context
306  // of variable-length phases
309  myActivePhases, step, (*i)->minDuration + myNet.getCurrentTimeStep(),
310  myAdditionalParameter, basePath);
311  break;
312  case TLTYPE_DELAYBASED: // placeholder for future development
313  case TLTYPE_STATIC:
314  tlLogic =
317  myActivePhases, step, firstEventOffset,
319  break;
320  case TLTYPE_RAIL:
321  if (myType == NODETYPE_RAIL_SIGNAL) {
322  tlLogic = new MSRailSignal(getTLLogicControlToUse(),
325  } else if (myType == NODETYPE_RAIL_CROSSING) {
326  tlLogic = new MSRailCrossing(getTLLogicControlToUse(),
329  } else {
330  throw ProcessError("Invalid node type '" + toString(myType)
331  + "' for traffic light type '" + toString(myLogicType) + "'");
332  }
333  break;
334  case TLTYPE_INVALID:
335  throw ProcessError("Invalid traffic light type '" + toString(myLogicType) + "'");
336  }
337  myActivePhases.clear();
338  if (tlLogic != 0) {
339  if (getTLLogicControlToUse().add(myActiveKey, myActiveProgram, tlLogic)) {
340  if (myNetIsLoaded) {
341  tlLogic->init(myDetectorBuilder);
342  } else {
343  myLogics2PostLoadInit.push_back(tlLogic);
344  }
345  } else {
346  WRITE_ERROR("Another logic with id '" + myActiveKey + "' and subid '" + myActiveProgram + "' exists.");
347  delete tlLogic;
348  }
349  }
350 }
351 
352 
353 void
355  myActiveKey = id;
356  myActiveProgram = "";
357  myActiveLogic.clear();
358  myActiveFoes.clear();
359  myActiveConts.reset();
360  myRequestSize = NO_REQUEST_SIZE; // seems not to be used
362  myCurrentHasError = false;
363 }
364 
365 
366 void
368  const std::string& response,
369  const std::string& foes,
370  bool cont) {
371  if (myCurrentHasError) {
372  // had an error
373  return;
374  }
375  if (request >= SUMO_MAX_CONNECTIONS) {
376  // bad request
377  myCurrentHasError = true;
378  throw InvalidArgument("Junction logic '" + myActiveKey + "' is larger than allowed; recheck the network.");
379  }
381  // initialize
382  myRequestSize = (int)response.size();
383  }
384  if (static_cast<int>(response.size()) != myRequestSize) {
385  myCurrentHasError = true;
386  throw InvalidArgument("Invalid response size " + toString(response.size()) +
387  " in Junction logic '" + myActiveKey + "' (expected " + toString(myRequestSize) + ")");
388  }
389  if (static_cast<int>(foes.size()) != myRequestSize) {
390  myCurrentHasError = true;
391  throw InvalidArgument("Invalid foes size " + toString(foes.size()) +
392  " in Junction logic '" + myActiveKey + "' (expected " + toString(myRequestSize) + ")");
393  }
394  // assert that the logicitems come ordered by their request index
395  assert((int)myActiveLogic.size() == request);
396  assert((int)myActiveFoes.size() == request);
397  // add the read response for the given request index
398  myActiveLogic.push_back(std::bitset<SUMO_MAX_CONNECTIONS>(response));
399  // add the read junction-internal foes for the given request index
400  myActiveFoes.push_back(std::bitset<SUMO_MAX_CONNECTIONS>(foes));
401  // add whether the vehicle may drive a little bit further
402  myActiveConts.set(request, cont);
403  // increse number of set information
405 }
406 
407 
408 void
409 NLJunctionControlBuilder::initTrafficLightLogic(const std::string& id, const std::string& programID,
410  TrafficLightType type, SUMOTime offset) {
411  myActiveKey = id;
412  myActiveProgram = programID;
413  myActivePhases.clear();
414  myAbsDuration = 0;
416  myLogicType = type;
417  myOffset = offset;
418  myAdditionalParameter.clear();
419 }
420 
421 
422 void
423 NLJunctionControlBuilder::addPhase(SUMOTime duration, const std::string& state, SUMOTime minDuration, SUMOTime maxDuration, bool transient_notdecisional, bool commit) throw() {
424  // build and add the phase definition to the list
425  myActivePhases.push_back(new MSPhaseDefinition(duration, minDuration, maxDuration, state, transient_notdecisional, commit));
426  // add phase duration to the absolute duration
427  myAbsDuration += duration;
428 }
429 
430 void
431 NLJunctionControlBuilder::addPhase(SUMOTime duration, const std::string& state, SUMOTime minDuration, SUMOTime maxDuration, bool transient_notdecisional, bool commit, MSPhaseDefinition::LaneIdVector& targetLanes) throw() {
432  // build and add the phase definition to the list
433  myActivePhases.push_back(new MSPhaseDefinition(duration, minDuration, maxDuration, state, transient_notdecisional, commit, targetLanes));
434  // add phase duration to the absolute duration
435  myAbsDuration += duration;
436 }
437 
438 
439 void
440 NLJunctionControlBuilder::addPhase(SUMOTime duration, const std::string& state,
441  SUMOTime minDuration, SUMOTime maxDuration) {
442  // build and add the phase definition to the list
443  myActivePhases.push_back(new MSPhaseDefinition(duration, minDuration, maxDuration, state));
444  // add phase duration to the absolute duration
445  myAbsDuration += duration;
446 }
447 
448 
449 void
452  // We have a legacy network. junction element did not contain logicitems; read the logic later
453  return;
454  }
455  if (myCurrentHasError) {
456  // had an error before...
457  return;
458  }
460  throw InvalidArgument("The description for the junction logic '" + myActiveKey + "' is malicious.");
461  }
462  if (myLogics.count(myActiveKey) > 0) {
463  throw InvalidArgument("Junction logic '" + myActiveKey + "' was defined twice.");
464  }
468  myActiveConts);
469  myLogics[myActiveKey] = logic;
470 }
471 
472 
475  postLoadInitialization(); // must happen after edgeBuilder is finished
477  throw ProcessError("Traffic lights could not be built.");
478  }
480  myLogicControl = 0;
481  return ret;
482 }
483 
484 
485 void
486 NLJunctionControlBuilder::addParam(const std::string& key,
487  const std::string& value) {
488  myAdditionalParameter[key] = value;
489 }
490 
491 
494  if (myLogicControl != 0) {
495  return *myLogicControl;
496  }
497  return myNet.getTLSControl();
498 }
499 
500 
501 const std::string&
503  return myActiveKey;
504 }
505 
506 
507 const std::string&
509  return myActiveProgram;
510 }
511 
512 
513 void
515  for (std::vector<MSTrafficLightLogic*>::const_iterator it = myLogics2PostLoadInit.begin();
516  it != myLogics2PostLoadInit.end(); ++it) {
517  (*it)->init(myDetectorBuilder);
518  }
519  myNetIsLoaded = true;
520 }
521 
522 
523 MSJunction*
524 NLJunctionControlBuilder::retrieve(const std::string id) {
525  if (myJunctions != 0) {
526  return myJunctions->get(id);
527  } else {
528  return 0;
529  }
530 }
531 
532 /****************************************************************************/
void postLoadInitialization()
initialize junctions after all connections have been loaded
void initTrafficLightLogic(const std::string &id, const std::string &programID, TrafficLightType type, SUMOTime offset)
Begins the reading of a traffic lights logic.
Builds detectors for microsim.
virtual ~NLJunctionControlBuilder()
Destructor.
NLDetectorBuilder & myDetectorBuilder
The detector builder to use.
long long int SUMOTime
Definition: SUMOTime.h:43
std::bitset< SUMO_MAX_CONNECTIONS > myActiveConts
The description about which lanes have an internal follower.
Storage for all programs of a single tls.
virtual bool add(const std::string &id, T item)
Adds an item.
Class for low-level platoon policy.
A signal for rails.
Definition: MSRailSignal.h:54
MSBitSetLogic< SUMO_MAX_CONNECTIONS > MSBitsetLogic
std::string myActiveKey
The key of the currently chosen junction.
The base class for an intersection.
Definition: MSJunction.h:64
virtual void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)=0
Changes the current phase and her duration.
T get(const std::string &id) const
Retrieves an item.
MSNet & myNet
The net to use.
MSBitsetLogic::Logic myActiveLogic
The right-of-way-logic of the currently chosen bitset-logic.
void closeJunctionLogic()
Ends the building of a junction logic (row-logic)
SUMOTime myAbsDuration
The absolute duration of a tls-control loop.
Class for low-level request policy.
Class for low-level marching policy.
#define SUMO_MAX_CONNECTIONS
the maximum number of connections across an intersection
Definition: StdDefs.h:42
SUMOTime DELTA_T
Definition: SUMOTime.cpp:39
const std::string & getActiveSubKey() const
Returns the active sub key.
Position myPosition
The position of the junction.
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
PositionVector myShape
The shape of the current junction.
The simulated network and simulation perfomer.
Definition: MSNet.h:93
A fixed traffic light logic.
Container for junctions; performs operations on all stored junctions.
SUMOTime myOffset
The switch offset within the tls.
A traffic lights logic which represents a tls in an off-mode.
void closeJunction(const std::string &basePath)
Closes (ends) the processing of the current junction.
MSTLLogicControl::TLSLogicVariants & getTLLogic(const std::string &id) const
Returns a previously build tls logic.
An actuated (adaptive) traffic light logic.
A class that stores and controls tls and switching of their programs.
SUMOTime getDefaultCycleTime() const
Returns the cycle time (in ms)
void addPhase(SUMOTime duration, const std::string &state, SUMOTime min, SUMOTime max)
Adds a phase to the currently built traffic lights logic.
A self-organizing traffic light logic based on a particular policy.
MSSimpleTrafficLightLogic::Phases myActivePhases
The current phase definitions for a simple traffic light.
TrafficLightType myLogicType
The current logic type.
std::string myActiveID
The id of the currently chosen junction.
MSJunctionControl * myJunctions
The junctions controls.
virtual void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
MSTLLogicControl & getTLSControl()
Returns the tls logics control.
Definition: MSNet.h:380
A list of positions.
virtual MSJunction * buildLogicJunction()
Builds a junction with a logic.
virtual void closeTrafficLightLogic(const std::string &basePath)
Ends the building of a traffic lights logic.
void openJunction(const std::string &id, const std::string &key, const SumoXMLNodeType type, SUMOReal x, SUMOReal y, const PositionVector &shape, const std::vector< MSLane *> &incomingLanes, const std::vector< MSLane *> &internalLanes)
Begins the processing of the named junction.
std::map< std::string, MSJunctionLogic * > myLogics
Map of loaded junction logics.
int myRequestSize
The size of the request.
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:63
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:254
bool myNetIsLoaded
whether the network has been loaded
MSJunction * retrieve(const std::string id)
try to retrieve junction by id
A signal for rails.
static bool gUsingInternalLanes
Information whether the simulation regards internal lanes.
Definition: MSGlobals.h:73
NLJunctionControlBuilder(MSNet &net, NLDetectorBuilder &db)
Constructor.
void addParam(const std::string &key, const std::string &value)
Adds a parameter.
int myRequestItemNumber
Counter for the inserted items.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
bool myCurrentHasError
Information whether the current logic had an error.
MSBitsetLogic::Foes myActiveFoes
The description about which lanes disallow other passing the junction simultaneously.
MSTLLogicControl & getTLLogicControlToUse() const
Returns the used tls control.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
virtual MSJunction * buildNoLogicJunction()
Builds a junction that does not use a logic.
virtual const Phases & getPhases() const =0
Returns the phases of this tls program.
bool closeNetworkReading()
Lets MSTLLogicControl know that the network has been loaded.
StringParameterMap myAdditionalParameter
Parameter map (key->value)
std::vector< std::string > LaneIdVector
SumoXMLNodeType myType
The type of the currently chosen junction.
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
void initJunctionLogic(const std::string &id)
Initialises a junction logic.
LaneVector myActiveIncomingLanes
The list of the incoming lanes of the currently chosen junction.
std::vector< std::bitset< N > > Logic
Container that holds the right of way bitsets. Each link has it&#39;s own bitset. The bits in the bitsets...
Definition: MSBitSetLogic.h:59
Class for low-level phase policy.
std::vector< MSTrafficLightLogic * > myLogics2PostLoadInit
The container for information which junctions shall be initialised using which values.
#define HAVE_INTERNAL_LANES
Definition: config.h:56
void set(SUMOReal x, SUMOReal y)
Definition: Position.h:78
MSTLLogicControl * myLogicControl
The tls control to use (0 if net&#39;s tls control shall be used)
A junction with right-of-way - rules.
The parent class for traffic light logics.
#define SUMOReal
Definition: config.h:213
MSJunctionLogic * getJunctionLogicSecure()
Returns the current junction logic.
MSJunctionControl * build() const
Builds the MSJunctionControl which holds all of the simulations junctions.
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
MSTLLogicControl * buildTLLogics()
Returns the built tls-logic control.
The definition of a single phase of a tls logic.
const std::string & getActiveKey() const
Returns the active key.
TrafficLightType
void addLogicItem(int request, const std::string &response, const std::string &foes, bool cont)
Adds a logic item.