Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2007-2024 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 423 : SAXWeightsHandler::ToRetrieveDefinition::ToRetrieveDefinition(const std::string& attributeName,
38 423 : bool edgeBased, EdgeFloatTimeLineRetriever& destination) :
39 423 : myAttributeName(attributeName),
40 423 : myAmEdgeBased(edgeBased),
41 423 : myDestination(destination),
42 423 : myAggValue(0),
43 423 : myNoLanes(0),
44 423 : myHadAttribute(0) {
45 423 : }
46 :
47 :
48 423 : SAXWeightsHandler::ToRetrieveDefinition::~ToRetrieveDefinition() {
49 423 : }
50 :
51 : // ---------------------------------------------------------------------------
52 : // SAXWeightsHandler methods
53 : // ---------------------------------------------------------------------------
54 :
55 413 : SAXWeightsHandler::SAXWeightsHandler(const std::vector<ToRetrieveDefinition*>& defs, const std::string& file) :
56 : SUMOSAXHandler(file),
57 413 : myDefinitions(defs),
58 413 : myCurrentTimeBeg(-1),
59 826 : myCurrentTimeEnd(-1) {
60 413 : }
61 :
62 :
63 0 : SAXWeightsHandler::SAXWeightsHandler(ToRetrieveDefinition* def, const std::string& file) :
64 : SUMOSAXHandler(file),
65 0 : myDefinitions({def}),
66 0 : myCurrentTimeBeg(-1),
67 0 : myCurrentTimeEnd(-1) {
68 0 : }
69 :
70 :
71 413 : SAXWeightsHandler::~SAXWeightsHandler() {
72 836 : for (const auto& definition : myDefinitions) {
73 423 : delete definition;
74 : }
75 413 : }
76 :
77 :
78 : void
79 6347 : SAXWeightsHandler::myStartElement(int element, const SUMOSAXAttributes& attrs) {
80 6347 : switch (element) {
81 656 : case SUMO_TAG_INTERVAL: {
82 656 : bool ok = true;
83 656 : myCurrentID = attrs.getOpt<std::string>(SUMO_ATTR_ID, nullptr, ok, "");
84 656 : myCurrentTimeBeg = STEPS2TIME(attrs.getSUMOTimeReporting(SUMO_ATTR_BEGIN, nullptr, ok));
85 656 : myCurrentTimeEnd = STEPS2TIME(attrs.getSUMOTimeReporting(SUMO_ATTR_END, nullptr, ok));
86 656 : if (myCurrentTimeEnd < myCurrentTimeBeg) {
87 0 : WRITE_ERROR("Interval end time " + toString(myCurrentTimeEnd) + " is lower than interval begin time " + toString(myCurrentTimeBeg));
88 0 : myCurrentTimeEnd = myCurrentTimeBeg;
89 : }
90 : }
91 656 : break;
92 5243 : case SUMO_TAG_EDGE: {
93 5243 : bool ok = true;
94 5243 : myCurrentEdgeID = attrs.getOpt<std::string>(SUMO_ATTR_ID, nullptr, ok, "");
95 5243 : tryParse(attrs, true);
96 : }
97 5243 : break;
98 0 : case SUMO_TAG_EDGEREL: {
99 0 : tryParseEdgeRel(attrs);
100 : }
101 0 : break;
102 7 : case SUMO_TAG_TAZREL: {
103 7 : tryParseTazRel(attrs);
104 : }
105 7 : break;
106 : break;
107 24 : case SUMO_TAG_LANE: {
108 24 : tryParse(attrs, false);
109 : }
110 24 : break;
111 : default:
112 : break;
113 : }
114 6347 : }
115 :
116 :
117 : void
118 5267 : SAXWeightsHandler::tryParse(const SUMOSAXAttributes& attrs, bool isEdge) {
119 : // !!!! no error handling!
120 5267 : if (isEdge) {
121 : // process all that want values directly from the edge
122 10676 : for (const auto& definition : myDefinitions) {
123 5433 : if (definition->myAmEdgeBased) {
124 5413 : if (attrs.hasAttribute(definition->myAttributeName)) {
125 5403 : definition->myAggValue = attrs.getFloat(definition->myAttributeName);
126 5403 : definition->myNoLanes = 1;
127 5403 : definition->myHadAttribute = true;
128 : } else {
129 10 : definition->myHadAttribute = false;
130 : }
131 : } else {
132 20 : definition->myAggValue = 0;
133 20 : definition->myNoLanes = 0;
134 : }
135 : }
136 : } else {
137 : // process the current lane values
138 48 : for (const auto& definition : myDefinitions) {
139 24 : if (!definition->myAmEdgeBased) {
140 : try {
141 24 : definition->myAggValue += attrs.getFloat(definition->myAttributeName);
142 24 : definition->myNoLanes++;
143 24 : definition->myHadAttribute = true;
144 0 : } catch (EmptyData&) {
145 0 : WRITE_ERRORF(TL("Missing value '%' in edge '%'."), definition->myAttributeName, myCurrentEdgeID);
146 0 : } catch (NumberFormatException&) {
147 0 : WRITE_ERROR("The value should be numeric, but is not.\n In edge '" + myCurrentEdgeID +
148 : "' at time step " + toString(myCurrentTimeBeg) + ".");
149 0 : }
150 : }
151 : }
152 : }
153 5267 : }
154 :
155 :
156 : void
157 0 : SAXWeightsHandler::tryParseEdgeRel(const SUMOSAXAttributes& attrs) {
158 0 : if (attrs.hasAttribute(SUMO_ATTR_FROM) && attrs.hasAttribute(SUMO_ATTR_TO)) {
159 0 : bool ok = true;
160 0 : const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, nullptr, ok);
161 0 : const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, nullptr, ok);
162 0 : for (ToRetrieveDefinition* ret : myDefinitions) {
163 0 : if (attrs.hasAttribute(ret->myAttributeName)) {
164 0 : ret->myDestination.addEdgeRelWeight(from, to,
165 0 : attrs.getFloat(ret->myAttributeName),
166 : myCurrentTimeBeg, myCurrentTimeEnd);
167 : }
168 : }
169 : }
170 0 : }
171 :
172 : void
173 7 : SAXWeightsHandler::tryParseTazRel(const SUMOSAXAttributes& attrs) {
174 7 : if (attrs.hasAttribute(SUMO_ATTR_FROM) && attrs.hasAttribute(SUMO_ATTR_TO)) {
175 7 : bool ok = true;
176 7 : const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, nullptr, ok);
177 7 : const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, nullptr, ok);
178 14 : for (ToRetrieveDefinition* ret : myDefinitions) {
179 7 : if (attrs.hasAttribute(ret->myAttributeName)) {
180 14 : ret->myDestination.addTazRelWeight(myCurrentID, from, to,
181 7 : attrs.getFloat(ret->myAttributeName),
182 : myCurrentTimeBeg, myCurrentTimeEnd);
183 : }
184 : }
185 : }
186 7 : }
187 :
188 :
189 : void
190 6347 : SAXWeightsHandler::myEndElement(int element) {
191 6347 : if (element == SUMO_TAG_EDGE) {
192 10676 : for (const auto& definition : myDefinitions) {
193 5433 : if (definition->myHadAttribute) {
194 5423 : definition->myDestination.addEdgeWeight(myCurrentEdgeID,
195 5423 : definition->myAggValue / (double)definition->myNoLanes,
196 : myCurrentTimeBeg, myCurrentTimeEnd);
197 : }
198 : }
199 : }
200 6347 : }
201 :
202 :
203 : /****************************************************************************/
|