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-2015 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>
48 #include <microsim/MSGlobals.h>
53 #include <utils/common/ToString.h>
54 #include <netbuild/NBNode.h>
55 #include "NLBuilder.h"
57 
58 #ifdef CHECK_MEMORY_LEAKS
59 #include <foreign/nvwa/debug_new.h>
60 #endif // CHECK_MEMORY_LEAKS
61 
62 
63 // ===========================================================================
64 // static members
65 // ===========================================================================
67 
68 // ===========================================================================
69 // method definitions
70 // ===========================================================================
72  myNet(net),
73  myDetectorBuilder(db),
74  myOffset(0),
75  myJunctions(0),
76  myNetIsLoaded(false) {
79 }
80 
81 
83  delete myLogicControl;
84  delete myJunctions;
85 }
86 
87 
88 void
90  const std::string& key,
91  const SumoXMLNodeType type,
92  SUMOReal x, SUMOReal y,
93  const PositionVector& shape,
94  const std::vector<MSLane*>& incomingLanes,
95  const std::vector<MSLane*>& internalLanes) {
96 #ifdef HAVE_INTERNAL_LANES
97  myActiveInternalLanes = internalLanes;
98 #else
99  UNUSED_PARAMETER(internalLanes);
100 #endif
101  myActiveIncomingLanes = incomingLanes;
102  myActiveID = id;
103  myActiveKey = key;
104  myType = type;
105  myPosition.set(x, y);
106  myShape = shape;
107 }
108 
109 
110 void
112  if (myJunctions == 0) {
113  throw ProcessError("Information about the number of nodes was missing.");
114  }
115  MSJunction* junction = 0;
116  switch (myType) {
117  case NODETYPE_NOJUNCTION:
118  case NODETYPE_DEAD_END:
120  case NODETYPE_DISTRICT:
122  junction = buildNoLogicJunction();
123  break;
126  case NODETYPE_PRIORITY:
129  junction = buildLogicJunction();
130  break;
131  case NODETYPE_INTERNAL:
132 #ifdef HAVE_INTERNAL_LANES
134  junction = buildInternalJunction();
135  }
136 #endif
137  break;
139  myOffset = 0;
141  myActiveProgram = "0";
144  junction = buildLogicJunction();
145  break;
146  default:
147  throw InvalidArgument("False junction logic type.");
148  }
149  if (junction != 0) {
150  if (!myJunctions->add(myActiveID, junction)) {
151  throw InvalidArgument("Another junction with the id '" + myActiveID + "' exists.");
152  }
153  }
154 }
155 
156 
160  myJunctions = 0;
161  return js;
162 }
163 
164 
165 MSJunction*
168 #ifdef HAVE_INTERNAL_LANES
169  , myActiveInternalLanes
170 #endif
171  );
172 }
173 
174 
175 MSJunction*
178  // build the junction
180 #ifdef HAVE_INTERNAL_LANES
181  myActiveInternalLanes,
182 #endif
183  jtype);
184 }
185 
186 
187 #ifdef HAVE_INTERNAL_LANES
188 MSJunction*
189 NLJunctionControlBuilder::buildInternalJunction() {
190  // build the junction
192  myActiveInternalLanes);
193 }
194 #endif
195 
196 
199  // get and check the junction logic
200  if (myLogics.find(myActiveID) == myLogics.end()) {
201  throw InvalidArgument("Missing junction logic '" + myActiveID + "'.");
202  }
203  return myLogics[myActiveID];
204 }
205 
206 
208 NLJunctionControlBuilder::getTLLogic(const std::string& id) const {
209  return getTLLogicControlToUse().get(id);
210 }
211 
212 
213 void
215  if (myActiveProgram == "off") {
216  if (myAbsDuration > 0) {
217  throw InvalidArgument("The off program for TLS '" + myActiveKey + "' has phases.");
218  }
221  throw InvalidArgument("Another logic with id '" + myActiveKey + "' and subid '" + myActiveProgram + "' exists.");
222  }
223  return;
224  }
225  SUMOTime firstEventOffset = 0;
226  unsigned int step = 0;
227  MSSimpleTrafficLightLogic::Phases::const_iterator i = myActivePhases.begin();
228  if (myLogicType != TLTYPE_RAIL) {
229  if (myAbsDuration == 0) {
230  throw InvalidArgument("TLS program '" + myActiveProgram + "' for TLS '" + myActiveKey + "' has a duration of 0.");
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  }
248 
249  if (myActiveProgram == "") {
250  myActiveProgram = "default";
251  }
252  MSTrafficLightLogic* tlLogic = 0;
253  // build the tls-logic in dependance to its type
254  switch (myLogicType) {
255  case TLTYPE_ACTUATED:
256  // @note it is unclear how to apply the given offset in the context
257  // of variable-length phases
260  myActivePhases, step, (*i)->minDuration + myNet.getCurrentTimeStep(),
262  break;
263  case TLTYPE_STATIC:
264  tlLogic =
267  myActivePhases, step, firstEventOffset,
269  break;
270  case TLTYPE_RAIL:
271  tlLogic = new MSRailSignal(getTLLogicControlToUse(),
274  break;
275  }
276  myActivePhases.clear();
277  if (tlLogic != 0) {
278  if (getTLLogicControlToUse().add(myActiveKey, myActiveProgram, tlLogic)) {
279  if (myNetIsLoaded) {
280  tlLogic->init(myDetectorBuilder);
281  } else {
282  myLogics2PostLoadInit.push_back(tlLogic);
283  }
284  } else {
285  WRITE_ERROR("Another logic with id '" + myActiveKey + "' and subid '" + myActiveProgram + "' exists.");
286  delete tlLogic;
287  }
288  }
289 }
290 
291 
292 void
294  myActiveKey = id;
295  myActiveProgram = "";
296  myActiveLogic.clear();
297  myActiveFoes.clear();
298  myActiveConts.reset();
299  myRequestSize = NO_REQUEST_SIZE; // seems not to be used
301  myCurrentHasError = false;
302 }
303 
304 
305 void
307  const std::string& response,
308  const std::string& foes,
309  bool cont) {
310  if (myCurrentHasError) {
311  // had an error
312  return;
313  }
314  if (request > 63) {
315  // bad request
316  myCurrentHasError = true;
317  throw InvalidArgument("Junction logic '" + myActiveKey + "' is larger than allowed; recheck the network.");
318  }
320  // initialize
321  myRequestSize = (int)response.size();
322  }
323  if (static_cast<int>(response.size()) != myRequestSize) {
324  myCurrentHasError = true;
325  throw InvalidArgument("Invalid response size " + toString(response.size()) +
326  " in Junction logic '" + myActiveKey + "' (expected " + toString(myRequestSize) + ")");
327  }
328  if (static_cast<int>(foes.size()) != myRequestSize) {
329  myCurrentHasError = true;
330  throw InvalidArgument("Invalid foes size " + toString(foes.size()) +
331  " in Junction logic '" + myActiveKey + "' (expected " + toString(myRequestSize) + ")");
332  }
333  // assert that the logicitems come ordered by their request index
334  assert(myActiveLogic.size() == (size_t) request);
335  assert(myActiveFoes.size() == (size_t) request);
336  // add the read response for the given request index
337  myActiveLogic.push_back(std::bitset<64>(response));
338  // add the read junction-internal foes for the given request index
339  myActiveFoes.push_back(std::bitset<64>(foes));
340  // add whether the vehicle may drive a little bit further
341  myActiveConts.set(request, cont);
342  // increse number of set information
344 }
345 
346 
347 void
348 NLJunctionControlBuilder::initTrafficLightLogic(const std::string& id, const std::string& programID,
349  TrafficLightType type, SUMOTime offset) {
350  myActiveKey = id;
351  myActiveProgram = programID;
352  myActivePhases.clear();
353  myAbsDuration = 0;
355  myLogicType = type;
356  myOffset = offset;
357  myAdditionalParameter.clear();
358 }
359 
360 
361 void
362 NLJunctionControlBuilder::addPhase(SUMOTime duration, const std::string& state,
363  int minDuration, int maxDuration) {
364  // build and add the phase definition to the list
365  myActivePhases.push_back(new MSPhaseDefinition(duration, minDuration, maxDuration, state));
366  // add phase duration to the absolute duration
367  myAbsDuration += duration;
368 }
369 
370 
371 void
374  // We have a legacy network. junction element did not contain logicitems; read the logic later
375  return;
376  }
377  if (myCurrentHasError) {
378  // had an error before...
379  return;
380  }
382  throw InvalidArgument("The description for the junction logic '" + myActiveKey + "' is malicious.");
383  }
384  if (myLogics.count(myActiveKey) > 0) {
385  throw InvalidArgument("Junction logic '" + myActiveKey + "' was defined twice.");
386  }
390  myActiveConts);
391  myLogics[myActiveKey] = logic;
392 }
393 
394 
398  throw ProcessError("Traffic lights could not be built.");
399  }
401  myLogicControl = 0;
402  return ret;
403 }
404 
405 
406 void
407 NLJunctionControlBuilder::addParam(const std::string& key,
408  const std::string& value) {
409  myAdditionalParameter[key] = value;
410 }
411 
412 
415  if (myLogicControl != 0) {
416  return *myLogicControl;
417  }
418  return myNet.getTLSControl();
419 }
420 
421 
422 const std::string&
424  return myActiveKey;
425 }
426 
427 
428 const std::string&
430  return myActiveProgram;
431 }
432 
433 
434 void
436  for (std::vector<MSTrafficLightLogic*>::const_iterator it = myLogics2PostLoadInit.begin();
437  it != myLogics2PostLoadInit.end(); ++it) {
438  (*it)->init(myDetectorBuilder);
439  }
440  myNetIsLoaded = true;
441 }
442 
443 
444 MSJunction*
445 NLJunctionControlBuilder::retrieve(const std::string id) {
446  if (myJunctions != 0) {
447  return myJunctions->get(id);
448  } else {
449  return 0;
450  }
451 }
452 
453 /****************************************************************************/
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.
Storage for all programs of a single tls.
virtual bool add(const std::string &id, T item)
Adds an item.
A signal for rails.
Definition: MSRailSignal.h:54
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:61
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.
Position myPosition
The position of the junction.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:235
#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: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.
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, int min, int max)
Adds a phase to the currently built traffic lights logic.
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.
std::bitset< 64 > myActiveConts
The description about which lanes have an internal follower.
virtual void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
MSTLLogicControl & getTLSControl()
Returns the tls logics control.
Definition: MSNet.h:349
A list of positions.
virtual MSJunction * buildLogicJunction()
Builds a junction with a 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
MSTLLogicControl * buildTLLogics() const
Returns the built tls-logic control.
bool myNetIsLoaded
whether the network has been loaded
MSJunction * retrieve(const std::string id)
try to retrieve junction by id
void closeJunction()
Closes (ends) the processing of the current junction.
static bool gUsingInternalLanes
Information whether the simulation regards internal lanes.
Definition: MSGlobals.h:71
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:53
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:205
virtual MSJunction * buildNoLogicJunction()
Builds a junction that does not use a logic.
MSBitSetLogic< 64 > MSBitsetLogic
bool closeNetworkReading()
Lets MSTLLogicControl know that the network has been loaded.
StringParameterMap myAdditionalParameter
Parameter map (key->value)
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's own bitset. The bits in the bitsets...
Definition: MSBitSetLogic.h:59
std::vector< MSTrafficLightLogic * > myLogics2PostLoadInit
The container for information which junctions shall be initialised using which values.
#define HAVE_INTERNAL_LANES
Definition: config.h:50
void set(SUMOReal x, SUMOReal y)
Definition: Position.h:78
MSTLLogicControl * myLogicControl
The tls control to use (0 if net's tls control shall be used)
A junction with right-of-way - rules.
int SUMOTime
Definition: SUMOTime.h:43
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
virtual void closeTrafficLightLogic()
Ends the building of a traffic lights logic.
The parent class for traffic light logics.
#define SUMOReal
Definition: config.h:218
MSJunctionLogic * getJunctionLogicSecure()
Returns the current junction logic.
MSJunctionControl * build() const
Builds the MSJunctionControl which holds all of the simulations junctions.
The definition of a single phase of a tls logic.
TrafficLightType
void addLogicItem(int request, const std::string &response, const std::string &foes, bool cont)
Adds a logic item.