Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
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-2026 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
26#include "SUMOTrafficObject.h"
27
28
29// ===========================================================================
30// method definitions
31// ===========================================================================
32std::string
33SUMOTrafficObject::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
57double
58SUMOTrafficObject::getFloatParam(const std::string& paramName, const bool required, const double deflt, bool checkDist) const {
59 // formatting the default is expensive (INVALID_DOUBLE has 309 digits), so only do it if getStringParam returns it
60 const bool useDefault = !getParameter().hasParameter(paramName) && !getVTypeParameter().hasParameter(paramName) && !OptionsCont::getOptions().exists(paramName);
61 const std::string val = getStringParam(paramName, required, useDefault ? toString(deflt, 16) : "");
62 if (!checkDist) {
63 try {
64 return StringUtils::toDouble(val);
65 } catch (NumberFormatException& e) {
66 const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
67 WRITE_ERRORF(TL("Invalid float value '%' for parameter '%' in % '%' (%)."), val, paramName, type, getID(), e.what());
68 return deflt;
69 }
70 }
71 try {
73 const std::string& error = dist.isValid();
74 if (error != "") {
75 throw ProcessError(error);
76 }
77 return dist.sample();
78 } catch (const ProcessError& e) {
79 const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
80 WRITE_ERRORF(TL("Invalid distribution / float value '%' for parameter '%' in % '%' (%)."), val, paramName, type, getID(), e.what());
81 return deflt;
82 }
83}
84
85
86bool
87SUMOTrafficObject::getBoolParam(const std::string& paramName, const bool required, const bool deflt) const {
88 const std::string val = getStringParam(paramName, required, toString(deflt));
89 try {
90 return StringUtils::toBool(val);
91 } catch (const ProcessError&) {
92 const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
93 WRITE_ERRORF(TL("Invalid boolean value '%' for parameter '%' in % '%'."), val, paramName, type, getID());
94 return deflt;
95 }
96}
97
98
100SUMOTrafficObject::getTimeParam(const std::string& paramName, const bool required, const SUMOTime deflt) const {
101 const std::string val = getStringParam(paramName, required, time2string(deflt));
102 try {
103 return string2time(val);
104 } catch (const ProcessError&) {
105 const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
106 WRITE_ERRORF(TL("Invalid time value '%' for parameter '%' in % '%'."), val, paramName, type, getID());
107 return deflt;
108 }
109}
110
111
112std::string
114 if (isPerson()) {
115 return "Person";
116 } else if (isContainer()) {
117 return "Container";
118 } else {
119 return "Vehicle";
120 }
121}
122
123/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define WRITE_ERRORF(...)
Definition MsgHandler.h:308
#define TL(string)
Definition MsgHandler.h:316
#define TLF(string,...)
Definition MsgHandler.h:318
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition SUMOTime.cpp:47
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:107
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:57
const std::string isValid() const
check whether the distribution is valid
double sample(SumoRNG *which=nullptr) const
Draw a sample of the distribution using the given RNG.
const std::string & getID() const
Returns the id.
Definition Named.h:73
A storage for options typed value containers)
Definition OptionsCont.h:92
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.
bool hasParameter(const std::string &key) const
Returns whether the parameter is set.
virtual const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
virtual bool isContainer() const
Whether it is a container.
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.
virtual const SUMOVTypeParameter & getVTypeParameter() const =0
Returns the object's "vehicle" type parameter.
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.
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle's parameter (including departure definition)
double getFloatParam(const std::string &paramName, const bool required=false, const double deflt=INVALID_DOUBLE, bool checkDist=true) const
Retrieve a floating point parameter for the traffic object.
std::string getCategory() const
return description for suitable for warnings and messages
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter