Eclipse SUMO - Simulation of Urban MObility
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 
33 DataHandler::DataHandler(const std::string& file) :
34  SUMOSAXHandler(file) {
35 }
36 
37 
39 
40 
41 bool
43  // run parser and return result
44  return XMLSubSys::runParser(*this, getFileName());
45 }
46 
47 
48 void
50  // switch tag
51  switch (obj->getTag()) {
52  // Stopping Places
53  case SUMO_TAG_INTERVAL:
58  break;
59  case SUMO_TAG_EDGE:
60  buildEdgeData(obj,
62  obj->getParameters());
63  break;
64  case SUMO_TAG_EDGEREL:
68  obj->getParameters());
69  break;
70  case SUMO_TAG_TAZREL:
74  obj->getParameters());
75  break;
76  default:
77  break;
78  }
79  // now iterate over childrens
80  for (const auto& child : obj->getSumoBaseObjectChildren()) {
81  // call this function recursively
82  parseSumoBaseObject(child);
83  }
84 }
85 
86 
87 void
88 DataHandler::myStartElement(int element, const SUMOSAXAttributes& attrs) {
89  // obtain tag
90  const SumoXMLTag tag = (element == 0) ? SUMO_TAG_ROOTFILE : static_cast<SumoXMLTag>(element);
91  // open SUMOBaseOBject
93  // check tag
94  try {
95  switch (tag) {
96  // interval
97  case SUMO_TAG_INTERVAL:
98  parseInterval(attrs);
99  break;
100  // datas
101  case SUMO_TAG_EDGE:
102  parseEdgeData(attrs);
103  break;
104  case SUMO_TAG_EDGEREL:
105  parseEdgeRelationData(attrs);
106  break;
107  case SUMO_TAG_TAZREL:
108  parseTAZRelationData(attrs);
109  break;
110  case SUMO_TAG_PARAM:
111  WRITE_WARNING(TL("Data elements cannot load attributes as params"));
112  break;
113  default:
114  break;
115  }
116  } catch (InvalidArgument& e) {
117  writeError(e.what());
118  }
119 }
120 
121 
122 void
124  // obtain tag
125  const SumoXMLTag tag = static_cast<SumoXMLTag>(element);
126  // get last inserted object
128  // close SUMOBaseOBject
130  // check tag
131  switch (tag) {
132  // only interval
133  case SUMO_TAG_INTERVAL:
134  // parse object and all their childrens
135  parseSumoBaseObject(obj);
136  // delete object (and all of their childrens)
137  delete obj;
138  break;
139  default:
140  break;
141  }
142 }
143 
144 
145 bool
147  return myErrorCreatingElement;
148 }
149 
150 
151 void
152 DataHandler::writeError(const std::string& error) {
154  myErrorCreatingElement = true;
155 }
156 
157 
158 void
160  // declare Ok Flag
161  bool parsedOk = true;
162  // needed attributes
163  const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "", parsedOk);
164  const double begin = attrs.get<double>(SUMO_ATTR_BEGIN, "", parsedOk);
165  const double end = attrs.get<double>(SUMO_ATTR_END, "", parsedOk);
166  // continue if flag is ok
167  if (parsedOk) {
168  // set tag
170  // add all attributes
174  }
175 }
176 
177 
178 void
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  }
193 }
194 
195 
196 void
198  // declare Ok Flag
199  bool parsedOk = true;
200  // needed attributes
201  const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, "", parsedOk);
202  const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, "", parsedOk);
203  // fill attributes
205  // continue if flag is ok
206  if (parsedOk) {
207  // set tag
209  // add all attributes
212  }
213 }
214 
215 
216 void
218  // declare Ok Flag
219  bool parsedOk = true;
220  // needed attributes
221  const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, "", parsedOk);
222  const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, "", parsedOk);
223  // fill attributes
225  // continue if flag is ok
226  if (parsedOk) {
227  // set tag
229  // add all attributes
232  }
233 }
234 
235 
236 void
237 DataHandler::getAttributes(const SUMOSAXAttributes& attrs, const std::vector<SumoXMLAttr> avoidAttributes) const {
238  // transform avoidAttributes to strings
239  std::vector<std::string> avoidAttributesStr;
240  for (const SumoXMLAttr& avoidAttribute : avoidAttributes) {
241  avoidAttributesStr.push_back(toString(avoidAttribute));
242  }
243  // iterate over attributes and fill parameters map
244  for (const std::string& attribute : attrs.getAttributeNames()) {
245  if (std::find(avoidAttributesStr.begin(), avoidAttributesStr.end(), attribute) == avoidAttributesStr.end()) {
247  }
248  }
249 }
250 
251 
252 void
253 DataHandler::checkParent(const SumoXMLTag currentTag, const SumoXMLTag parentTag, bool& ok) {
254  // check that parent SUMOBaseObject's tag is the parentTag
257  writeError(toString(currentTag) + " must be defined within the definition of a " + toString(parentTag));
258  ok = false;
259  }
260 }
261 
262 /****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:304
#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_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
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 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
CommonXMLStructure::SumoBaseObject * getCurrentSumoBaseObject() const
get current editedSumoBaseObject
void openSUMOBaseOBject()
open SUMOBaseOBject
void closeSUMOBaseOBject()
close myTag
DataHandler(const std::string &file)
Constructor.
Definition: DataHandler.cpp:33
void parseTAZRelationData(const SUMOSAXAttributes &attrs)
parse TAZRelationData attributes
~DataHandler()
Destructor.
Definition: DataHandler.cpp:38
bool myErrorCreatingElement
flag for check if a element wasn't created
Definition: DataHandler.h:105
void parseSumoBaseObject(CommonXMLStructure::SumoBaseObject *obj)
parse SumoBaseObject (it's called recursivelly)
Definition: DataHandler.cpp:49
bool parse()
parse
Definition: DataHandler.cpp:42
void writeError(const std::string &error)
write error and enable error creating element
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
virtual void buildTAZRelationData(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &fromTAZID, const std::string &toTAZID, const Parameterised::Map &parameters)=0
Builds TAZRelationData.
void parseEdgeData(const SUMOSAXAttributes &attrs)
parse edgeData attributes
virtual void buildDataInterval(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &dataSetID, const double begin, const double end)=0
Builds DataInterval.
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
Definition: DataHandler.cpp:88
virtual void 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
CommonXMLStructure myCommonXMLStructure
common XML Structure
Definition: DataHandler.h:102
void parseInterval(const SUMOSAXAttributes &attrs)
virtual void buildEdgeRelationData(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &fromEdgeID, const std::string &toEdgeID, const Parameterised::Map &parameters)=0
Builds edgeRelationData.
bool isErrorCreatingElement() const
get flag for check if a element wasn't created
void error(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Handler for XML-errors.
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.
Definition: XMLSubSys.cpp:148