SUMO - Simulation of Urban MObility
SAXWeightsHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // An XML-handler for network weights
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2007-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <string>
37 #include <utils/common/ToString.h>
40 #include "SAXWeightsHandler.h"
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
46 // ---------------------------------------------------------------------------
47 // SAXWeightsHandler::ToRetrieveDefinition methods
48 // ---------------------------------------------------------------------------
50  bool edgeBased, EdgeFloatTimeLineRetriever& destination)
51  : myAttributeName(attributeName), myAmEdgeBased(edgeBased), myDestination(destination) {
52 }
53 
54 
56 }
57 
58 
59 // ---------------------------------------------------------------------------
60 // SAXWeightsHandler methods
61 // ---------------------------------------------------------------------------
62 SAXWeightsHandler::SAXWeightsHandler(const std::vector<ToRetrieveDefinition*>& defs,
63  const std::string& file)
64  : SUMOSAXHandler(file), myDefinitions(defs),
66 
67 
69  const std::string& file)
70  : SUMOSAXHandler(file),
72  myDefinitions.push_back(def);
73 }
74 
75 
77  std::vector<ToRetrieveDefinition*>::iterator i;
78  for (i = myDefinitions.begin(); i != myDefinitions.end(); ++i) {
79  delete *i;
80  }
81 }
82 
83 
85  const SUMOSAXAttributes& attrs) {
86  switch (element) {
87  case SUMO_TAG_INTERVAL: {
88  bool ok = true;
89  myCurrentTimeBeg = attrs.get<double>(SUMO_ATTR_BEGIN, 0, ok);
90  myCurrentTimeEnd = attrs.get<double>(SUMO_ATTR_END, 0, ok);
91  }
92  break;
93  case SUMO_TAG_EDGE: {
94  bool ok = true;
95  myCurrentEdgeID = attrs.getOpt<std::string>(SUMO_ATTR_ID, 0, ok, "");
96  tryParse(attrs, true);
97  }
98  break;
99  case SUMO_TAG_LANE: {
100  tryParse(attrs, false);
101  }
102  break;
103  default:
104  break;
105  }
106 }
107 
108 
109 void
110 SAXWeightsHandler::tryParse(const SUMOSAXAttributes& attrs, bool isEdge) {
111  // !!!! no error handling!
112  std::vector<ToRetrieveDefinition*>::iterator i;
113  if (isEdge) {
114  // process all that want values directly from the edge
115  for (i = myDefinitions.begin(); i != myDefinitions.end(); ++i) {
116  if ((*i)->myAmEdgeBased) {
117  if (attrs.hasAttribute((*i)->myAttributeName)) {
118  (*i)->myAggValue = attrs.getFloat((*i)->myAttributeName);
119  (*i)->myNoLanes = 1;
120  (*i)->myHadAttribute = true;
121  } else {
122  (*i)->myHadAttribute = false;
123  }
124  } else {
125  (*i)->myAggValue = 0;
126  (*i)->myNoLanes = 0;
127  }
128  }
129  } else {
130  // process the current lane values
131  for (i = myDefinitions.begin(); i != myDefinitions.end(); ++i) {
132  if (!(*i)->myAmEdgeBased) {
133  try {
134  (*i)->myAggValue += attrs.getFloat((*i)->myAttributeName);
135  ++((*i)->myNoLanes);
136  (*i)->myHadAttribute = true;
137  } catch (EmptyData&) {
138  WRITE_ERROR("Missing value '" + (*i)->myAttributeName + "' in edge '" + myCurrentEdgeID + "'.");
139  } catch (NumberFormatException&) {
140  WRITE_ERROR("The value should be numeric, but is not.\n In edge '" + myCurrentEdgeID + "' at time step " + toString(myCurrentTimeBeg) + ".");
141  }
142  }
143  }
144  }
145 }
146 
147 
148 void
150  if (element == SUMO_TAG_EDGE) {
151  std::vector<ToRetrieveDefinition*>::iterator i;
152  for (i = myDefinitions.begin(); i != myDefinitions.end(); ++i) {
153  if ((*i)->myHadAttribute) {
154  (*i)->myDestination.addEdgeWeight(myCurrentEdgeID,
155  (*i)->myAggValue / (double)(*i)->myNoLanes,
157  }
158  }
159  }
160 }
161 
162 
163 
164 /****************************************************************************/
165 
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:56
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:206
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