Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
SUMOSAXAttributes.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2007-2026 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/****************************************************************************/
20// Encapsulated SAX-Attributes
21/****************************************************************************/
22#include <config.h>
23
24#include <string>
25#include <iostream>
26#include <sstream>
32#include <utils/geom/Boundary.h>
34#include "SUMOSAXAttributes.h"
35
36
37// ===========================================================================
38// static members
39// ===========================================================================
40const std::string SUMOSAXAttributes::ENCODING = " encoding=\"UTF-8\"";
41
42
43// ===========================================================================
44// method definitions
45// ===========================================================================
46SUMOSAXAttributes::SUMOSAXAttributes(const std::string& objectType):
47 myObjectType(objectType) {}
48
49
50const std::string invalid_return<std::string>::value = "";
51template<>
52std::string SUMOSAXAttributes::fromString(const std::string& value) const {
53 if (value == "") {
54 throw EmptyData();
55 }
56 return value;
57}
58
59
61SUMOSAXAttributes::getSUMOTimeReporting(int attr, const char* objectid,
62 bool& ok, bool report) const {
63 try {
64 bool isPresent = true;
65 const std::string& val = getString(attr, &isPresent);
66 if (!isPresent) {
67 if (report) {
68 emitUngivenError(getName(attr), objectid);
69 }
70 ok = false;
71 return -1;
72 }
73 return string2time(val);
74 } catch (EmptyData&) {
75 if (report) {
76 emitEmptyError(getName(attr), objectid);
77 }
78 } catch (ProcessError&) {
79 if (report) {
80 emitFormatError(getName(attr), "is not a valid time value", objectid);
81 }
82 }
83 ok = false;
84 return -1;
85}
86
87
89SUMOSAXAttributes::getPeriod(const char* objectid, bool& ok, bool report) const {
90 int attr = SUMO_ATTR_PERIOD;
91 try {
92 bool isPresent = true;
93 const std::string& val = getString(attr, &isPresent);
94 if (!isPresent) {
95 // try 'freq' as alias for 'period'
97 isPresent = true;
98 const std::string& valFreq = getString(attr, &isPresent);
99 if (!isPresent) {
100 if (report) {
102 }
103 ok = false;
104 return -1;
105 }
106 return string2time(valFreq);
107 }
108 return string2time(val);
109 } catch (EmptyData&) {
110 if (report) {
111 emitEmptyError(getName(attr), objectid);
112 }
113 } catch (ProcessError&) {
114 if (report) {
115 emitFormatError(getName(attr), "is not a valid time value", objectid);
116 }
117 }
118 ok = false;
119 return -1;
120}
121
122
124SUMOSAXAttributes::getOptSUMOTimeReporting(int attr, const char* objectid,
125 bool& ok, SUMOTime defaultValue, bool report) const {
126 try {
127 bool isPresent = true;
128 const std::string& val = getString(attr, &isPresent);
129 if (!isPresent) {
130 return defaultValue;
131 }
132 return string2time(val);
133 } catch (EmptyData&) {
134 if (report) {
135 emitEmptyError(getName(attr), objectid);
136 }
137 } catch (ProcessError&) {
138 if (report) {
139 emitFormatError(getName(attr), "is not a valid time value", objectid);
140 }
141 }
142 ok = false;
143 return -1;
144}
145
146
148SUMOSAXAttributes::getOptOffsetReporting(int attr, const char* objectid,
149 bool& ok, SUMOTime defaultValue, bool report) const {
150 try {
151 bool isPresent = true;
152 const std::string& val = getString(attr, &isPresent);
153 if (!isPresent) {
154 return defaultValue;
155 }
156 if (val == "begin") {
157 return SUMOTime_MAX;
158 } else {
159 return string2time(val);
160 }
161 } catch (EmptyData&) {
162 if (report) {
163 emitEmptyError(getName(attr), objectid);
164 }
165 } catch (ProcessError&) {
166 if (report) {
167 emitFormatError(getName(attr), "is not a valid time value", objectid);
168 }
169 }
170 ok = false;
171 return -1;
172}
173
174
176SUMOSAXAttributes::getOptPeriod(const char* objectid, bool& ok, SUMOTime defaultValue, bool report) const {
177 int attr = SUMO_ATTR_PERIOD;
178 try {
179 bool isPresent = true;
180 const std::string& val = getString(attr, &isPresent);
181 if (!isPresent) {
182 // try 'freq' as alias for 'period'
183 attr = SUMO_ATTR_FREQUENCY;
184 isPresent = true;
185 const std::string& valFreq = getString(attr, &isPresent);
186 if (!isPresent) {
187 return defaultValue;
188 }
189 return string2time(valFreq);
190 }
191 return string2time(val);
192 } catch (EmptyData&) {
193 if (report) {
194 emitEmptyError(getName(attr), objectid);
195 }
196 } catch (ProcessError&) {
197 if (report) {
198 emitFormatError(getName(attr), "is not a valid time value", objectid);
199 }
200 }
201 ok = false;
202 return -1;
203}
204
205
206void
207SUMOSAXAttributes::emitUngivenError(const std::string& attrname, const char* objectid) const {
208 std::ostringstream oss;
209 oss << "Attribute '" << attrname << "' is missing in definition of ";
210 if (objectid == nullptr || objectid[0] == 0) {
211 oss << "a " << myObjectType;
212 } else {
213 oss << myObjectType << " '" << objectid << "'";
214 }
215 oss << ".";
216 WRITE_ERROR(oss.str());
217}
218
219
220void
221SUMOSAXAttributes::emitEmptyError(const std::string& attrname, const char* objectid) const {
222 std::ostringstream oss;
223 oss << "Attribute '" << attrname << "' in definition of ";
224 if (objectid == nullptr || objectid[0] == 0) {
225 oss << "a " << myObjectType;
226 } else {
227 oss << myObjectType << " '" << objectid << "'";
228 }
229 oss << " is empty.";
230 WRITE_ERROR(oss.str());
231}
232
233
234void
235SUMOSAXAttributes::emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const {
236 std::ostringstream oss;
237 oss << "Attribute '" << attrname << "' in definition of ";
238 if (objectid == nullptr || objectid[0] == 0) {
239 oss << "a " << myObjectType;
240 } else {
241 oss << myObjectType << " '" << objectid << "'";
242 }
243 oss << " " << type << ".";
244 WRITE_ERROR(oss.str());
245}
246
247
248const int invalid_return<int>::value = -1;
249template<>
250int SUMOSAXAttributes::fromString(const std::string& value) const {
251 return StringUtils::toInt(value);
252}
253
254
255const long long int invalid_return<long long int>::value = -1;
256template<>
257long long int SUMOSAXAttributes::fromString(const std::string& value) const {
258 return StringUtils::toLong(value);
259}
260
261
262const double invalid_return<double>::value = -1;
263template<>
264double SUMOSAXAttributes::fromString(const std::string& value) const {
265 return StringUtils::toDouble(value);
266}
267
268
269const bool invalid_return<bool>::value = false;
270template<>
271bool SUMOSAXAttributes::fromString(const std::string& value) const {
272 return StringUtils::toBool(value);
273}
274
275
277template<>
278RGBColor SUMOSAXAttributes::fromString(const std::string& value) const {
279 return RGBColor::parseColor(value);
280}
281
282
284template<>
285Position SUMOSAXAttributes::fromString(const std::string& value) const {
286 StringTokenizer st(value);
287 // check StringTokenizer
288 while (st.hasNext()) {
289 // obtain position
290 StringTokenizer pos(st.next(), ",");
291 // check that position has X-Y or X-Y-Z
292 if ((pos.size() != 2) && (pos.size() != 3)) {
293 throw FormatException("is not a valid position");
294 }
295 // obtain x and y
296 double x = StringUtils::toDouble(pos.next());
297 double y = StringUtils::toDouble(pos.next());
298 // check if return a X-Y or a X-Y-Z Position
299 if (pos.size() == 2) {
300 return Position(x, y);
301 } else {
302 // obtain z
303 double z = StringUtils::toDouble(pos.next());
304 return Position(x, y, z);
305 }
306 }
307 // empty positions aren't allowed
308 throw FormatException("is not a valid position");
309}
310
311
313template<>
314PositionVector SUMOSAXAttributes::fromString(const std::string& value) const {
315 StringTokenizer st(value);
316 PositionVector shape;
317 while (st.hasNext()) {
318 StringTokenizer pos(st.next(), ",");
319 if (pos.size() != 2 && pos.size() != 3) {
320 throw FormatException("is not a valid list of positions");
321 }
322 double x = StringUtils::toDouble(pos.next());
323 double y = StringUtils::toDouble(pos.next());
324 if (pos.size() == 2) {
325 shape.push_back(Position(x, y));
326 } else {
327 double z = StringUtils::toDouble(pos.next());
328 shape.push_back(Position(x, y, z));
329 }
330 }
331 return shape;
332}
333
334
336template<>
337Boundary SUMOSAXAttributes::fromString(const std::string& value) const {
338 StringTokenizer st(value, ",");
339 if (st.size() != 4) {
340 throw FormatException("is not a valid boundary");
341 }
342 const double xmin = StringUtils::toDouble(st.next());
343 const double ymin = StringUtils::toDouble(st.next());
344 const double xmax = StringUtils::toDouble(st.next());
345 const double ymax = StringUtils::toDouble(st.next());
346 return Boundary(xmin, ymin, xmax, ymax);
347}
348
349
351template<>
352SumoXMLEdgeFunc SUMOSAXAttributes::fromString(const std::string& value) const {
353 if (SUMOXMLDefinitions::EdgeFunctions.hasString(value)) {
355 }
356 throw FormatException("is not a valid edge function");
357}
358
359
361template<>
362SumoXMLNodeType SUMOSAXAttributes::fromString(const std::string& value) const {
363 if (SUMOXMLDefinitions::NodeTypes.hasString(value)) {
365 }
366 throw FormatException("is not a valid node type");
367}
368
369
371template<>
372RightOfWay SUMOSAXAttributes::fromString(const std::string& value) const {
373 if (SUMOXMLDefinitions::RightOfWayValues.hasString(value)) {
375 }
376 throw FormatException("is not a valid right of way value");
377}
378
379
381template<>
382FringeType SUMOSAXAttributes::fromString(const std::string& value) const {
383 if (SUMOXMLDefinitions::FringeTypeValues.hasString(value)) {
385 }
386 throw FormatException("is not a valid fringe type");
387}
388
389
391template<>
392RoundaboutType SUMOSAXAttributes::fromString(const std::string& value) const {
393 if (SUMOXMLDefinitions::RoundaboutTypeValues.hasString(value)) {
395 }
396 throw FormatException("is not a valid roundabout type");
397}
398
399
401template<>
402ParkingType SUMOSAXAttributes::fromString(const std::string& value) const {
403 if (value == toString(ParkingType::OPPORTUNISTIC)) {
405 } else {
407 }
408}
409
410
411const std::vector<std::string> invalid_return<std::vector<std::string> >::value = std::vector<std::string>();
412template<>
413std::vector<std::string> SUMOSAXAttributes::fromString(const std::string& value) const {
414 const std::vector<std::string>& ret = StringTokenizer(value).getVector();
415 if (ret.empty()) {
416 throw EmptyData();
417 }
418 return ret;
419}
420
421
422const std::vector<int> invalid_return<std::vector<int> >::value = std::vector<int>();
423template<>
424std::vector<int> SUMOSAXAttributes::fromString(const std::string& value) const {
425 const std::vector<std::string>& tmp = StringTokenizer(value).getVector();
426 if (tmp.empty()) {
427 throw EmptyData();
428 }
429 std::vector<int> ret;
430 for (const std::string& s : tmp) {
431 ret.push_back(StringUtils::toInt(s));
432 }
433 return ret;
434}
435
436
437const std::vector<double> invalid_return<std::vector<double> >::value = std::vector<double>();
438template<>
439std::vector<double> SUMOSAXAttributes::fromString(const std::string& value) const {
440 const std::vector<std::string>& tmp = StringTokenizer(value).getVector();
441 if (tmp.empty()) {
442 throw EmptyData();
443 }
444 std::vector<double> ret;
445 for (const std::string& s : tmp) {
446 ret.push_back(StringUtils::toDouble(s));
447 }
448 return ret;
449}
450
451
454 // first check conditions
456 WRITE_ERROR(TL("Simultaneous specification of vClasses and exceptions is not allowed"));
457 ok = false;
458 }
460 WRITE_ERROR(TL("StopOffset requires an offset value"));
461 ok = false;
462 }
463 // parse elements
464 const std::string vClasses = getOpt<std::string>(SUMO_ATTR_VCLASSES, nullptr, ok, "");
465 const std::string exceptions = getOpt<std::string>(SUMO_ATTR_EXCEPTIONS, nullptr, ok, "");
466 // parse permissions
467 SVCPermissions permissions;
469 permissions = parseVehicleClasses(vClasses);
471 permissions = ~parseVehicleClasses(exceptions);
472 } else {
473 // no vClasses specified, thus apply to all
474 permissions = parseVehicleClasses("all");
475 }
476 // parse offset
477 return StopOffset(permissions, getOpt<double>(SUMO_ATTR_VALUE, nullptr, ok, 0));
478}
479
480/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define WRITE_ERROR(msg)
Definition MsgHandler.h:307
#define TL(string)
Definition MsgHandler.h:316
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition SUMOTime.cpp:47
#define SUMOTime_MAX
Definition SUMOTime.h:34
SVCPermissions parseVehicleClasses(const std::string &allowedS)
Parses the given definition of allowed vehicle classes into the given containers Deprecated classes g...
long long int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
ParkingType
Numbers representing special SUMO-XML-attribute values Information on whether a car is parking on the...
RoundaboutType
classifying roundabout type for nodes
FringeType
classifying boundary nodes
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
RightOfWay
algorithms for computing right of way
@ SUMO_ATTR_VALUE
@ SUMO_ATTR_VCLASSES
@ SUMO_ATTR_EXCEPTIONS
@ SUMO_ATTR_PERIOD
@ SUMO_ATTR_FREQUENCY
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:57
A class that stores a 2D geometrical boundary.
Definition Boundary.h:39
A point in 2D or 3D with translation and scaling methods.
Definition Position.h:37
A list of positions.
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition RGBColor.cpp:267
virtual std::string getString(int id, bool *isPresent=nullptr) const =0
Returns the string-value of the named (by its enum-value) attribute.
SUMOTime getOptPeriod(const char *objectid, bool &ok, SUMOTime defaultValue, bool report=true) const
Tries to read the SUMOTime 'period' attribute.
T fromString(const std::string &value) const
SUMOSAXAttributes(const std::string &objectType)
SUMOTime getOptOffsetReporting(int attr, const char *objectid, bool &ok, SUMOTime defaultValue, bool report=true) const
Tries to read given attribute assuming it is a tls offset (SUMOTime or "begin")
SUMOTime getOptSUMOTimeReporting(int attr, const char *objectid, bool &ok, SUMOTime defaultValue, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
StopOffset getStopOffset(bool &ok) const
Parses a stop offset definition (vClasses/exceptions and value) from the attributes.
void emitFormatError(const std::string &attrname, const std::string &type, const char *objectid) const
virtual std::string getName(int attr) const =0
Converts the given attribute id into a man readable string.
void emitEmptyError(const std::string &attrname, const char *objectid) const
std::string myObjectType
the object type to use in error reporting
static const std::string ENCODING
The encoding of parsed strings.
void emitUngivenError(const std::string &attrname, const char *objectid) const
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
SUMOTime getPeriod(const char *objectid, bool &ok, bool report=true) const
Tries to read the SUMOTime 'period' attribute.
SUMOTime getSUMOTimeReporting(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
static StringBijection< SumoXMLNodeType > NodeTypes
node types
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
static StringBijection< RightOfWay > RightOfWayValues
righ of way algorithms
static StringBijection< RoundaboutType > RoundaboutTypeValues
fringe types
static StringBijection< FringeType > FringeTypeValues
fringe types
stop offset
T get(const std::string &str) const
get key
int size() const
returns the number of existing substrings
std::vector< std::string > getVector()
return vector of strings
bool hasNext()
returns the information whether further substrings exist
std::string next()
returns the next substring when it exists. Otherwise the behaviour is undefined
static long long int toLong(const std::string &sData)
converts a string into the long value described by it by calling the char-type converter,...
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter,...
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter