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-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/****************************************************************************/
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& file) :
34 SUMOSAXHandler(file) {
35}
36
37
39
40
41bool
43 // run parser and return result
44 return XMLSubSys::runParser(*this, getFileName());
45}
46
47
48void
50 // switch tag
51 switch (obj->getTag()) {
52 // Stopping Places
54 if (buildDataInterval(obj,
58 obj->markAsCreated();
59 }
60 break;
61 case SUMO_TAG_EDGE:
62 if (buildEdgeData(obj,
64 obj->getParameters())) {
65 obj->markAsCreated();
66 }
67 break;
72 obj->getParameters())) {
73 obj->markAsCreated();
74 }
75 break;
76 case SUMO_TAG_TAZREL:
80 obj->getParameters())) {
81 obj->markAsCreated();
82 }
83 break;
84 default:
85 break;
86 }
87 // now iterate over childrens
88 for (const auto& child : obj->getSumoBaseObjectChildren()) {
89 // call this function recursively
91 }
92}
93
94
95void
97 // obtain tag
98 const SumoXMLTag tag = (element == 0) ? SUMO_TAG_ROOTFILE : static_cast<SumoXMLTag>(element);
99 // open SUMOBaseOBject
101 // check tag
102 try {
103 switch (tag) {
104 // interval
106 parseInterval(attrs);
107 break;
108 // datas
109 case SUMO_TAG_EDGE:
110 parseEdgeData(attrs);
111 break;
112 case SUMO_TAG_EDGEREL:
114 break;
115 case SUMO_TAG_TAZREL:
117 break;
118 case SUMO_TAG_PARAM:
119 WRITE_WARNING(TL("Data elements cannot load attributes as params"));
121 break;
122 default:
123 // tag cannot be parsed in routeHandler
125 break;
126 }
127 } catch (InvalidArgument& e) {
128 writeError(e.what());
129 }
130}
131
132
133void
135 // obtain tag
136 const SumoXMLTag tag = static_cast<SumoXMLTag>(element);
137 // get last inserted object
139 // close SUMOBaseOBject
141 // check tag
142 switch (tag) {
143 // only interval
145 // parse object and all their childrens
147 // delete object (and all of their childrens)
148 delete obj;
149 break;
150 default:
151 break;
152 }
153}
154
155
156void
158 // declare Ok Flag
159 bool parsedOk = true;
160 // needed attributes
161 const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "", parsedOk);
162 const double begin = attrs.get<double>(SUMO_ATTR_BEGIN, "", parsedOk);
163 const double end = attrs.get<double>(SUMO_ATTR_END, "", parsedOk);
164 // continue if flag is ok
165 if (parsedOk) {
166 // set tag
168 // add all attributes
172 } else {
174 }
175}
176
177
178void
180 // declare Ok Flag
181 bool parsedOk = true;
182 // needed attributes
183 const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "", parsedOk);
184 // fill attributes
185 getAttributes(attrs, {SUMO_ATTR_ID});
186 // continue if flag is ok
187 if (parsedOk) {
188 // set tag
190 // add all attributes
192 } else {
194 }
195}
196
197
198void
200 // declare Ok Flag
201 bool parsedOk = true;
202 // needed attributes
203 const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, "", parsedOk);
204 const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, "", parsedOk);
205 // fill attributes
207 // continue if flag is ok
208 if (parsedOk) {
209 // set tag
211 // add all attributes
214 } else {
216 }
217}
218
219
220void
222 // declare Ok Flag
223 bool parsedOk = true;
224 // needed attributes
225 const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, "", parsedOk);
226 const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, "", parsedOk);
227 // fill attributes
229 // continue if flag is ok
230 if (parsedOk) {
231 // set tag
233 // add all attributes
236 } else {
238 }
239}
240
241
242void
243DataHandler::getAttributes(const SUMOSAXAttributes& attrs, const std::vector<SumoXMLAttr> avoidAttributes) const {
244 // transform avoidAttributes to strings
245 std::vector<std::string> avoidAttributesStr;
246 for (const SumoXMLAttr& avoidAttribute : avoidAttributes) {
247 avoidAttributesStr.push_back(toString(avoidAttribute));
248 }
249 // iterate over attributes and fill parameters map
250 for (const std::string& attribute : attrs.getAttributeNames()) {
251 if (std::find(avoidAttributesStr.begin(), avoidAttributesStr.end(), attribute) == avoidAttributesStr.end()) {
253 }
254 }
255}
256
257
258void
259DataHandler::checkParent(const SumoXMLTag currentTag, const SumoXMLTag parentTag, bool& ok) {
260 // check that parent SUMOBaseObject's tag is the parentTag
263 writeError(toString(currentTag) + " must be defined within the definition of a " + toString(parentTag));
264 ok = false;
265 }
266}
267
268/****************************************************************************/
#define WRITE_WARNING(msg)
Definition MsgHandler.h:295
#define TL(string)
Definition MsgHandler.h:315
SumoXMLTag
Numbers representing SUMO-XML - element names.
@ SUMO_TAG_INTERVAL
an aggreagated-output interval
@ SUMO_TAG_EDGEREL
a relation between two edges
@ SUMO_TAG_NOTHING
invalid tag, must be the last one
@ SUMO_TAG_ROOTFILE
root file
@ SUMO_TAG_PARAM
parameter associated to a certain key
@ 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
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.
DataHandler(const std::string &file)
Constructor.
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
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.