Line data Source code
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 22138342 : SUMOTrafficObject::getStringParam(const std::string& paramName, const bool required, const std::string& deflt) const {
34 22138342 : if (getParameter().hasParameter(paramName)) {
35 1896 : return getParameter().getParameter(paramName, "");
36 22137394 : } else if (getVTypeParameter().hasParameter(paramName)) {
37 2675010 : return getVTypeParameter().getParameter(paramName, "");
38 : } else {
39 20799889 : const OptionsCont& oc = OptionsCont::getOptions();
40 20799889 : if (oc.exists(paramName) && oc.isSet(paramName)) {
41 15155580 : return oc.getValueString(paramName);
42 : } else {
43 5644309 : if (required) {
44 2 : const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
45 6 : throw ProcessError(TLF("Missing parameter '%' for % '%'.", paramName, type, getID()));
46 : } else {
47 5644307 : if (oc.exists(paramName)) {
48 51 : return oc.getValueString(paramName);
49 : }
50 : return deflt;
51 : }
52 : }
53 : }
54 : }
55 :
56 :
57 : double
58 8913778 : SUMOTrafficObject::getFloatParam(const std::string& paramName, const bool required, const double deflt, bool checkDist) const {
59 8913778 : const std::string val = getStringParam(paramName, required, toString(deflt, 16));
60 8913778 : if (!checkDist) {
61 : try {
62 5573335 : return StringUtils::toDouble(val);
63 0 : } catch (NumberFormatException& e) {
64 0 : const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
65 0 : WRITE_ERRORF(TL("Invalid float value '%' for parameter '%' in % '%' (%)."), val, paramName, type, getID(), e.what());
66 0 : return deflt;
67 0 : }
68 : }
69 : try {
70 3340443 : Distribution_Parameterized dist(val);
71 3340443 : const std::string& error = dist.isValid();
72 3340443 : if (error != "") {
73 12 : throw ProcessError(error);
74 : }
75 3340431 : return dist.sample();
76 3340455 : } catch (const ProcessError& e) {
77 12 : const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
78 36 : WRITE_ERRORF(TL("Invalid distribution / float value '%' for parameter '%' in % '%' (%)."), val, paramName, type, getID(), e.what());
79 12 : return deflt;
80 12 : }
81 : }
82 :
83 :
84 : bool
85 1575854 : SUMOTrafficObject::getBoolParam(const std::string& paramName, const bool required, const bool deflt) const {
86 1575854 : const std::string val = getStringParam(paramName, required, toString(deflt));
87 : try {
88 1575854 : return StringUtils::toBool(val);
89 0 : } catch (const ProcessError&) {
90 0 : const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
91 0 : WRITE_ERRORF(TL("Invalid boolean value '%' for parameter '%' in % '%'."), val, paramName, type, getID());
92 0 : return deflt;
93 0 : }
94 : }
95 :
96 :
97 : SUMOTime
98 4929808 : SUMOTrafficObject::getTimeParam(const std::string& paramName, const bool required, const SUMOTime deflt) const {
99 4929808 : const std::string val = getStringParam(paramName, required, time2string(deflt));
100 : try {
101 4929808 : return string2time(val);
102 0 : } catch (const ProcessError&) {
103 0 : const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
104 0 : WRITE_ERRORF(TL("Invalid time value '%' for parameter '%' in % '%'."), val, paramName, type, getID());
105 : return deflt;
106 0 : }
107 : }
108 :
109 :
110 : /****************************************************************************/
|