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-2017 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>
59 #include <microsim/MSGlobals.h>
60 #include <microsim/MSNet.h>
65 #include <utils/common/ToString.h>
66 #include <netbuild/NBNode.h>
67 #include "NLBuilder.h"
69 
70 
71 // ===========================================================================
72 // static members
73 // ===========================================================================
75 
76 // ===========================================================================
77 // method definitions
78 // ===========================================================================
80  myNet(net),
81  myDetectorBuilder(db),
82  myOffset(0),
83  myJunctions(0),
84  myNetIsLoaded(false) {
87 }
88 
89 
91  delete myLogicControl;
92  delete myJunctions;
93 }
94 
95 
96 void
98  const std::string& key,
99  const SumoXMLNodeType type,
100  double x, double y,
101  const PositionVector& shape,
102  const std::vector<MSLane*>& incomingLanes,
103  const std::vector<MSLane*>& internalLanes) {
104  myActiveInternalLanes = internalLanes;
105  myActiveIncomingLanes = incomingLanes;
106  myActiveID = id;
107  myActiveKey = key;
108  myType = type;
109  myPosition.set(x, y);
110  myShape = shape;
111 }
112 
113 
114 void
115 NLJunctionControlBuilder::closeJunction(const std::string& basePath) {
116  if (myJunctions == 0) {
117  throw ProcessError("Information about the number of nodes was missing.");
118  }
119  MSJunction* junction = 0;
120  switch (myType) {
121  case NODETYPE_NOJUNCTION:
122  case NODETYPE_DEAD_END:
124  case NODETYPE_DISTRICT:
126  junction = buildNoLogicJunction();
127  break;
131  case NODETYPE_PRIORITY:
134  case NODETYPE_ZIPPER:
135  junction = buildLogicJunction();
136  break;
137  case NODETYPE_INTERNAL:
139  junction = buildInternalJunction();
140  }
141  break;
144  myOffset = 0;
146  myActiveProgram = "0";
148  closeTrafficLightLogic(basePath);
149  junction = buildLogicJunction();
150  break;
151  default:
152  throw InvalidArgument("False junction logic type.");
153  }
154  if (junction != 0) {
155  if (!myJunctions->add(myActiveID, junction)) {
156  throw InvalidArgument("Another junction with the id '" + myActiveID + "' exists.");
157  }
158  }
159 }
160 
161 
165  myJunctions = 0;
166  return js;
167 }
168 
169 
170 MSJunction*
174 }
175 
176 
177 MSJunction*
180  // build the junction
183  jtype);
184 }
185 
186 
187 MSJunction*
189  // build the junction
192 }
193 
194 
197  // get and check the junction logic
198  if (myLogics.find(myActiveID) == myLogics.end()) {
199  throw InvalidArgument("Missing junction logic '" + myActiveID + "'.");
200  }
201  return myLogics[myActiveID];
202 }
203 
204 
206 NLJunctionControlBuilder::getTLLogic(const std::string& id) const {
207  return getTLLogicControlToUse().get(id);
208 }
209 
210 
211 void
213  if (myActiveProgram == "off") {
214  if (myAbsDuration > 0) {
215  throw InvalidArgument("The off program for TLS '" + myActiveKey + "' has phases.");
216  }
219  throw InvalidArgument("Another logic with id '" + myActiveKey + "' and subid '" + myActiveProgram + "' exists.");
220  }
221  return;
222  }
223  SUMOTime firstEventOffset = 0;
224  int step = 0;
225  MSTrafficLightLogic* existing = 0;
226  MSSimpleTrafficLightLogic::Phases::const_iterator i = myActivePhases.begin();
227  if (myLogicType != TLTYPE_RAIL) {
228  if (myAbsDuration == 0) {
230  if (existing == 0) {
231  throw InvalidArgument("TLS program '" + myActiveProgram + "' for TLS '" + myActiveKey + "' has a duration of 0.");
232  } else {
233  // only modify the offset of an existing logic
234  myAbsDuration = existing->getDefaultCycleTime();
235  i = existing->getPhases().begin();
236  }
237  }
238  // compute the initial step and first switch time of the tls-logic
239  // a positive offset delays all phases by x (advance by absDuration - x) while a negative offset advances all phases by x seconds
240  // @note The implementation of % for negative values is implementation defined in ISO1998
241  SUMOTime offset; // the time to run the traffic light in advance
242  if (myOffset >= 0) {
244  } else {
246  }
247  while (offset >= (*i)->duration) {
248  step++;
249  offset -= (*i)->duration;
250  ++i;
251  }
252  firstEventOffset = (*i)->duration - offset + myNet.getCurrentTimeStep();
253  if (existing != 0) {
255  myNet.getCurrentTimeStep(), step, (*i)->duration - offset);
256  return;
257  }
258  }
259 
260  if (myActiveProgram == "") {
261  myActiveProgram = "default";
262  }
263  MSTrafficLightLogic* tlLogic = 0;
264  // build the tls-logic in dependance to its type
265  switch (myLogicType) {
266  case TLTYPE_SWARM_BASED:
267  firstEventOffset = DELTA_T; //this is needed because swarm needs to update the pheromone on the lanes at every step
269  break;
272  break;
273  case TLTYPE_SOTL_REQUEST:
275  break;
276  case TLTYPE_SOTL_PLATOON:
278  break;
279  case TLTYPE_SOTL_WAVE:
281  break;
282  case TLTYPE_SOTL_PHASE:
284  break;
287  break;
288  case TLTYPE_ACTUATED:
289  // @note it is unclear how to apply the given offset in the context
290  // of variable-length phases
293  myActivePhases, step, (*i)->minDuration + myNet.getCurrentTimeStep(),
294  myAdditionalParameter, basePath);
295  break;
296  case TLTYPE_DELAYBASED:
299  myActivePhases, step, (*i)->minDuration + myNet.getCurrentTimeStep(),
300  myAdditionalParameter, basePath);
301  break;
302  case TLTYPE_STATIC:
303  tlLogic =
306  myActivePhases, step, firstEventOffset,
308  break;
309  case TLTYPE_RAIL:
310  if (myType == NODETYPE_RAIL_SIGNAL) {
311  tlLogic = new MSRailSignal(getTLLogicControlToUse(),
314  } else if (myType == NODETYPE_RAIL_CROSSING) {
315  tlLogic = new MSRailCrossing(getTLLogicControlToUse(),
318  } else {
319  throw ProcessError("Invalid node type '" + toString(myType)
320  + "' for traffic light type '" + toString(myLogicType) + "'");
321  }
322  break;
323  case TLTYPE_INVALID:
324  throw ProcessError("Invalid traffic light type '" + toString(myLogicType) + "'");
325  }
326  myActivePhases.clear();
327  if (tlLogic != 0) {
328  if (getTLLogicControlToUse().add(myActiveKey, myActiveProgram, tlLogic)) {
329  if (myNetIsLoaded) {
330  tlLogic->init(myDetectorBuilder);
331  } else {
332  myLogics2PostLoadInit.push_back(tlLogic);
333  }
334  } else {
335  WRITE_ERROR("Another logic with id '" + myActiveKey + "' and subid '" + myActiveProgram + "' exists.");
336  delete tlLogic;
337  }
338  }
339 }
340 
341 
342 void
344  myActiveKey = id;
345  myActiveProgram = "";
346  myActiveLogic.clear();
347  myActiveFoes.clear();
348  myActiveConts.reset();
349  myRequestSize = NO_REQUEST_SIZE; // seems not to be used
351  myCurrentHasError = false;
352 }
353 
354 
355 void
357  const std::string& response,
358  const std::string& foes,
359  bool cont) {
360  if (myCurrentHasError) {
361  // had an error
362  return;
363  }
364  if (request >= SUMO_MAX_CONNECTIONS) {
365  // bad request
366  myCurrentHasError = true;
367  throw InvalidArgument("Junction logic '" + myActiveKey + "' is larger than allowed; recheck the network.");
368  }
370  // initialize
371  myRequestSize = (int)response.size();
372  }
373  if (static_cast<int>(response.size()) != myRequestSize) {
374  myCurrentHasError = true;
375  throw InvalidArgument("Invalid response size " + toString(response.size()) +
376  " in Junction logic '" + myActiveKey + "' (expected " + toString(myRequestSize) + ")");
377  }
378  if (static_cast<int>(foes.size()) != myRequestSize) {
379  myCurrentHasError = true;
380  throw InvalidArgument("Invalid foes size " + toString(foes.size()) +
381  " in Junction logic '" + myActiveKey + "' (expected " + toString(myRequestSize) + ")");
382  }
383  // assert that the logicitems come ordered by their request index
384  assert((int)myActiveLogic.size() == request);
385  assert((int)myActiveFoes.size() == request);
386  // add the read response for the given request index
387  myActiveLogic.push_back(std::bitset<SUMO_MAX_CONNECTIONS>(response));
388  // add the read junction-internal foes for the given request index
389  myActiveFoes.push_back(std::bitset<SUMO_MAX_CONNECTIONS>(foes));
390  // add whether the vehicle may drive a little bit further
391  myActiveConts.set(request, cont);
392  // increse number of set information
394 }
395 
396 
397 void
398 NLJunctionControlBuilder::initTrafficLightLogic(const std::string& id, const std::string& programID,
399  TrafficLightType type, SUMOTime offset) {
400  myActiveKey = id;
401  myActiveProgram = programID;
402  myActivePhases.clear();
403  myAbsDuration = 0;
405  myLogicType = type;
406  myOffset = offset;
407  myAdditionalParameter.clear();
408 }
409 
410 
411 void
412 NLJunctionControlBuilder::addPhase(SUMOTime duration, const std::string& state, SUMOTime minDuration, SUMOTime maxDuration, bool transient_notdecisional, bool commit) throw() {
413  // build and add the phase definition to the list
414  myActivePhases.push_back(new MSPhaseDefinition(duration, minDuration, maxDuration, state, transient_notdecisional, commit));
415  // add phase duration to the absolute duration
416  myAbsDuration += duration;
417 }
418 
419 void
420 NLJunctionControlBuilder::addPhase(SUMOTime duration, const std::string& state, SUMOTime minDuration, SUMOTime maxDuration, bool transient_notdecisional, bool commit, MSPhaseDefinition::LaneIdVector& targetLanes) throw() {
421  // build and add the phase definition to the list
422  myActivePhases.push_back(new MSPhaseDefinition(duration, minDuration, maxDuration, state, transient_notdecisional, commit, targetLanes));
423  // add phase duration to the absolute duration
424  myAbsDuration += duration;
425 }
426 
427 
428 void
429 NLJunctionControlBuilder::addPhase(SUMOTime duration, const std::string& state,
430  SUMOTime minDuration, SUMOTime maxDuration) {
431  // build and add the phase definition to the list
432  myActivePhases.push_back(new MSPhaseDefinition(duration, minDuration, maxDuration, state));
433  // add phase duration to the absolute duration
434  myAbsDuration += duration;
435 }
436 
437 
438 void
441  // We have a legacy network. junction element did not contain logicitems; read the logic later
442  return;
443  }
444  if (myCurrentHasError) {
445  // had an error before...
446  return;
447  }
449  throw InvalidArgument("The description for the junction logic '" + myActiveKey + "' is malicious.");
450  }
451  if (myLogics.count(myActiveKey) > 0) {
452  throw InvalidArgument("Junction logic '" + myActiveKey + "' was defined twice.");
453  }
457  myActiveConts);
458  myLogics[myActiveKey] = logic;
459 }
460 
461 
464  postLoadInitialization(); // must happen after edgeBuilder is finished
466  throw ProcessError("Traffic lights could not be built.");
467  }
469  myLogicControl = 0;
470  return ret;
471 }
472 
473 
474 void
475 NLJunctionControlBuilder::addParam(const std::string& key,
476  const std::string& value) {
477  myAdditionalParameter[key] = value;
478 }
479 
480 
483  if (myLogicControl != 0) {
484  return *myLogicControl;
485  }
486  return myNet.getTLSControl();
487 }
488 
489 
490 const std::string&
492  return myActiveKey;
493 }
494 
495 
496 const std::string&
498  return myActiveProgram;
499 }
500 
501 
502 void
504  for (std::vector<MSTrafficLightLogic*>::const_iterator it = myLogics2PostLoadInit.begin();
505  it != myLogics2PostLoadInit.end(); ++it) {
506  (*it)->init(myDetectorBuilder);
507  }
508  myNetIsLoaded = true;
509 }
510 
511 
512 MSJunction*
513 NLJunctionControlBuilder::retrieve(const std::string id) {
514  if (myJunctions != 0) {
515  return myJunctions->get(id);
516  } else {
517  return 0;
518  }
519 }
520 
521 /****************************************************************************/
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.
An actuated traffic light logic based on time delay of approaching vehicles.
std::bitset< SUMO_MAX_CONNECTIONS > myActiveConts
The description about which lanes have an internal follower.
Storage for all programs of a single tls.
LaneVector myActiveInternalLanes
The list of the internal lanes of the currently chosen junction.
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)
virtual MSJunction * buildInternalJunction()
Builds an internal junction.
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:41
SUMOTime DELTA_T
Definition: SUMOTime.cpp:40
const std::string & getActiveSubKey() const
Returns the active sub key.
void set(double x, double y)
set positions x and y
Definition: Position.h:93
Position myPosition
The position of the junction.
PositionVector myShape
The shape of the current junction.
void openJunction(const std::string &id, const std::string &key, const SumoXMLNodeType type, double x, double y, const PositionVector &shape, const std::vector< MSLane *> &incomingLanes, const std::vector< MSLane *> &internalLanes)
Begins the processing of the named junction.
The simulated network and simulation perfomer.
Definition: MSNet.h:94
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.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
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:383
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
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:257
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:76
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.
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.
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.
long long int SUMOTime
Definition: TraCIDefs.h:52
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.