Line data Source code
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 : /****************************************************************************/
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 61575719 : SUMOTrafficObject::getStringParam(const std::string& paramName, const bool required, const std::string& deflt) const {
34 61575719 : if (getParameter().hasParameter(paramName)) {
35 1512 : return getParameter().getParameter(paramName, "");
36 61574963 : } else if (getVTypeParameter().hasParameter(paramName)) {
37 29942 : return getVTypeParameter().getParameter(paramName, "");
38 : } else {
39 61559992 : const OptionsCont& oc = OptionsCont::getOptions();
40 61559992 : if (oc.exists(paramName) && oc.isSet(paramName)) {
41 61504060 : return oc.getValueString(paramName);
42 : } else {
43 55932 : 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 55930 : if (oc.exists(paramName)) {
48 51 : return oc.getValueString(paramName);
49 : }
50 : return deflt;
51 : }
52 : }
53 : }
54 : }
55 :
56 :
57 : double
58 51703741 : SUMOTrafficObject::getFloatParam(const std::string& paramName, const bool required, const double deflt) const {
59 51703741 : const std::string val = getStringParam(paramName, required, toString(deflt));
60 : try {
61 51703741 : Distribution_Parameterized dist(val);
62 51703741 : const std::string& error = dist.isValid();
63 51703741 : if (error != "") {
64 24 : throw ProcessError(error);
65 : }
66 51703729 : return dist.sample();
67 51703753 : } catch (const ProcessError& e) {
68 12 : const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
69 36 : WRITE_ERRORF(TL("Invalid distribution / float value '%' for parameter '%' in % '%' (%)."), val, paramName, type, getID(), e.what());
70 12 : return deflt;
71 12 : }
72 : }
73 :
74 :
75 : bool
76 1514822 : SUMOTrafficObject::getBoolParam(const std::string& paramName, const bool required, const bool deflt) const {
77 1514822 : const std::string val = getStringParam(paramName, required, toString(deflt));
78 : try {
79 1514822 : return StringUtils::toBool(val);
80 0 : } catch (const ProcessError&) {
81 0 : const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
82 0 : WRITE_ERRORF(TL("Invalid boolean value '%' for parameter '%' in % '%'."), val, paramName, type, getID());
83 0 : return deflt;
84 0 : }
85 : }
86 :
87 :
88 : SUMOTime
89 3250571 : SUMOTrafficObject::getTimeParam(const std::string& paramName, const bool required, const SUMOTime deflt) const {
90 3250571 : const std::string val = getStringParam(paramName, required, time2string(deflt));
91 : try {
92 3250571 : return string2time(val);
93 0 : } catch (const ProcessError&) {
94 0 : const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
95 0 : WRITE_ERRORF(TL("Invalid time value '%' for parameter '%' in % '%'."), val, paramName, type, getID());
96 : return deflt;
97 0 : }
98 : }
99 :
100 :
101 : /****************************************************************************/
|