Eclipse SUMO - Simulation of Urban MObility
Parameterised.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2022 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 // A super class for objects with additional parameters
19 /****************************************************************************/
20 #include <config.h>
25 
26 #include "Parameterised.h"
27 
28 
29 // ===========================================================================
30 // method definitions
31 // ===========================================================================
32 
34 
35 
36 Parameterised::Parameterised(const std::map<std::string, std::string>& mapArg) :
37  myMap(mapArg) {
38 }
39 
40 
42 
43 
44 void
45 Parameterised::setParameter(const std::string& key, const std::string& value) {
46  myMap[key] = value;
47 }
48 
49 
50 void
51 Parameterised::unsetParameter(const std::string& key) {
52  myMap.erase(key);
53 }
54 
55 
56 void
57 Parameterised::updateParameters(const std::map<std::string, std::string>& mapArg) {
58  for (const auto& keyValue : mapArg) {
59  setParameter(keyValue.first, keyValue.second);
60  }
61 }
62 
63 
64 bool
65 Parameterised::knowsParameter(const std::string& key) const {
66  return myMap.find(key) != myMap.end();
67 }
68 
69 
70 const std::string
71 Parameterised::getParameter(const std::string& key, const std::string defaultValue) const {
72  const auto i = myMap.find(key);
73  if (i != myMap.end()) {
74  return i->second;
75  }
76  return defaultValue;
77 }
78 
79 
80 double
81 Parameterised::getDouble(const std::string& key, const double defaultValue) const {
82  const auto i = myMap.find(key);
83  if (i != myMap.end()) {
84  try {
85  return StringUtils::toDouble(i->second);
86  } catch (NumberFormatException&) {
87  WRITE_WARNING("Invalid conversion from string to double (" + i->second + ")");
88  return defaultValue;
89  } catch (EmptyData&) {
90  WRITE_WARNING("Invalid conversion from string to double (empty value)");
91  return defaultValue;
92  }
93  }
94  return defaultValue;
95 }
96 
97 
98 std::vector<double>
99 Parameterised::getDoubles(const std::string& key, std::vector<double> defaultValue) const {
100  const auto i = myMap.find(key);
101  if (i != myMap.end()) {
102  try {
103  std::vector<double> result;
104  for (const std::string& s : StringTokenizer(i->second).getVector()) {
105  result.push_back(StringUtils::toDouble(s));
106  }
107  return result;
108  } catch (NumberFormatException&) {
109  WRITE_WARNING("Invalid conversion from string to doubles (" + i->second + ")");
110  return defaultValue;
111  } catch (EmptyData&) {
112  WRITE_WARNING("Invalid conversion from string to doubles (empty value)");
113  return defaultValue;
114  }
115  }
116  return defaultValue;
117 }
118 
119 void
121  myMap.clear();
122 }
123 
124 
125 const std::map<std::string, std::string>&
127  return myMap;
128 }
129 
130 
131 std::string
132 Parameterised::getParametersStr(const std::string kvsep, const std::string sep) const {
133  std::string result;
134  // Generate an string using configurable seperatrs, default: "key1=value1|key2=value2|...|keyN=valueN"
135  bool addSep = false;
136  for (const auto& keyValue : myMap) {
137  if (addSep) {
138  result += sep;
139  }
140  result += keyValue.first + kvsep + keyValue.second;
141  addSep = true;
142  }
143  return result;
144 }
145 
146 
147 void
149  // first clear map
150  myMap.clear();
151  // set parameter
152  for (const auto& keyValue : params.getParametersMap()) {
153  setParameter(keyValue.first, keyValue.second);
154  }
155 }
156 
157 
158 void
159 Parameterised::setParametersMap(const std::map<std::string, std::string>& paramsMap) {
160  // first clear map
161  myMap.clear();
162  // set parameter
163  for (const auto& keyValue : paramsMap) {
164  setParameter(keyValue.first, keyValue.second);
165  }
166 }
167 
168 
169 void
170 Parameterised::setParametersStr(const std::string& paramsString, const std::string kvsep, const std::string sep) {
171  // clear parameters
172  myMap.clear();
173  // separate value in a vector of string using | as separator
174  std::vector<std::string> parameters = StringTokenizer(paramsString, sep).getVector();
175  // iterate over all values
176  for (const auto& keyValue : parameters) {
177  // obtain key and value and save it in myParameters
178  std::vector<std::string> keyValueStr = StringTokenizer(keyValue, kvsep).getVector();
179  setParameter(keyValueStr.front(), keyValueStr.back());
180  }
181 }
182 
183 
184 void
186  // iterate over all parameters and write it
187  for (const auto& keyValue : myMap) {
188  device.openTag(SUMO_TAG_PARAM);
189  device.writeAttr(SUMO_ATTR_KEY, StringUtils::escapeXML(keyValue.first));
190  device.writeAttr(SUMO_ATTR_VALUE, StringUtils::escapeXML(keyValue.second));
191  device.closeTag();
192  }
193 }
194 
195 
196 bool
197 Parameterised::areParametersValid(const std::string& value, bool report, const std::string kvsep, const std::string sep) {
198  std::vector<std::string> parameters = StringTokenizer(value, sep).getVector();
199  // first check if parsed parameters are valid
200  for (const auto& keyValueStr : parameters) {
201  // check if parameter is valid
202  if (!isParameterValid(keyValueStr, kvsep, sep)) {
203  // report depending of flag
204  if (report) {
205  WRITE_WARNING("Invalid format of parameter (" + keyValueStr + ")");
206  }
207  return false;
208  }
209  }
210  // all ok, then return true
211  return true;
212 }
213 
214 // ===========================================================================
215 // private
216 // ===========================================================================
217 
218 bool
219 Parameterised::isParameterValid(const std::string& value, const std::string& kvsep, const std::string& sep) {
220  if (value.find(sep) != std::string::npos || value.find(kvsep) == std::string::npos) {
221  return false;
222  }
223  // separate key and value
224  std::vector<std::string> keyValueStr = StringTokenizer(value, kvsep).getVector();
225  // Check that keyValue size is exactly 2 (key, value)
226  if (keyValueStr.size() == 2) {
227  // check if key and value contains valid characters
228  if (SUMOXMLDefinitions::isValidParameterKey(keyValueStr.front()) == false) {
229  return false;
230  } else {
231  // key=value valid, then return true
232  return true;
233  }
234  } else {
235  // invalid format
236  return false;
237  }
238 }
239 
240 /****************************************************************************/
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:280
@ SUMO_TAG_PARAM
parameter associated to a certain key
@ SUMO_ATTR_VALUE
@ SUMO_ATTR_KEY
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:248
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
An upper class for objects with additional parameters.
Definition: Parameterised.h:41
std::vector< double > getDoubles(const std::string &key, std::vector< double > defaultValue=std::vector< double >()) const
Returns the value for a given key converted to a list of doubles.
static bool isParameterValid(const std::string &value, const std::string &kvsep, const std::string &sep)
check if given string can be parsed to a parameter of type "key=value"
static bool areParametersValid(const std::string &value, bool report=false, const std::string kvsep="=", const std::string sep="|")
check if given string can be parsed to a parameters map "key1=value1|key2=value2|....
void unsetParameter(const std::string &key)
Removes a parameter.
double getDouble(const std::string &key, const double defaultValue) const
Returns the value for a given key converted to a double.
Parameterised()
Default constructor (for Strings)
void setParameters(const Parameterised &params)
set the inner key/value map in map<string, string> format
void setParametersStr(const std::string &paramsString, const std::string kvsep="=", const std::string sep="|")
set the inner key/value map in string format "key1=value1|key2=value2|...|keyN=valueN"
~Parameterised()
Destructor.
void clearParameter()
Clears the parameter map.
virtual const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
std::map< std::string, std::string > myMap
The key->value map.
void writeParams(OutputDevice &device) const
write Params in the given outputdevice
virtual void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
std::string getParametersStr(const std::string kvsep="=", const std::string sep="|") const
Returns the inner key/value map in string format "key1=value1|key2=value2|...|keyN=valueN".
void updateParameters(const std::map< std::string, std::string > &mapArg)
Adds or updates all given parameters from the map.
void setParametersMap(const std::map< std::string, std::string > &paramsMap)
set the inner key/value map in map<string, string> format
const std::map< std::string, std::string > & getParametersMap() const
Returns the inner key/value map.
bool knowsParameter(const std::string &key) const
Returns whether the parameter is known.
static bool isValidParameterKey(const std::string &value)
whether the given string is a valid key for a parameter
std::vector< std::string > getVector()
return vector of strings
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
static std::string escapeXML(const std::string &orig, const bool maskDoubleHyphen=false)
Replaces the standard escapes by their XML entities.