Eclipse SUMO - Simulation of Urban MObility
SAXWeightsHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2007-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 /****************************************************************************/
20 // An XML-handler for network weights
21 /****************************************************************************/
22 #include <config.h>
23 
25 
26 #include "SAXWeightsHandler.h"
27 
28 
29 // ===========================================================================
30 // method definitions
31 // ===========================================================================
32 
33 // ---------------------------------------------------------------------------
34 // SAXWeightsHandler::ToRetrieveDefinition methods
35 // ---------------------------------------------------------------------------
36 
38  bool edgeBased, EdgeFloatTimeLineRetriever& destination) :
39  myAttributeName(attributeName),
40  myAmEdgeBased(edgeBased),
41  myDestination(destination),
42  myAggValue(0),
43  myNoLanes(0),
44  myHadAttribute(0) {
45 }
46 
47 
49 }
50 
51 // ---------------------------------------------------------------------------
52 // SAXWeightsHandler methods
53 // ---------------------------------------------------------------------------
54 
55 SAXWeightsHandler::SAXWeightsHandler(const std::vector<ToRetrieveDefinition*>& defs, const std::string& file) :
56  SUMOSAXHandler(file),
57  myDefinitions(defs),
58  myCurrentTimeBeg(-1),
59  myCurrentTimeEnd(-1) {
60 }
61 
62 
64  SUMOSAXHandler(file),
65  myDefinitions({def}),
66  myCurrentTimeBeg(-1),
67 myCurrentTimeEnd(-1) {
68 }
69 
70 
72  for (const auto& definition : myDefinitions) {
73  delete definition;
74  }
75 }
76 
77 
78 void
80  switch (element) {
81  case SUMO_TAG_INTERVAL: {
82  bool ok = true;
83  myCurrentID = attrs.getOpt<std::string>(SUMO_ATTR_ID, nullptr, ok, "");
87  WRITE_ERROR("Interval end time " + toString(myCurrentTimeEnd) + " is lower than interval begin time " + toString(myCurrentTimeBeg));
89  }
90  }
91  break;
92  case SUMO_TAG_EDGE: {
93  bool ok = true;
94  myCurrentEdgeID = attrs.getOpt<std::string>(SUMO_ATTR_ID, nullptr, ok, "");
95  tryParse(attrs, true);
96  }
97  break;
98  case SUMO_TAG_EDGEREL: {
99  tryParseEdgeRel(attrs);
100  }
101  break;
102  case SUMO_TAG_TAZREL: {
103  tryParseTazRel(attrs);
104  }
105  break;
106  break;
107  case SUMO_TAG_LANE: {
108  tryParse(attrs, false);
109  }
110  break;
111  default:
112  break;
113  }
114 }
115 
116 
117 void
118 SAXWeightsHandler::tryParse(const SUMOSAXAttributes& attrs, bool isEdge) {
119  // !!!! no error handling!
120  if (isEdge) {
121  // process all that want values directly from the edge
122  for (const auto& definition : myDefinitions) {
123  if (definition->myAmEdgeBased) {
124  if (attrs.hasAttribute(definition->myAttributeName)) {
125  definition->myAggValue = attrs.getFloat(definition->myAttributeName);
126  definition->myNoLanes = 1;
127  definition->myHadAttribute = true;
128  } else {
129  definition->myHadAttribute = false;
130  }
131  } else {
132  definition->myAggValue = 0;
133  definition->myNoLanes = 0;
134  }
135  }
136  } else {
137  // process the current lane values
138  for (const auto& definition : myDefinitions) {
139  if (!definition->myAmEdgeBased) {
140  try {
141  definition->myAggValue += attrs.getFloat(definition->myAttributeName);
142  definition->myNoLanes++;
143  definition->myHadAttribute = true;
144  } catch (EmptyData&) {
145  WRITE_ERRORF(TL("Missing value '%' in edge '%'."), definition->myAttributeName, myCurrentEdgeID);
146  } catch (NumberFormatException&) {
147  WRITE_ERROR("The value should be numeric, but is not.\n In edge '" + myCurrentEdgeID +
148  "' at time step " + toString(myCurrentTimeBeg) + ".");
149  }
150  }
151  }
152  }
153 }
154 
155 
156 void
158  if (attrs.hasAttribute(SUMO_ATTR_FROM) && attrs.hasAttribute(SUMO_ATTR_TO)) {
159  bool ok = true;
160  const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, nullptr, ok);
161  const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, nullptr, ok);
162  for (ToRetrieveDefinition* ret : myDefinitions) {
163  if (attrs.hasAttribute(ret->myAttributeName)) {
164  ret->myDestination.addEdgeRelWeight(from, to,
165  attrs.getFloat(ret->myAttributeName),
167  }
168  }
169  }
170 }
171 
172 void
174  if (attrs.hasAttribute(SUMO_ATTR_FROM) && attrs.hasAttribute(SUMO_ATTR_TO)) {
175  bool ok = true;
176  const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, nullptr, ok);
177  const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, nullptr, ok);
178  for (ToRetrieveDefinition* ret : myDefinitions) {
179  if (attrs.hasAttribute(ret->myAttributeName)) {
180  ret->myDestination.addTazRelWeight(myCurrentID, from, to,
181  attrs.getFloat(ret->myAttributeName),
183  }
184  }
185  }
186 }
187 
188 
189 void
191  if (element == SUMO_TAG_EDGE) {
192  for (const auto& definition : myDefinitions) {
193  if (definition->myHadAttribute) {
194  definition->myDestination.addEdgeWeight(myCurrentEdgeID,
195  definition->myAggValue / (double)definition->myNoLanes,
197  }
198  }
199  }
200 }
201 
202 
203 /****************************************************************************/
#define WRITE_ERRORF(...)
Definition: MsgHandler.h:305
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:304
#define TL(string)
Definition: MsgHandler.h:315
#define STEPS2TIME(x)
Definition: SUMOTime.h:55
@ SUMO_TAG_INTERVAL
an aggreagated-output interval
@ SUMO_TAG_EDGEREL
a relation between two edges
@ SUMO_TAG_LANE
begin/end of the description of a single lane
@ 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
Interface for a class which obtains read weights for named edges.
Complete definition about what shall be retrieved and where to store it.
ToRetrieveDefinition(const std::string &attributeName, bool edgeBased, EdgeFloatTimeLineRetriever &destination)
Constructor.
double myCurrentTimeEnd
the end of the time period that is currently processed
double myCurrentTimeBeg
the begin of the time period that is currently processed
void myEndElement(int elemente)
Called when a closing tag occurs.
void tryParseTazRel(const SUMOSAXAttributes &attrs)
Parses the data of an tazRelation for the previously read times.
~SAXWeightsHandler()
Destructor.
void tryParseEdgeRel(const SUMOSAXAttributes &attrs)
Parses the data of an edgeRelation for the previously read times.
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
void tryParse(const SUMOSAXAttributes &attrs, bool isEdge)
Parses the data of an edge or lane for the previously read times.
SAXWeightsHandler(const std::vector< ToRetrieveDefinition * > &defs, const std::string &file)
Constructor.
std::string myCurrentEdgeID
the edge which is currently being processed
std::string myCurrentID
the id of the interval being parsed
std::vector< ToRetrieveDefinition * > myDefinitions
List of definitions what shall be read and whereto stored while parsing the file.
Encapsulated SAX-Attributes.
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue=T(), bool report=true) const
Tries to read given attribute assuming it is an int.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
SUMOTime getSUMOTimeReporting(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
double getFloat(int id) const
Returns the double-value of the named (by its enum-value) attribute.
SAX-handler base for SUMO-files.