Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/xml/SAXWeightsHandler.cpp
193717 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2007-2026 German Aerospace Center (DLR) and others.
4
// This program and the accompanying materials are made available under the
5
// terms of the Eclipse Public License 2.0 which is available at
6
// https://www.eclipse.org/legal/epl-2.0/
7
// This Source Code may also be made available under the following Secondary
8
// Licenses when the conditions for such availability set forth in the Eclipse
9
// Public License 2.0 are satisfied: GNU General Public License, version 2
10
// or later which is available at
11
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
/****************************************************************************/
14
/// @file SAXWeightsHandler.cpp
15
/// @author Daniel Krajzewicz
16
/// @author Jakob Erdmann
17
/// @author Michael Behrisch
18
/// @date Fri, 30 Mar 2007
19
///
20
// An XML-handler for network weights
21
/****************************************************************************/
22
#include <config.h>
23
24
#include <utils/common/MsgHandler.h>
25
26
#include "SAXWeightsHandler.h"
27
28
29
// ===========================================================================
30
// method definitions
31
// ===========================================================================
32
33
// ---------------------------------------------------------------------------
34
// SAXWeightsHandler::ToRetrieveDefinition methods
35
// ---------------------------------------------------------------------------
36
37
SAXWeightsHandler::ToRetrieveDefinition::ToRetrieveDefinition(const std::string& attributeName,
38
bool edgeBased, EdgeFloatTimeLineRetriever& destination) :
39
myAttributeName(attributeName),
40
myAmEdgeBased(edgeBased),
41
myDestination(destination),
42
myAggValue(0),
43
myNoLanes(0),
44
myHadAttribute(0),
45
myHadNonNumeric(false)
46
{ }
47
48
49
SAXWeightsHandler::ToRetrieveDefinition::~ToRetrieveDefinition() {
50
}
51
52
// ---------------------------------------------------------------------------
53
// SAXWeightsHandler methods
54
// ---------------------------------------------------------------------------
55
56
SAXWeightsHandler::SAXWeightsHandler(const std::vector<ToRetrieveDefinition*>& defs, const std::string& file) :
57
SUMOSAXHandler(file),
58
myDefinitions(defs),
59
myCurrentTimeBeg(-1),
60
myCurrentTimeEnd(-1) {
61
}
62
63
64
SAXWeightsHandler::SAXWeightsHandler(ToRetrieveDefinition* def, const std::string& file) :
65
SUMOSAXHandler(file),
66
myDefinitions({def}),
67
myCurrentTimeBeg(-1),
68
myCurrentTimeEnd(-1) {
69
}
70
71
72
SAXWeightsHandler::~SAXWeightsHandler() {
73
for (const auto& definition : myDefinitions) {
74
delete definition;
75
}
76
}
77
78
79
void
80
SAXWeightsHandler::myStartElement(int element, const SUMOSAXAttributes& attrs) {
81
switch (element) {
82
case SUMO_TAG_INTERVAL: {
83
bool ok = true;
84
myCurrentID = attrs.getOpt<std::string>(SUMO_ATTR_ID, nullptr, ok, "");
85
myCurrentTimeBeg = STEPS2TIME(attrs.getSUMOTimeReporting(SUMO_ATTR_BEGIN, nullptr, ok));
86
myCurrentTimeEnd = STEPS2TIME(attrs.getSUMOTimeReporting(SUMO_ATTR_END, nullptr, ok));
87
if (myCurrentTimeEnd < myCurrentTimeBeg) {
88
WRITE_ERROR("Interval end time " + toString(myCurrentTimeEnd) + " is lower than interval begin time " + toString(myCurrentTimeBeg));
89
myCurrentTimeEnd = myCurrentTimeBeg;
90
}
91
}
92
break;
93
case SUMO_TAG_EDGE: {
94
bool ok = true;
95
myCurrentEdgeID = attrs.getOpt<std::string>(SUMO_ATTR_ID, nullptr, ok, "");
96
tryParse(attrs, true);
97
}
98
break;
99
case SUMO_TAG_EDGEREL: {
100
tryParseEdgeRel(attrs);
101
}
102
break;
103
case SUMO_TAG_TAZREL: {
104
tryParseTazRel(attrs);
105
}
106
break;
107
case SUMO_TAG_LANE: {
108
tryParse(attrs, false);
109
}
110
break;
111
default:
112
break;
113
}
114
}
115
116
117
void
118
SAXWeightsHandler::tryParse(const SUMOSAXAttributes& attrs, bool isEdge) {
119
if (isEdge) {
120
// process all that want values directly from the edge
121
for (const auto& definition : myDefinitions) {
122
if (definition->myAmEdgeBased) {
123
if (attrs.hasAttribute(definition->myAttributeName)) {
124
try {
125
definition->myAggValue = attrs.getFloat(definition->myAttributeName);
126
definition->myNoLanes = 1;
127
definition->myHadAttribute = true;
128
} catch (EmptyData&) {
129
WRITE_ERRORF(TL("Missing value '%' in edge '%'."), definition->myAttributeName, myCurrentEdgeID);
130
} catch (NumberFormatException&) {
131
if (!definition->myHadNonNumeric) {
132
// warn only once
133
definition->myHadNonNumeric = true;
134
WRITE_ERRORF(TL("The value '%' of attribute '%' should be numeric in edge '%' at time step %."),
135
attrs.getStringSecure(definition->myAttributeName, ""), definition->myAttributeName,
136
myCurrentEdgeID, time2string(TIME2STEPS(myCurrentTimeBeg)));
137
}
138
}
139
} else {
140
definition->myHadAttribute = false;
141
}
142
} else {
143
definition->myAggValue = 0;
144
definition->myNoLanes = 0;
145
}
146
}
147
} else {
148
// process the current lane values
149
for (const auto& definition : myDefinitions) {
150
if (!definition->myAmEdgeBased) {
151
try {
152
definition->myAggValue += attrs.getFloat(definition->myAttributeName);
153
definition->myNoLanes++;
154
definition->myHadAttribute = true;
155
} catch (EmptyData&) {
156
WRITE_ERRORF(TL("Missing value '%' in edge '%'."), definition->myAttributeName, myCurrentEdgeID);
157
} catch (NumberFormatException&) {
158
if (!definition->myHadNonNumeric) {
159
// warn only once
160
definition->myHadNonNumeric = true;
161
WRITE_ERRORF(TL("The value '%' of attribute '%' should be numeric in edge '%' at time step %."),
162
attrs.getStringSecure(definition->myAttributeName, ""), definition->myAttributeName,
163
myCurrentEdgeID, time2string(TIME2STEPS(myCurrentTimeBeg)));
164
}
165
}
166
}
167
}
168
}
169
}
170
171
172
void
173
SAXWeightsHandler::tryParseEdgeRel(const SUMOSAXAttributes& attrs) {
174
if (attrs.hasAttribute(SUMO_ATTR_FROM) && attrs.hasAttribute(SUMO_ATTR_TO)) {
175
bool ok = true;
176
const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, nullptr, ok);
177
const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, nullptr, ok);
178
for (ToRetrieveDefinition* ret : myDefinitions) {
179
if (attrs.hasAttribute(ret->myAttributeName)) {
180
ret->myDestination.addEdgeRelWeight(from, to,
181
attrs.getFloat(ret->myAttributeName),
182
myCurrentTimeBeg, myCurrentTimeEnd);
183
}
184
}
185
}
186
}
187
188
void
189
SAXWeightsHandler::tryParseTazRel(const SUMOSAXAttributes& attrs) {
190
if (attrs.hasAttribute(SUMO_ATTR_FROM) && attrs.hasAttribute(SUMO_ATTR_TO)) {
191
bool ok = true;
192
const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, nullptr, ok);
193
const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, nullptr, ok);
194
for (ToRetrieveDefinition* ret : myDefinitions) {
195
if (attrs.hasAttribute(ret->myAttributeName)) {
196
ret->myDestination.addTazRelWeight(myCurrentID, from, to,
197
attrs.getFloat(ret->myAttributeName),
198
myCurrentTimeBeg, myCurrentTimeEnd);
199
}
200
}
201
}
202
}
203
204
205
void
206
SAXWeightsHandler::myEndElement(int element) {
207
if (element == SUMO_TAG_EDGE) {
208
for (const auto& definition : myDefinitions) {
209
if (definition->myHadAttribute) {
210
definition->myDestination.addEdgeWeight(myCurrentEdgeID,
211
definition->myAggValue / (double)definition->myNoLanes,
212
myCurrentTimeBeg, myCurrentTimeEnd);
213
}
214
}
215
}
216
}
217
218
219
/****************************************************************************/
220
221