Eclipse SUMO - Simulation of Urban MObility
SUMOTrafficObject.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2002-2024 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
18 // Abstract base class for vehicle, person, and container representations
19 /****************************************************************************/
20 #include <config.h>
21 
23 #include <utils/common/ToString.h>
25 #include "SUMOVehicleParameter.h"
26 #include "SUMOTrafficObject.h"
27 
28 
29 // ===========================================================================
30 // method definitions
31 // ===========================================================================
32 std::string
33 SUMOTrafficObject::getStringParam(const std::string& paramName, const bool required, const std::string& deflt) const {
34  if (getParameter().hasParameter(paramName)) {
35  return getParameter().getParameter(paramName, "");
36  } else if (getVTypeParameter().hasParameter(paramName)) {
37  return getVTypeParameter().getParameter(paramName, "");
38  } else {
40  if (oc.exists(paramName) && oc.isSet(paramName)) {
41  return oc.getValueString(paramName);
42  } else {
43  if (required) {
44  const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
45  throw ProcessError(TLF("Missing parameter '%' for % '%'.", paramName, type, getID()));
46  } else {
47  if (oc.exists(paramName)) {
48  return oc.getValueString(paramName);
49  }
50  return deflt;
51  }
52  }
53  }
54 }
55 
56 
57 double
58 SUMOTrafficObject::getFloatParam(const std::string& paramName, const bool required, const double deflt) const {
59  const std::string val = getStringParam(paramName, required, toString(deflt));
60  try {
61  return Distribution_Parameterized(val).sample();
62  } catch (const ProcessError&) {
63  const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
64  WRITE_ERRORF(TL("Invalid distribution / float value '%' for parameter '%' in % '%'."), val, paramName, type, getID());
65  return deflt;
66  }
67 }
68 
69 
70 bool
71 SUMOTrafficObject::getBoolParam(const std::string& paramName, const bool required, const bool deflt) const {
72  const std::string val = getStringParam(paramName, required, toString(deflt));
73  try {
74  return StringUtils::toBool(val);
75  } catch (const ProcessError&) {
76  const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
77  WRITE_ERRORF(TL("Invalid boolean value '%' for parameter '%' in % '%'."), val, paramName, type, getID());
78  return deflt;
79  }
80 }
81 
82 
84 SUMOTrafficObject::getTimeParam(const std::string& paramName, const bool required, const SUMOTime deflt) const {
85  const std::string val = getStringParam(paramName, required, time2string(deflt));
86  try {
87  return string2time(val);
88  } catch (const ProcessError&) {
89  const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
90  WRITE_ERRORF(TL("Invalid time value '%' for parameter '%' in % '%'."), val, paramName, type, getID());
91  return deflt;
92  }
93 }
94 
95 
96 /****************************************************************************/
long long int SUMOTime
Definition: GUI.h:35
#define WRITE_ERRORF(...)
Definition: MsgHandler.h:305
#define TL(string)
Definition: MsgHandler.h:315
#define TLF(string,...)
Definition: MsgHandler.h:317
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:46
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition: SUMOTime.cpp:69
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
double sample(SumoRNG *which=0) const
Draw a sample of the distribution.
const std::string & getID() const
Returns the id.
Definition: Named.h:74
A storage for options typed value containers)
Definition: OptionsCont.h:89
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
bool exists(const std::string &name) const
Returns the information whether the named option is known.
std::string getValueString(const std::string &name) const
Returns the string-value of the named option (all options)
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:60
virtual const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
virtual bool isVehicle() const
Whether it is a vehicle.
bool getBoolParam(const std::string &paramName, const bool required=false, const bool deflt=false) const
Retrieve a boolean parameter for the traffic object.
std::string getStringParam(const std::string &paramName, const bool required=false, const std::string &deflt="") const
Retrieve a string parameter for the traffic object.
SUMOTime getTimeParam(const std::string &paramName, const bool required=false, const SUMOTime deflt=SUMOTime_MIN) const
Retrieve a time parameter for the traffic object.
virtual bool isPerson() const
Whether it is a person.
double getFloatParam(const std::string &paramName, const bool required=false, const double deflt=INVALID_DOUBLE) const
Retrieve a floating point parameter for the traffic object.
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle's parameter (including departure definition)
virtual const SUMOVTypeParameter & getVTypeParameter() const =0
Returns the object's "vehicle" type parameter.
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter