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),
64  myCurrentTimeBeg(-1), myCurrentTimeEnd(-1) {
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 
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:77
SUMOSAXHandler
SAX-handler base for SUMO-files.
Definition: SUMOSAXHandler.h:42
SAXWeightsHandler::ToRetrieveDefinition::~ToRetrieveDefinition
~ToRetrieveDefinition()
Destructor.
Definition: SAXWeightsHandler.cpp:48
OptionsCont.h
MsgHandler.h
SUMOSAXHandler.h
SUMO_TAG_LANE
begin/end of the description of a single lane
Definition: SUMOXMLDefinitions.h:50
EmptyData
Definition: UtilExceptions.h:69
SAXWeightsHandler.h
SUMO_ATTR_ID
Definition: SUMOXMLDefinitions.h:379
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:64
SAXWeightsHandler::~SAXWeightsHandler
~SAXWeightsHandler()
Destructor.
Definition: SAXWeightsHandler.cpp:69
SUMO_ATTR_BEGIN
weights: time range begin
Definition: SUMOXMLDefinitions.h:675
SAXWeightsHandler::myCurrentEdgeID
std::string myCurrentEdgeID
the edge which is currently being processed
Definition: SAXWeightsHandler.h:202
NumberFormatException
Definition: UtilExceptions.h:96
SAXWeightsHandler::ToRetrieveDefinition
Complete definition about what shall be retrieved and where to store it.
Definition: SAXWeightsHandler.h:101
SAXWeightsHandler::tryParse
void tryParse(const SUMOSAXAttributes &attrs, bool isEdge)
Parses the efforts of a lane for the previously read times.
Definition: SAXWeightsHandler.cpp:103
STEPS2TIME
#define STEPS2TIME(x)
Definition: SUMOTime.h:57
SAXWeightsHandler::myCurrentTimeEnd
double myCurrentTimeEnd
the end of the time period that is currently processed
Definition: SAXWeightsHandler.h:199
SAXWeightsHandler::ToRetrieveDefinition::ToRetrieveDefinition
ToRetrieveDefinition(const std::string &attributeName, bool edgeBased, EdgeFloatTimeLineRetriever &destination)
Constructor.
Definition: SAXWeightsHandler.cpp:42
SUMO_TAG_EDGE
begin/end of the description of an edge
Definition: SUMOXMLDefinitions.h:48
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:519
SAXWeightsHandler::SAXWeightsHandler
SAXWeightsHandler(const std::vector< ToRetrieveDefinition * > &defs, const std::string &file)
Constructor.
Definition: SAXWeightsHandler.cpp:55
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
SAXWeightsHandler::myDefinitions
std::vector< ToRetrieveDefinition * > myDefinitions
List of definitions what shall be read and whereto stored while parsing the file.
Definition: SAXWeightsHandler.h:193
SAXWeightsHandler::myEndElement
void myEndElement(int elemente)
Called when a closing tag occurs.
Definition: SAXWeightsHandler.cpp:142
config.h
SUMO_ATTR_END
weights: time range end
Definition: SUMOXMLDefinitions.h:677
SAXWeightsHandler::myCurrentTimeBeg
double myCurrentTimeBeg
the begin of the time period that is currently processed
Definition: SAXWeightsHandler.h:196
SUMO_TAG_INTERVAL
an aggreagated-output interval
Definition: SUMOXMLDefinitions.h:160
SUMOSAXAttributes
Encapsulated SAX-Attributes.
Definition: SUMOSAXAttributes.h:57
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
SAXWeightsHandler::EdgeFloatTimeLineRetriever
Interface for a class which obtains read weights for named edges.
Definition: SAXWeightsHandler.h:75
SUMOXMLDefinitions.h