Eclipse SUMO - Simulation of Urban MObility
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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-2025 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
24#include <utils/xml/XMLSubSys.h>
25
26#include "DataHandler.h"
27
28
29// ===========================================================================
30// method definitions
31// ===========================================================================
32
33DataHandler::DataHandler(const std::string& filename) :
34 CommonHandler(filename),
35 SUMOSAXHandler(filename) {
36}
37
38
40
41
42bool
44 // run parser and return result
45 return XMLSubSys::runParser(*this, getFileName());
46}
47
48
49void
51 // switch tag
52 switch (obj->getTag()) {
53 // Stopping Places
55 if (buildDataInterval(obj,
59 obj->markAsCreated();
60 }
61 break;
62 case SUMO_TAG_EDGE:
63 if (buildEdgeData(obj,
65 obj->getParameters())) {
66 obj->markAsCreated();
67 }
68 break;
73 obj->getParameters())) {
74 obj->markAsCreated();
75 }
76 break;
77 case SUMO_TAG_TAZREL:
81 obj->getParameters())) {
82 obj->markAsCreated();
83 }
84 break;
85 default:
86 break;
87 }
88 // now iterate over childrens
89 for (const auto& child : obj->getSumoBaseObjectChildren()) {
90 // call this function recursively
92 }
93}
94
95
96void
98 // obtain tag
99 const SumoXMLTag tag = (element == 0) ? SUMO_TAG_ROOTFILE : static_cast<SumoXMLTag>(element);
100 // open SUMOBaseOBject
102 // check tag
103 try {
104 switch (tag) {
105 // interval
107 parseInterval(attrs);
108 break;
109 // datas
110 case SUMO_TAG_EDGE:
111 parseEdgeData(attrs);
112 break;
113 case SUMO_TAG_EDGEREL:
115 break;
116 case SUMO_TAG_TAZREL:
118 break;
119 case SUMO_TAG_PARAM:
120 WRITE_WARNING(TL("Data elements cannot load attributes as params"));
122 break;
123 default:
124 // tag cannot be parsed in routeHandler
126 break;
127 }
128 } catch (InvalidArgument& e) {
129 writeError(e.what());
130 }
131}
132
133
134void
136 // obtain tag
137 const SumoXMLTag tag = static_cast<SumoXMLTag>(element);
138 // get last inserted object
140 // close SUMOBaseOBject
142 if (obj) {
143 // check tag
144 switch (tag) {
145 // only interval
147 // parse object and all their childrens
149 // delete object (and all of their childrens)
150 delete obj;
151 break;
152 default:
153 break;
154 }
155 }
156}
157
158
159void
161 // declare Ok Flag
162 bool parsedOk = true;
163 // needed attributes
164 const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "", parsedOk);
165 const double begin = attrs.get<double>(SUMO_ATTR_BEGIN, "", parsedOk);
166 const double end = attrs.get<double>(SUMO_ATTR_END, "", parsedOk);
167 // continue if flag is ok
168 if (parsedOk) {
169 // set tag
171 // add all attributes
175 } else {
177 }
178}
179
180
181void
183 // declare Ok Flag
184 bool parsedOk = true;
185 // needed attributes
186 const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "", parsedOk);
187 // fill attributes
188 getAttributes(attrs, {SUMO_ATTR_ID});
189 // continue if flag is ok
190 if (parsedOk) {
191 // set tag
193 // add all attributes
195 } else {
197 }
198}
199
200
201void
203 // declare Ok Flag
204 bool parsedOk = true;
205 // needed attributes
206 const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, "", parsedOk);
207 const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, "", parsedOk);
208 // fill attributes
210 // continue if flag is ok
211 if (parsedOk) {
212 // set tag
214 // add all attributes
217 } else {
219 }
220}
221
222
223void
225 // declare Ok Flag
226 bool parsedOk = true;
227 // needed attributes
228 const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, "", parsedOk);
229 const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, "", parsedOk);
230 // fill attributes
232 // continue if flag is ok
233 if (parsedOk) {
234 // set tag
236 // add all attributes
239 } else {
241 }
242}
243
244
245void
246DataHandler::getAttributes(const SUMOSAXAttributes& attrs, const std::vector<SumoXMLAttr> avoidAttributes) const {
247 // transform avoidAttributes to strings
248 std::vector<std::string> avoidAttributesStr;
249 for (const SumoXMLAttr& avoidAttribute : avoidAttributes) {
250 avoidAttributesStr.push_back(toString(avoidAttribute));
251 }
252 // iterate over attributes and fill parameters map
253 for (const std::string& attribute : attrs.getAttributeNames()) {
254 if (std::find(avoidAttributesStr.begin(), avoidAttributesStr.end(), attribute) == avoidAttributesStr.end()) {
256 }
257 }
258}
259
260
261void
262DataHandler::checkParent(const SumoXMLTag currentTag, const SumoXMLTag parentTag, bool& ok) {
263 // check that parent SUMOBaseObject's tag is the parentTag
266 writeError(toString(currentTag) + " must be defined within the definition of a " + toString(parentTag));
267 ok = false;
268 }
269}
270
271/****************************************************************************/
#define WRITE_WARNING(msg)
Definition MsgHandler.h:287
#define TL(string)
Definition MsgHandler.h:305
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:46
The XML-Handler for network loading.
bool writeError(const std::string &error)
write error and enable error creating element
CommonXMLStructure myCommonXMLStructure
common XML Structure
void setTag(const SumoXMLTag tag)
set SumoBaseObject tag
SumoBaseObject * getParentSumoBaseObject() const
get pointer to mySumoBaseObjectParent SumoBaseObject (if is null, then is the root)
const std::map< std::string, std::string > & getParameters() const
get parameters
SumoXMLTag getTag() const
get XML myTag
void addParameter(const std::string &key, const std::string &value)
add parameter into current SumoBaseObject node
void markAsCreated()
mark as successfully created
void addDoubleAttribute(const SumoXMLAttr attr, const double value)
add double attribute into current SumoBaseObject node
void addStringAttribute(const SumoXMLAttr attr, const std::string &value)
add string attribute into current SumoBaseObject node
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
void abortSUMOBaseOBject()
abort SUMOBaseOBject
CommonXMLStructure::SumoBaseObject * getCurrentSumoBaseObject() const
get current editedSumoBaseObject
void openSUMOBaseOBject()
open SUMOBaseOBject
void closeSUMOBaseOBject()
close SUMOBaseOBject
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.
SAX-handler base for SUMO-files.
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.