Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/netedit/elements/data/GNEMeanDataHandler.cpp
169684 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 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 GNEMeanDataHandler.cpp
15
/// @author Pablo Alvarez Lopez
16
/// @date Nov 22
17
///
18
// Builds meanData objects for netedit
19
/****************************************************************************/
20
21
#include <netedit/changes/GNEChange_MeanData.h>
22
#include <netedit/dialogs/basic/GNEOverwriteElement.h>
23
#include <netedit/elements/data/GNEMeanData.h>
24
#include <netedit/GNENet.h>
25
#include <netedit/GNETagProperties.h>
26
#include <netedit/GNEUndoList.h>
27
#include <netedit/GNEViewNet.h>
28
#include <utils/gui/div/GUIDesigns.h>
29
30
#include "GNEMeanDataHandler.h"
31
32
// ===========================================================================
33
// member method definitions
34
// ===========================================================================
35
36
GNEMeanDataHandler::GNEMeanDataHandler(GNENet* net, const std::string& filename, const bool allowUndoRedo) :
37
MeanDataHandler(filename),
38
myNet(net),
39
myAllowUndoRedo(allowUndoRedo) {
40
}
41
42
43
GNEMeanDataHandler::~GNEMeanDataHandler() {}
44
45
46
bool
47
GNEMeanDataHandler::postParserTasks() {
48
// nothing to do
49
return true;
50
}
51
52
53
bool
54
GNEMeanDataHandler::buildEdgeMeanData(const CommonXMLStructure::SumoBaseObject* /*sumoBaseObject*/, const std::string& id,
55
const std::string& file, SUMOTime period, SUMOTime begin, SUMOTime end, const bool trackVehicles,
56
const std::vector<std::string>& writtenAttributes, const bool aggregate, const std::vector<std::string>& edgeIDs,
57
const std::string& edgeFile, std::string excludeEmpty, const bool withInternal,
58
const std::vector<std::string>& detectPersons, const double minSamples, const double maxTravelTime,
59
const std::vector<std::string>& vTypes, const double speedThreshold) {
60
// parse attributes
61
const auto edges = parseEdges(SUMO_TAG_MEANDATA_EDGE, edgeIDs);
62
// parse edges
63
const auto attributes = parseAttributes(SUMO_TAG_MEANDATA_EDGE, writtenAttributes);
64
// check if meanData edge exists
65
if (!checkValidAdditionalID(SUMO_TAG_MEANDATA_EDGE, id)) {
66
return false;
67
} else if (!checkDuplicatedMeanDataElement(SUMO_TAG_MEANDATA_EDGE, id)) {
68
return false;
69
} else if ((period != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_PERIOD, period, true)) {
70
return false;
71
} else if ((begin != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_BEGIN, begin, true)) {
72
return false;
73
} else if ((end != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_END, end, true)) {
74
return false;
75
} else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_MAX_TRAVELTIME, maxTravelTime, true)) {
76
return false;
77
} else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_MIN_SAMPLES, minSamples, true)) {
78
return false;
79
} else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_HALTING_SPEED_THRESHOLD, speedThreshold, true)) {
80
return false;
81
} else if (!checkExcludeEmpty(SUMO_TAG_MEANDATA_EDGE, id, excludeEmpty)) {
82
return false;
83
} else if ((edges.size() == edgeIDs.size()) && (attributes.size() == writtenAttributes.size())) {
84
GNEMeanData* edgeMeanData = new GNEMeanData(SUMO_TAG_MEANDATA_EDGE, id, myNet, myFilename, file, period, begin, end,
85
trackVehicles, attributes, aggregate, edgeIDs, edgeFile, excludeEmpty, withInternal,
86
detectPersons, minSamples, maxTravelTime, vTypes, speedThreshold);
87
if (myAllowUndoRedo) {
88
myNet->getViewNet()->getUndoList()->begin(edgeMeanData, TL("add meanDataEdge"));
89
myNet->getViewNet()->getUndoList()->add(new GNEChange_MeanData(edgeMeanData, true), true);
90
myNet->getViewNet()->getUndoList()->end();
91
} else {
92
myNet->getAttributeCarriers()->insertMeanData(edgeMeanData);
93
edgeMeanData->incRef("buildEdgeMeanData");
94
}
95
return true;
96
} else {
97
return false;
98
}
99
}
100
101
102
bool
103
GNEMeanDataHandler::buildLaneMeanData(const CommonXMLStructure::SumoBaseObject* /*sumoBaseObject*/, const std::string& id,
104
const std::string& file, SUMOTime period, SUMOTime begin, SUMOTime end, const bool trackVehicles,
105
const std::vector<std::string>& writtenAttributes, const bool aggregate, const std::vector<std::string>& edgeIDs,
106
const std::string& edgeFile, std::string excludeEmpty, const bool withInternal,
107
const std::vector<std::string>& detectPersons, const double minSamples, const double maxTravelTime,
108
const std::vector<std::string>& vTypes, const double speedThreshold) {
109
// parse attributes
110
const auto edges = parseEdges(SUMO_TAG_MEANDATA_LANE, edgeIDs);
111
// parse edges
112
const auto attributes = parseAttributes(SUMO_TAG_MEANDATA_LANE, writtenAttributes);
113
// check if meanData edge exists
114
if (!checkValidAdditionalID(SUMO_TAG_MEANDATA_LANE, id)) {
115
return false;
116
} else if (!checkDuplicatedMeanDataElement(SUMO_TAG_MEANDATA_LANE, id)) {
117
return false;
118
} else if ((period != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_PERIOD, period, true)) {
119
return false;
120
} else if ((begin != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_BEGIN, begin, true)) {
121
return false;
122
} else if ((end != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_END, end, true)) {
123
return false;
124
} else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_MAX_TRAVELTIME, maxTravelTime, true)) {
125
return false;
126
} else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_MIN_SAMPLES, minSamples, true)) {
127
return false;
128
} else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_HALTING_SPEED_THRESHOLD, speedThreshold, true)) {
129
return false;
130
} else if (!checkExcludeEmpty(SUMO_TAG_MEANDATA_EDGE, id, excludeEmpty)) {
131
return false;
132
} else if ((edges.size() == edgeIDs.size()) && (attributes.size() == writtenAttributes.size())) {
133
GNEMeanData* edgeMeanData = new GNEMeanData(SUMO_TAG_MEANDATA_LANE, id, myNet, myFilename, file, period, begin, end,
134
trackVehicles, attributes, aggregate, edgeIDs, edgeFile, excludeEmpty, withInternal,
135
detectPersons, minSamples, maxTravelTime, vTypes, speedThreshold);
136
if (myAllowUndoRedo) {
137
myNet->getViewNet()->getUndoList()->begin(edgeMeanData, TL("add meanDataLane"));
138
myNet->getViewNet()->getUndoList()->add(new GNEChange_MeanData(edgeMeanData, true), true);
139
myNet->getViewNet()->getUndoList()->end();
140
} else {
141
myNet->getAttributeCarriers()->insertMeanData(edgeMeanData);
142
edgeMeanData->incRef("buildEdgeMeanData");
143
}
144
return true;
145
} else {
146
return false;
147
}
148
}
149
150
151
std::vector<GNEEdge*>
152
GNEMeanDataHandler::parseEdges(const SumoXMLTag tag, const std::vector<std::string>& edgeIDs) {
153
std::vector<GNEEdge*> edges;
154
for (const auto& edgeID : edgeIDs) {
155
GNEEdge* edge = myNet->getAttributeCarriers()->retrieveEdge(edgeID, false);
156
// empty edges aren't allowed. If edge is empty, write error, clear edges and stop
157
if (edge == nullptr) {
158
writeError(TLF("Could not build % in netedit", toString(tag)) + std::string("; ") + TL("Edge doesn't exist."));
159
edges.clear();
160
return edges;
161
} else {
162
edges.push_back(edge);
163
}
164
}
165
return edges;
166
}
167
168
169
std::vector<SumoXMLAttr>
170
GNEMeanDataHandler::parseAttributes(const SumoXMLTag tag, const std::vector<std::string>& attrStrs) {
171
std::vector<SumoXMLAttr> attrs;
172
for (const auto& attrStr : attrStrs) {
173
if (SUMOXMLDefinitions::Attrs.hasString(attrStr)) {
174
attrs.push_back(static_cast<SumoXMLAttr>(SUMOXMLDefinitions::Attrs.get(attrStr)));
175
} else {
176
writeError(TLF("Could not build % in netedit", toString(tag)) + std::string("; ") + TLF("Attribute '%' doesn't exist.", attrStr));
177
attrs.clear();
178
return attrs;
179
}
180
}
181
return attrs;
182
}
183
184
185
bool
186
GNEMeanDataHandler::checkDuplicatedMeanDataElement(const SumoXMLTag tag, const std::string& id) {
187
// retrieve meanData element
188
auto meanDataElement = myNet->getAttributeCarriers()->retrieveMeanData(tag, id, false);
189
// if meanData exist, check if overwrite (delete)
190
if (meanDataElement) {
191
if (myOverwriteElements) {
192
// delete meanData element (and all of their childrens)
193
myNet->deleteMeanData(meanDataElement, myNet->getViewNet()->getUndoList());
194
} else if (myRemainElements) {
195
// duplicated dataset
196
return writeWarningDuplicated(tag, meanDataElement->getID(), meanDataElement->getTagProperty()->getTag());
197
} else {
198
// open overwrite dialog
199
GNEOverwriteElement overwriteElementDialog(this, meanDataElement);
200
// continue depending of result
201
if (overwriteElementDialog.getResult() == GNEOverwriteElement::Result::ACCEPT) {
202
// delete meanData element (and all of their childrens)
203
myNet->deleteMeanData(meanDataElement, myNet->getViewNet()->getUndoList());
204
} else if (overwriteElementDialog.getResult() == GNEOverwriteElement::Result::CANCEL) {
205
// duplicated demand
206
return writeWarningDuplicated(tag, meanDataElement->getID(), meanDataElement->getTagProperty()->getTag());
207
} else {
208
return false;
209
}
210
}
211
}
212
return true;
213
}
214
215
216
bool
217
GNEMeanDataHandler::checkExcludeEmpty(const SumoXMLTag tag, const std::string& id, const std::string& excludeEmpty) {
218
if (GNEAttributeCarrier::canParse<bool>(excludeEmpty)) {
219
return true;
220
} else if (excludeEmpty == SUMOXMLDefinitions::ExcludeEmptys.getString(ExcludeEmpty::DEFAULTS)) {
221
return true;
222
} else {
223
return writeError(TLF("Could not build % with ID '%' in netedit; Invalid value '%' for %.", toString(tag), id, excludeEmpty, toString(SUMO_ATTR_EXCLUDE_EMPTY)));
224
}
225
}
226
227
/****************************************************************************/
228
229