SUMO - Simulation of Urban MObility
SUMOVehicleParameter.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Structure representing possible vehicle parameter
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include "SUMOVehicleParameter.h"
34 #include <utils/common/ToString.h>
39 
40 #ifdef CHECK_MEMORY_LEAKS
41 #include <foreign/nvwa/debug_new.h>
42 #endif // CHECK_MEMORY_LEAKS
43 
44 
45 // ===========================================================================
46 // member method definitions
47 // ===========================================================================
49  : vtypeid(DEFAULT_VTYPE_ID), color(RGBColor::DEFAULT_COLOR),
50  depart(-1), departProcedure(DEPART_GIVEN),
51  departLane(0), departLaneProcedure(DEPART_LANE_DEFAULT),
52  departPos(0), departPosProcedure(DEPART_POS_DEFAULT),
53  departSpeed(-1), departSpeedProcedure(DEPART_SPEED_DEFAULT),
54  arrivalLane(0), arrivalLaneProcedure(ARRIVAL_LANE_DEFAULT),
55  arrivalPos(0), arrivalPosProcedure(ARRIVAL_POS_DEFAULT),
56  arrivalSpeed(-1), arrivalSpeedProcedure(ARRIVAL_SPEED_DEFAULT),
57  repetitionNumber(-1), repetitionsDone(-1), repetitionOffset(-1), repetitionProbability(-1), repetitionEnd(-1),
58  line(), fromTaz(), toTaz(), personNumber(0), containerNumber(0), setParameter(0) {
59 }
60 
61 
62 bool
63 SUMOVehicleParameter::defaultOptionOverrides(const OptionsCont& oc, const std::string& optionName) const {
64  return oc.isSet(optionName) && oc.getBool("defaults-override");
65 }
66 
67 
68 void
70  dev.openTag(tag).writeAttr(SUMO_ATTR_ID, id);
73  }
75  dev.writeAttr(SUMO_ATTR_DEPART, "triggered");
77  dev.writeAttr(SUMO_ATTR_DEPART, "containerTriggered");
78  } else {
80  }
81 
82  // optional parameter
83  // departlane
84  if (wasSet(VEHPARS_DEPARTLANE_SET) && !defaultOptionOverrides(oc, "departlane")) {
85  std::string val;
86  switch (departLaneProcedure) {
87  case DEPART_LANE_GIVEN:
88  val = toString(departLane);
89  break;
90  case DEPART_LANE_RANDOM:
91  val = "random";
92  break;
93  case DEPART_LANE_FREE:
94  val = "free";
95  break;
97  val = "allowed";
98  break;
100  val = "best";
101  break;
103  val = "first";
104  break;
105  case DEPART_LANE_DEFAULT:
106  default:
107  break;
108  }
110  } else if (oc.isSet("departlane")) {
111  dev.writeNonEmptyAttr(SUMO_ATTR_DEPARTLANE, oc.getString("departlane"));
112  }
113  // departpos
114  if (wasSet(VEHPARS_DEPARTPOS_SET) && !defaultOptionOverrides(oc, "departpos")) {
115  std::string val;
116  switch (departPosProcedure) {
117  case DEPART_POS_GIVEN:
118  val = toString(departPos);
119  break;
120  case DEPART_POS_RANDOM:
121  val = "random";
122  break;
124  val = "random_free";
125  break;
126  case DEPART_POS_FREE:
127  val = "free";
128  break;
129  case DEPART_POS_LAST:
130  val = "last";
131  break;
132  case DEPART_POS_BASE:
133  val = "base";
134  break;
135  case DEPART_POS_DEFAULT:
136  default:
137  break;
138  }
140  } else if (oc.isSet("departpos")) {
141  dev.writeNonEmptyAttr(SUMO_ATTR_DEPARTPOS, oc.getString("departpos"));
142  }
143  // departspeed
144  if (wasSet(VEHPARS_DEPARTSPEED_SET) && !defaultOptionOverrides(oc, "departspeed")) {
145  std::string val;
146  switch (departSpeedProcedure) {
147  case DEPART_SPEED_GIVEN:
148  val = toString(departSpeed);
149  break;
150  case DEPART_SPEED_RANDOM:
151  val = "random";
152  break;
153  case DEPART_SPEED_MAX:
154  val = "max";
155  break;
157  default:
158  break;
159  }
161  } else if (oc.isSet("departspeed")) {
162  dev.writeNonEmptyAttr(SUMO_ATTR_DEPARTSPEED, oc.getString("departspeed"));
163  }
164 
165  // arrivallane
166  if (wasSet(VEHPARS_ARRIVALLANE_SET) && !defaultOptionOverrides(oc, "arrivallane")) {
167  std::string val;
168  switch (arrivalLaneProcedure) {
169  case ARRIVAL_LANE_GIVEN:
170  val = toString(arrivalLane);
171  break;
173  val = "current";
174  break;
176  default:
177  break;
178  }
180  } else if (oc.isSet("arrivallane")) {
181  dev.writeNonEmptyAttr(SUMO_ATTR_ARRIVALLANE, oc.getString("arrivallane"));
182  }
183  // arrivalpos
184  if (wasSet(VEHPARS_ARRIVALPOS_SET) && !defaultOptionOverrides(oc, "arrivalpos")) {
185  std::string val;
186  switch (arrivalPosProcedure) {
187  case ARRIVAL_POS_GIVEN:
188  val = toString(arrivalPos);
189  break;
190  case ARRIVAL_POS_RANDOM:
191  val = "random";
192  break;
193  case ARRIVAL_POS_MAX:
194  val = "max";
195  break;
196  case ARRIVAL_POS_DEFAULT:
197  default:
198  break;
199  }
201  } else if (oc.isSet("arrivalpos")) {
202  dev.writeNonEmptyAttr(SUMO_ATTR_ARRIVALPOS, oc.getString("arrivalpos"));
203  }
204  // arrivalspeed
205  if (wasSet(VEHPARS_ARRIVALSPEED_SET) && !defaultOptionOverrides(oc, "arrivalspeed")) {
206  std::string val;
207  switch (arrivalSpeedProcedure) {
208  case ARRIVAL_SPEED_GIVEN:
209  val = toString(arrivalSpeed);
210  break;
212  val = "current";
213  break;
215  default:
216  break;
217  }
219  } else if (oc.isSet("arrivalspeed")) {
220  dev.writeNonEmptyAttr(SUMO_ATTR_ARRIVALSPEED, oc.getString("arrivalspeed"));
221  }
222 
223  // color
224  if (wasSet(VEHPARS_COLOR_SET)) {
226  }
227  if (wasSet(VEHPARS_LINE_SET)) {
229  }
232  }
233  if (wasSet(VEHPARS_TO_TAZ_SET)) {
235  }
238  }
241  }
242 }
243 
244 
245 void
247  dev.openTag(SUMO_TAG_STOP);
248  if (busstop != "") {
249  dev.writeAttr(SUMO_ATTR_BUS_STOP, busstop);
250  }
251  if (containerstop != "") {
252  dev.writeAttr(SUMO_ATTR_CONTAINER_STOP, containerstop);
253  }
254  if (busstop == "" && containerstop == "") {
255  dev.writeAttr(SUMO_ATTR_LANE, lane);
256  if ((setParameter & STOP_START_SET) != 0) {
257  dev.writeAttr(SUMO_ATTR_STARTPOS, startPos);
258  }
259  if ((setParameter & STOP_END_SET) != 0) {
260  dev.writeAttr(SUMO_ATTR_ENDPOS, endPos);
261  }
262  }
263  if (duration >= 0) {
264  dev.writeAttr(SUMO_ATTR_DURATION, STEPS2TIME(duration));
265  }
266  if (until >= 0) {
267  dev.writeAttr(SUMO_ATTR_UNTIL, STEPS2TIME(until));
268  }
269  if ((setParameter & STOP_TRIGGER_SET) != 0) {
270  dev.writeAttr(SUMO_ATTR_TRIGGERED, triggered);
271  }
273  dev.writeAttr(SUMO_ATTR_CONTAINER_TRIGGERED, containerTriggered);
274  }
275  if ((setParameter & STOP_PARKING_SET) != 0) {
276  dev.writeAttr(SUMO_ATTR_PARKING, parking);
277  }
278  // look, we are writing the set of expected persons in its current state...
279  // if this method is used somewhere in the simulation output,
280  // one should consider keeping the original values additionally,
281  // as the ones we write may hev changed.
282  if ((setParameter & STOP_EXPECTED_SET) != 0) {
283  dev.writeAttr(SUMO_ATTR_EXPECTED, awaitedPersons);
284  }
286  dev.writeAttr(SUMO_ATTR_EXPECTED_CONTAINERS, awaitedContainers);
287  }
288  dev.closeTag();
289 }
290 
291 
292 bool
293 SUMOVehicleParameter::parseDepart(const std::string& val, const std::string& element, const std::string& id,
294  SUMOTime& depart, DepartDefinition& dd, std::string& error) {
295  if (val == "triggered") {
296  dd = DEPART_TRIGGERED;
297  } else if (val == "containerTriggered") {
299  } else if (val == "now") {
300  dd = DEPART_NOW;
301  } else {
302  try {
303  depart = string2time(val);
304  dd = DEPART_GIVEN;
305  if (depart < 0) {
306  error = "Negative departure time in the definition of '" + id + "'.";
307  return false;
308  }
309  } catch (...) {
310  error = "Invalid departure time for " + element + " '" + id + "';\n must be one of (\"triggered\", \"containerTriggered\", \"now\", or a float >= 0)";
311  return false;
312  }
313  }
314  return true;
315 }
316 
317 
318 bool
319 SUMOVehicleParameter::parseDepartLane(const std::string& val, const std::string& element, const std::string& id,
320  int& lane, DepartLaneDefinition& dld, std::string& error) {
321  bool ok = true;
322  if (val == "random") {
323  dld = DEPART_LANE_RANDOM;
324  } else if (val == "free") {
325  dld = DEPART_LANE_FREE;
326  } else if (val == "allowed") {
328  } else if (val == "best") {
329  dld = DEPART_LANE_BEST_FREE;
330  } else if (val == "first") {
332  } else {
333  try {
334  lane = TplConvert::_2int(val.c_str());
335  dld = DEPART_LANE_GIVEN;
336  if (lane < 0) {
337  ok = false;
338  }
339  } catch (...) {
340  ok = false;
341  }
342  }
343  if (!ok) {
344  error = "Invalid departLane definition for " + element + " '" + id + "';\n must be one of (\"random\", \"free\", \"allowed\", \"best\", \"first\", or an int>=0)";
345  }
346  return ok;
347 }
348 
349 
350 bool
351 SUMOVehicleParameter::parseDepartPos(const std::string& val, const std::string& element, const std::string& id,
352  SUMOReal& pos, DepartPosDefinition& dpd, std::string& error) {
353  bool ok = true;
354  if (val == "random") {
355  dpd = DEPART_POS_RANDOM;
356  } else if (val == "random_free") {
358  } else if (val == "free") {
359  dpd = DEPART_POS_FREE;
360  } else if (val == "base") {
361  dpd = DEPART_POS_BASE;
362  } else if (val == "last") {
363  dpd = DEPART_POS_LAST;
364  } else {
365  try {
366  pos = TplConvert::_2SUMOReal(val.c_str());
367  dpd = DEPART_POS_GIVEN;
368  } catch (...) {
369  ok = false;
370  }
371  }
372  if (!ok) {
373  error = "Invalid departPos definition for " + element + " '" + id + "';\n must be one of (\"random\", \"random_free\", \"free\", \"base\", \"last\" or a float)";
374  }
375  return ok;
376 }
377 
378 
379 bool
380 SUMOVehicleParameter::parseDepartSpeed(const std::string& val, const std::string& element, const std::string& id,
381  SUMOReal& speed, DepartSpeedDefinition& dsd, std::string& error) {
382  bool ok = true;
383  if (val == "random") {
384  dsd = DEPART_SPEED_RANDOM;
385  } else if (val == "max") {
386  dsd = DEPART_SPEED_MAX;
387  } else {
388  try {
389  speed = TplConvert::_2SUMOReal(val.c_str());
390  dsd = DEPART_SPEED_GIVEN;
391  if (speed < 0) {
392  ok = false;
393  }
394  } catch (...) {
395  ok = false;
396  }
397  }
398  if (!ok) {
399  error = "Invalid departSpeed definition for " + element + " '" + id + "';\n must be one of (\"random\", \"max\", or a float>=0)";
400  }
401  return ok;
402 }
403 
404 
405 bool
406 SUMOVehicleParameter::parseArrivalLane(const std::string& val, const std::string& element, const std::string& id,
407  int& lane, ArrivalLaneDefinition& ald, std::string& error) {
408  bool ok = true;
409  if (val == "current") {
410  ald = ARRIVAL_LANE_CURRENT;
411  } else {
412  try {
413  lane = TplConvert::_2int(val.c_str());
414  ald = ARRIVAL_LANE_GIVEN;
415  if (lane < 0) {
416  ok = false;
417  }
418  } catch (...) {
419  ok = false;
420  }
421  }
422  if (!ok) {
423  error = "Invalid arrivalLane definition for " + element + " '" + id + "';\n must be one of (\"current\", or an int>=0)";
424  }
425  return ok;
426 }
427 
428 
429 bool
430 SUMOVehicleParameter::parseArrivalPos(const std::string& val, const std::string& element, const std::string& id,
431  SUMOReal& pos, ArrivalPosDefinition& apd, std::string& error) {
432  bool ok = true;
433  if (val == "random") {
434  apd = ARRIVAL_POS_RANDOM;
435  } else if (val == "max") {
436  apd = ARRIVAL_POS_MAX;
437  } else {
438  try {
439  pos = TplConvert::_2SUMOReal(val.c_str());
440  apd = ARRIVAL_POS_GIVEN;
441  } catch (...) {
442  ok = false;
443  }
444  }
445  if (!ok) {
446  error = "Invalid arrivalPos definition for " + element + " '" + id + "';\n must be one of (\"random\", \"max\", or a float)";
447  }
448  return ok;
449 }
450 
451 
452 bool
453 SUMOVehicleParameter::parseArrivalSpeed(const std::string& val, const std::string& element, const std::string& id,
454  SUMOReal& speed, ArrivalSpeedDefinition& asd, std::string& error) {
455  bool ok = true;
456  if (val == "current") {
457  asd = ARRIVAL_SPEED_CURRENT;
458  } else {
459  try {
460  speed = TplConvert::_2SUMOReal(val.c_str());
461  if (speed < 0) {
462  ok = false;
463  }
464  asd = ARRIVAL_SPEED_GIVEN;
465  } catch (...) {
466  ok = false;
467  }
468  }
469  if (!ok) {
470  error = "Invalid arrivalSpeed definition for " + element + " '" + id + "';\n must be one of (\"current\", or a float>=0)";
471  }
472  return ok;
473 }
474 
475 
476 SUMOReal
477 SUMOVehicleParameter::interpretEdgePos(SUMOReal pos, SUMOReal maximumValue, SumoXMLAttr attr, const std::string& id) {
478  if (pos < 0) {
479  pos = maximumValue + pos;
480  }
481  if (pos > maximumValue) {
482  WRITE_WARNING("Invalid " + toString(attr) + " " + toString(pos) + " given for " + id + ". Using edge end instead.");
483  pos = maximumValue;
484  }
485  return pos;
486 }
487 
488 
489 /****************************************************************************/
The departure is person triggered.
const int VEHPARS_TO_TAZ_SET
The current lane shall be used.
const int STOP_CONTAINER_TRIGGER_SET
No information given; use default.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
SumoXMLTag
Numbers representing SUMO-XML - element names.
RGBColor color
The vehicle&#39;s color.
long long int SUMOTime
Definition: SUMOTime.h:43
The time is given.
static bool parseDepartSpeed(const std::string &val, const std::string &element, const std::string &id, SUMOReal &speed, DepartSpeedDefinition &dsd, std::string &error)
Validates a given departSpeed value.
The vehicle is discarded if emission fails (not fully implemented yet)
std::string vtypeid
The vehicle&#39;s type id.
static bool parseArrivalPos(const std::string &val, const std::string &element, const std::string &id, SUMOReal &pos, ArrivalPosDefinition &apd, std::string &error)
Validates a given arrivalPos value.
ArrivalLaneDefinition
Possible ways to choose the arrival lane.
static SUMOReal _2SUMOReal(const E *const data)
Definition: TplConvert.h:242
ArrivalLaneDefinition arrivalLaneProcedure
Information how the vehicle shall choose the lane to arrive on.
The speed is given.
The departure is container triggered.
DepartLaneDefinition departLaneProcedure
Information how the vehicle shall choose the lane to depart from.
SUMOReal departSpeed
(optional) The initial speed of the vehicle
The position is given.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
SUMOReal arrivalSpeed
(optional) The final speed of the vehicle (not used yet)
The least occupied lane is used.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:59
SUMOReal arrivalPos
(optional) The position the vehicle shall arrive on
const int VEHPARS_ARRIVALLANE_SET
The speed is chosen randomly.
void write(OutputDevice &dev) const
Writes the stop as XML.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
The lane is chosen randomly.
unsigned int personNumber
The static number of persons in the vehicle when it departs (not including boarding persons) ...
ArrivalSpeedDefinition arrivalSpeedProcedure
Information how the vehicle&#39;s end speed shall be chosen.
The arrival position is given.
const std::string DEFAULT_VTYPE_ID
The least occupied lane from best lanes.
The position is chosen randomly.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
The speed is given.
std::string toTaz
The vehicle&#39;s destination zone (district)
The lane is given.
const int VEHPARS_ARRIVALSPEED_SET
static SUMOReal interpretEdgePos(SUMOReal pos, SUMOReal maximumValue, SumoXMLAttr attr, const std::string &id)
Interprets negative edge positions and fits them onto a given edge.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
SUMOVehicleParameter()
Constructor.
const int STOP_START_SET
bool defaultOptionOverrides(const OptionsCont &oc, const std::string &optionName) const
Returns whether the defaults shall be used.
DepartSpeedDefinition departSpeedProcedure
Information how the vehicle&#39;s initial speed shall be chosen.
DepartLaneDefinition
Possible ways to choose a lane on depart.
OutputDevice & writeNonEmptyAttr(const SumoXMLAttr attr, const std::string &val)
writes a string attribute only if it is not the empty string and not the string "default" ...
Definition: OutputDevice.h:290
DepartPosDefinition departPosProcedure
Information how the vehicle shall choose the departure position.
The least occupied lane from lanes which allow the continuation.
bool wasSet(int what) const
Returns whether the given parameter was set.
const int VEHPARS_DEPARTSPEED_SET
The current speed is used.
int arrivalLane
(optional) The lane the vehicle shall arrive on (not used yet)
SUMOTime depart
The vehicle&#39;s departure time.
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
The maximum speed is used.
DepartDefinition departProcedure
Information how the vehicle shall choose the depart time.
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:46
void write(OutputDevice &dev, const OptionsCont &oc, const SumoXMLTag tag=SUMO_TAG_VEHICLE) const
Writes the parameters as a beginning element.
virtual bool ok()
returns the information whether one can write into the device
std::string fromTaz
The vehicle&#39;s origin zone (district)
No information given; use default.
const int STOP_EXPECTED_SET
static bool parseArrivalLane(const std::string &val, const std::string &element, const std::string &id, int &lane, ArrivalLaneDefinition &ald, std::string &error)
Validates a given arrivalLane value.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:54
const int VEHPARS_COLOR_SET
If a fixed number of random choices fails, a free position is chosen.
static bool parseDepartPos(const std::string &val, const std::string &element, const std::string &id, SUMOReal &pos, DepartPosDefinition &dpd, std::string &error)
Validates a given departPos value.
const int VEHPARS_FROM_TAZ_SET
The rightmost lane the vehicle may use.
int departLane
(optional) The lane the vehicle shall depart from (index in edge)
std::string line
The vehicle&#39;s line (mainly for public transport)
DepartSpeedDefinition
Possible ways to choose the departure speed.
const int STOP_END_SET
const int VEHPARS_LINE_SET
static bool parseArrivalSpeed(const std::string &val, const std::string &element, const std::string &id, SUMOReal &speed, ArrivalSpeedDefinition &asd, std::string &error)
Validates a given arrivalSpeed value.
const int VEHPARS_ARRIVALPOS_SET
No information given; use default.
const int STOP_PARKING_SET
static int _2int(const E *const data)
Definition: TplConvert.h:114
const int STOP_TRIGGER_SET
const int STOP_EXPECTED_CONTAINERS_SET
int setParameter
Information for the router which parameter were set.
A storage for options typed value containers)
Definition: OptionsCont.h:108
const int VEHPARS_DEPARTLANE_SET
No information given; use default.
The arrival lane is given.
SUMOReal departPos
(optional) The position the vehicle shall depart from
const int VEHPARS_VTYPE_SET
No information given; use default.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:213
ArrivalPosDefinition
Possible ways to choose the arrival position.
No information given; use default.
A free position is chosen.
const int VEHPARS_DEPARTPOS_SET
Insert behind the last vehicle as close as possible to still allow the specified departSpeed. Fallback to DEPART_POS_BASE if there is no vehicle on the departLane yet.
const int VEHPARS_PERSON_NUMBER_SET
unsigned int containerNumber
The static number of containers in the vehicle when it departs.
ArrivalSpeedDefinition
Possible ways to choose the arrival speed.
DepartPosDefinition
Possible ways to choose the departure position.
const int VEHPARS_CONTAINER_NUMBER_SET
A color information.
The maximum arrival position is used.
Back-at-zero position.
DepartDefinition
Possible ways to depart.
static bool parseDepart(const std::string &val, const std::string &element, const std::string &id, SUMOTime &depart, DepartDefinition &dd, std::string &error)
Validates a given depart value.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
ArrivalPosDefinition arrivalPosProcedure
Information how the vehicle shall choose the arrival position.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
static bool parseDepartLane(const std::string &val, const std::string &element, const std::string &id, int &lane, DepartLaneDefinition &dld, std::string &error)
Validates a given departLane value.
The arrival position is chosen randomly.