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-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
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) const {
59 const std::string val = getStringParam(paramName, required, toString(deflt));
60 try {
62 const std::string& error = dist.isValid();
63 if (error != "") {
64 throw ProcessError(error);
65 }
66 return dist.sample();
67 } catch (const ProcessError& e) {
68 const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
69 WRITE_ERRORF(TL("Invalid distribution / float value '%' for parameter '%' in % '%' (%)."), val, paramName, type, getID(), e.what());
70 return deflt;
71 }
72}
73
74
75bool
76SUMOTrafficObject::getBoolParam(const std::string& paramName, const bool required, const bool deflt) const {
77 const std::string val = getStringParam(paramName, required, toString(deflt));
78 try {
79 return StringUtils::toBool(val);
80 } catch (const ProcessError&) {
81 const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
82 WRITE_ERRORF(TL("Invalid boolean value '%' for parameter '%' in % '%'."), val, paramName, type, getID());
83 return deflt;
84 }
85}
86
87
89SUMOTrafficObject::getTimeParam(const std::string& paramName, const bool required, const SUMOTime deflt) const {
90 const std::string val = getStringParam(paramName, required, time2string(deflt));
91 try {
92 return string2time(val);
93 } catch (const ProcessError&) {
94 const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
95 WRITE_ERRORF(TL("Invalid time value '%' for parameter '%' in % '%'."), val, paramName, type, getID());
96 return deflt;
97 }
98}
99
100
101/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#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 isValid() const
check whether the distribution is valid
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.
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.
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) const
Retrieve a floating point parameter for the traffic object.
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter