Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/vehicle/SUMOTrafficObject.cpp
169678 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2002-2025 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
/****************************************************************************/
14
/// @file SUMOTrafficObject.cpp
15
/// @author Michael Behrisch
16
/// @date 2024-06-27
17
///
18
// Abstract base class for vehicle, person, and container representations
19
/****************************************************************************/
20
#include <config.h>
21
22
#include <utils/common/MsgHandler.h>
23
#include <utils/common/ToString.h>
24
#include <utils/options/OptionsCont.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 {
39
const OptionsCont& oc = OptionsCont::getOptions();
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, bool checkDist) const {
59
const std::string val = getStringParam(paramName, required, toString(deflt));
60
if (!checkDist) {
61
try {
62
return StringUtils::toDouble(val);
63
} catch (NumberFormatException& e) {
64
const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
65
WRITE_ERRORF(TL("Invalid float value '%' for parameter '%' in % '%' (%)."), val, paramName, type, getID(), e.what());
66
return deflt;
67
}
68
}
69
try {
70
Distribution_Parameterized dist(val);
71
const std::string& error = dist.isValid();
72
if (error != "") {
73
throw ProcessError(error);
74
}
75
return dist.sample();
76
} catch (const ProcessError& e) {
77
const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
78
WRITE_ERRORF(TL("Invalid distribution / float value '%' for parameter '%' in % '%' (%)."), val, paramName, type, getID(), e.what());
79
return deflt;
80
}
81
}
82
83
84
bool
85
SUMOTrafficObject::getBoolParam(const std::string& paramName, const bool required, const bool deflt) const {
86
const std::string val = getStringParam(paramName, required, toString(deflt));
87
try {
88
return StringUtils::toBool(val);
89
} catch (const ProcessError&) {
90
const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
91
WRITE_ERRORF(TL("Invalid boolean value '%' for parameter '%' in % '%'."), val, paramName, type, getID());
92
return deflt;
93
}
94
}
95
96
97
SUMOTime
98
SUMOTrafficObject::getTimeParam(const std::string& paramName, const bool required, const SUMOTime deflt) const {
99
const std::string val = getStringParam(paramName, required, time2string(deflt));
100
try {
101
return string2time(val);
102
} catch (const ProcessError&) {
103
const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
104
WRITE_ERRORF(TL("Invalid time value '%' for parameter '%' in % '%'."), val, paramName, type, getID());
105
return deflt;
106
}
107
}
108
109
110
/****************************************************************************/
111
112