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_STATIC:
313  tlLogic =
316  myActivePhases, step, firstEventOffset,
318  break;
319  case TLTYPE_RAIL:
320  if (myType == NODETYPE_RAIL_SIGNAL) {
321  tlLogic = new MSRailSignal(getTLLogicControlToUse(),
324  } else if (myType == NODETYPE_RAIL_CROSSING) {
325  tlLogic = new MSRailCrossing(getTLLogicControlToUse(),
328  } else {
329  throw ProcessError("Invalid node type '" + toString(myType)
330  + "' for traffic light type '" + toString(myLogicType) + "'");
331  }
332  break;
333  case TLTYPE_INVALID:
334  throw ProcessError("Invalid traffic light type '" + toString(myLogicType) + "'");
335  }
336  myActivePhases.clear();
337  if (tlLogic != 0) {
338  if (getTLLogicControlToUse().add(myActiveKey, myActiveProgram, tlLogic)) {
339  if (myNetIsLoaded) {
340  tlLogic->init(myDetectorBuilder);
341  } else {
342  myLogics2PostLoadInit.push_back(tlLogic);
343  }
344  } else {
345  WRITE_ERROR("Another logic with id '" + myActiveKey + "' and subid '" + myActiveProgram + "' exists.");
346  delete tlLogic;
347  }
348  }
349 }
350 
351 
352 void
354  myActiveKey = id;
355  myActiveProgram = "";
356  myActiveLogic.clear();
357  myActiveFoes.clear();
358  myActiveConts.reset();
359  myRequestSize = NO_REQUEST_SIZE; // seems not to be used
361  myCurrentHasError = false;
362 }
363 
364 
365 void
367  const std::string& response,
368  const std::string& foes,
369  bool cont) {
370  if (myCurrentHasError) {
371  // had an error
372  return;
373  }
374  if (request >= SUMO_MAX_CONNECTIONS) {
375  // bad request
376  myCurrentHasError = true;
377  throw InvalidArgument("Junction logic '" + myActiveKey + "' is larger than allowed; recheck the network.");
378  }
380  // initialize
381  myRequestSize = (int)response.size();
382  }
383  if (static_cast<int>(response.size()) != myRequestSize) {
384  myCurrentHasError = true;
385  throw InvalidArgument("Invalid response size " + toString(response.size()) +
386  " in Junction logic '" + myActiveKey + "' (expected " + toString(myRequestSize) + ")");
387  }
388  if (static_cast<int>(foes.size()) != myRequestSize) {
389  myCurrentHasError = true;
390  throw InvalidArgument("Invalid foes size " + toString(foes.size()) +
391  " in Junction logic '" + myActiveKey + "' (expected " + toString(myRequestSize) + ")");
392  }
393  // assert that the logicitems come ordered by their request index
394  assert((int)myActiveLogic.size() == request);
395  assert((int)myActiveFoes.size() == request);
396  // add the read response for the given request index
397  myActiveLogic.push_back(std::bitset<SUMO_MAX_CONNECTIONS>(response));
398  // add the read junction-internal foes for the given request index
399  myActiveFoes.push_back(std::bitset<SUMO_MAX_CONNECTIONS>(foes));
400  // add whether the vehicle may drive a little bit further
401  myActiveConts.set(request, cont);
402  // increse number of set information
404 }
405 
406 
407 void
408 NLJunctionControlBuilder::initTrafficLightLogic(const std::string& id, const std::string& programID,
409  TrafficLightType type, SUMOTime offset) {
410  myActiveKey = id;
411  myActiveProgram = programID;
412  myActivePhases.clear();
413  myAbsDuration = 0;
415  myLogicType = type;
416  myOffset = offset;
417  myAdditionalParameter.clear();
418 }
419 
420 
421 void
422 NLJunctionControlBuilder::addPhase(SUMOTime duration, const std::string& state, SUMOTime minDuration, SUMOTime maxDuration, bool transient_notdecisional, bool commit) throw() {
423  // build and add the phase definition to the list
424  myActivePhases.push_back(new MSPhaseDefinition(duration, minDuration, maxDuration, state, transient_notdecisional, commit));
425  // add phase duration to the absolute duration
426  myAbsDuration += duration;
427 }
428 
429 void
430 NLJunctionControlBuilder::addPhase(SUMOTime duration, const std::string& state, SUMOTime minDuration, SUMOTime maxDuration, bool transient_notdecisional, bool commit, MSPhaseDefinition::LaneIdVector& targetLanes) throw() {
431  // build and add the phase definition to the list
432  myActivePhases.push_back(new MSPhaseDefinition(duration, minDuration, maxDuration, state, transient_notdecisional, commit, targetLanes));
433  // add phase duration to the absolute duration
434  myAbsDuration += duration;
435 }
436 
437 
438 void
439 NLJunctionControlBuilder::addPhase(SUMOTime duration, const std::string& state,
440  SUMOTime minDuration, SUMOTime maxDuration) {
441  // build and add the phase definition to the list
442  myActivePhases.push_back(new MSPhaseDefinition(duration, minDuration, maxDuration, state));
443  // add phase duration to the absolute duration
444  myAbsDuration += duration;
445 }
446 
447 
448 void
451  // We have a legacy network. junction element did not contain logicitems; read the logic later
452  return;
453  }
454  if (myCurrentHasError) {
455  // had an error before...
456  return;
457  }
459  throw InvalidArgument("The description for the junction logic '" + myActiveKey + "' is malicious.");
460  }
461  if (myLogics.count(myActiveKey) > 0) {
462  throw InvalidArgument("Junction logic '" + myActiveKey + "' was defined twice.");
463  }
467  myActiveConts);
468  myLogics[myActiveKey] = logic;
469 }
470 
471 
474  postLoadInitialization(); // must happen after edgeBuilder is finished
476  throw ProcessError("Traffic lights could not be built.");
477  }
479  myLogicControl = 0;
480  return ret;
481 }
482 
483 
484 void
485 NLJunctionControlBuilder::addParam(const std::string& key,
486  const std::string& value) {
487  myAdditionalParameter[key] = value;
488 }
489 
490 
493  if (myLogicControl != 0) {
494  return *myLogicControl;
495  }
496  return myNet.getTLSControl();
497 }
498 
499 
500 const std::string&
502  return myActiveKey;
503 }
504 
505 
506 const std::string&
508  return myActiveProgram;
509 }
510 
511 
512 void
514  for (std::vector<MSTrafficLightLogic*>::const_iterator it = myLogics2PostLoadInit.begin();
515  it != myLogics2PostLoadInit.end(); ++it) {
516  (*it)->init(myDetectorBuilder);
517  }
518  myNetIsLoaded = true;
519 }
520 
521 
522 MSJunction*
523 NLJunctionControlBuilder::retrieve(const std::string id) {
524  if (myJunctions != 0) {
525  return myJunctions->get(id);
526  } else {
527  return 0;
528  }
529 }
530 
531 /****************************************************************************/
void postLoadInitialization()
initialize junctions after all connections have been loaded
const std::string & getActiveKey() const
Returns the active key.
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.
const std::string & getActiveSubKey() const
Returns the active sub key.
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.
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
Position myPosition
The position of the junction.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:254
#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.
T get(const std::string &id) const
Retrieves an item.
An actuated (adaptive) traffic light logic.
A class that stores and controls tls and switching of their programs.
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.
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
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:69
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.
MSTLLogicControl & getTLLogicControlToUse() const
Returns the used tls control.
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::TLSLogicVariants & getTLLogic(const std::string &id) const
Returns a previously build tls logic.
#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.
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
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.
MSTLLogicControl * buildTLLogics()
Returns the built tls-logic control.
The definition of a single phase of a tls logic.
SUMOTime getDefaultCycleTime() const
Returns the cycle time (in ms)
TrafficLightType
void addLogicItem(int request, const std::string &response, const std::string &foes, bool cont)
Adds a logic item.