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.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 // 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  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 
92 void
94  myTag = tag;
95 }
96 
97 
100  return myTag;
101 }
102 
103 
106  return mySumoBaseObjectParent;
107 }
108 
109 
110 std::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 
144 const std::string&
146  if (hasStringAttribute(attr)) {
147  return myStringAttributes.at(attr);
148  } else {
149  handleAttributeError(attr, "string");
150  throw ProcessError();
151  }
152 }
153 
154 
155 int
157  if (hasIntAttribute(attr)) {
158  return myIntAttributes.at(attr);
159  } else {
160  handleAttributeError(attr, "int");
161  throw ProcessError();
162  }
163 }
164 
165 
166 double
168  if (hasDoubleAttribute(attr)) {
169  return myDoubleAttributes.at(attr);
170  } else {
171  handleAttributeError(attr, "double");
172  throw ProcessError();
173  }
174 }
175 
176 
177 bool
179  if (hasBoolAttribute(attr)) {
180  return myBoolAttributes.at(attr);
181  } else {
182  handleAttributeError(attr, "bool");
183  throw ProcessError();
184  }
185 }
186 
187 
188 const Position&
190  if (hasPositionAttribute(attr)) {
191  return myPositionAttributes.at(attr);
192  } else {
193  handleAttributeError(attr, "position");
194  throw ProcessError();
195  }
196 }
197 
198 
199 SUMOTime
201  if (hasTimeAttribute(attr)) {
202  return myTimeAttributes.at(attr);
203  } else {
204  handleAttributeError(attr, "time");
205  throw ProcessError();
206  }
207 }
208 
209 
210 const RGBColor&
212  if (hasColorAttribute(attr)) {
213  return myColorAttributes.at(attr);
214  } else {
215  handleAttributeError(attr, "color");
216  throw ProcessError();
217  }
218 }
219 
220 
221 const std::vector<std::string>&
223  if (hasStringListAttribute(attr)) {
224  return myStringListAttributes.at(attr);
225  } else {
226  handleAttributeError(attr, "string list");
227  throw ProcessError();
228  }
229 }
230 
231 
232 const PositionVector&
234  if (hasPositionVectorAttribute(attr)) {
235  return myPositionVectorAttributes.at(attr);
236  } else {
237  handleAttributeError(attr, "position vector");
238  throw ProcessError();;
239  }
240 }
241 
242 
245  return myVClass;
246 }
247 
248 
249 const SUMOVTypeParameter&
251  if (myDefinedVehicleTypeParameter) {
252  return myVehicleTypeParameter;
253  } else {
254  throw ProcessError("Undefined vehicleType parameter");
255  }
256 }
257 
258 
261  if (myDefinedVehicleParameter) {
262  return myVehicleParameter;
263  } else {
264  throw ProcessError("Undefined vehicle parameter");
265  }
266 }
267 
268 
271  if (myDefinedStopParameter) {
272  return myStopParameter;
273  } else {
274  throw ProcessError("Undefined stop parameter");
275  }
276 
277 }
278 
279 
280 const std::map<std::string, std::string>&
282  return myParameters;
283 }
284 
285 
286 const std::vector<CommonXMLStructure::SumoBaseObject*>&
288  return mySumoBaseObjectChildren;
289 }
290 
291 
292 bool
294  return myStringAttributes.count(attr) > 0;
295 }
296 
297 
298 bool
300  return myIntAttributes.count(attr) > 0;
301 }
302 
303 
304 bool
306  return myDoubleAttributes.count(attr) > 0;
307 }
308 
309 
310 bool
312  return myBoolAttributes.count(attr) > 0;
313 }
314 
315 
316 bool
318  return myPositionAttributes.count(attr) > 0;
319 }
320 
321 
322 bool
324  return myTimeAttributes.count(attr) > 0;
325 }
326 
327 
328 bool
330  return myColorAttributes.count(attr) > 0;
331 }
332 
333 
334 bool
336  return myStringListAttributes.count(attr) > 0;
337 }
338 
339 
340 bool
342  return myPositionVectorAttributes.count(attr) > 0;
343 }
344 
345 
346 void
348  myStringAttributes[attr] = value;
349 }
350 
351 
352 void
354  myIntAttributes[attr] = value;
355 }
356 
357 
358 void
360  myDoubleAttributes[attr] = value;
361 }
362 
363 
364 void
366  myBoolAttributes[attr] = value;
367 }
368 
369 
370 void
372  myPositionAttributes[attr] = value;
373 }
374 
375 
376 void
378  myTimeAttributes[attr] = value;
379 }
380 
381 
382 void
384  myColorAttributes[attr] = value;
385 }
386 
387 
388 void
389 CommonXMLStructure::SumoBaseObject::addStringListAttribute(const SumoXMLAttr attr, const std::vector<std::string>& value) {
390  myStringListAttributes[attr] = value;
391 }
392 
393 
394 void
396  myPositionVectorAttributes[attr] = value;
397 }
398 
399 
400 void
402  myVClass = vClass;
403 }
404 
405 
406 void
408  myVehicleTypeParameter = *vehicleTypeParameter;
409  myDefinedVehicleTypeParameter = true;
410  // set attribute id
411  addStringAttribute(SUMO_ATTR_ID, myVehicleTypeParameter.id);
412 }
413 
414 
415 void
417  myVehicleParameter = *vehicleParameter;
418  myDefinedVehicleParameter = true;
419  // set attribute id
420  if (!myVehicleParameter.id.empty()) {
421  addStringAttribute(SUMO_ATTR_ID, myVehicleParameter.id);
422  }
423  // set attribute route
424  if (!vehicleParameter->routeid.empty()) {
425  addStringAttribute(SUMO_ATTR_ROUTE, myVehicleParameter.routeid);
426  }
427 }
428 
429 
430 void
432  myStopParameter = stopParameter;
433  myDefinedStopParameter = true;
434  // set attribute edge
435  if (!myStopParameter.edge.empty()) {
436  addStringAttribute(SUMO_ATTR_ID, myStopParameter.edge);
437  }
438  // set attribute lane
439  if (!myStopParameter.lane.empty()) {
440  addStringAttribute(SUMO_ATTR_LANE, myStopParameter.lane);
441  }
442  // set attribute busStop
443  if (!myStopParameter.busstop.empty()) {
444  addStringAttribute(SUMO_ATTR_BUS_STOP, myStopParameter.busstop);
445  }
446  // set attribute containerstop
447  if (!myStopParameter.containerstop.empty()) {
448  addStringAttribute(SUMO_ATTR_CONTAINER_STOP, myStopParameter.containerstop);
449  }
450  // set attribute parkingarea
451  if (!myStopParameter.parkingarea.empty()) {
452  addStringAttribute(SUMO_ATTR_PARKING_AREA, myStopParameter.parkingarea);
453  }
454  // set attribute chargingStation
455  if (!myStopParameter.chargingStation.empty()) {
456  addStringAttribute(SUMO_ATTR_CHARGING_STATION, myStopParameter.chargingStation);
457  }
458 }
459 
460 
461 void
462 CommonXMLStructure::SumoBaseObject::addParameter(const std::string& key, const std::string& value) {
463  // check if we have to insert in vType, vehicle or stop parameters
464  if (myDefinedVehicleTypeParameter) {
465  myVehicleTypeParameter.setParameter(key, value);
466  } else if (myDefinedVehicleParameter) {
467  myVehicleParameter.setParameter(key, value);
468  } else if (myDefinedStopParameter) {
469  myStopParameter.setParameter(key, value);
470  } else {
471  myParameters[key] = value;
472  }
473 }
474 
475 
476 void
478  // just add it into mySumoBaseObjectChildren
479  mySumoBaseObjectChildren.push_back(sumoBaseObject);
480 }
481 
482 
483 void
485  // find sumoBaseObject
486  auto it = std::find(mySumoBaseObjectChildren.begin(), mySumoBaseObjectChildren.end(), sumoBaseObject);
487  // check iterator
488  if (it != mySumoBaseObjectChildren.end()) {
489  mySumoBaseObjectChildren.erase(it);
490  }
491 }
492 
493 
494 void
495 CommonXMLStructure::SumoBaseObject::handleAttributeError(const SumoXMLAttr attr, const std::string& type) const {
496  WRITE_ERROR("Trying to get undefined " + type + " attribute '" + toString(attr) + "' in SUMOBaseObject '" + toString(myTag) + "'");
497 }
498 
499 // ---------------------------------------------------------------------------
500 // CommonXMLStructure - methods
501 // ---------------------------------------------------------------------------
502 
504  mySumoBaseObjectRoot(nullptr),
505  myCurrentSumoBaseObject(nullptr) {
506 
507 }
508 
509 
511  // delete mySumoBaseObjectRoot (this will also delete all SumoBaseObjectChildrens)
512  if (mySumoBaseObjectRoot) {
513  delete mySumoBaseObjectRoot;
514  }
515 }
516 
517 
518 void
520  // first check if root is empty
521  if (mySumoBaseObjectRoot == nullptr) {
522  // create root
523  mySumoBaseObjectRoot = new SumoBaseObject(nullptr);
524  // set tag
526  // update last inserted Root
528  } else {
529  // create new node
530  SumoBaseObject* newSumoBaseObject = new SumoBaseObject(myCurrentSumoBaseObject);
531  // update last inserted node
532  myCurrentSumoBaseObject = newSumoBaseObject;
533  }
534 }
535 
536 
537 void
539  // check that myCurrentSumoBaseObject is valid
541  // check if last inserted SumoBaseObject is the root
543  // reset both pointers
544  myCurrentSumoBaseObject = nullptr;
545  mySumoBaseObjectRoot = nullptr;
546  } else {
547  // update last inserted SumoBaseObject
549  }
550  }
551 }
552 
553 
556  return mySumoBaseObjectRoot;
557 }
558 
559 
563 }
564 
565 /****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:288
std::string time2string(SUMOTime t)
convert SUMOTime to string
Definition: SUMOTime.cpp:68
long long int SUMOTime
Definition: SUMOTime.h:32
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_BUS_STOP
@ SUMO_ATTR_CHARGING_STATION
@ 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
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 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.