Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
DataHandler.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-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/****************************************************************************/
18// The XML-Handler for data elements loading
19/****************************************************************************/
20#include <config.h>
21
25#include <utils/xml/XMLSubSys.h>
26
27#include "DataHandler.h"
28
29
30// ===========================================================================
31// method definitions
32// ===========================================================================
33
35 CommonHandler(fileBucket),
36 SUMOSAXHandler(fileBucket->getFilename()) {
37}
38
39
41
42
43bool
45 // run parser and return result
46 return XMLSubSys::runParser(*this, getFileName());
47}
48
49
50void
52 // check if loading was aborted
53 if (!myAbortLoading) {
54 // switch tag
55 switch (obj->getTag()) {
56 // Stopping Places
58 if (buildDataInterval(obj,
62 obj->markAsCreated();
63 }
64 break;
65 case SUMO_TAG_EDGE:
66 if (buildEdgeData(obj,
68 obj->getParameters())) {
69 obj->markAsCreated();
70 }
71 break;
76 obj->getParameters())) {
77 obj->markAsCreated();
78 }
79 break;
80 case SUMO_TAG_TAZREL:
84 obj->getParameters())) {
85 obj->markAsCreated();
86 }
87 break;
88 default:
89 break;
90 }
91 // now iterate over childrens
92 for (const auto& child : obj->getSumoBaseObjectChildren()) {
93 // call this function recursively
95 }
96 }
97}
98
99
100void
102 // obtain tag
103 const SumoXMLTag tag = (element == 0) ? SUMO_TAG_ROOTFILE : static_cast<SumoXMLTag>(element);
104 // open SUMOBaseOBject
105 myCommonXMLStructure.openSUMOBaseOBject();
106 // check tag
107 try {
108 switch (tag) {
109 // interval
111 parseInterval(attrs);
112 break;
113 // datas
114 case SUMO_TAG_EDGE:
115 parseEdgeData(attrs);
116 break;
117 case SUMO_TAG_EDGEREL:
119 break;
120 case SUMO_TAG_TAZREL:
122 break;
123 case SUMO_TAG_PARAM:
124 WRITE_WARNING(TL("Data elements cannot load attributes as params"));
125 myCommonXMLStructure.abortSUMOBaseOBject();
126 break;
127 default:
128 // tag cannot be parsed in routeHandler
129 myCommonXMLStructure.abortSUMOBaseOBject();
130 break;
131 }
132 } catch (InvalidArgument& e) {
133 writeError(e.what());
134 }
135}
136
137
138void
140 // obtain tag
141 const SumoXMLTag tag = static_cast<SumoXMLTag>(element);
142 // get last inserted object
143 CommonXMLStructure::SumoBaseObject* obj = myCommonXMLStructure.getCurrentSumoBaseObject();
144 // close SUMOBaseOBject
145 myCommonXMLStructure.closeSUMOBaseOBject();
146 if (obj) {
147 // check tag
148 switch (tag) {
149 // only interval
151 // parse object and all their childrens
153 // delete object (and all of their childrens)
154 delete obj;
155 break;
156 default:
157 break;
158 }
159 }
160}
161
162
163void
165 // declare Ok Flag
166 bool parsedOk = true;
167 // needed attributes
168 const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "", parsedOk);
169 const double begin = STEPS2TIME(attrs.getSUMOTimeReporting(SUMO_ATTR_BEGIN, "", parsedOk));
170 const double end = STEPS2TIME(attrs.getSUMOTimeReporting(SUMO_ATTR_END, "", parsedOk));
171 // continue if flag is ok
172 if (parsedOk) {
173 // set tag
174 myCommonXMLStructure.getCurrentSumoBaseObject()->setTag(SUMO_TAG_INTERVAL);
175 // add all attributes
176 myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_ID, id);
177 myCommonXMLStructure.getCurrentSumoBaseObject()->addDoubleAttribute(SUMO_ATTR_BEGIN, begin);
178 myCommonXMLStructure.getCurrentSumoBaseObject()->addDoubleAttribute(SUMO_ATTR_END, end);
179 } else {
180 myCommonXMLStructure.getCurrentSumoBaseObject()->setTag(SUMO_TAG_ERROR);
181 }
182}
183
184
185void
187 // declare Ok Flag
188 bool parsedOk = true;
189 // needed attributes
190 const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "", parsedOk);
191 // fill attributes
192 getAttributes(attrs, {SUMO_ATTR_ID});
193 // continue if flag is ok
194 if (parsedOk) {
195 // set tag
196 myCommonXMLStructure.getCurrentSumoBaseObject()->setTag(SUMO_TAG_EDGE);
197 // add all attributes
198 myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_ID, id);
199 } else {
200 myCommonXMLStructure.getCurrentSumoBaseObject()->setTag(SUMO_TAG_ERROR);
201 }
202}
203
204
205void
207 // declare Ok Flag
208 bool parsedOk = true;
209 // needed attributes
210 const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, "", parsedOk);
211 const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, "", parsedOk);
212 // fill attributes
214 // continue if flag is ok
215 if (parsedOk) {
216 // set tag
217 myCommonXMLStructure.getCurrentSumoBaseObject()->setTag(SUMO_TAG_EDGEREL);
218 // add all attributes
219 myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_FROM, from);
220 myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_TO, to);
221 } else {
222 myCommonXMLStructure.getCurrentSumoBaseObject()->setTag(SUMO_TAG_ERROR);
223 }
224}
225
226
227void
229 // declare Ok Flag
230 bool parsedOk = true;
231 // needed attributes
232 const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, "", parsedOk);
233 const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, "", parsedOk);
234 // fill attributes
236 // continue if flag is ok
237 if (parsedOk) {
238 // set tag
239 myCommonXMLStructure.getCurrentSumoBaseObject()->setTag(SUMO_TAG_TAZREL);
240 // add all attributes
241 myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_FROM, from);
242 myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_TO, to);
243 } else {
244 myCommonXMLStructure.getCurrentSumoBaseObject()->setTag(SUMO_TAG_ERROR);
245 }
246}
247
248
249void
250DataHandler::getAttributes(const SUMOSAXAttributes& attrs, const std::vector<SumoXMLAttr> avoidAttributes) const {
251 // transform avoidAttributes to strings
252 std::vector<std::string> avoidAttributesStr;
253 for (const SumoXMLAttr& avoidAttribute : avoidAttributes) {
254 avoidAttributesStr.push_back(toString(avoidAttribute));
255 }
256 // iterate over attributes and fill parameters map
257 for (const std::string& attribute : attrs.getAttributeNames()) {
258 if (std::find(avoidAttributesStr.begin(), avoidAttributesStr.end(), attribute) == avoidAttributesStr.end()) {
259 myCommonXMLStructure.getCurrentSumoBaseObject()->addParameter(attribute, attrs.getStringSecure(attribute, ""));
260 }
261 }
262}
263
264
265void
266DataHandler::checkParent(const SumoXMLTag currentTag, const SumoXMLTag parentTag, bool& ok) {
267 // check that parent SUMOBaseObject's tag is the parentTag
268 if ((myCommonXMLStructure.getCurrentSumoBaseObject()->getParentSumoBaseObject() &&
269 (myCommonXMLStructure.getCurrentSumoBaseObject()->getParentSumoBaseObject()->getTag() == parentTag)) == false) {
270 writeError(toString(currentTag) + " must be defined within the definition of a " + toString(parentTag));
271 ok = false;
272 }
273}
274
275/****************************************************************************/
#define WRITE_WARNING(msg)
Definition MsgHandler.h:286
#define TL(string)
Definition MsgHandler.h:304
#define STEPS2TIME(x)
Definition SUMOTime.h:58
SumoXMLTag
Numbers representing SUMO-XML - element names.
@ SUMO_TAG_INTERVAL
an aggreagated-output interval
@ SUMO_TAG_EDGEREL
a relation between two edges
@ SUMO_TAG_ROOTFILE
root file
@ SUMO_TAG_PARAM
parameter associated to a certain key
@ SUMO_TAG_ERROR
tag used for indicate that there is an error (usually loading elements in handlers)
@ SUMO_TAG_TAZREL
a relation between two TAZs
@ SUMO_TAG_EDGE
begin/end of the description of an edge
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ SUMO_ATTR_BEGIN
weights: time range begin
@ SUMO_ATTR_TO
@ SUMO_ATTR_FROM
@ SUMO_ATTR_END
weights: time range end
@ SUMO_ATTR_ID
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:49
bool writeError(const std::string &error)
write error and enable error creating element
bool myAbortLoading
abort loading
CommonXMLStructure myCommonXMLStructure
common XML Structure
CommonHandler(FileBucket *fileBucket)
Constructor.
const std::map< std::string, std::string > & getParameters() const
get parameters
SumoXMLTag getTag() const
get XML myTag
void markAsCreated()
mark as successfully created
double getDoubleAttribute(const SumoXMLAttr attr) const
get double attribute
const std::string & getStringAttribute(const SumoXMLAttr attr) const
get string attribute
const std::vector< SumoBaseObject * > & getSumoBaseObjectChildren() const
get SumoBaseObject children
virtual bool buildDataInterval(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &dataSetID, const double begin, const double end)=0
Builds DataInterval.
void parseTAZRelationData(const SUMOSAXAttributes &attrs)
parse TAZRelationData attributes
~DataHandler()
Destructor.
void parseSumoBaseObject(CommonXMLStructure::SumoBaseObject *obj)
parse SumoBaseObject (it's called recursivelly)
bool parse()
parse
void parseEdgeRelationData(const SUMOSAXAttributes &attrs)
parse edgeRelationData attributes
virtual void myEndElement(int element)
Called when a closing tag occurs.
void checkParent(const SumoXMLTag currentTag, const SumoXMLTag parentTag, bool &ok)
check parents
void parseEdgeData(const SUMOSAXAttributes &attrs)
parse edgeData attributes
virtual bool buildEdgeRelationData(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &fromEdgeID, const std::string &toEdgeID, const Parameterised::Map &parameters)=0
Builds edgeRelationData.
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
virtual bool buildEdgeData(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &edgeID, const Parameterised::Map &parameters)=0
Builds edgeData.
void getAttributes(const SUMOSAXAttributes &attrs, const std::vector< SumoXMLAttr > avoidAttributes) const
parse attributes as parameters
DataHandler()=delete
invalidate default onstructor
virtual bool buildTAZRelationData(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &fromTAZID, const std::string &toTAZID, const Parameterised::Map &parameters)=0
Builds TAZRelationData.
void parseInterval(const SUMOSAXAttributes &attrs)
const std::string & getFileName() const
returns the current file name
Encapsulated SAX-Attributes.
virtual std::vector< std::string > getAttributeNames() const =0
Retrieves all attribute names.
virtual std::string getStringSecure(int id, const std::string &def) const =0
Returns the string-value of the named (by its enum-value) attribute.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
SUMOTime getSUMOTimeReporting(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
SUMOSAXHandler(const std::string &file="", const std::string &expectedRoot="")
Constructor.
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false, const bool isRoute=false, const bool isExternal=false, const bool catchExceptions=true)
Runs the given handler on the given file; returns if everything's ok.