SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-sim.org/
12 // Copyright (C) 2007-2014 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 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
50 // ---------------------------------------------------------------------------
51 // SAXWeightsHandler::ToRetrieveDefinition methods
52 // ---------------------------------------------------------------------------
54  bool edgeBased, EdgeFloatTimeLineRetriever& destination)
55  : myAttributeName(attributeName), myAmEdgeBased(edgeBased), myDestination(destination) {
56 }
57 
58 
60 }
61 
62 
63 // ---------------------------------------------------------------------------
64 // SAXWeightsHandler methods
65 // ---------------------------------------------------------------------------
66 SAXWeightsHandler::SAXWeightsHandler(const std::vector<ToRetrieveDefinition*>& defs,
67  const std::string& file)
68  : SUMOSAXHandler(file), myDefinitions(defs),
70 
71 
73  const std::string& file)
74  : SUMOSAXHandler(file),
75  myCurrentTimeBeg(-1), myCurrentTimeEnd(-1) {
76  myDefinitions.push_back(def);
77 }
78 
79 
81  std::vector<ToRetrieveDefinition*>::iterator i;
82  for (i = myDefinitions.begin(); i != myDefinitions.end(); ++i) {
83  delete *i;
84  }
85 }
86 
87 
89  const SUMOSAXAttributes& attrs) {
90  switch (element) {
91  case SUMO_TAG_INTERVAL: {
92  bool ok = true;
94  myCurrentTimeEnd = attrs.get<SUMOReal>(SUMO_ATTR_END, 0, ok);
95  }
96  break;
97  case SUMO_TAG_EDGE: {
98  bool ok = true;
99  myCurrentEdgeID = attrs.getOpt<std::string>(SUMO_ATTR_ID, 0, ok, "");
100  tryParse(attrs, true);
101  }
102  break;
103  case SUMO_TAG_LANE: {
104  tryParse(attrs, false);
105  }
106  break;
107  default:
108  break;
109  }
110 }
111 
112 
113 void
114 SAXWeightsHandler::tryParse(const SUMOSAXAttributes& attrs, bool isEdge) {
115  // !!!! no error handling!
116  std::vector<ToRetrieveDefinition*>::iterator i;
117  if (isEdge) {
118  // process all that want values directly from the edge
119  for (i = myDefinitions.begin(); i != myDefinitions.end(); ++i) {
120  if ((*i)->myAmEdgeBased) {
121  if (attrs.hasAttribute((*i)->myAttributeName)) {
122  (*i)->myAggValue = attrs.getFloat((*i)->myAttributeName);
123  (*i)->myNoLanes = 1;
124  (*i)->myHadAttribute = true;
125  } else {
126  (*i)->myHadAttribute = false;
127  }
128  } else {
129  (*i)->myAggValue = 0;
130  (*i)->myNoLanes = 0;
131  }
132  }
133  } else {
134  // process the current lane values
135  for (i = myDefinitions.begin(); i != myDefinitions.end(); ++i) {
136  if (!(*i)->myAmEdgeBased) {
137  try {
138  (*i)->myAggValue += attrs.getFloat((*i)->myAttributeName);
139  ++((*i)->myNoLanes);
140  (*i)->myHadAttribute = true;
141  } catch (EmptyData&) {
142  WRITE_ERROR("Missing value '" + (*i)->myAttributeName + "' in edge '" + myCurrentEdgeID + "'.");
143  } catch (NumberFormatException&) {
144  WRITE_ERROR("The value should be numeric, but is not.\n In edge '" + myCurrentEdgeID + "' at time step " + toString(myCurrentTimeBeg) + ".");
145  }
146  }
147  }
148  }
149 }
150 
151 
152 void
154  if (element == SUMO_TAG_EDGE) {
155  std::vector<ToRetrieveDefinition*>::iterator i;
156  for (i = myDefinitions.begin(); i != myDefinitions.end(); ++i) {
157  if ((*i)->myHadAttribute) {
158  (*i)->myDestination.addEdgeWeight(myCurrentEdgeID,
159  (*i)->myAggValue / (SUMOReal)(*i)->myNoLanes,
161  }
162  }
163  }
164 }
165 
166 
167 
168 /****************************************************************************/
169