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-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 // check if loading was aborted
52 if (!myAbortLoading) {
53 // switch tag
54 switch (obj->getTag()) {
55 // Stopping Places
57 if (buildDataInterval(obj,
61 obj->markAsCreated();
62 }
63 break;
64 case SUMO_TAG_EDGE:
65 if (buildEdgeData(obj,
67 obj->getParameters())) {
68 obj->markAsCreated();
69 }
70 break;
75 obj->getParameters())) {
76 obj->markAsCreated();
77 }
78 break;
79 case SUMO_TAG_TAZREL:
83 obj->getParameters())) {
84 obj->markAsCreated();
85 }
86 break;
87 default:
88 break;
89 }
90 // now iterate over childrens
91 for (const auto& child : obj->getSumoBaseObjectChildren()) {
92 // call this function recursively
94 }
95 }
96}
97
98
99void
101 // obtain tag
102 const SumoXMLTag tag = (element == 0) ? SUMO_TAG_ROOTFILE : static_cast<SumoXMLTag>(element);
103 // open SUMOBaseOBject
105 // check tag
106 try {
107 switch (tag) {
108 // interval
110 parseInterval(attrs);
111 break;
112 // datas
113 case SUMO_TAG_EDGE:
114 parseEdgeData(attrs);
115 break;
116 case SUMO_TAG_EDGEREL:
118 break;
119 case SUMO_TAG_TAZREL:
121 break;
122 case SUMO_TAG_PARAM:
123 WRITE_WARNING(TL("Data elements cannot load attributes as params"));
125 break;
126 default:
127 // tag cannot be parsed in routeHandler
129 break;
130 }
131 } catch (InvalidArgument& e) {
132 writeError(e.what());
133 }
134}
135
136
137void
139 // obtain tag
140 const SumoXMLTag tag = static_cast<SumoXMLTag>(element);
141 // get last inserted object
143 // close SUMOBaseOBject
145 if (obj) {
146 // check tag
147 switch (tag) {
148 // only interval
150 // parse object and all their childrens
152 // delete object (and all of their childrens)
153 delete obj;
154 break;
155 default:
156 break;
157 }
158 }
159}
160
161
162void
164 // declare Ok Flag
165 bool parsedOk = true;
166 // needed attributes
167 const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "", parsedOk);
168 const double begin = attrs.get<double>(SUMO_ATTR_BEGIN, "", parsedOk);
169 const double end = attrs.get<double>(SUMO_ATTR_END, "", parsedOk);
170 // continue if flag is ok
171 if (parsedOk) {
172 // set tag
174 // add all attributes
178 } else {
180 }
181}
182
183
184void
186 // declare Ok Flag
187 bool parsedOk = true;
188 // needed attributes
189 const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "", parsedOk);
190 // fill attributes
191 getAttributes(attrs, {SUMO_ATTR_ID});
192 // continue if flag is ok
193 if (parsedOk) {
194 // set tag
196 // add all attributes
198 } else {
200 }
201}
202
203
204void
206 // declare Ok Flag
207 bool parsedOk = true;
208 // needed attributes
209 const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, "", parsedOk);
210 const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, "", parsedOk);
211 // fill attributes
213 // continue if flag is ok
214 if (parsedOk) {
215 // set tag
217 // add all attributes
220 } else {
222 }
223}
224
225
226void
228 // declare Ok Flag
229 bool parsedOk = true;
230 // needed attributes
231 const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, "", parsedOk);
232 const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, "", parsedOk);
233 // fill attributes
235 // continue if flag is ok
236 if (parsedOk) {
237 // set tag
239 // add all attributes
242 } else {
244 }
245}
246
247
248void
249DataHandler::getAttributes(const SUMOSAXAttributes& attrs, const std::vector<SumoXMLAttr> avoidAttributes) const {
250 // transform avoidAttributes to strings
251 std::vector<std::string> avoidAttributesStr;
252 for (const SumoXMLAttr& avoidAttribute : avoidAttributes) {
253 avoidAttributesStr.push_back(toString(avoidAttribute));
254 }
255 // iterate over attributes and fill parameters map
256 for (const std::string& attribute : attrs.getAttributeNames()) {
257 if (std::find(avoidAttributesStr.begin(), avoidAttributesStr.end(), attribute) == avoidAttributesStr.end()) {
259 }
260 }
261}
262
263
264void
265DataHandler::checkParent(const SumoXMLTag currentTag, const SumoXMLTag parentTag, bool& ok) {
266 // check that parent SUMOBaseObject's tag is the parentTag
269 writeError(toString(currentTag) + " must be defined within the definition of a " + toString(parentTag));
270 ok = false;
271 }
272}
273
274/****************************************************************************/
#define WRITE_WARNING(msg)
Definition MsgHandler.h:286
#define TL(string)
Definition MsgHandler.h:304
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
bool myAbortLoading
abort loading
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.