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 : if (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 0 : obj->markAsCreated();
114 : };
115 : break;
116 0 : case SUMO_TAG_MEANDATA_LANE:
117 0 : if (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 : obj->markAsCreated();
136 : }
137 : break;
138 : default:
139 : break;
140 : }
141 : // now iterate over childrens
142 0 : for (const auto& child : obj->getSumoBaseObjectChildren()) {
143 : // call this function recursively
144 0 : parseSumoBaseObject(child);
145 : }
146 0 : }
147 :
148 :
149 : void
150 0 : MeanDataHandler::parseEdgeMeanData(const SUMOSAXAttributes& attrs) {
151 : // declare Ok Flag
152 0 : bool parsedOk = true;
153 : // needed attributes
154 0 : const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "", parsedOk);
155 0 : const std::string file = attrs.get<std::string>(SUMO_ATTR_FILE, id.c_str(), parsedOk);
156 : // optional attributes
157 0 : const SUMOTime period = attrs.getOptSUMOTimeReporting(SUMO_ATTR_PERIOD, id.c_str(), parsedOk, -1);
158 0 : const SUMOTime begin = attrs.getOptSUMOTimeReporting(SUMO_ATTR_BEGIN, id.c_str(), parsedOk, -1);
159 0 : const SUMOTime end = attrs.getOptSUMOTimeReporting(SUMO_ATTR_END, id.c_str(), parsedOk, -1);
160 0 : const bool trackVehicles = attrs.getOpt<bool>(SUMO_ATTR_TRACK_VEHICLES, id.c_str(), parsedOk, false);
161 0 : const std::vector<std::string> writeAttributes = attrs.getOpt<std::vector<std::string> >(SUMO_ATTR_WRITE_ATTRIBUTES, id.c_str(), parsedOk, {});
162 0 : const bool aggregate = attrs.getOpt<bool>(SUMO_ATTR_AGGREGATE, id.c_str(), parsedOk, false);
163 0 : const std::vector<std::string> edges = attrs.getOpt<std::vector<std::string> >(SUMO_ATTR_EDGES, id.c_str(), parsedOk, {});
164 0 : const std::string edgeFile = attrs.getOpt<std::string>(SUMO_ATTR_EDGESFILE, id.c_str(), parsedOk, "");
165 0 : const std::string excludeEmpty = attrs.getOpt<std::string>(SUMO_ATTR_EXCLUDE_EMPTY, id.c_str(), parsedOk, "default");
166 0 : const bool withInternal = attrs.getOpt<bool>(SUMO_ATTR_WITH_INTERNAL, id.c_str(), parsedOk, false);
167 0 : const std::vector<std::string> detectPersons = attrs.getOpt<std::vector<std::string> >(SUMO_ATTR_DETECT_PERSONS, id.c_str(), parsedOk, {});
168 0 : const double minSamples = attrs.getOpt<double>(SUMO_ATTR_MIN_SAMPLES, id.c_str(), parsedOk, 0);
169 0 : const double maxTravel = attrs.getOpt<double>(SUMO_ATTR_MAX_TRAVELTIME, id.c_str(), parsedOk, 100000);
170 0 : const std::vector<std::string> vTypes = attrs.getOpt<std::vector<std::string> >(SUMO_ATTR_VTYPES, id.c_str(), parsedOk, {});
171 0 : const double speedThreshold = attrs.getOpt<double>(SUMO_ATTR_HALTING_SPEED_THRESHOLD, id.c_str(), parsedOk, 0.1);
172 : // continue if flag is ok
173 0 : if (parsedOk) {
174 : // set tag
175 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->setTag(SUMO_TAG_MEANDATA_EDGE);
176 : // add all attributes
177 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_ID, id);
178 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_FILE, file);
179 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addTimeAttribute(SUMO_ATTR_PERIOD, period);
180 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addTimeAttribute(SUMO_ATTR_BEGIN, begin);
181 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addTimeAttribute(SUMO_ATTR_END, end);
182 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addBoolAttribute(SUMO_ATTR_TRACK_VEHICLES, trackVehicles);
183 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringListAttribute(SUMO_ATTR_WRITE_ATTRIBUTES, writeAttributes);
184 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addBoolAttribute(SUMO_ATTR_AGGREGATE, aggregate);
185 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringListAttribute(SUMO_ATTR_EDGES, edges);
186 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_EDGESFILE, edgeFile);
187 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_EXCLUDE_EMPTY, excludeEmpty);
188 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addBoolAttribute(SUMO_ATTR_WITH_INTERNAL, withInternal);
189 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringListAttribute(SUMO_ATTR_DETECT_PERSONS, detectPersons);
190 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addDoubleAttribute(SUMO_ATTR_MIN_SAMPLES, minSamples);
191 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addDoubleAttribute(SUMO_ATTR_MAX_TRAVELTIME, maxTravel);
192 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringListAttribute(SUMO_ATTR_VTYPES, vTypes);
193 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addDoubleAttribute(SUMO_ATTR_HALTING_SPEED_THRESHOLD, speedThreshold);
194 : } else {
195 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->setTag(SUMO_TAG_NOTHING);
196 : }
197 0 : }
198 :
199 :
200 : void
201 0 : MeanDataHandler::parseLaneMeanData(const SUMOSAXAttributes& attrs) {
202 : // declare Ok Flag
203 0 : bool parsedOk = true;
204 : // needed attributes
205 0 : const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "", parsedOk);
206 0 : const std::string file = attrs.get<std::string>(SUMO_ATTR_FILE, id.c_str(), parsedOk);
207 : // optional attributes
208 0 : const SUMOTime period = attrs.getOptSUMOTimeReporting(SUMO_ATTR_PERIOD, id.c_str(), parsedOk, -1);
209 0 : const SUMOTime begin = attrs.getOptSUMOTimeReporting(SUMO_ATTR_BEGIN, id.c_str(), parsedOk, -1);
210 0 : const SUMOTime end = attrs.getOptSUMOTimeReporting(SUMO_ATTR_END, id.c_str(), parsedOk, -1);
211 0 : const bool trackVehicles = attrs.getOpt<bool>(SUMO_ATTR_TRACK_VEHICLES, id.c_str(), parsedOk, false);
212 0 : const std::vector<std::string> writeAttributes = attrs.getOpt<std::vector<std::string> >(SUMO_ATTR_WRITE_ATTRIBUTES, id.c_str(), parsedOk, {});
213 0 : const bool aggregate = attrs.getOpt<bool>(SUMO_ATTR_AGGREGATE, id.c_str(), parsedOk, false);
214 0 : const std::vector<std::string> edges = attrs.getOpt<std::vector<std::string> >(SUMO_ATTR_EDGES, id.c_str(), parsedOk, {});
215 0 : const std::string edgeFile = attrs.getOpt<std::string>(SUMO_ATTR_EDGESFILE, id.c_str(), parsedOk, "");
216 0 : const std::string excludeEmpty = attrs.getOpt<std::string>(SUMO_ATTR_EXCLUDE_EMPTY, id.c_str(), parsedOk, "default");
217 0 : const bool withInternal = attrs.getOpt<bool>(SUMO_ATTR_WITH_INTERNAL, id.c_str(), parsedOk, false);
218 0 : const std::vector<std::string> detectPersons = attrs.getOpt<std::vector<std::string> >(SUMO_ATTR_DETECT_PERSONS, id.c_str(), parsedOk, {});
219 0 : const double minSamples = attrs.getOpt<double>(SUMO_ATTR_MIN_SAMPLES, id.c_str(), parsedOk, 0);
220 0 : const double maxTravel = attrs.getOpt<double>(SUMO_ATTR_MAX_TRAVELTIME, id.c_str(), parsedOk, 100000);
221 0 : const std::vector<std::string> vTypes = attrs.getOpt<std::vector<std::string> >(SUMO_ATTR_VTYPES, id.c_str(), parsedOk, {});
222 0 : const double speedThreshold = attrs.getOpt<double>(SUMO_ATTR_HALTING_SPEED_THRESHOLD, id.c_str(), parsedOk, 0.1);
223 : // continue if flag is ok
224 0 : if (parsedOk) {
225 : // set tag
226 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->setTag(SUMO_TAG_MEANDATA_LANE);
227 : // add all attributes
228 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_ID, id);
229 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_FILE, file);
230 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addTimeAttribute(SUMO_ATTR_PERIOD, period);
231 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addTimeAttribute(SUMO_ATTR_BEGIN, begin);
232 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addTimeAttribute(SUMO_ATTR_END, end);
233 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addBoolAttribute(SUMO_ATTR_TRACK_VEHICLES, trackVehicles);
234 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringListAttribute(SUMO_ATTR_WRITE_ATTRIBUTES, writeAttributes);
235 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addBoolAttribute(SUMO_ATTR_AGGREGATE, aggregate);
236 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringListAttribute(SUMO_ATTR_EDGES, edges);
237 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_EDGESFILE, edgeFile);
238 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_EXCLUDE_EMPTY, excludeEmpty);
239 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addBoolAttribute(SUMO_ATTR_WITH_INTERNAL, withInternal);
240 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringListAttribute(SUMO_ATTR_DETECT_PERSONS, detectPersons);
241 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addDoubleAttribute(SUMO_ATTR_MIN_SAMPLES, minSamples);
242 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addDoubleAttribute(SUMO_ATTR_MAX_TRAVELTIME, maxTravel);
243 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringListAttribute(SUMO_ATTR_VTYPES, vTypes);
244 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addDoubleAttribute(SUMO_ATTR_HALTING_SPEED_THRESHOLD, speedThreshold);
245 : } else {
246 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->setTag(SUMO_TAG_NOTHING);
247 : }
248 0 : }
249 :
250 : /****************************************************************************/
|