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.org/sumo
3 // Copyright (C) 2001-2022 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  default:
111  break;
112  }
113  } catch (InvalidArgument& e) {
114  WRITE_ERROR(e.what());
115  }
116 }
117 
118 
119 void
121  // obtain tag
122  const SumoXMLTag tag = static_cast<SumoXMLTag>(element);
123  // get last inserted object
125  // close SUMOBaseOBject
127  // check tag
128  switch (tag) {
129  // only interval
130  case SUMO_TAG_INTERVAL:
131  // parse object and all their childrens
132  parseSumoBaseObject(obj);
133  // delete object (and all of their childrens)
134  delete obj;
135  break;
136  default:
137  break;
138  }
139 }
140 
141 
142 void
144  // declare Ok Flag
145  bool parsedOk = true;
146  // needed attributes
147  const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "", parsedOk);
148  const double begin = attrs.get<double>(SUMO_ATTR_BEGIN, "", parsedOk);
149  const double end = attrs.get<double>(SUMO_ATTR_END, "", parsedOk);
150  // continue if flag is ok
151  if (parsedOk) {
152  // set tag
154  // add all attributes
158  }
159 }
160 
161 
162 void
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  // obtain all attributes
169  const std::vector<std::string> attributes = attrs.getAttributeNames();
170  // iterate over attributes and fill parameters map
171  for (const auto& attribute : attributes) {
172  if (attribute != toString(SUMO_ATTR_ID)) {
174  }
175  }
176  // continue if flag is ok
177  if (parsedOk) {
178  // set tag
180  // add all attributes
182  }
183 }
184 
185 
186 void
188  // declare Ok Flag
189  bool parsedOk = true;
190  // needed attributes
191  const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, "", parsedOk);
192  const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, "", parsedOk);
193  // obtain all attributes
194  const std::vector<std::string> attributes = attrs.getAttributeNames();
195  // iterate over attributes and fill parameters map
196  for (const auto& attribute : attributes) {
197  if ((attribute != toString(SUMO_ATTR_FROM)) && (attribute != toString(SUMO_ATTR_TO))) {
199  }
200  }
201  // continue if flag is ok
202  if (parsedOk) {
203  // set tag
205  // add all attributes
208  }
209 }
210 
211 
212 void
214  // declare Ok Flag
215  bool parsedOk = true;
216  // needed attributes
217  const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, "", parsedOk);
218  const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, "", parsedOk);
219  // obtain all attributes
220  const std::vector<std::string> attributes = attrs.getAttributeNames();
221  // iterate over attributes and fill parameters map
222  for (const auto& attribute : attributes) {
223  if ((attribute != toString(SUMO_ATTR_FROM)) && (attribute != toString(SUMO_ATTR_TO))) {
225  }
226  }
227  // continue if flag is ok
228  if (parsedOk) {
229  // set tag
231  // add all attributes
234  }
235 }
236 
237 
238 void
239 DataHandler::checkParent(const SumoXMLTag currentTag, const SumoXMLTag parentTag, bool& ok) const {
240  // check that parent SUMOBaseObject's tag is the parentTag
243  WRITE_ERROR(toString(currentTag) + " must be defined within the definition of a " + toString(parentTag));
244  ok = false;
245  }
246 }
247 
248 /****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:288
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_TAZREL
a relation between two TAZs
@ SUMO_TAG_EDGE
begin/end of the description of an edge
@ 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
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)
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
void parseSumoBaseObject(CommonXMLStructure::SumoBaseObject *obj)
parse SumoBaseObject (it's called recursivelly)
Definition: DataHandler.cpp:49
bool parse()
parse
Definition: DataHandler.cpp:42
virtual void buildEdgeData(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &edgeID, const std::map< std::string, std::string > &parameters)=0
Builds edgeData.
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) const
check parents
virtual void buildTAZRelationData(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &fromTAZID, const std::string &toTAZID, const std::map< std::string, std::string > &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
CommonXMLStructure myCommonXMLStructure
common XML Structure
Definition: DataHandler.h:95
void parseInterval(const SUMOSAXAttributes &attrs)
virtual void buildEdgeRelationData(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &fromEdgeID, const std::string &toEdgeID, const std::map< std::string, std::string > &parameters)=0
Builds edgeRelationData.
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)
Runs the given handler on the given file; returns if everything's ok.
Definition: XMLSubSys.cpp:149