Line data Source code
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 : /****************************************************************************/
14 : /// @file CommonXMLStructure.cpp
15 : /// @author Pablo Alvarez Lopez
16 : /// @date May 2021
17 : ///
18 : // Structure for common XML Parsing
19 : /****************************************************************************/
20 : #include <config.h>
21 :
22 : #include <utils/common/MsgHandler.h>
23 : #include <utils/xml/SUMOSAXHandler.h>
24 :
25 : #include "CommonXMLStructure.h"
26 :
27 :
28 : // ===========================================================================
29 : // method definitions
30 : // ===========================================================================
31 :
32 : // ---------------------------------------------------------------------------
33 : // CommonXMLStructure::SumoBaseObject - methods
34 : // ---------------------------------------------------------------------------
35 :
36 0 : CommonXMLStructure::SumoBaseObject::SumoBaseObject(SumoBaseObject* parent) :
37 0 : mySumoBaseObjectParent(parent),
38 0 : myTag(SUMO_TAG_NOTHING),
39 0 : myVClass(SVC_IGNORING),
40 0 : myVehicleTypeParameter(""),
41 0 : myDefinedVehicleTypeParameter(false),
42 0 : myDefinedVehicleParameter(false),
43 0 : myDefinedStopParameter(false) {
44 : // add this SumoBaseObject into parent children
45 0 : if (mySumoBaseObjectParent) {
46 0 : mySumoBaseObjectParent->addSumoBaseObjectChild(this);
47 : }
48 0 : }
49 :
50 :
51 0 : CommonXMLStructure::SumoBaseObject::~SumoBaseObject() {
52 : // remove this SumoBaseObject from parent children
53 0 : if (mySumoBaseObjectParent) {
54 0 : mySumoBaseObjectParent->removeSumoBaseObjectChild(this);
55 : }
56 : // delete all SumoBaseObjectChildrens
57 0 : while (mySumoBaseObjectChildren.size() > 0) {
58 0 : delete mySumoBaseObjectChildren.back();
59 : }
60 0 : }
61 :
62 :
63 : void
64 0 : CommonXMLStructure::SumoBaseObject::clear() {
65 : // reset tag
66 0 : myTag = SUMO_TAG_NOTHING;
67 : // reset vClass
68 0 : 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 0 : myDefinedVehicleTypeParameter = false;
84 0 : myDefinedVehicleParameter = false;
85 0 : myDefinedStopParameter = false;
86 : // delete all SumoBaseObjectChildrens
87 0 : while (mySumoBaseObjectChildren.size() > 0) {
88 0 : delete mySumoBaseObjectChildren.back();
89 : }
90 0 : }
91 :
92 :
93 : void
94 0 : CommonXMLStructure::SumoBaseObject::setTag(const SumoXMLTag tag) {
95 0 : myTag = tag;
96 0 : }
97 :
98 :
99 : SumoXMLTag
100 0 : CommonXMLStructure::SumoBaseObject::getTag() const {
101 0 : return myTag;
102 : }
103 :
104 :
105 : CommonXMLStructure::SumoBaseObject*
106 0 : CommonXMLStructure::SumoBaseObject::getParentSumoBaseObject() const {
107 0 : return mySumoBaseObjectParent;
108 : }
109 :
110 :
111 : std::map<std::string, std::string>
112 0 : CommonXMLStructure::SumoBaseObject::getAllAttributes() const {
113 : std::map<std::string, std::string> result;
114 0 : for (const auto& attr : myStringAttributes) {
115 0 : result[toString(attr.first)] = attr.second;
116 : }
117 0 : for (const auto& attr : myIntAttributes) {
118 0 : result[toString(attr.first)] = toString(attr.second);
119 : }
120 0 : for (const auto& attr : myDoubleAttributes) {
121 0 : result[toString(attr.first)] = toString(attr.second);
122 : }
123 0 : for (const auto& attr : myBoolAttributes) {
124 0 : result[toString(attr.first)] = toString(attr.second);
125 : }
126 0 : for (const auto& attr : myPositionAttributes) {
127 0 : result[toString(attr.first)] = toString(attr.second);
128 : }
129 0 : for (const auto& attr : myTimeAttributes) {
130 0 : result[toString(attr.first)] = time2string(attr.second);
131 : }
132 0 : for (const auto& attr : myColorAttributes) {
133 0 : result[toString(attr.first)] = toString(attr.second);
134 : }
135 0 : for (const auto& attr : myStringListAttributes) {
136 0 : result[toString(attr.first)] = toString(attr.second);
137 : }
138 0 : for (const auto& attr : myDoubleListAttributes) {
139 0 : result[toString(attr.first)] = toString(attr.second);
140 : }
141 0 : for (const auto& attr : myPositionVectorAttributes) {
142 0 : result[toString(attr.first)] = toString(attr.second);
143 : }
144 0 : return result;
145 : }
146 :
147 :
148 : const std::string&
149 0 : CommonXMLStructure::SumoBaseObject::getStringAttribute(const SumoXMLAttr attr) const {
150 0 : if (hasStringAttribute(attr)) {
151 0 : return myStringAttributes.at(attr);
152 : } else {
153 0 : handleAttributeError(attr, "string");
154 0 : throw ProcessError();
155 : }
156 : }
157 :
158 :
159 : int
160 0 : CommonXMLStructure::SumoBaseObject::getIntAttribute(const SumoXMLAttr attr) const {
161 0 : if (hasIntAttribute(attr)) {
162 0 : return myIntAttributes.at(attr);
163 : } else {
164 0 : handleAttributeError(attr, "int");
165 0 : throw ProcessError();
166 : }
167 : }
168 :
169 :
170 : double
171 0 : CommonXMLStructure::SumoBaseObject::getDoubleAttribute(const SumoXMLAttr attr) const {
172 0 : if (hasDoubleAttribute(attr)) {
173 0 : return myDoubleAttributes.at(attr);
174 : } else {
175 0 : handleAttributeError(attr, "double");
176 0 : throw ProcessError();
177 : }
178 : }
179 :
180 :
181 : bool
182 0 : CommonXMLStructure::SumoBaseObject::getBoolAttribute(const SumoXMLAttr attr) const {
183 0 : if (hasBoolAttribute(attr)) {
184 0 : return myBoolAttributes.at(attr);
185 : } else {
186 0 : handleAttributeError(attr, "bool");
187 0 : throw ProcessError();
188 : }
189 : }
190 :
191 :
192 : const Position&
193 0 : CommonXMLStructure::SumoBaseObject::getPositionAttribute(const SumoXMLAttr attr) const {
194 0 : if (hasPositionAttribute(attr)) {
195 0 : return myPositionAttributes.at(attr);
196 : } else {
197 0 : handleAttributeError(attr, "position");
198 0 : throw ProcessError();
199 : }
200 : }
201 :
202 :
203 : SUMOTime
204 0 : CommonXMLStructure::SumoBaseObject::getTimeAttribute(const SumoXMLAttr attr) const {
205 0 : if (hasTimeAttribute(attr)) {
206 0 : return myTimeAttributes.at(attr);
207 : } else {
208 0 : handleAttributeError(attr, "time");
209 0 : throw ProcessError();
210 : }
211 : }
212 :
213 :
214 : SUMOTime
215 0 : CommonXMLStructure::SumoBaseObject::getPeriodAttribute() const {
216 0 : SumoXMLAttr attr = SUMO_ATTR_PERIOD;
217 0 : if (hasTimeAttribute(attr)) {
218 0 : return myTimeAttributes.at(attr);
219 : } else {
220 : // try 'freq' as alias for 'period'
221 0 : attr = SUMO_ATTR_FREQUENCY;
222 0 : if (hasTimeAttribute(attr)) {
223 0 : return myTimeAttributes.at(attr);
224 : }
225 0 : handleAttributeError(SUMO_ATTR_PERIOD, "time");
226 0 : throw ProcessError();
227 : }
228 : }
229 :
230 :
231 : const RGBColor&
232 0 : CommonXMLStructure::SumoBaseObject::getColorAttribute(const SumoXMLAttr attr) const {
233 0 : if (hasColorAttribute(attr)) {
234 0 : return myColorAttributes.at(attr);
235 : } else {
236 0 : handleAttributeError(attr, "color");
237 0 : throw ProcessError();
238 : }
239 : }
240 :
241 :
242 : const std::vector<std::string>&
243 0 : CommonXMLStructure::SumoBaseObject::getStringListAttribute(const SumoXMLAttr attr) const {
244 0 : if (hasStringListAttribute(attr)) {
245 0 : return myStringListAttributes.at(attr);
246 : } else {
247 0 : handleAttributeError(attr, "string list");
248 0 : throw ProcessError();
249 : }
250 : }
251 :
252 :
253 : const std::vector<double>&
254 0 : CommonXMLStructure::SumoBaseObject::getDoubleListAttribute(const SumoXMLAttr attr) const {
255 0 : if (hasDoubleListAttribute(attr)) {
256 0 : return myDoubleListAttributes.at(attr);
257 : } else {
258 0 : handleAttributeError(attr, "double list");
259 0 : throw ProcessError();
260 : }
261 : }
262 :
263 :
264 : const PositionVector&
265 0 : CommonXMLStructure::SumoBaseObject::getPositionVectorAttribute(const SumoXMLAttr attr) const {
266 0 : if (hasPositionVectorAttribute(attr)) {
267 0 : return myPositionVectorAttributes.at(attr);
268 : } else {
269 0 : handleAttributeError(attr, "position vector");
270 0 : throw ProcessError();
271 : }
272 : }
273 :
274 :
275 : SUMOVehicleClass
276 0 : CommonXMLStructure::SumoBaseObject::getVClass() const {
277 0 : return myVClass;
278 : }
279 :
280 :
281 : const SUMOVTypeParameter&
282 0 : CommonXMLStructure::SumoBaseObject::getVehicleTypeParameter() const {
283 0 : if (myDefinedVehicleTypeParameter) {
284 0 : return myVehicleTypeParameter;
285 : } else {
286 0 : throw ProcessError(TL("Undefined vehicleType parameter"));
287 : }
288 : }
289 :
290 :
291 : const SUMOVehicleParameter&
292 0 : CommonXMLStructure::SumoBaseObject::getVehicleParameter() const {
293 0 : if (myDefinedVehicleParameter) {
294 0 : return myVehicleParameter;
295 : } else {
296 0 : throw ProcessError(TL("Undefined vehicle parameter"));
297 : }
298 : }
299 :
300 :
301 : const SUMOVehicleParameter::Stop&
302 0 : CommonXMLStructure::SumoBaseObject::getStopParameter() const {
303 0 : if (myDefinedStopParameter) {
304 0 : return myStopParameter;
305 : } else {
306 0 : throw ProcessError(TL("Undefined stop parameter"));
307 : }
308 :
309 : }
310 :
311 :
312 : const std::map<std::string, std::string>&
313 0 : CommonXMLStructure::SumoBaseObject::getParameters() const {
314 0 : return myParameters;
315 : }
316 :
317 :
318 : const std::vector<CommonXMLStructure::SumoBaseObject*>&
319 0 : CommonXMLStructure::SumoBaseObject::getSumoBaseObjectChildren() const {
320 0 : return mySumoBaseObjectChildren;
321 : }
322 :
323 :
324 : bool
325 0 : CommonXMLStructure::SumoBaseObject::hasStringAttribute(const SumoXMLAttr attr) const {
326 0 : return myStringAttributes.count(attr) > 0;
327 : }
328 :
329 :
330 : bool
331 0 : CommonXMLStructure::SumoBaseObject::hasIntAttribute(const SumoXMLAttr attr) const {
332 0 : return myIntAttributes.count(attr) > 0;
333 : }
334 :
335 :
336 : bool
337 0 : CommonXMLStructure::SumoBaseObject::hasDoubleAttribute(const SumoXMLAttr attr) const {
338 0 : return myDoubleAttributes.count(attr) > 0;
339 : }
340 :
341 :
342 : bool
343 0 : CommonXMLStructure::SumoBaseObject::hasBoolAttribute(const SumoXMLAttr attr) const {
344 0 : return myBoolAttributes.count(attr) > 0;
345 : }
346 :
347 :
348 : bool
349 0 : CommonXMLStructure::SumoBaseObject::hasPositionAttribute(const SumoXMLAttr attr) const {
350 0 : return myPositionAttributes.count(attr) > 0;
351 : }
352 :
353 :
354 : bool
355 0 : CommonXMLStructure::SumoBaseObject::hasTimeAttribute(const SumoXMLAttr attr) const {
356 0 : return myTimeAttributes.count(attr) > 0;
357 : }
358 :
359 :
360 : bool
361 0 : CommonXMLStructure::SumoBaseObject::hasColorAttribute(const SumoXMLAttr attr) const {
362 0 : return myColorAttributes.count(attr) > 0;
363 : }
364 :
365 :
366 : bool
367 0 : CommonXMLStructure::SumoBaseObject::hasStringListAttribute(const SumoXMLAttr attr) const {
368 0 : return myStringListAttributes.count(attr) > 0;
369 : }
370 :
371 :
372 : bool
373 0 : CommonXMLStructure::SumoBaseObject::hasDoubleListAttribute(const SumoXMLAttr attr) const {
374 0 : return myDoubleListAttributes.count(attr) > 0;
375 : }
376 :
377 :
378 : bool
379 0 : CommonXMLStructure::SumoBaseObject::hasPositionVectorAttribute(const SumoXMLAttr attr) const {
380 0 : return myPositionVectorAttributes.count(attr) > 0;
381 : }
382 :
383 :
384 : void
385 0 : CommonXMLStructure::SumoBaseObject::addStringAttribute(const SumoXMLAttr attr, const std::string& value) {
386 0 : myStringAttributes[attr] = value;
387 0 : }
388 :
389 :
390 : void
391 0 : CommonXMLStructure::SumoBaseObject::addIntAttribute(const SumoXMLAttr attr, const int value) {
392 0 : myIntAttributes[attr] = value;
393 0 : }
394 :
395 :
396 : void
397 0 : CommonXMLStructure::SumoBaseObject::addDoubleAttribute(const SumoXMLAttr attr, const double value) {
398 0 : myDoubleAttributes[attr] = value;
399 0 : }
400 :
401 :
402 : void
403 0 : CommonXMLStructure::SumoBaseObject::addBoolAttribute(const SumoXMLAttr attr, const bool value) {
404 0 : myBoolAttributes[attr] = value;
405 0 : }
406 :
407 :
408 : void
409 0 : CommonXMLStructure::SumoBaseObject::addPositionAttribute(const SumoXMLAttr attr, const Position& value) {
410 0 : myPositionAttributes[attr] = value;
411 0 : }
412 :
413 :
414 : void
415 0 : CommonXMLStructure::SumoBaseObject::addTimeAttribute(const SumoXMLAttr attr, const SUMOTime value) {
416 0 : myTimeAttributes[attr] = value;
417 0 : }
418 :
419 :
420 : void
421 0 : CommonXMLStructure::SumoBaseObject::addColorAttribute(const SumoXMLAttr attr, const RGBColor& value) {
422 0 : myColorAttributes[attr] = value;
423 0 : }
424 :
425 :
426 : void
427 0 : CommonXMLStructure::SumoBaseObject::addStringListAttribute(const SumoXMLAttr attr, const std::vector<std::string>& value) {
428 0 : myStringListAttributes[attr] = value;
429 0 : }
430 :
431 :
432 : void
433 0 : CommonXMLStructure::SumoBaseObject::addDoubleListAttribute(const SumoXMLAttr attr, const std::vector<double>& value) {
434 0 : myDoubleListAttributes[attr] = value;
435 0 : }
436 :
437 :
438 : void
439 0 : CommonXMLStructure::SumoBaseObject::addPositionVectorAttribute(const SumoXMLAttr attr, const PositionVector& value) {
440 0 : myPositionVectorAttributes[attr] = value;
441 0 : }
442 :
443 :
444 : void
445 0 : CommonXMLStructure::SumoBaseObject::setVClass(SUMOVehicleClass vClass) {
446 0 : myVClass = vClass;
447 0 : }
448 :
449 :
450 : void
451 0 : CommonXMLStructure::SumoBaseObject::setVehicleTypeParameter(const SUMOVTypeParameter* vehicleTypeParameter) {
452 0 : myVehicleTypeParameter = *vehicleTypeParameter;
453 0 : myDefinedVehicleTypeParameter = true;
454 : // set attribute id
455 0 : addStringAttribute(SUMO_ATTR_ID, myVehicleTypeParameter.id);
456 0 : }
457 :
458 :
459 : void
460 0 : CommonXMLStructure::SumoBaseObject::setVehicleParameter(const SUMOVehicleParameter* vehicleParameter) {
461 0 : myVehicleParameter = *vehicleParameter;
462 0 : myDefinedVehicleParameter = true;
463 : // set attribute id
464 0 : if (!myVehicleParameter.id.empty()) {
465 0 : addStringAttribute(SUMO_ATTR_ID, myVehicleParameter.id);
466 : }
467 : // set attribute route
468 0 : if (!vehicleParameter->routeid.empty()) {
469 0 : addStringAttribute(SUMO_ATTR_ROUTE, myVehicleParameter.routeid);
470 : }
471 0 : }
472 :
473 :
474 : void
475 0 : CommonXMLStructure::SumoBaseObject::setStopParameter(const SUMOVehicleParameter::Stop& stopParameter) {
476 0 : myStopParameter = stopParameter;
477 0 : myDefinedStopParameter = true;
478 : // set attribute edge
479 0 : if (!myStopParameter.edge.empty()) {
480 0 : addStringAttribute(SUMO_ATTR_EDGE, myStopParameter.edge);
481 : }
482 : // set attribute lane
483 0 : if (!myStopParameter.lane.empty()) {
484 0 : addStringAttribute(SUMO_ATTR_LANE, myStopParameter.lane);
485 : }
486 : // set attribute busStop
487 0 : if (!myStopParameter.busstop.empty()) {
488 0 : addStringAttribute(SUMO_ATTR_BUS_STOP, myStopParameter.busstop);
489 : }
490 : // set attribute containerstop
491 0 : if (!myStopParameter.containerstop.empty()) {
492 0 : addStringAttribute(SUMO_ATTR_CONTAINER_STOP, myStopParameter.containerstop);
493 : }
494 : // set attribute parkingarea
495 0 : if (!myStopParameter.parkingarea.empty()) {
496 0 : addStringAttribute(SUMO_ATTR_PARKING_AREA, myStopParameter.parkingarea);
497 : }
498 : // set attribute chargingStation
499 0 : if (!myStopParameter.chargingStation.empty()) {
500 0 : addStringAttribute(SUMO_ATTR_CHARGING_STATION, myStopParameter.chargingStation);
501 : }
502 0 : }
503 :
504 :
505 : void
506 0 : 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 0 : if (myDefinedVehicleTypeParameter) {
509 0 : myVehicleTypeParameter.setParameter(key, value);
510 0 : } else if (myDefinedVehicleParameter) {
511 0 : myVehicleParameter.setParameter(key, value);
512 0 : } else if (myDefinedStopParameter) {
513 0 : myStopParameter.setParameter(key, value);
514 : } else {
515 0 : myParameters[key] = value;
516 : }
517 0 : }
518 :
519 :
520 : void
521 0 : CommonXMLStructure::SumoBaseObject::addSumoBaseObjectChild(SumoBaseObject* sumoBaseObject) {
522 : // just add it into mySumoBaseObjectChildren
523 0 : mySumoBaseObjectChildren.push_back(sumoBaseObject);
524 0 : }
525 :
526 :
527 : void
528 0 : CommonXMLStructure::SumoBaseObject::removeSumoBaseObjectChild(SumoBaseObject* sumoBaseObject) {
529 : // find sumoBaseObject
530 0 : auto it = std::find(mySumoBaseObjectChildren.begin(), mySumoBaseObjectChildren.end(), sumoBaseObject);
531 : // check iterator
532 0 : if (it != mySumoBaseObjectChildren.end()) {
533 0 : mySumoBaseObjectChildren.erase(it);
534 : }
535 0 : }
536 :
537 :
538 : void
539 0 : CommonXMLStructure::SumoBaseObject::handleAttributeError(const SumoXMLAttr attr, const std::string& type) const {
540 0 : WRITE_ERRORF(TL("Trying to get undefined % attribute '%' in SUMOBaseObject '%'"), type, toString(attr), toString(myTag));
541 0 : }
542 :
543 : // ---------------------------------------------------------------------------
544 : // CommonXMLStructure - methods
545 : // ---------------------------------------------------------------------------
546 :
547 0 : CommonXMLStructure::CommonXMLStructure() :
548 0 : mySumoBaseObjectRoot(nullptr),
549 0 : myCurrentSumoBaseObject(nullptr) {
550 :
551 0 : }
552 :
553 :
554 0 : CommonXMLStructure::~CommonXMLStructure() {
555 : // delete mySumoBaseObjectRoot (this will also delete all SumoBaseObjectChildrens)
556 0 : if (mySumoBaseObjectRoot) {
557 0 : delete mySumoBaseObjectRoot;
558 : }
559 0 : }
560 :
561 :
562 : void
563 0 : CommonXMLStructure::openSUMOBaseOBject() {
564 : // first check if root is empty
565 0 : if (mySumoBaseObjectRoot == nullptr) {
566 : // create root
567 0 : mySumoBaseObjectRoot = new SumoBaseObject(nullptr);
568 : // set tag
569 0 : mySumoBaseObjectRoot->setTag(SUMO_TAG_ROOTFILE);
570 : // update last inserted Root
571 0 : myCurrentSumoBaseObject = mySumoBaseObjectRoot;
572 : } else {
573 : // create new node
574 0 : SumoBaseObject* newSumoBaseObject = new SumoBaseObject(myCurrentSumoBaseObject);
575 : // update last inserted node
576 0 : myCurrentSumoBaseObject = newSumoBaseObject;
577 : }
578 0 : }
579 :
580 :
581 : void
582 0 : CommonXMLStructure::closeSUMOBaseOBject() {
583 : // check that myCurrentSumoBaseObject is valid
584 0 : if (myCurrentSumoBaseObject) {
585 : // check if last inserted SumoBaseObject is the root
586 0 : if (myCurrentSumoBaseObject->getParentSumoBaseObject() == nullptr) {
587 : // reset both pointers
588 0 : myCurrentSumoBaseObject = nullptr;
589 0 : mySumoBaseObjectRoot = nullptr;
590 : } else {
591 : // update last inserted SumoBaseObject
592 0 : myCurrentSumoBaseObject = myCurrentSumoBaseObject->getParentSumoBaseObject();
593 : }
594 : }
595 0 : }
596 :
597 :
598 : CommonXMLStructure::SumoBaseObject*
599 0 : CommonXMLStructure::getSumoBaseObjectRoot() const {
600 0 : return mySumoBaseObjectRoot;
601 : }
602 :
603 :
604 : CommonXMLStructure::SumoBaseObject*
605 0 : CommonXMLStructure::getCurrentSumoBaseObject() const {
606 0 : return myCurrentSumoBaseObject;
607 : }
608 :
609 : /****************************************************************************/
|