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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // An XML-handler for network weights
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <string>
30 #include <utils/common/ToString.h>
33 #include "SAXWeightsHandler.h"
34 
35 
36 // ===========================================================================
37 // method definitions
38 // ===========================================================================
39 // ---------------------------------------------------------------------------
40 // SAXWeightsHandler::ToRetrieveDefinition methods
41 // ---------------------------------------------------------------------------
43  bool edgeBased, EdgeFloatTimeLineRetriever& destination)
44  : myAttributeName(attributeName), myAmEdgeBased(edgeBased), myDestination(destination) {
45 }
46 
47 
49 }
50 
51 
52 // ---------------------------------------------------------------------------
53 // SAXWeightsHandler methods
54 // ---------------------------------------------------------------------------
55 SAXWeightsHandler::SAXWeightsHandler(const std::vector<ToRetrieveDefinition*>& defs,
56  const std::string& file)
57  : SUMOSAXHandler(file), myDefinitions(defs),
59 
60 
62  const std::string& file)
63  : SUMOSAXHandler(file),
65  myDefinitions.push_back(def);
66 }
67 
68 
70  std::vector<ToRetrieveDefinition*>::iterator i;
71  for (i = myDefinitions.begin(); i != myDefinitions.end(); ++i) {
72  delete *i;
73  }
74 }
75 
76 
78  const SUMOSAXAttributes& attrs) {
79  switch (element) {
80  case SUMO_TAG_INTERVAL: {
81  bool ok = true;
84  }
85  break;
86  case SUMO_TAG_EDGE: {
87  bool ok = true;
88  myCurrentEdgeID = attrs.getOpt<std::string>(SUMO_ATTR_ID, nullptr, ok, "");
89  tryParse(attrs, true);
90  }
91  break;
92  case SUMO_TAG_LANE: {
93  tryParse(attrs, false);
94  }
95  break;
96  default:
97  break;
98  }
99 }
100 
101 
102 void
103 SAXWeightsHandler::tryParse(const SUMOSAXAttributes& attrs, bool isEdge) {
104  // !!!! no error handling!
105  std::vector<ToRetrieveDefinition*>::iterator i;
106  if (isEdge) {
107  // process all that want values directly from the edge
108  for (i = myDefinitions.begin(); i != myDefinitions.end(); ++i) {
109  if ((*i)->myAmEdgeBased) {
110  if (attrs.hasAttribute((*i)->myAttributeName)) {
111  (*i)->myAggValue = attrs.getFloat((*i)->myAttributeName);
112  (*i)->myNoLanes = 1;
113  (*i)->myHadAttribute = true;
114  } else {
115  (*i)->myHadAttribute = false;
116  }
117  } else {
118  (*i)->myAggValue = 0;
119  (*i)->myNoLanes = 0;
120  }
121  }
122  } else {
123  // process the current lane values
124  for (i = myDefinitions.begin(); i != myDefinitions.end(); ++i) {
125  if (!(*i)->myAmEdgeBased) {
126  try {
127  (*i)->myAggValue += attrs.getFloat((*i)->myAttributeName);
128  ++((*i)->myNoLanes);
129  (*i)->myHadAttribute = true;
130  } catch (EmptyData&) {
131  WRITE_ERROR("Missing value '" + (*i)->myAttributeName + "' in edge '" + myCurrentEdgeID + "'.");
132  } catch (NumberFormatException&) {
133  WRITE_ERROR("The value should be numeric, but is not.\n In edge '" + myCurrentEdgeID + "' at time step " + toString(myCurrentTimeBeg) + ".");
134  }
135  }
136  }
137  }
138 }
139 
140 
141 void
143  if (element == SUMO_TAG_EDGE) {
144  std::vector<ToRetrieveDefinition*>::iterator i;
145  for (i = myDefinitions.begin(); i != myDefinitions.end(); ++i) {
146  if ((*i)->myHadAttribute) {
147  (*i)->myDestination.addEdgeWeight(myCurrentEdgeID,
148  (*i)->myAggValue / (double)(*i)->myNoLanes,
150  }
151  }
152  }
153 }
154 
155 
156 
157 /****************************************************************************/
158 
Interface for a class which obtains read weights for named edges.
begin/end of the description of a single lane
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
double myCurrentTimeBeg
the begin of the time period that is currently processed
weights: time range begin
SAX-handler base for SUMO-files.
~SAXWeightsHandler()
Destructor.
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.
std::string myCurrentEdgeID
the edge which is currently being processed
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
void tryParse(const SUMOSAXAttributes &attrs, bool isEdge)
Parses the efforts of a lane for the previously read times.
Encapsulated SAX-Attributes.
double myCurrentTimeEnd
the end of the time period that is currently processed
SAXWeightsHandler(const std::vector< ToRetrieveDefinition *> &defs, const std::string &file)
Constructor.
void myEndElement(int elemente)
Called when a closing tag occurs.
#define STEPS2TIME(x)
Definition: SUMOTime.h:57
std::vector< ToRetrieveDefinition * > myDefinitions
List of definitions what shall be read and whereto stored while parsing the file. ...
begin/end of the description of an edge
Complete definition about what shall be retrieved and where to store it.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
virtual double getFloat(int id) const =0
Returns the double-value of the named (by its enum-value) attribute.
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.
weights: time range end
ToRetrieveDefinition(const std::string &attributeName, bool edgeBased, EdgeFloatTimeLineRetriever &destination)
Constructor.
an aggreagated-output interval