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 /****************************************************************************/
16 // An XML-handler for network weights
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <string>
29 #include <utils/common/ToString.h>
32 #include "SAXWeightsHandler.h"
33 
34 
35 // ===========================================================================
36 // method definitions
37 // ===========================================================================
38 // ---------------------------------------------------------------------------
39 // SAXWeightsHandler::ToRetrieveDefinition methods
40 // ---------------------------------------------------------------------------
42  bool edgeBased, EdgeFloatTimeLineRetriever& destination)
43  : myAttributeName(attributeName), myAmEdgeBased(edgeBased), myDestination(destination) {
44 }
45 
46 
48 }
49 
50 
51 // ---------------------------------------------------------------------------
52 // SAXWeightsHandler methods
53 // ---------------------------------------------------------------------------
54 SAXWeightsHandler::SAXWeightsHandler(const std::vector<ToRetrieveDefinition*>& defs,
55  const std::string& file)
56  : SUMOSAXHandler(file), myDefinitions(defs),
58 
59 
61  const std::string& file)
62  : SUMOSAXHandler(file),
63  myCurrentTimeBeg(-1), myCurrentTimeEnd(-1) {
64  myDefinitions.push_back(def);
65 }
66 
67 
69  std::vector<ToRetrieveDefinition*>::iterator i;
70  for (i = myDefinitions.begin(); i != myDefinitions.end(); ++i) {
71  delete *i;
72  }
73 }
74 
75 
77  const SUMOSAXAttributes& attrs) {
78  switch (element) {
79  case SUMO_TAG_INTERVAL: {
80  bool ok = true;
83  }
84  break;
85  case SUMO_TAG_EDGE: {
86  bool ok = true;
87  myCurrentEdgeID = attrs.getOpt<std::string>(SUMO_ATTR_ID, nullptr, ok, "");
88  tryParse(attrs, true);
89  }
90  break;
91  case SUMO_TAG_LANE: {
92  tryParse(attrs, false);
93  }
94  break;
95  default:
96  break;
97  }
98 }
99 
100 
101 void
102 SAXWeightsHandler::tryParse(const SUMOSAXAttributes& attrs, bool isEdge) {
103  // !!!! no error handling!
104  std::vector<ToRetrieveDefinition*>::iterator i;
105  if (isEdge) {
106  // process all that want values directly from the edge
107  for (i = myDefinitions.begin(); i != myDefinitions.end(); ++i) {
108  if ((*i)->myAmEdgeBased) {
109  if (attrs.hasAttribute((*i)->myAttributeName)) {
110  (*i)->myAggValue = attrs.getFloat((*i)->myAttributeName);
111  (*i)->myNoLanes = 1;
112  (*i)->myHadAttribute = true;
113  } else {
114  (*i)->myHadAttribute = false;
115  }
116  } else {
117  (*i)->myAggValue = 0;
118  (*i)->myNoLanes = 0;
119  }
120  }
121  } else {
122  // process the current lane values
123  for (i = myDefinitions.begin(); i != myDefinitions.end(); ++i) {
124  if (!(*i)->myAmEdgeBased) {
125  try {
126  (*i)->myAggValue += attrs.getFloat((*i)->myAttributeName);
127  ++((*i)->myNoLanes);
128  (*i)->myHadAttribute = true;
129  } catch (EmptyData&) {
130  WRITE_ERROR("Missing value '" + (*i)->myAttributeName + "' in edge '" + myCurrentEdgeID + "'.");
131  } catch (NumberFormatException&) {
132  WRITE_ERROR("The value should be numeric, but is not.\n In edge '" + myCurrentEdgeID + "' at time step " + toString(myCurrentTimeBeg) + ".");
133  }
134  }
135  }
136  }
137 }
138 
139 
140 void
142  if (element == SUMO_TAG_EDGE) {
143  std::vector<ToRetrieveDefinition*>::iterator i;
144  for (i = myDefinitions.begin(); i != myDefinitions.end(); ++i) {
145  if ((*i)->myHadAttribute) {
146  (*i)->myDestination.addEdgeWeight(myCurrentEdgeID,
147  (*i)->myAggValue / (double)(*i)->myNoLanes,
149  }
150  }
151  }
152 }
153 
154 
155 
156 /****************************************************************************/
157 
ToString.h
SUMOSAXAttributes::hasAttribute
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
SAXWeightsHandler::myStartElement
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
Definition: SAXWeightsHandler.cpp:76
SUMOSAXHandler
SAX-handler base for SUMO-files.
Definition: SUMOSAXHandler.h:41
SAXWeightsHandler::ToRetrieveDefinition::~ToRetrieveDefinition
~ToRetrieveDefinition()
Destructor.
Definition: SAXWeightsHandler.cpp:47
OptionsCont.h
MsgHandler.h
SUMOSAXHandler.h
SUMO_TAG_LANE
begin/end of the description of a single lane
Definition: SUMOXMLDefinitions.h:49
EmptyData
Definition: UtilExceptions.h:68
SAXWeightsHandler.h
SUMO_ATTR_ID
Definition: SUMOXMLDefinitions.h:378
SUMOSAXAttributes::getFloat
virtual double getFloat(int id) const =0
Returns the double-value of the named (by its enum-value) attribute.
SUMOSAXAttributes::getSUMOTimeReporting
SUMOTime getSUMOTimeReporting(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
Definition: SUMOSAXAttributes.cpp:63
SAXWeightsHandler::~SAXWeightsHandler
~SAXWeightsHandler()
Destructor.
Definition: SAXWeightsHandler.cpp:68
SUMO_ATTR_BEGIN
weights: time range begin
Definition: SUMOXMLDefinitions.h:678
SAXWeightsHandler::myCurrentEdgeID
std::string myCurrentEdgeID
the edge which is currently being processed
Definition: SAXWeightsHandler.h:201
NumberFormatException
Definition: UtilExceptions.h:95
SAXWeightsHandler::ToRetrieveDefinition
Complete definition about what shall be retrieved and where to store it.
Definition: SAXWeightsHandler.h:100
SAXWeightsHandler::tryParse
void tryParse(const SUMOSAXAttributes &attrs, bool isEdge)
Parses the efforts of a lane for the previously read times.
Definition: SAXWeightsHandler.cpp:102
STEPS2TIME
#define STEPS2TIME(x)
Definition: SUMOTime.h:56
SAXWeightsHandler::myCurrentTimeEnd
double myCurrentTimeEnd
the end of the time period that is currently processed
Definition: SAXWeightsHandler.h:198
SAXWeightsHandler::ToRetrieveDefinition::ToRetrieveDefinition
ToRetrieveDefinition(const std::string &attributeName, bool edgeBased, EdgeFloatTimeLineRetriever &destination)
Constructor.
Definition: SAXWeightsHandler.cpp:41
SUMO_TAG_EDGE
begin/end of the description of an edge
Definition: SUMOXMLDefinitions.h:47
UtilExceptions.h
SUMOSAXAttributes::getOpt
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.
Definition: SUMOSAXAttributes.h:518
SAXWeightsHandler::SAXWeightsHandler
SAXWeightsHandler(const std::vector< ToRetrieveDefinition * > &defs, const std::string &file)
Constructor.
Definition: SAXWeightsHandler.cpp:54
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
SAXWeightsHandler::myDefinitions
std::vector< ToRetrieveDefinition * > myDefinitions
List of definitions what shall be read and whereto stored while parsing the file.
Definition: SAXWeightsHandler.h:192
SAXWeightsHandler::myEndElement
void myEndElement(int elemente)
Called when a closing tag occurs.
Definition: SAXWeightsHandler.cpp:141
config.h
SUMO_ATTR_END
weights: time range end
Definition: SUMOXMLDefinitions.h:680
SAXWeightsHandler::myCurrentTimeBeg
double myCurrentTimeBeg
the begin of the time period that is currently processed
Definition: SAXWeightsHandler.h:195
SUMO_TAG_INTERVAL
an aggreagated-output interval
Definition: SUMOXMLDefinitions.h:159
SUMOSAXAttributes
Encapsulated SAX-Attributes.
Definition: SUMOSAXAttributes.h:56
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:283
SAXWeightsHandler::EdgeFloatTimeLineRetriever
Interface for a class which obtains read weights for named edges.
Definition: SAXWeightsHandler.h:74
SUMOXMLDefinitions.h