Eclipse SUMO - Simulation of Urban MObility
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-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 /****************************************************************************/
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 
63 void
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  myDoubleListAttributes.clear();
79  myPositionVectorAttributes.clear();
80  myParameters.clear();
81  mySumoBaseObjectChildren.clear();
82  // reset flags
83  myDefinedVehicleTypeParameter = false;
84  myDefinedVehicleParameter = false;
85  myDefinedStopParameter = false;
86  // delete all SumoBaseObjectChildrens
87  while (mySumoBaseObjectChildren.size() > 0) {
88  delete mySumoBaseObjectChildren.back();
89  }
90 }
91 
92 
93 void
95  myTag = tag;
96 }
97 
98 
101  return myTag;
102 }
103 
104 
107  return mySumoBaseObjectParent;
108 }
109 
110 
111 std::map<std::string, std::string>
113  std::map<std::string, std::string> result;
114  for (const auto& attr : myStringAttributes) {
115  result[toString(attr.first)] = attr.second;
116  }
117  for (const auto& attr : myIntAttributes) {
118  result[toString(attr.first)] = toString(attr.second);
119  }
120  for (const auto& attr : myDoubleAttributes) {
121  result[toString(attr.first)] = toString(attr.second);
122  }
123  for (const auto& attr : myBoolAttributes) {
124  result[toString(attr.first)] = toString(attr.second);
125  }
126  for (const auto& attr : myPositionAttributes) {
127  result[toString(attr.first)] = toString(attr.second);
128  }
129  for (const auto& attr : myTimeAttributes) {
130  result[toString(attr.first)] = time2string(attr.second);
131  }
132  for (const auto& attr : myColorAttributes) {
133  result[toString(attr.first)] = toString(attr.second);
134  }
135  for (const auto& attr : myStringListAttributes) {
136  result[toString(attr.first)] = toString(attr.second);
137  }
138  for (const auto& attr : myDoubleListAttributes) {
139  result[toString(attr.first)] = toString(attr.second);
140  }
141  for (const auto& attr : myPositionVectorAttributes) {
142  result[toString(attr.first)] = toString(attr.second);
143  }
144  return result;
145 }
146 
147 
148 const std::string&
150  if (hasStringAttribute(attr)) {
151  return myStringAttributes.at(attr);
152  } else {
153  handleAttributeError(attr, "string");
154  throw ProcessError();
155  }
156 }
157 
158 
159 int
161  if (hasIntAttribute(attr)) {
162  return myIntAttributes.at(attr);
163  } else {
164  handleAttributeError(attr, "int");
165  throw ProcessError();
166  }
167 }
168 
169 
170 double
172  if (hasDoubleAttribute(attr)) {
173  return myDoubleAttributes.at(attr);
174  } else {
175  handleAttributeError(attr, "double");
176  throw ProcessError();
177  }
178 }
179 
180 
181 bool
183  if (hasBoolAttribute(attr)) {
184  return myBoolAttributes.at(attr);
185  } else {
186  handleAttributeError(attr, "bool");
187  throw ProcessError();
188  }
189 }
190 
191 
192 const Position&
194  if (hasPositionAttribute(attr)) {
195  return myPositionAttributes.at(attr);
196  } else {
197  handleAttributeError(attr, "position");
198  throw ProcessError();
199  }
200 }
201 
202 
203 SUMOTime
205  if (hasTimeAttribute(attr)) {
206  return myTimeAttributes.at(attr);
207  } else {
208  handleAttributeError(attr, "time");
209  throw ProcessError();
210  }
211 }
212 
213 
214 SUMOTime
217  if (hasTimeAttribute(attr)) {
218  return myTimeAttributes.at(attr);
219  } else {
220  // try 'freq' as alias for 'period'
221  attr = SUMO_ATTR_FREQUENCY;
222  if (hasTimeAttribute(attr)) {
223  return myTimeAttributes.at(attr);
224  }
225  handleAttributeError(SUMO_ATTR_PERIOD, "time");
226  throw ProcessError();
227  }
228 }
229 
230 
231 const RGBColor&
233  if (hasColorAttribute(attr)) {
234  return myColorAttributes.at(attr);
235  } else {
236  handleAttributeError(attr, "color");
237  throw ProcessError();
238  }
239 }
240 
241 
242 const std::vector<std::string>&
244  if (hasStringListAttribute(attr)) {
245  return myStringListAttributes.at(attr);
246  } else {
247  handleAttributeError(attr, "string list");
248  throw ProcessError();
249  }
250 }
251 
252 
253 const std::vector<double>&
255  if (hasDoubleListAttribute(attr)) {
256  return myDoubleListAttributes.at(attr);
257  } else {
258  handleAttributeError(attr, "double list");
259  throw ProcessError();
260  }
261 }
262 
263 
264 const PositionVector&
266  if (hasPositionVectorAttribute(attr)) {
267  return myPositionVectorAttributes.at(attr);
268  } else {
269  handleAttributeError(attr, "position vector");
270  throw ProcessError();
271  }
272 }
273 
274 
277  return myVClass;
278 }
279 
280 
281 const SUMOVTypeParameter&
283  if (myDefinedVehicleTypeParameter) {
284  return myVehicleTypeParameter;
285  } else {
286  throw ProcessError(TL("Undefined vehicleType parameter"));
287  }
288 }
289 
290 
293  if (myDefinedVehicleParameter) {
294  return myVehicleParameter;
295  } else {
296  throw ProcessError(TL("Undefined vehicle parameter"));
297  }
298 }
299 
300 
303  if (myDefinedStopParameter) {
304  return myStopParameter;
305  } else {
306  throw ProcessError(TL("Undefined stop parameter"));
307  }
308 
309 }
310 
311 
312 const std::map<std::string, std::string>&
314  return myParameters;
315 }
316 
317 
318 const std::vector<CommonXMLStructure::SumoBaseObject*>&
320  return mySumoBaseObjectChildren;
321 }
322 
323 
324 bool
326  return myStringAttributes.count(attr) > 0;
327 }
328 
329 
330 bool
332  return myIntAttributes.count(attr) > 0;
333 }
334 
335 
336 bool
338  return myDoubleAttributes.count(attr) > 0;
339 }
340 
341 
342 bool
344  return myBoolAttributes.count(attr) > 0;
345 }
346 
347 
348 bool
350  return myPositionAttributes.count(attr) > 0;
351 }
352 
353 
354 bool
356  return myTimeAttributes.count(attr) > 0;
357 }
358 
359 
360 bool
362  return myColorAttributes.count(attr) > 0;
363 }
364 
365 
366 bool
368  return myStringListAttributes.count(attr) > 0;
369 }
370 
371 
372 bool
374  return myDoubleListAttributes.count(attr) > 0;
375 }
376 
377 
378 bool
380  return myPositionVectorAttributes.count(attr) > 0;
381 }
382 
383 
384 void
386  myStringAttributes[attr] = value;
387 }
388 
389 
390 void
392  myIntAttributes[attr] = value;
393 }
394 
395 
396 void
398  myDoubleAttributes[attr] = value;
399 }
400 
401 
402 void
404  myBoolAttributes[attr] = value;
405 }
406 
407 
408 void
410  myPositionAttributes[attr] = value;
411 }
412 
413 
414 void
416  myTimeAttributes[attr] = value;
417 }
418 
419 
420 void
422  myColorAttributes[attr] = value;
423 }
424 
425 
426 void
427 CommonXMLStructure::SumoBaseObject::addStringListAttribute(const SumoXMLAttr attr, const std::vector<std::string>& value) {
428  myStringListAttributes[attr] = value;
429 }
430 
431 
432 void
433 CommonXMLStructure::SumoBaseObject::addDoubleListAttribute(const SumoXMLAttr attr, const std::vector<double>& value) {
434  myDoubleListAttributes[attr] = value;
435 }
436 
437 
438 void
440  myPositionVectorAttributes[attr] = value;
441 }
442 
443 
444 void
446  myVClass = vClass;
447 }
448 
449 
450 void
452  myVehicleTypeParameter = *vehicleTypeParameter;
453  myDefinedVehicleTypeParameter = true;
454  // set attribute id
455  addStringAttribute(SUMO_ATTR_ID, myVehicleTypeParameter.id);
456 }
457 
458 
459 void
461  myVehicleParameter = *vehicleParameter;
462  myDefinedVehicleParameter = true;
463  // set attribute id
464  if (!myVehicleParameter.id.empty()) {
465  addStringAttribute(SUMO_ATTR_ID, myVehicleParameter.id);
466  }
467  // set attribute route
468  if (!vehicleParameter->routeid.empty()) {
469  addStringAttribute(SUMO_ATTR_ROUTE, myVehicleParameter.routeid);
470  }
471 }
472 
473 
474 void
476  myStopParameter = stopParameter;
477  myDefinedStopParameter = true;
478  // set attribute edge
479  if (!myStopParameter.edge.empty()) {
480  addStringAttribute(SUMO_ATTR_EDGE, myStopParameter.edge);
481  }
482  // set attribute lane
483  if (!myStopParameter.lane.empty()) {
484  addStringAttribute(SUMO_ATTR_LANE, myStopParameter.lane);
485  }
486  // set attribute busStop
487  if (!myStopParameter.busstop.empty()) {
488  addStringAttribute(SUMO_ATTR_BUS_STOP, myStopParameter.busstop);
489  }
490  // set attribute containerstop
491  if (!myStopParameter.containerstop.empty()) {
492  addStringAttribute(SUMO_ATTR_CONTAINER_STOP, myStopParameter.containerstop);
493  }
494  // set attribute parkingarea
495  if (!myStopParameter.parkingarea.empty()) {
496  addStringAttribute(SUMO_ATTR_PARKING_AREA, myStopParameter.parkingarea);
497  }
498  // set attribute chargingStation
499  if (!myStopParameter.chargingStation.empty()) {
500  addStringAttribute(SUMO_ATTR_CHARGING_STATION, myStopParameter.chargingStation);
501  }
502 }
503 
504 
505 void
506 CommonXMLStructure::SumoBaseObject::addParameter(const std::string& key, const std::string& value) {
507  // check if we have to insert in vType, vehicle or stop parameters
508  if (myDefinedVehicleTypeParameter) {
509  myVehicleTypeParameter.setParameter(key, value);
510  } else if (myDefinedVehicleParameter) {
511  myVehicleParameter.setParameter(key, value);
512  } else if (myDefinedStopParameter) {
513  myStopParameter.setParameter(key, value);
514  } else {
515  myParameters[key] = value;
516  }
517 }
518 
519 
520 void
522  // just add it into mySumoBaseObjectChildren
523  mySumoBaseObjectChildren.push_back(sumoBaseObject);
524 }
525 
526 
527 void
529  // find sumoBaseObject
530  auto it = std::find(mySumoBaseObjectChildren.begin(), mySumoBaseObjectChildren.end(), sumoBaseObject);
531  // check iterator
532  if (it != mySumoBaseObjectChildren.end()) {
533  mySumoBaseObjectChildren.erase(it);
534  }
535 }
536 
537 
538 void
539 CommonXMLStructure::SumoBaseObject::handleAttributeError(const SumoXMLAttr attr, const std::string& type) const {
540  WRITE_ERRORF(TL("Trying to get undefined % attribute '%' in SUMOBaseObject '%'"), type, toString(attr), toString(myTag));
541 }
542 
543 // ---------------------------------------------------------------------------
544 // CommonXMLStructure - methods
545 // ---------------------------------------------------------------------------
546 
548  mySumoBaseObjectRoot(nullptr),
549  myCurrentSumoBaseObject(nullptr) {
550 
551 }
552 
553 
555  // delete mySumoBaseObjectRoot (this will also delete all SumoBaseObjectChildrens)
556  if (mySumoBaseObjectRoot) {
557  delete mySumoBaseObjectRoot;
558  }
559 }
560 
561 
562 void
564  // first check if root is empty
565  if (mySumoBaseObjectRoot == nullptr) {
566  // create root
567  mySumoBaseObjectRoot = new SumoBaseObject(nullptr);
568  // set tag
570  // update last inserted Root
572  } else {
573  // create new node
574  SumoBaseObject* newSumoBaseObject = new SumoBaseObject(myCurrentSumoBaseObject);
575  // update last inserted node
576  myCurrentSumoBaseObject = newSumoBaseObject;
577  }
578 }
579 
580 
581 void
583  // check that myCurrentSumoBaseObject is valid
585  // check if last inserted SumoBaseObject is the root
587  // reset both pointers
588  myCurrentSumoBaseObject = nullptr;
589  mySumoBaseObjectRoot = nullptr;
590  } else {
591  // update last inserted SumoBaseObject
593  }
594  }
595 }
596 
597 
600  return mySumoBaseObjectRoot;
601 }
602 
603 
607 }
608 
609 /****************************************************************************/
long long int SUMOTime
Definition: GUI.h:35
#define WRITE_ERRORF(...)
Definition: MsgHandler.h:305
#define TL(string)
Definition: MsgHandler.h:315
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, must be the last one
@ 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 addDoubleListAttribute(const SumoXMLAttr attr, const std::vector< double > &value)
add double list attribute into current SumoBaseObject node
const std::vector< double > & getDoubleListAttribute(const SumoXMLAttr attr) const
get double list attribute
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
SumoXMLTag getTag() const
get XML myTag
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)
add string attribute into current SumoBaseObject node
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
bool hasDoubleListAttribute(const SumoXMLAttr attr) const
check if current SumoBaseObject has the given double list attribute
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 clear()
clear SumoBaseObject
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
~CommonXMLStructure()
Destructor.
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.