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.org/sumo
3 // Copyright (C) 2007-2020 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;
85  }
86  break;
87  case SUMO_TAG_EDGE: {
88  bool ok = true;
89  myCurrentEdgeID = attrs.getOpt<std::string>(SUMO_ATTR_ID, nullptr, ok, "");
90  tryParse(attrs, true);
91  }
92  break;
93  case SUMO_TAG_EDGEREL: {
94  tryParseEdgeRel(attrs);
95  }
96  break;
97  case SUMO_TAG_LANE: {
98  tryParse(attrs, false);
99  }
100  break;
101  default:
102  break;
103  }
104 }
105 
106 
107 void
108 SAXWeightsHandler::tryParse(const SUMOSAXAttributes& attrs, bool isEdge) {
109  // !!!! no error handling!
110  if (isEdge) {
111  // process all that want values directly from the edge
112  for (const auto& definition : myDefinitions) {
113  if (definition->myAmEdgeBased) {
114  if (attrs.hasAttribute(definition->myAttributeName)) {
115  definition->myAggValue = attrs.getFloat(definition->myAttributeName);
116  definition->myNoLanes = 1;
117  definition->myHadAttribute = true;
118  } else {
119  definition->myHadAttribute = false;
120  }
121  } else {
122  definition->myAggValue = 0;
123  definition->myNoLanes = 0;
124  }
125  }
126  } else {
127  // process the current lane values
128  for (const auto& definition : myDefinitions) {
129  if (!definition->myAmEdgeBased) {
130  try {
131  definition->myAggValue += attrs.getFloat(definition->myAttributeName);
132  definition->myNoLanes++;
133  definition->myHadAttribute = true;
134  } catch (EmptyData&) {
135  WRITE_ERROR("Missing value '" + definition->myAttributeName + "' in edge '" + myCurrentEdgeID + "'.");
136  } catch (NumberFormatException&) {
137  WRITE_ERROR("The value should be numeric, but is not.\n In edge '" + myCurrentEdgeID +
138  "' at time step " + toString(myCurrentTimeBeg) + ".");
139  }
140  }
141  }
142  }
143 }
144 
145 
146 void
148  if (attrs.hasAttribute(SUMO_ATTR_FROM) && attrs.hasAttribute(SUMO_ATTR_TO)) {
149  bool ok = true;
150  const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, nullptr, ok);
151  const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, nullptr, ok);
152  for (ToRetrieveDefinition* ret : myDefinitions) {
153  if (attrs.hasAttribute(ret->myAttributeName)) {
154  ret->myDestination.addEdgeRelWeight(from, to,
155  attrs.getFloat(ret->myAttributeName),
157  }
158  }
159  }
160 }
161 
162 
163 void
165  if (element == SUMO_TAG_EDGE) {
166  for (const auto& definition : myDefinitions) {
167  if (definition->myHadAttribute) {
168  definition->myDestination.addEdgeWeight(myCurrentEdgeID,
169  definition->myAggValue / (double)definition->myNoLanes,
171  }
172  }
173  }
174 }
175 
176 
177 /****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:284
#define STEPS2TIME(x)
Definition: SUMOTime.h:53
@ 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_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:44
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.
~SAXWeightsHandler()
Destructor.
void tryParseEdgeRel(const SUMOSAXAttributes &attrs)
Parses the data of an edgeRel 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::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, bool report=true) const
Tries to read given attribute assuming it is an int.
virtual double getFloat(int id) const =0
Returns the double-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.
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.
SAX-handler base for SUMO-files.