Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GNEMeanDataHandler.cpp
Go to the documentation of this file.
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/****************************************************************************/
18// Builds meanData objects for netedit
19/****************************************************************************/
20
24#include <netedit/GNENet.h>
26#include <netedit/GNEUndoList.h>
27#include <netedit/GNEViewNet.h>
29
30#include "GNEMeanDataHandler.h"
31
32// ===========================================================================
33// member method definitions
34// ===========================================================================
35
36GNEMeanDataHandler::GNEMeanDataHandler(GNENet* net, const std::string& filename, const bool allowUndoRedo) :
37 MeanDataHandler(filename),
38 myNet(net),
39 myAllowUndoRedo(allowUndoRedo) {
40}
41
42
44
45
46bool
48 // nothing to do
49 return true;
50}
51
52
53bool
54GNEMeanDataHandler::buildEdgeMeanData(const CommonXMLStructure::SumoBaseObject* /*sumoBaseObject*/, const std::string& id,
55 const std::string& file, const std::string& type, const SUMOTime period, const SUMOTime begin,
56 const SUMOTime end, const bool trackVehicles, const std::vector<std::string>& writtenAttributes,
57 const bool aggregate, const std::vector<std::string>& edgeIDs, const std::string& edgeFile,
58 const std::string& excludeEmpty, const bool withInternal, const std::vector<std::string>& detectPersons,
59 const double minSamples, const double maxTravelTime, const std::vector<std::string>& vTypes,
60 const double speedThreshold) {
61 // parse attributes
62 const auto edges = parseEdges(SUMO_TAG_MEANDATA_EDGE, edgeIDs);
63 // parse edges
64 const auto attributes = parseAttributes(SUMO_TAG_MEANDATA_EDGE, writtenAttributes);
65 // check if meanData edge exists
67 return false;
69 return false;
70 } else if ((period != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_PERIOD, period, true)) {
71 return false;
72 } else if ((begin != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_BEGIN, begin, true)) {
73 return false;
74 } else if ((end != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_END, end, true)) {
75 return false;
76 } else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_MAX_TRAVELTIME, maxTravelTime, true)) {
77 return false;
78 } else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_MIN_SAMPLES, minSamples, true)) {
79 return false;
80 } else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_HALTING_SPEED_THRESHOLD, speedThreshold, true)) {
81 return false;
82 } else if (!checkExcludeEmpty(SUMO_TAG_MEANDATA_EDGE, id, excludeEmpty)) {
83 return false;
84 } else if ((edges.size() == edgeIDs.size()) && (attributes.size() == writtenAttributes.size())) {
85 GNEMeanData* edgeMeanData = new GNEMeanData(SUMO_TAG_MEANDATA_EDGE, id, myNet, myFilename, file, type, period, begin, end,
86 trackVehicles, attributes, aggregate, edgeIDs, edgeFile, excludeEmpty, withInternal,
87 detectPersons, minSamples, maxTravelTime, vTypes, speedThreshold);
88 if (myAllowUndoRedo) {
89 myNet->getViewNet()->getUndoList()->begin(edgeMeanData, TL("add meanDataEdge"));
90 myNet->getViewNet()->getUndoList()->add(new GNEChange_MeanData(edgeMeanData, true), true);
91 myNet->getViewNet()->getUndoList()->end();
92 } else {
93 myNet->getAttributeCarriers()->insertMeanData(edgeMeanData);
94 edgeMeanData->incRef("buildEdgeMeanData");
95 }
96 return true;
97 } else {
98 return false;
99 }
100}
101
102
103bool
104GNEMeanDataHandler::buildLaneMeanData(const CommonXMLStructure::SumoBaseObject* /*sumoBaseObject*/, const std::string& id,
105 const std::string& file, const std::string& type, const SUMOTime period, const SUMOTime begin,
106 const SUMOTime end, const bool trackVehicles, const std::vector<std::string>& writtenAttributes,
107 const bool aggregate, const std::vector<std::string>& edgeIDs, const std::string& edgeFile,
108 const std::string& excludeEmpty, const bool withInternal, const std::vector<std::string>& detectPersons,
109 const double minSamples, const double maxTravelTime, const std::vector<std::string>& vTypes,
110 const double speedThreshold) {
111 // parse attributes
112 const auto edges = parseEdges(SUMO_TAG_MEANDATA_LANE, edgeIDs);
113 // parse edges
114 const auto attributes = parseAttributes(SUMO_TAG_MEANDATA_LANE, writtenAttributes);
115 // check if meanData edge exists
117 return false;
119 return false;
120 } else if ((period != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_PERIOD, period, true)) {
121 return false;
122 } else if ((begin != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_BEGIN, begin, true)) {
123 return false;
124 } else if ((end != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_END, end, true)) {
125 return false;
126 } else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_MAX_TRAVELTIME, maxTravelTime, true)) {
127 return false;
128 } else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_MIN_SAMPLES, minSamples, true)) {
129 return false;
130 } else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_HALTING_SPEED_THRESHOLD, speedThreshold, true)) {
131 return false;
132 } else if (!checkExcludeEmpty(SUMO_TAG_MEANDATA_EDGE, id, excludeEmpty)) {
133 return false;
134 } else if ((edges.size() == edgeIDs.size()) && (attributes.size() == writtenAttributes.size())) {
135 GNEMeanData* edgeMeanData = new GNEMeanData(SUMO_TAG_MEANDATA_LANE, id, myNet, myFilename, file, type, period, begin, end,
136 trackVehicles, attributes, aggregate, edgeIDs, edgeFile, excludeEmpty, withInternal,
137 detectPersons, minSamples, maxTravelTime, vTypes, speedThreshold);
138 if (myAllowUndoRedo) {
139 myNet->getViewNet()->getUndoList()->begin(edgeMeanData, TL("add meanDataLane"));
140 myNet->getViewNet()->getUndoList()->add(new GNEChange_MeanData(edgeMeanData, true), true);
141 myNet->getViewNet()->getUndoList()->end();
142 } else {
143 myNet->getAttributeCarriers()->insertMeanData(edgeMeanData);
144 edgeMeanData->incRef("buildEdgeMeanData");
145 }
146 return true;
147 } else {
148 return false;
149 }
150}
151
152
153std::vector<GNEEdge*>
154GNEMeanDataHandler::parseEdges(const SumoXMLTag tag, const std::vector<std::string>& edgeIDs) {
155 std::vector<GNEEdge*> edges;
156 for (const auto& edgeID : edgeIDs) {
157 GNEEdge* edge = myNet->getAttributeCarriers()->retrieveEdge(edgeID, false);
158 // empty edges aren't allowed. If edge is empty, write error, clear edges and stop
159 if (edge == nullptr) {
160 writeError(TLF("Could not build % in netedit", toString(tag)) + std::string("; ") + TL("Edge doesn't exist."));
161 edges.clear();
162 return edges;
163 } else {
164 edges.push_back(edge);
165 }
166 }
167 return edges;
168}
169
170
171std::vector<SumoXMLAttr>
172GNEMeanDataHandler::parseAttributes(const SumoXMLTag tag, const std::vector<std::string>& attrStrs) {
173 std::vector<SumoXMLAttr> attrs;
174 for (const auto& attrStr : attrStrs) {
175 if (SUMOXMLDefinitions::Attrs.hasString(attrStr)) {
176 attrs.push_back(static_cast<SumoXMLAttr>(SUMOXMLDefinitions::Attrs.get(attrStr)));
177 } else {
178 writeError(TLF("Could not build % in netedit", toString(tag)) + std::string("; ") + TLF("Attribute '%' doesn't exist.", attrStr));
179 attrs.clear();
180 return attrs;
181 }
182 }
183 return attrs;
184}
185
186
187bool
189 // retrieve meanData element
190 auto meanDataElement = myNet->getAttributeCarriers()->retrieveMeanData(tag, id, false);
191 // if meanData exist, check if overwrite (delete)
192 if (meanDataElement) {
194 // delete meanData element (and all of their childrens)
195 myNet->deleteMeanData(meanDataElement, myNet->getViewNet()->getUndoList());
196 } else if (myRemainElements) {
197 // duplicated dataset
198 return writeWarningDuplicated(tag, meanDataElement->getID(), meanDataElement->getTagProperty()->getTag());
199 } else {
200 // open overwrite dialog
201 GNEOverwriteElement overwriteElementDialog(this, meanDataElement);
202 // continue depending of result
203 if (overwriteElementDialog.getResult() == GNEOverwriteElement::Result::ACCEPT) {
204 // delete meanData element (and all of their childrens)
205 myNet->deleteMeanData(meanDataElement, myNet->getViewNet()->getUndoList());
206 } else if (overwriteElementDialog.getResult() == GNEOverwriteElement::Result::CANCEL) {
207 // duplicated demand
208 return writeWarningDuplicated(tag, meanDataElement->getID(), meanDataElement->getTagProperty()->getTag());
209 } else {
210 return false;
211 }
212 }
213 }
214 return true;
215}
216
217
218bool
219GNEMeanDataHandler::checkExcludeEmpty(const SumoXMLTag tag, const std::string& id, const std::string& excludeEmpty) {
220 if (GNEAttributeCarrier::canParse<bool>(excludeEmpty)) {
221 return true;
222 } else if (excludeEmpty == SUMOXMLDefinitions::ExcludeEmptys.getString(ExcludeEmpty::DEFAULTS)) {
223 return true;
224 } else {
225 return writeError(TLF("Could not build % with ID '%' in netedit; Invalid value '%' for %.", toString(tag), id, excludeEmpty, toString(SUMO_ATTR_EXCLUDE_EMPTY)));
226 }
227}
228
229/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define TL(string)
Definition MsgHandler.h:304
#define TLF(string,...)
Definition MsgHandler.h:306
#define TIME2STEPS(x)
Definition SUMOTime.h:57
SumoXMLTag
Numbers representing SUMO-XML - element names.
@ SUMO_TAG_MEANDATA_LANE
a lane based mean data detector
@ SUMO_TAG_MEANDATA_EDGE
an edge based mean data detector
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ SUMO_ATTR_BEGIN
weights: time range begin
@ SUMO_ATTR_MAX_TRAVELTIME
@ SUMO_ATTR_PERIOD
@ SUMO_ATTR_HALTING_SPEED_THRESHOLD
@ SUMO_ATTR_END
weights: time range end
@ SUMO_ATTR_MIN_SAMPLES
@ SUMO_ATTR_EXCLUDE_EMPTY
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
bool writeError(const std::string &error)
write error and enable error creating element
bool writeWarningDuplicated(const SumoXMLTag tag, const std::string &id, const SumoXMLTag checkedTag)
write warning duplicated element
bool myOverwriteElements
overwrite elements
bool checkValidAdditionalID(const SumoXMLTag tag, const std::string &value)
check if the given additional ID is valid
const std::string myFilename
filename
bool checkNegative(const SumoXMLTag tag, const std::string &id, const SumoXMLAttr attribute, const int value, const bool canBeZero)
check if the given int value is NOT negative
bool myRemainElements
remain elements
static bool canParse(const std::string &string)
true if a value of type T can be parsed from string
Result getResult() const
get result to indicate if this dialog was closed accepting or rejecting changes
Definition GNEDialog.cpp:96
bool postParserTasks()
run post parser tasks
std::vector< SumoXMLAttr > parseAttributes(const SumoXMLTag tag, const std::vector< std::string > &attrStrs)
parse attributes
bool checkExcludeEmpty(const SumoXMLTag tag, const std::string &id, const std::string &excludeEmpty)
check if given excludeEmpty is valid
GNEMeanDataHandler()=delete
invalidate default onstructor
bool buildEdgeMeanData(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &id, const std::string &file, const std::string &type, const SUMOTime period, const SUMOTime begin, const SUMOTime end, const bool trackVehicles, const std::vector< std::string > &writtenAttributes, const bool aggregate, const std::vector< std::string > &edgeIDs, const std::string &edgeFile, const std::string &excludeEmpty, const bool withInternal, const std::vector< std::string > &detectPersons, const double minSamples, const double maxTravelTime, const std::vector< std::string > &vTypes, const double speedThreshold)
Builds edgeMeanData.
bool checkDuplicatedMeanDataElement(const SumoXMLTag tag, const std::string &id)
check if given ID correspond to a duplicated mean data element
virtual ~GNEMeanDataHandler()
Destructor.
const bool myAllowUndoRedo
allow undo/redo
bool buildLaneMeanData(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &id, const std::string &file, const std::string &type, const SUMOTime period, const SUMOTime begin, const SUMOTime end, const bool trackVehicles, const std::vector< std::string > &writtenAttributes, const bool aggregate, const std::vector< std::string > &edgeIDs, const std::string &edgeFile, const std::string &excludeEmpty, const bool withInternal, const std::vector< std::string > &detectPersons, const double minSamples, const double maxTravelTime, const std::vector< std::string > &vTypes, const double speedThreshold)
Builds laneMeanData.
std::vector< GNEEdge * > parseEdges(const SumoXMLTag tag, const std::vector< std::string > &edgeIDs)
parse edges
GNENet * myNet
pointer to GNENet
A NBNetBuilder extended by visualisation and editing capabilities.
Definition GNENet.h:42
void incRef(const std::string &debugMsg="")
Increase reference.
MeanDataHandler(const std::string &filename)
Constructor.
static StringBijection< ExcludeEmpty > ExcludeEmptys
exclude empty values
static SequentialStringBijection Attrs
The names of SUMO-XML attributes for use in netbuild.