SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-2014 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(), personCapacity(0), personNumber(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
73  }
75  dev.writeAttr(SUMO_ATTR_DEPART, "triggered");
76  } else {
78  }
79 
80  // optional parameter
81  // departlane
82  if (wasSet(VEHPARS_DEPARTLANE_SET) && !defaultOptionOverrides(oc, "departlane")) {
83  std::string val;
84  switch (departLaneProcedure) {
85  case DEPART_LANE_GIVEN:
86  val = toString(departLane);
87  break;
88  case DEPART_LANE_RANDOM:
89  val = "random";
90  break;
91  case DEPART_LANE_FREE:
92  val = "free";
93  break;
95  val = "allowed";
96  break;
98  val = "best";
99  break;
101  val = "first";
102  break;
103  case DEPART_LANE_DEFAULT:
104  default:
105  break;
106  }
108  } else if (oc.isSet("departlane")) {
109  dev.writeNonEmptyAttr(SUMO_ATTR_DEPARTLANE, oc.getString("departlane"));
110  }
111  // departpos
112  if (wasSet(VEHPARS_DEPARTPOS_SET) && !defaultOptionOverrides(oc, "departpos")) {
113  std::string val;
114  switch (departPosProcedure) {
115  case DEPART_POS_GIVEN:
116  val = toString(departPos);
117  break;
118  case DEPART_POS_RANDOM:
119  val = "random";
120  break;
122  val = "random_free";
123  break;
124  case DEPART_POS_FREE:
125  val = "free";
126  break;
128  val = "pwagSimple";
129  break;
131  val = "pwagGeneric";
132  break;
134  val = "maxSpeedGap";
135  break;
136  case DEPART_POS_BASE:
137  val = "base";
138  break;
139  case DEPART_POS_DEFAULT:
140  default:
141  break;
142  }
144  } else if (oc.isSet("departpos")) {
145  dev.writeNonEmptyAttr(SUMO_ATTR_DEPARTPOS, oc.getString("departpos"));
146  }
147  // departspeed
148  if (wasSet(VEHPARS_DEPARTSPEED_SET) && !defaultOptionOverrides(oc, "departspeed")) {
149  std::string val;
150  switch (departSpeedProcedure) {
151  case DEPART_SPEED_GIVEN:
152  val = toString(departSpeed);
153  break;
154  case DEPART_SPEED_RANDOM:
155  val = "random";
156  break;
157  case DEPART_SPEED_MAX:
158  val = "max";
159  break;
161  default:
162  break;
163  }
165  } else if (oc.isSet("departspeed")) {
166  dev.writeNonEmptyAttr(SUMO_ATTR_DEPARTSPEED, oc.getString("departspeed"));
167  }
168 
169  // arrivallane
170  if (wasSet(VEHPARS_ARRIVALLANE_SET) && !defaultOptionOverrides(oc, "arrivallane")) {
171  std::string val;
172  switch (arrivalLaneProcedure) {
173  case ARRIVAL_LANE_GIVEN:
174  val = toString(arrivalLane);
175  break;
177  val = "current";
178  break;
180  default:
181  break;
182  }
184  } else if (oc.isSet("arrivallane")) {
185  dev.writeNonEmptyAttr(SUMO_ATTR_ARRIVALLANE, oc.getString("arrivallane"));
186  }
187  // arrivalpos
188  if (wasSet(VEHPARS_ARRIVALPOS_SET) && !defaultOptionOverrides(oc, "arrivalpos")) {
189  std::string val;
190  switch (arrivalPosProcedure) {
191  case ARRIVAL_POS_GIVEN:
192  val = toString(arrivalPos);
193  break;
194  case ARRIVAL_POS_RANDOM:
195  val = "random";
196  break;
197  case ARRIVAL_POS_MAX:
198  val = "max";
199  break;
200  case ARRIVAL_POS_DEFAULT:
201  default:
202  break;
203  }
205  } else if (oc.isSet("arrivalpos")) {
206  dev.writeNonEmptyAttr(SUMO_ATTR_ARRIVALPOS, oc.getString("arrivalpos"));
207  }
208  // arrivalspeed
209  if (wasSet(VEHPARS_ARRIVALSPEED_SET) && !defaultOptionOverrides(oc, "arrivalspeed")) {
210  std::string val;
211  switch (arrivalSpeedProcedure) {
212  case ARRIVAL_SPEED_GIVEN:
213  val = toString(arrivalSpeed);
214  break;
216  val = "current";
217  break;
219  default:
220  break;
221  }
223  } else if (oc.isSet("arrivalspeed")) {
224  dev.writeNonEmptyAttr(SUMO_ATTR_ARRIVALSPEED, oc.getString("arrivalspeed"));
225  }
226 
227  // color
228  if (wasSet(VEHPARS_COLOR_SET)) {
230  }
231  if (wasSet(VEHPARS_LINE_SET)) {
233  }
234  if (wasSet(VEHPARS_TAZ_SET)) {
236  }
239  }
242  }
243 }
244 
245 
246 void
248  for (std::vector<Stop>::const_iterator stop = stops.begin(); stop != stops.end(); ++stop) {
249  dev.openTag(SUMO_TAG_STOP);
250  if (stop->busstop != "") {
251  dev.writeAttr(SUMO_ATTR_BUS_STOP, stop->busstop);
252  } else {
253  dev.writeAttr(SUMO_ATTR_LANE, stop->lane);
254  if ((stop->setParameter & STOP_START_SET) != 0) {
255  dev.writeAttr(SUMO_ATTR_STARTPOS, stop->startPos);
256  }
257  if ((stop->setParameter & STOP_END_SET) != 0) {
258  dev.writeAttr(SUMO_ATTR_ENDPOS, stop->endPos);
259  }
260  }
261  if (stop->duration >= 0) {
262  dev.writeAttr(SUMO_ATTR_DURATION, STEPS2TIME(stop->duration));
263  }
264  if (stop->until >= 0) {
265  dev.writeAttr(SUMO_ATTR_UNTIL, STEPS2TIME(stop->until));
266  }
267  if ((stop->setParameter & STOP_TRIGGER_SET) != 0) {
268  dev.writeAttr(SUMO_ATTR_TRIGGERED, stop->triggered);
269  }
270  if ((stop->setParameter & STOP_PARKING_SET) != 0) {
271  dev.writeAttr(SUMO_ATTR_PARKING, stop->parking);
272  }
273  // look, we are writing the set of expected persons in its current state...
274  // if this method is used somewhere in the simulation output,
275  // one should consider keeping the original values additionally,
276  // as the ones we write may hev changed.
277  if ((stop->setParameter & STOP_EXPECTED_SET) != 0) {
278  dev.writeAttr(SUMO_ATTR_EXPECTED, stop->awaitedPersons);
279  }
280  dev.closeTag();
281  }
282 }
283 
284 
285 bool
286 SUMOVehicleParameter::parseDepart(const std::string& val, const std::string& element, const std::string& id,
287  SUMOTime& depart, DepartDefinition& dd, std::string& error) {
288  if (val == "triggered") {
289  dd = DEPART_TRIGGERED;
290  } else if (val == "now") {
291  dd = DEPART_NOW;
292  } else {
293  try {
294  depart = string2time(val);
295  dd = DEPART_GIVEN;
296  if (depart < 0) {
297  error = "Negative departure time in the definition of '" + id + "'.";
298  return false;
299  }
300  } catch (...) {
301  error = "Invalid departure time for " + element + " '" + id + "';\n must be one of (\"triggered\", \"now\", or a float >= 0)";
302  return false;
303  }
304  }
305  return true;
306 }
307 
308 
309 bool
310 SUMOVehicleParameter::parseDepartLane(const std::string& val, const std::string& element, const std::string& id,
311  int& lane, DepartLaneDefinition& dld, std::string& error) {
312  bool ok = true;
313  if (val == "random") {
314  dld = DEPART_LANE_RANDOM;
315  } else if (val == "free") {
316  dld = DEPART_LANE_FREE;
317  } else if (val == "allowed") {
319  } else if (val == "best") {
320  dld = DEPART_LANE_BEST_FREE;
321  } else if (val == "first") {
323  } else {
324  try {
325  lane = TplConvert::_2int(val.c_str());
326  dld = DEPART_LANE_GIVEN;
327  if (lane < 0) {
328  ok = false;
329  }
330  } catch (...) {
331  ok = false;
332  }
333  }
334  if (!ok) {
335  error = "Invalid departLane definition for " + element + " '" + id + "';\n must be one of (\"random\", \"free\", \"allowed\", \"best\", \"first\", or an int>=0)";
336  }
337  return ok;
338 }
339 
340 
341 bool
342 SUMOVehicleParameter::parseDepartPos(const std::string& val, const std::string& element, const std::string& id,
343  SUMOReal& pos, DepartPosDefinition& dpd, std::string& error) {
344  bool ok = true;
345  if (val == "random") {
346  dpd = DEPART_POS_RANDOM;
347  } else if (val == "random_free") {
349  } else if (val == "free") {
350  dpd = DEPART_POS_FREE;
351  } else if (val == "base") {
352  dpd = DEPART_POS_BASE;
353  } else if (val == "pwagSimple") {
355  } else if (val == "pwagGeneric") {
357  } else if (val == "maxSpeedGap") {
359  } else {
360  try {
361  pos = TplConvert::_2SUMOReal(val.c_str());
362  dpd = DEPART_POS_GIVEN;
363  } catch (...) {
364  ok = false;
365  }
366  }
367  if (!ok) {
368  error = "Invalid departPos definition for " + element + " '" + id + "';\n must be one of (\"random\", \"random_free\", \"free\", \"base\", \"pwagSimple\", \"pwagGeneric\", \"maxSpeedGap\", or a float)";
369  }
370  return ok;
371 }
372 
373 
374 bool
375 SUMOVehicleParameter::parseDepartSpeed(const std::string& val, const std::string& element, const std::string& id,
376  SUMOReal& speed, DepartSpeedDefinition& dsd, std::string& error) {
377  bool ok = true;
378  if (val == "random") {
379  dsd = DEPART_SPEED_RANDOM;
380  } else if (val == "max") {
381  dsd = DEPART_SPEED_MAX;
382  } else {
383  try {
384  speed = TplConvert::_2SUMOReal(val.c_str());
385  dsd = DEPART_SPEED_GIVEN;
386  if (speed < 0) {
387  ok = false;
388  }
389  } catch (...) {
390  ok = false;
391  }
392  }
393  if (!ok) {
394  error = "Invalid departSpeed definition for " + element + " '" + id + "';\n must be one of (\"random\", \"max\", or a float>=0)";
395  }
396  return ok;
397 }
398 
399 
400 bool
401 SUMOVehicleParameter::parseArrivalLane(const std::string& val, const std::string& element, const std::string& id,
402  int& lane, ArrivalLaneDefinition& ald, std::string& error) {
403  bool ok = true;
404  if (val == "current") {
405  ald = ARRIVAL_LANE_CURRENT;
406  } else {
407  try {
408  lane = TplConvert::_2int(val.c_str());
409  ald = ARRIVAL_LANE_GIVEN;
410  if (lane < 0) {
411  ok = false;
412  }
413  } catch (...) {
414  ok = false;
415  }
416  }
417  if (!ok) {
418  error = "Invalid arrivalLane definition for " + element + " '" + id + "';\n must be one of (\"current\", or an int>=0)";
419  }
420  return ok;
421 }
422 
423 
424 bool
425 SUMOVehicleParameter::parseArrivalPos(const std::string& val, const std::string& element, const std::string& id,
426  SUMOReal& pos, ArrivalPosDefinition& apd, std::string& error) {
427  bool ok = true;
428  if (val == "random") {
429  apd = ARRIVAL_POS_RANDOM;
430  } else if (val == "max") {
431  apd = ARRIVAL_POS_MAX;
432  } else {
433  try {
434  pos = TplConvert::_2SUMOReal(val.c_str());
435  apd = ARRIVAL_POS_GIVEN;
436  } catch (...) {
437  ok = false;
438  }
439  }
440  if (!ok) {
441  error = "Invalid arrivalPos definition for " + element + " '" + id + "';\n must be one of (\"random\", \"max\", or a float)";
442  }
443  return ok;
444 }
445 
446 
447 bool
448 SUMOVehicleParameter::parseArrivalSpeed(const std::string& val, const std::string& element, const std::string& id,
449  SUMOReal& speed, ArrivalSpeedDefinition& asd, std::string& error) {
450  bool ok = true;
451  if (val == "current") {
452  asd = ARRIVAL_SPEED_CURRENT;
453  } else {
454  try {
455  speed = TplConvert::_2SUMOReal(val.c_str());
456  if (speed < 0) {
457  ok = false;
458  }
459  asd = ARRIVAL_SPEED_GIVEN;
460  } catch (...) {
461  ok = false;
462  }
463  }
464  if (!ok) {
465  error = "Invalid arrivalSpeed definition for " + element + " '" + id + "';\n must be one of (\"current\", or a float>=0)";
466  }
467  return ok;
468 }
469 
470 
471 SUMOReal
472 SUMOVehicleParameter::interpretEdgePos(SUMOReal pos, SUMOReal maximumValue, SumoXMLAttr attr, const std::string& id) {
473  if (pos < 0) {
474  pos = maximumValue + pos;
475  }
476  if (pos > maximumValue) {
477  WRITE_WARNING("Invalid " + toString(attr) + " " + toString(pos) + " given for " + id + ". Inserting at edge end instead.");
478  pos = maximumValue;
479  }
480  return pos;
481 }
482 
483 
484 /****************************************************************************/
The departure is person triggered.
The current lane shall be used.
No information given; use default.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
RGBColor color
The vehicle&#39;s color.
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.
void writeStops(OutputDevice &dev) const
Writes the enclosed stops.
static SUMOReal _2SUMOReal(const E *const data)
Definition: TplConvert.h:223
ArrivalLaneDefinition arrivalLaneProcedure
Information how the vehicle shall choose the lane to arrive on.
The speed is given.
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.
unsigned int personCapacity
The vehicle&#39;s capacity (persons)
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:61
SUMOReal arrivalPos
(optional) The position the vehicle shall arrive on
const int VEHPARS_ARRIVALLANE_SET
The speed is chosen randomly.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
The lane is chosen randomly.
unsigned int personNumber
The number of persons in the vehicle.
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.
Generic max-flow insertion by P.Wagner.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
The speed is given.
A gap is chosen where the maximum speed may be achieved.
std::string toTaz
The vehicle&#39;s destination zone (district)
The lane is given.
std::vector< Stop > stops
List of the stops the vehicle will make.
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 &quot;default&quot; ...
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:48
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:53
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.
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
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.
int SUMOTime
Definition: SUMOTime.h:43
SUMOReal departPos
(optional) The position the vehicle shall depart from
void write(OutputDevice &dev, const OptionsCont &oc) const
Writes the parameters as a beginning element.
const int VEHPARS_TAZ_SET
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:218
ArrivalPosDefinition
Possible ways to choose the arrival position.
No information given; use default.
A free position is chosen.
const int VEHPARS_DEPARTPOS_SET
Simple max-flow insertion by P.Wagner.
const int VEHPARS_PERSON_NUMBER_SET
ArrivalSpeedDefinition
Possible ways to choose the arrival speed.
DepartPosDefinition
Possible ways to choose the departure position.
A color information.
The maximum arrival position is used.
Back-at-zero position.
const int VEHPARS_PERSON_CAPACITY_SET
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.