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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // An XML-handler for network weights
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <string>
36 #include <utils/common/ToString.h>
39 #include "SAXWeightsHandler.h"
40 
41 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
45 // ---------------------------------------------------------------------------
46 // SAXWeightsHandler::ToRetrieveDefinition methods
47 // ---------------------------------------------------------------------------
49  bool edgeBased, EdgeFloatTimeLineRetriever& destination)
50  : myAttributeName(attributeName), myAmEdgeBased(edgeBased), myDestination(destination) {
51 }
52 
53 
55 }
56 
57 
58 // ---------------------------------------------------------------------------
59 // SAXWeightsHandler methods
60 // ---------------------------------------------------------------------------
61 SAXWeightsHandler::SAXWeightsHandler(const std::vector<ToRetrieveDefinition*>& defs,
62  const std::string& file)
63  : SUMOSAXHandler(file), myDefinitions(defs),
65 
66 
68  const std::string& file)
69  : SUMOSAXHandler(file),
71  myDefinitions.push_back(def);
72 }
73 
74 
76  std::vector<ToRetrieveDefinition*>::iterator i;
77  for (i = myDefinitions.begin(); i != myDefinitions.end(); ++i) {
78  delete *i;
79  }
80 }
81 
82 
84  const SUMOSAXAttributes& attrs) {
85  switch (element) {
86  case SUMO_TAG_INTERVAL: {
87  bool ok = true;
88  myCurrentTimeBeg = attrs.get<double>(SUMO_ATTR_BEGIN, 0, ok);
89  myCurrentTimeEnd = attrs.get<double>(SUMO_ATTR_END, 0, ok);
90  }
91  break;
92  case SUMO_TAG_EDGE: {
93  bool ok = true;
94  myCurrentEdgeID = attrs.getOpt<std::string>(SUMO_ATTR_ID, 0, ok, "");
95  tryParse(attrs, true);
96  }
97  break;
98  case SUMO_TAG_LANE: {
99  tryParse(attrs, false);
100  }
101  break;
102  default:
103  break;
104  }
105 }
106 
107 
108 void
109 SAXWeightsHandler::tryParse(const SUMOSAXAttributes& attrs, bool isEdge) {
110  // !!!! no error handling!
111  std::vector<ToRetrieveDefinition*>::iterator i;
112  if (isEdge) {
113  // process all that want values directly from the edge
114  for (i = myDefinitions.begin(); i != myDefinitions.end(); ++i) {
115  if ((*i)->myAmEdgeBased) {
116  if (attrs.hasAttribute((*i)->myAttributeName)) {
117  (*i)->myAggValue = attrs.getFloat((*i)->myAttributeName);
118  (*i)->myNoLanes = 1;
119  (*i)->myHadAttribute = true;
120  } else {
121  (*i)->myHadAttribute = false;
122  }
123  } else {
124  (*i)->myAggValue = 0;
125  (*i)->myNoLanes = 0;
126  }
127  }
128  } else {
129  // process the current lane values
130  for (i = myDefinitions.begin(); i != myDefinitions.end(); ++i) {
131  if (!(*i)->myAmEdgeBased) {
132  try {
133  (*i)->myAggValue += attrs.getFloat((*i)->myAttributeName);
134  ++((*i)->myNoLanes);
135  (*i)->myHadAttribute = true;
136  } catch (EmptyData&) {
137  WRITE_ERROR("Missing value '" + (*i)->myAttributeName + "' in edge '" + myCurrentEdgeID + "'.");
138  } catch (NumberFormatException&) {
139  WRITE_ERROR("The value should be numeric, but is not.\n In edge '" + myCurrentEdgeID + "' at time step " + toString(myCurrentTimeBeg) + ".");
140  }
141  }
142  }
143  }
144 }
145 
146 
147 void
149  if (element == SUMO_TAG_EDGE) {
150  std::vector<ToRetrieveDefinition*>::iterator i;
151  for (i = myDefinitions.begin(); i != myDefinitions.end(); ++i) {
152  if ((*i)->myHadAttribute) {
153  (*i)->myDestination.addEdgeWeight(myCurrentEdgeID,
154  (*i)->myAggValue / (double)(*i)->myNoLanes,
156  }
157  }
158  }
159 }
160 
161 
162 
163 /****************************************************************************/
164 
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...
std::string myCurrentEdgeID
the edge which is currently being processed
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
void tryParse(const SUMOSAXAttributes &attrs, bool isEdge)
Parses the efforts of a lane for the previously read times.
Encapsulated SAX-Attributes.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
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.
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:205
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