Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
CommonXMLStructure.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2023 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// Structure for common XML Parsing
19/****************************************************************************/
20#include <config.h>
21
24
25#include "CommonXMLStructure.h"
26
27
28// ===========================================================================
29// method definitions
30// ===========================================================================
31
32// ---------------------------------------------------------------------------
33// CommonXMLStructure::SumoBaseObject - methods
34// ---------------------------------------------------------------------------
35
37 mySumoBaseObjectParent(parent),
38 myTag(SUMO_TAG_NOTHING),
39 myVClass(SVC_IGNORING),
40 myVehicleTypeParameter(""),
41 myDefinedVehicleTypeParameter(false),
42 myDefinedVehicleParameter(false),
43 myDefinedStopParameter(false) {
44 // add this SumoBaseObject into parent children
47 }
48}
49
50
52 // remove this SumoBaseObject from parent children
53 if (mySumoBaseObjectParent) {
54 mySumoBaseObjectParent->removeSumoBaseObjectChild(this);
55 }
56 // delete all SumoBaseObjectChildrens
57 while (mySumoBaseObjectChildren.size() > 0) {
58 delete mySumoBaseObjectChildren.back();
59 }
60}
61
62
63void
65 // reset tag
66 myTag = SUMO_TAG_NOTHING;
67 // reset vClass
68 myVClass = SVC_IGNORING;
69 // clear containers
70 myStringAttributes.clear();
71 myIntAttributes.clear();
72 myDoubleAttributes.clear();
73 myBoolAttributes.clear();
74 myPositionAttributes.clear();
75 myTimeAttributes.clear();
76 myColorAttributes.clear();
77 myStringListAttributes.clear();
78 myPositionVectorAttributes.clear();
79 myParameters.clear();
80 mySumoBaseObjectChildren.clear();
81 // reset flags
82 myDefinedVehicleTypeParameter = false;
83 myDefinedVehicleParameter = false;
84 myDefinedStopParameter = false;
85 // delete all SumoBaseObjectChildrens
86 while (mySumoBaseObjectChildren.size() > 0) {
87 delete mySumoBaseObjectChildren.back();
88 }
89}
90
91
92void
96
97
100 return myTag;
101}
102
103
106 return mySumoBaseObjectParent;
107}
108
109
110std::map<std::string, std::string>
112 std::map<std::string, std::string> result;
113 for (const auto& attr : myStringAttributes) {
114 result[toString(attr.first)] = attr.second;
115 }
116 for (const auto& attr : myIntAttributes) {
117 result[toString(attr.first)] = toString(attr.second);
118 }
119 for (const auto& attr : myDoubleAttributes) {
120 result[toString(attr.first)] = toString(attr.second);
121 }
122 for (const auto& attr : myBoolAttributes) {
123 result[toString(attr.first)] = toString(attr.second);
124 }
125 for (const auto& attr : myPositionAttributes) {
126 result[toString(attr.first)] = toString(attr.second);
127 }
128 for (const auto& attr : myTimeAttributes) {
129 result[toString(attr.first)] = time2string(attr.second);
130 }
131 for (const auto& attr : myColorAttributes) {
132 result[toString(attr.first)] = toString(attr.second);
133 }
134 for (const auto& attr : myStringListAttributes) {
135 result[toString(attr.first)] = toString(attr.second);
136 }
137 for (const auto& attr : myPositionVectorAttributes) {
138 result[toString(attr.first)] = toString(attr.second);
139 }
140 return result;
141}
142
143
144const std::string&
146 if (hasStringAttribute(attr)) {
147 return myStringAttributes.at(attr);
148 } else {
149 handleAttributeError(attr, "string");
150 throw ProcessError();
151 }
152}
153
154
155int
157 if (hasIntAttribute(attr)) {
158 return myIntAttributes.at(attr);
159 } else {
160 handleAttributeError(attr, "int");
161 throw ProcessError();
162 }
163}
164
165
166double
168 if (hasDoubleAttribute(attr)) {
169 return myDoubleAttributes.at(attr);
170 } else {
171 handleAttributeError(attr, "double");
172 throw ProcessError();
173 }
174}
175
176
177bool
179 if (hasBoolAttribute(attr)) {
180 return myBoolAttributes.at(attr);
181 } else {
182 handleAttributeError(attr, "bool");
183 throw ProcessError();
184 }
185}
186
187
188const Position&
190 if (hasPositionAttribute(attr)) {
191 return myPositionAttributes.at(attr);
192 } else {
193 handleAttributeError(attr, "position");
194 throw ProcessError();
195 }
196}
197
198
201 if (hasTimeAttribute(attr)) {
202 return myTimeAttributes.at(attr);
203 } else {
204 handleAttributeError(attr, "time");
205 throw ProcessError();
206 }
207}
208
209
213 if (hasTimeAttribute(attr)) {
214 return myTimeAttributes.at(attr);
215 } else {
216 // try 'freq' as alias for 'period'
217 attr = SUMO_ATTR_FREQUENCY;
218 if (hasTimeAttribute(attr)) {
219 return myTimeAttributes.at(attr);
220 }
221 handleAttributeError(SUMO_ATTR_PERIOD, "time");
222 throw ProcessError();
223 }
224}
225
226
227const RGBColor&
229 if (hasColorAttribute(attr)) {
230 return myColorAttributes.at(attr);
231 } else {
232 handleAttributeError(attr, "color");
233 throw ProcessError();
234 }
235}
236
237
238const std::vector<std::string>&
240 if (hasStringListAttribute(attr)) {
241 return myStringListAttributes.at(attr);
242 } else {
243 handleAttributeError(attr, "string list");
244 throw ProcessError();
245 }
246}
247
248
249const PositionVector&
251 if (hasPositionVectorAttribute(attr)) {
252 return myPositionVectorAttributes.at(attr);
253 } else {
254 handleAttributeError(attr, "position vector");
255 throw ProcessError();
256 }
257}
258
259
262 return myVClass;
263}
264
265
268 if (myDefinedVehicleTypeParameter) {
269 return myVehicleTypeParameter;
270 } else {
271 throw ProcessError(TL("Undefined vehicleType parameter"));
272 }
273}
274
275
278 if (myDefinedVehicleParameter) {
279 return myVehicleParameter;
280 } else {
281 throw ProcessError(TL("Undefined vehicle parameter"));
282 }
283}
284
285
288 if (myDefinedStopParameter) {
289 return myStopParameter;
290 } else {
291 throw ProcessError(TL("Undefined stop parameter"));
292 }
293
294}
295
296
297const std::map<std::string, std::string>&
299 return myParameters;
300}
301
302
303const std::vector<CommonXMLStructure::SumoBaseObject*>&
305 return mySumoBaseObjectChildren;
306}
307
308
309bool
311 return myStringAttributes.count(attr) > 0;
312}
313
314
315bool
317 return myIntAttributes.count(attr) > 0;
318}
319
320
321bool
323 return myDoubleAttributes.count(attr) > 0;
324}
325
326
327bool
329 return myBoolAttributes.count(attr) > 0;
330}
331
332
333bool
335 return myPositionAttributes.count(attr) > 0;
336}
337
338
339bool
341 return myTimeAttributes.count(attr) > 0;
342}
343
344
345bool
347 return myColorAttributes.count(attr) > 0;
348}
349
350
351bool
353 return myStringListAttributes.count(attr) > 0;
354}
355
356
357bool
359 return myPositionVectorAttributes.count(attr) > 0;
360}
361
362
363void
365 myStringAttributes[attr] = value;
366}
367
368
369void
371 myIntAttributes[attr] = value;
372}
373
374
375void
377 myDoubleAttributes[attr] = value;
378}
379
380
381void
383 myBoolAttributes[attr] = value;
384}
385
386
387void
389 myPositionAttributes[attr] = value;
390}
391
392
393void
395 myTimeAttributes[attr] = value;
396}
397
398
399void
401 myColorAttributes[attr] = value;
402}
403
404
405void
406CommonXMLStructure::SumoBaseObject::addStringListAttribute(const SumoXMLAttr attr, const std::vector<std::string>& value) {
407 myStringListAttributes[attr] = value;
408}
409
410
411void
413 myPositionVectorAttributes[attr] = value;
414}
415
416
417void
421
422
423void
425 myVehicleTypeParameter = *vehicleTypeParameter;
426 myDefinedVehicleTypeParameter = true;
427 // set attribute id
428 addStringAttribute(SUMO_ATTR_ID, myVehicleTypeParameter.id);
429}
430
431
432void
434 myVehicleParameter = *vehicleParameter;
435 myDefinedVehicleParameter = true;
436 // set attribute id
437 if (!myVehicleParameter.id.empty()) {
438 addStringAttribute(SUMO_ATTR_ID, myVehicleParameter.id);
439 }
440 // set attribute route
441 if (!vehicleParameter->routeid.empty()) {
442 addStringAttribute(SUMO_ATTR_ROUTE, myVehicleParameter.routeid);
443 }
444}
445
446
447void
449 myStopParameter = stopParameter;
450 myDefinedStopParameter = true;
451 // set attribute edge
452 if (!myStopParameter.edge.empty()) {
453 addStringAttribute(SUMO_ATTR_EDGE, myStopParameter.edge);
454 }
455 // set attribute lane
456 if (!myStopParameter.lane.empty()) {
457 addStringAttribute(SUMO_ATTR_LANE, myStopParameter.lane);
458 }
459 // set attribute busStop
460 if (!myStopParameter.busstop.empty()) {
461 addStringAttribute(SUMO_ATTR_BUS_STOP, myStopParameter.busstop);
462 }
463 // set attribute containerstop
464 if (!myStopParameter.containerstop.empty()) {
465 addStringAttribute(SUMO_ATTR_CONTAINER_STOP, myStopParameter.containerstop);
466 }
467 // set attribute parkingarea
468 if (!myStopParameter.parkingarea.empty()) {
469 addStringAttribute(SUMO_ATTR_PARKING_AREA, myStopParameter.parkingarea);
470 }
471 // set attribute chargingStation
472 if (!myStopParameter.chargingStation.empty()) {
473 addStringAttribute(SUMO_ATTR_CHARGING_STATION, myStopParameter.chargingStation);
474 }
475}
476
477
478void
479CommonXMLStructure::SumoBaseObject::addParameter(const std::string& key, const std::string& value) {
480 // check if we have to insert in vType, vehicle or stop parameters
481 if (myDefinedVehicleTypeParameter) {
482 myVehicleTypeParameter.setParameter(key, value);
483 } else if (myDefinedVehicleParameter) {
484 myVehicleParameter.setParameter(key, value);
485 } else if (myDefinedStopParameter) {
486 myStopParameter.setParameter(key, value);
487 } else {
488 myParameters[key] = value;
489 }
490}
491
492
493void
495 // just add it into mySumoBaseObjectChildren
496 mySumoBaseObjectChildren.push_back(sumoBaseObject);
497}
498
499
500void
502 // find sumoBaseObject
503 auto it = std::find(mySumoBaseObjectChildren.begin(), mySumoBaseObjectChildren.end(), sumoBaseObject);
504 // check iterator
505 if (it != mySumoBaseObjectChildren.end()) {
506 mySumoBaseObjectChildren.erase(it);
507 }
508}
509
510
511void
513 WRITE_ERRORF(TL("Trying to get undefined % attribute '%' in SUMOBaseObject '%'"), type, toString(attr), toString(myTag));
514}
515
516// ---------------------------------------------------------------------------
517// CommonXMLStructure - methods
518// ---------------------------------------------------------------------------
519
525
526
528 // delete mySumoBaseObjectRoot (this will also delete all SumoBaseObjectChildrens)
531 }
532}
533
534
535void
537 // first check if root is empty
538 if (mySumoBaseObjectRoot == nullptr) {
539 // create root
541 // set tag
543 // update last inserted Root
545 } else {
546 // create new node
547 SumoBaseObject* newSumoBaseObject = new SumoBaseObject(myCurrentSumoBaseObject);
548 // update last inserted node
549 myCurrentSumoBaseObject = newSumoBaseObject;
550 }
551}
552
553
554void
556 // check that myCurrentSumoBaseObject is valid
558 // check if last inserted SumoBaseObject is the root
560 // reset both pointers
561 myCurrentSumoBaseObject = nullptr;
562 mySumoBaseObjectRoot = nullptr;
563 } else {
564 // update last inserted SumoBaseObject
566 }
567 }
568}
569
570
575
576
581
582/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define WRITE_ERRORF(...)
Definition MsgHandler.h:280
#define TL(string)
Definition MsgHandler.h:287
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:69
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
@ SVC_IGNORING
vehicles ignoring classes
SumoXMLTag
Numbers representing SUMO-XML - element names.
@ SUMO_TAG_NOTHING
invalid tag
@ SUMO_TAG_ROOTFILE
root file
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ SUMO_ATTR_LANE
@ SUMO_ATTR_CONTAINER_STOP
@ SUMO_ATTR_PARKING_AREA
@ SUMO_ATTR_EDGE
@ SUMO_ATTR_BUS_STOP
@ SUMO_ATTR_CHARGING_STATION
@ SUMO_ATTR_PERIOD
@ SUMO_ATTR_FREQUENCY
@ SUMO_ATTR_ROUTE
@ SUMO_ATTR_ID
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
void addIntAttribute(const SumoXMLAttr attr, const int value)
add int attribute into current SumoBaseObject node
void setVehicleTypeParameter(const SUMOVTypeParameter *vehicleTypeParameter)
set vehicle type parameters
void addSumoBaseObjectChild(SumoBaseObject *sumoBaseObject)
add SumoBaseObject child
const SUMOVehicleParameter::Stop & getStopParameter() const
get stop parameters
const RGBColor & getColorAttribute(const SumoXMLAttr attr) const
get color attribute
SUMOTime getTimeAttribute(const SumoXMLAttr attr) const
get time attribute
const PositionVector & getPositionVectorAttribute(const SumoXMLAttr attr) const
get PositionVector attribute
SumoBaseObject(SumoBaseObject *sumoBaseObjectParent)
constructor
bool hasBoolAttribute(const SumoXMLAttr attr) const
check if current SumoBaseObject has the given bool attribute
void removeSumoBaseObjectChild(SumoBaseObject *sumoBaseObject)
remove SumoBaseObject child
bool hasStringAttribute(const SumoXMLAttr attr) const
has function
std::map< std::string, std::string > getAllAttributes() const
get all attributes in string format
bool hasPositionAttribute(const SumoXMLAttr attr) const
check if current SumoBaseObject has the given bool attribute
void setTag(const SumoXMLTag tag)
set SumoBaseObject tag
SumoBaseObject * getParentSumoBaseObject() const
get pointer to mySumoBaseObjectParent SumoBaseObject (if is null, then is the root)
const Position & getPositionAttribute(const SumoXMLAttr attr) const
get Position attribute
void addPositionVectorAttribute(const SumoXMLAttr attr, const PositionVector &value)
add PositionVector attribute into current SumoBaseObject node
const std::map< std::string, std::string > & getParameters() const
get parameters
SUMOVehicleClass getVClass() const
vehicle class
SumoBaseObject * mySumoBaseObjectParent
pointer to SumoBaseObject parent (If is null, then is the root)
bool hasPositionVectorAttribute(const SumoXMLAttr attr) const
check if current SumoBaseObject has the given positionVector attribute
bool hasTimeAttribute(const SumoXMLAttr attr) const
check if current SumoBaseObject has the given time attribute
void addBoolAttribute(const SumoXMLAttr attr, const bool value)
add bool attribute into current SumoBaseObject node
const SUMOVTypeParameter & getVehicleTypeParameter() const
get current vType
void addParameter(const std::string &key, const std::string &value)
add parameter into current SumoBaseObject node
bool hasColorAttribute(const SumoXMLAttr attr) const
check if current SumoBaseObject has the given color attribute
void addTimeAttribute(const SumoXMLAttr attr, const SUMOTime value)
add time attribute into current SumoBaseObject node
void addStringListAttribute(const SumoXMLAttr attr, const std::vector< std::string > &value)
add string list attribute into current SumoBaseObject node
bool hasIntAttribute(const SumoXMLAttr attr) const
check if current SumoBaseObject has the given int attribute
int getIntAttribute(const SumoXMLAttr attr) const
get int attribute
void addDoubleAttribute(const SumoXMLAttr attr, const double value)
add double attribute into current SumoBaseObject node
void handleAttributeError(const SumoXMLAttr attr, const std::string &type) const
handle attribute error
bool hasDoubleAttribute(const SumoXMLAttr attr) const
check if current SumoBaseObject has the given double attribute
void addPositionAttribute(const SumoXMLAttr attr, const Position &value)
add Position attribute into current SumoBaseObject node
bool getBoolAttribute(const SumoXMLAttr attr) const
get bool attribute
void setVClass(SUMOVehicleClass vClass)
set vehicle class
SUMOTime getPeriodAttribute() const
get 'period' attribute
void setVehicleParameter(const SUMOVehicleParameter *vehicleParameter)
set vehicle parameters
void addStringAttribute(const SumoXMLAttr attr, const std::string &value)
void setStopParameter(const SUMOVehicleParameter::Stop &stopParameter)
add stop parameters
double getDoubleAttribute(const SumoXMLAttr attr) const
get double attribute
const SUMOVehicleParameter & getVehicleParameter() const
get vehicle parameters
const std::vector< std::string > & getStringListAttribute(const SumoXMLAttr attr) const
get string list attribute
bool hasStringListAttribute(const SumoXMLAttr attr) const
check if current SumoBaseObject has the given string list attribute
void addColorAttribute(const SumoXMLAttr attr, const RGBColor &value)
add color attribute into current SumoBaseObject node
const std::string & getStringAttribute(const SumoXMLAttr attr) const
get string attribute
const std::vector< SumoBaseObject * > & getSumoBaseObjectChildren() const
get SumoBaseObject children
CommonXMLStructure::SumoBaseObject * getSumoBaseObjectRoot() const
get SumoBaseObject root
CommonXMLStructure::SumoBaseObject * getCurrentSumoBaseObject() const
get current editedSumoBaseObject
void openSUMOBaseOBject()
open SUMOBaseOBject
CommonXMLStructure::SumoBaseObject * mySumoBaseObjectRoot
SumoBaseObject root.
CommonXMLStructure()
Constructor.
CommonXMLStructure::SumoBaseObject * myCurrentSumoBaseObject
last inserted SumoBaseObject
void closeSUMOBaseOBject()
close myTag
A point in 2D or 3D with translation and scaling methods.
Definition Position.h:37
A list of positions.
Structure representing possible vehicle parameter.
Definition of vehicle stop (position and duration)
Structure representing possible vehicle parameter.
std::string routeid
The vehicle's route id.