Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-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 MeanDataHandler.cpp
15 : /// @author Pablo Alvarez Lopez
16 : /// @date Nov 2022
17 : ///
18 : // The XML-Handler for meanMeanData elements loading
19 : /****************************************************************************/
20 : #include <config.h>
21 :
22 : #include <utils/common/MsgHandler.h>
23 : #include <utils/common/StringUtils.h>
24 : #include <utils/xml/XMLSubSys.h>
25 :
26 : #include "MeanDataHandler.h"
27 :
28 :
29 : // ===========================================================================
30 : // method definitions
31 : // ===========================================================================
32 :
33 0 : MeanDataHandler::MeanDataHandler() {
34 0 : }
35 :
36 :
37 0 : MeanDataHandler::~MeanDataHandler() {}
38 :
39 :
40 : bool
41 0 : MeanDataHandler::beginParseAttributes(SumoXMLTag tag, const SUMOSAXAttributes& attrs) {
42 : // open SUMOBaseOBject
43 0 : myCommonXMLStructure.openSUMOBaseOBject();
44 : // check tag
45 : try {
46 0 : switch (tag) {
47 0 : case SUMO_TAG_MEANDATA_EDGE:
48 0 : parseEdgeMeanData(attrs);
49 : break;
50 0 : case SUMO_TAG_MEANDATA_LANE:
51 0 : parseLaneMeanData(attrs);
52 : break;
53 0 : case SUMO_TAG_PARAM:
54 0 : WRITE_WARNING(TL("MeanData elements cannot load attributes as params"));
55 0 : break;
56 0 : default:
57 : // tag cannot be parsed in MeanDataHandler
58 0 : myCommonXMLStructure.abortSUMOBaseOBject();
59 : return false;
60 : }
61 0 : } catch (InvalidArgument& e) {
62 0 : writeError(e.what());
63 0 : }
64 : return true;
65 : }
66 :
67 :
68 : void
69 0 : MeanDataHandler::endParseAttributes() {
70 : // get last inserted object
71 0 : CommonXMLStructure::SumoBaseObject* obj = myCommonXMLStructure.getCurrentSumoBaseObject();
72 : // close SUMOBaseOBject
73 0 : if (obj) {
74 0 : myCommonXMLStructure.closeSUMOBaseOBject();
75 : // check tag
76 0 : switch (obj->getTag()) {
77 0 : case SUMO_TAG_MEANDATA_EDGE:
78 : case SUMO_TAG_MEANDATA_LANE:
79 0 : parseSumoBaseObject(obj);
80 : // delete object
81 0 : delete obj;
82 : break;
83 : default:
84 : break;
85 : }
86 : }
87 0 : }
88 :
89 :
90 : void
91 0 : MeanDataHandler::parseSumoBaseObject(CommonXMLStructure::SumoBaseObject* obj) {
92 : // switch tag
93 0 : switch (obj->getTag()) {
94 0 : case SUMO_TAG_MEANDATA_EDGE:
95 0 : buildEdgeMeanData(obj,
96 : obj->getStringAttribute(SUMO_ATTR_ID),
97 : obj->getStringAttribute(SUMO_ATTR_FILE),
98 : obj->getTimeAttribute(SUMO_ATTR_PERIOD),
99 : obj->getTimeAttribute(SUMO_ATTR_BEGIN),
100 : obj->getTimeAttribute(SUMO_ATTR_END),
101 0 : obj->getBoolAttribute(SUMO_ATTR_TRACK_VEHICLES),
102 : obj->getStringListAttribute(SUMO_ATTR_WRITE_ATTRIBUTES),
103 0 : obj->getBoolAttribute(SUMO_ATTR_AGGREGATE),
104 : obj->getStringListAttribute(SUMO_ATTR_EDGES),
105 : obj->getStringAttribute(SUMO_ATTR_EDGESFILE),
106 0 : obj->getStringAttribute(SUMO_ATTR_EXCLUDE_EMPTY),
107 0 : obj->getBoolAttribute(SUMO_ATTR_WITH_INTERNAL),
108 : obj->getStringListAttribute(SUMO_ATTR_DETECT_PERSONS),
109 : obj->getDoubleAttribute(SUMO_ATTR_MIN_SAMPLES),
110 : obj->getDoubleAttribute(SUMO_ATTR_MAX_TRAVELTIME),
111 : obj->getStringListAttribute(SUMO_ATTR_VTYPES),
112 : obj->getDoubleAttribute(SUMO_ATTR_HALTING_SPEED_THRESHOLD));
113 :
114 :
115 0 : break;
116 0 : case SUMO_TAG_MEANDATA_LANE:
117 0 : buildLaneMeanData(obj,
118 : obj->getStringAttribute(SUMO_ATTR_ID),
119 : obj->getStringAttribute(SUMO_ATTR_FILE),
120 : obj->getTimeAttribute(SUMO_ATTR_PERIOD),
121 : obj->getTimeAttribute(SUMO_ATTR_BEGIN),
122 : obj->getTimeAttribute(SUMO_ATTR_END),
123 0 : obj->getBoolAttribute(SUMO_ATTR_TRACK_VEHICLES),
124 : obj->getStringListAttribute(SUMO_ATTR_WRITE_ATTRIBUTES),
125 0 : obj->getBoolAttribute(SUMO_ATTR_AGGREGATE),
126 : obj->getStringListAttribute(SUMO_ATTR_EDGES),
127 : obj->getStringAttribute(SUMO_ATTR_EDGESFILE),
128 0 : obj->getStringAttribute(SUMO_ATTR_EXCLUDE_EMPTY),
129 0 : obj->getBoolAttribute(SUMO_ATTR_WITH_INTERNAL),
130 : obj->getStringListAttribute(SUMO_ATTR_DETECT_PERSONS),
131 : obj->getDoubleAttribute(SUMO_ATTR_MIN_SAMPLES),
132 : obj->getDoubleAttribute(SUMO_ATTR_MAX_TRAVELTIME),
133 : obj->getStringListAttribute(SUMO_ATTR_VTYPES),
134 : obj->getDoubleAttribute(SUMO_ATTR_HALTING_SPEED_THRESHOLD));
135 0 : break;
136 : default:
137 : break;
138 : }
139 : // now iterate over childrens
140 0 : for (const auto& child : obj->getSumoBaseObjectChildren()) {
141 : // call this function recursively
142 0 : parseSumoBaseObject(child);
143 : }
144 0 : }
145 :
146 :
147 : bool
148 0 : MeanDataHandler::isErrorCreatingElement() const {
149 0 : return myErrorCreatingElement;
150 : }
151 :
152 :
153 : void
154 0 : MeanDataHandler::writeError(const std::string& error) {
155 0 : WRITE_ERROR(error);
156 0 : myErrorCreatingElement = true;
157 0 : }
158 :
159 :
160 : void
161 0 : MeanDataHandler::parseEdgeMeanData(const SUMOSAXAttributes& attrs) {
162 : // declare Ok Flag
163 0 : bool parsedOk = true;
164 : // needed attributes
165 0 : const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "", parsedOk);
166 0 : const std::string file = attrs.get<std::string>(SUMO_ATTR_FILE, id.c_str(), parsedOk);
167 : // optional attributes
168 0 : const SUMOTime period = attrs.getOptSUMOTimeReporting(SUMO_ATTR_PERIOD, id.c_str(), parsedOk, -1);
169 0 : const SUMOTime begin = attrs.getOptSUMOTimeReporting(SUMO_ATTR_BEGIN, id.c_str(), parsedOk, -1);
170 0 : const SUMOTime end = attrs.getOptSUMOTimeReporting(SUMO_ATTR_END, id.c_str(), parsedOk, -1);
171 0 : const bool trackVehicles = attrs.getOpt<bool>(SUMO_ATTR_TRACK_VEHICLES, id.c_str(), parsedOk, false);
172 0 : const std::vector<std::string> writeAttributes = attrs.getOpt<std::vector<std::string> >(SUMO_ATTR_WRITE_ATTRIBUTES, id.c_str(), parsedOk, {});
173 0 : const bool aggregate = attrs.getOpt<bool>(SUMO_ATTR_AGGREGATE, id.c_str(), parsedOk, false);
174 0 : const std::vector<std::string> edges = attrs.getOpt<std::vector<std::string> >(SUMO_ATTR_EDGES, id.c_str(), parsedOk, {});
175 0 : const std::string edgeFile = attrs.getOpt<std::string>(SUMO_ATTR_EDGESFILE, id.c_str(), parsedOk, "");
176 0 : const std::string excludeEmpty = attrs.getOpt<std::string>(SUMO_ATTR_EXCLUDE_EMPTY, id.c_str(), parsedOk, "default");
177 0 : const bool withInternal = attrs.getOpt<bool>(SUMO_ATTR_WITH_INTERNAL, id.c_str(), parsedOk, false);
178 0 : const std::vector<std::string> detectPersons = attrs.getOpt<std::vector<std::string> >(SUMO_ATTR_DETECT_PERSONS, id.c_str(), parsedOk, {});
179 0 : const double minSamples = attrs.getOpt<double>(SUMO_ATTR_MIN_SAMPLES, id.c_str(), parsedOk, 0);
180 0 : const double maxTravel = attrs.getOpt<double>(SUMO_ATTR_MAX_TRAVELTIME, id.c_str(), parsedOk, 100000);
181 0 : const std::vector<std::string> vTypes = attrs.getOpt<std::vector<std::string> >(SUMO_ATTR_VTYPES, id.c_str(), parsedOk, {});
182 0 : const double speedThreshold = attrs.getOpt<double>(SUMO_ATTR_HALTING_SPEED_THRESHOLD, id.c_str(), parsedOk, 0.1);
183 : // continue if flag is ok
184 0 : if (parsedOk) {
185 : // set tag
186 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->setTag(SUMO_TAG_MEANDATA_EDGE);
187 : // add all attributes
188 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_ID, id);
189 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_FILE, file);
190 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addTimeAttribute(SUMO_ATTR_PERIOD, period);
191 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addTimeAttribute(SUMO_ATTR_BEGIN, begin);
192 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addTimeAttribute(SUMO_ATTR_END, end);
193 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addBoolAttribute(SUMO_ATTR_TRACK_VEHICLES, trackVehicles);
194 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringListAttribute(SUMO_ATTR_WRITE_ATTRIBUTES, writeAttributes);
195 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addBoolAttribute(SUMO_ATTR_AGGREGATE, aggregate);
196 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringListAttribute(SUMO_ATTR_EDGES, edges);
197 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_EDGESFILE, edgeFile);
198 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_EXCLUDE_EMPTY, excludeEmpty);
199 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addBoolAttribute(SUMO_ATTR_WITH_INTERNAL, withInternal);
200 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringListAttribute(SUMO_ATTR_DETECT_PERSONS, detectPersons);
201 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addDoubleAttribute(SUMO_ATTR_MIN_SAMPLES, minSamples);
202 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addDoubleAttribute(SUMO_ATTR_MAX_TRAVELTIME, maxTravel);
203 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringListAttribute(SUMO_ATTR_VTYPES, vTypes);
204 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addDoubleAttribute(SUMO_ATTR_HALTING_SPEED_THRESHOLD, speedThreshold);
205 : }
206 0 : }
207 :
208 :
209 : void
210 0 : MeanDataHandler::parseLaneMeanData(const SUMOSAXAttributes& attrs) {
211 : // declare Ok Flag
212 0 : bool parsedOk = true;
213 : // needed attributes
214 0 : const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "", parsedOk);
215 0 : const std::string file = attrs.get<std::string>(SUMO_ATTR_FILE, id.c_str(), parsedOk);
216 : // optional attributes
217 0 : const SUMOTime period = attrs.getOptSUMOTimeReporting(SUMO_ATTR_PERIOD, id.c_str(), parsedOk, -1);
218 0 : const SUMOTime begin = attrs.getOptSUMOTimeReporting(SUMO_ATTR_BEGIN, id.c_str(), parsedOk, -1);
219 0 : const SUMOTime end = attrs.getOptSUMOTimeReporting(SUMO_ATTR_END, id.c_str(), parsedOk, -1);
220 0 : const bool trackVehicles = attrs.getOpt<bool>(SUMO_ATTR_TRACK_VEHICLES, id.c_str(), parsedOk, false);
221 0 : const std::vector<std::string> writeAttributes = attrs.getOpt<std::vector<std::string> >(SUMO_ATTR_WRITE_ATTRIBUTES, id.c_str(), parsedOk, {});
222 0 : const bool aggregate = attrs.getOpt<bool>(SUMO_ATTR_AGGREGATE, id.c_str(), parsedOk, false);
223 0 : const std::vector<std::string> edges = attrs.getOpt<std::vector<std::string> >(SUMO_ATTR_EDGES, id.c_str(), parsedOk, {});
224 0 : const std::string edgeFile = attrs.getOpt<std::string>(SUMO_ATTR_EDGESFILE, id.c_str(), parsedOk, "");
225 0 : const std::string excludeEmpty = attrs.getOpt<std::string>(SUMO_ATTR_EXCLUDE_EMPTY, id.c_str(), parsedOk, "default");
226 0 : const bool withInternal = attrs.getOpt<bool>(SUMO_ATTR_WITH_INTERNAL, id.c_str(), parsedOk, false);
227 0 : const std::vector<std::string> detectPersons = attrs.getOpt<std::vector<std::string> >(SUMO_ATTR_DETECT_PERSONS, id.c_str(), parsedOk, {});
228 0 : const double minSamples = attrs.getOpt<double>(SUMO_ATTR_MIN_SAMPLES, id.c_str(), parsedOk, 0);
229 0 : const double maxTravel = attrs.getOpt<double>(SUMO_ATTR_MAX_TRAVELTIME, id.c_str(), parsedOk, 100000);
230 0 : const std::vector<std::string> vTypes = attrs.getOpt<std::vector<std::string> >(SUMO_ATTR_VTYPES, id.c_str(), parsedOk, {});
231 0 : const double speedThreshold = attrs.getOpt<double>(SUMO_ATTR_HALTING_SPEED_THRESHOLD, id.c_str(), parsedOk, 0.1);
232 : // continue if flag is ok
233 0 : if (parsedOk) {
234 : // set tag
235 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->setTag(SUMO_TAG_MEANDATA_LANE);
236 : // add all attributes
237 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_ID, id);
238 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_FILE, file);
239 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addTimeAttribute(SUMO_ATTR_PERIOD, period);
240 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addTimeAttribute(SUMO_ATTR_BEGIN, begin);
241 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addTimeAttribute(SUMO_ATTR_END, end);
242 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addBoolAttribute(SUMO_ATTR_TRACK_VEHICLES, trackVehicles);
243 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringListAttribute(SUMO_ATTR_WRITE_ATTRIBUTES, writeAttributes);
244 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addBoolAttribute(SUMO_ATTR_AGGREGATE, aggregate);
245 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringListAttribute(SUMO_ATTR_EDGES, edges);
246 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_EDGESFILE, edgeFile);
247 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_EXCLUDE_EMPTY, excludeEmpty);
248 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addBoolAttribute(SUMO_ATTR_WITH_INTERNAL, withInternal);
249 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringListAttribute(SUMO_ATTR_DETECT_PERSONS, detectPersons);
250 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addDoubleAttribute(SUMO_ATTR_MIN_SAMPLES, minSamples);
251 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addDoubleAttribute(SUMO_ATTR_MAX_TRAVELTIME, maxTravel);
252 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringListAttribute(SUMO_ATTR_VTYPES, vTypes);
253 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addDoubleAttribute(SUMO_ATTR_HALTING_SPEED_THRESHOLD, speedThreshold);
254 : }
255 0 : }
256 :
257 : /****************************************************************************/
|