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
25#include <netedit/GNENet.h>
27#include <netedit/GNEUndoList.h>
28#include <netedit/GNEViewNet.h>
30
31#include "GNEMeanDataHandler.h"
32
33// ===========================================================================
34// member method definitions
35// ===========================================================================
36
37GNEMeanDataHandler::GNEMeanDataHandler(GNENet* net, FileBucket* fileBucket, const bool allowUndoRedo) :
38 MeanDataHandler(fileBucket),
39 myNet(net),
40 myAllowUndoRedo(allowUndoRedo) {
41}
42
43
45 // update options based in current buckets
47}
48
49
50bool
51GNEMeanDataHandler::buildEdgeMeanData(const CommonXMLStructure::SumoBaseObject* /*sumoBaseObject*/, const std::string& id,
52 const std::string& file, const std::string& type, const SUMOTime period, const SUMOTime begin,
53 const SUMOTime end, const bool trackVehicles, const std::vector<std::string>& writtenAttributes,
54 const bool aggregate, const std::vector<std::string>& edgeIDs, const std::string& edgeFile,
55 const std::string& excludeEmpty, const bool withInternal, const std::vector<std::string>& detectPersons,
56 const double minSamples, const double maxTravelTime, const std::vector<std::string>& vTypes,
57 const double speedThreshold) {
58 // parse attributes
59 const auto edges = parseEdges(SUMO_TAG_MEANDATA_EDGE, edgeIDs);
60 // parse edges
61 const auto attributes = parseAttributes(SUMO_TAG_MEANDATA_EDGE, writtenAttributes);
62 // check if meanData edge exists
64 return false;
66 return false;
67 } else if ((period != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_PERIOD, period, true)) {
68 return false;
69 } else if ((begin != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_BEGIN, begin, true)) {
70 return false;
71 } else if ((end != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_END, end, true)) {
72 return false;
73 } else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_MAX_TRAVELTIME, maxTravelTime, true)) {
74 return false;
75 } else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_MIN_SAMPLES, minSamples, true)) {
76 return false;
77 } else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_HALTING_SPEED_THRESHOLD, speedThreshold, true)) {
78 return false;
79 } else if (!checkExcludeEmpty(SUMO_TAG_MEANDATA_EDGE, id, excludeEmpty)) {
80 return false;
81 } else if ((edges.size() == edgeIDs.size()) && (attributes.size() == writtenAttributes.size())) {
82 GNEMeanData* edgeMeanData = new GNEMeanData(SUMO_TAG_MEANDATA_EDGE, id, myNet, myFileBucket, file, type, period, begin, end,
83 trackVehicles, attributes, aggregate, edgeIDs, edgeFile, excludeEmpty, withInternal,
84 detectPersons, minSamples, maxTravelTime, vTypes, speedThreshold);
85 if (myAllowUndoRedo) {
86 myNet->getUndoList()->begin(edgeMeanData, TL("add meanDataEdge"));
87 myNet->getUndoList()->add(new GNEChange_MeanData(edgeMeanData, true), true);
88 myNet->getUndoList()->end();
89 } else {
91 edgeMeanData->incRef("buildEdgeMeanData");
92 }
93 return true;
94 } else {
95 return false;
96 }
97}
98
99
100bool
101GNEMeanDataHandler::buildLaneMeanData(const CommonXMLStructure::SumoBaseObject* /*sumoBaseObject*/, const std::string& id,
102 const std::string& file, const std::string& type, const SUMOTime period, const SUMOTime begin,
103 const SUMOTime end, const bool trackVehicles, const std::vector<std::string>& writtenAttributes,
104 const bool aggregate, const std::vector<std::string>& edgeIDs, const std::string& edgeFile,
105 const std::string& excludeEmpty, const bool withInternal, const std::vector<std::string>& detectPersons,
106 const double minSamples, const double maxTravelTime, const std::vector<std::string>& vTypes,
107 const double speedThreshold) {
108 // parse attributes
109 const auto edges = parseEdges(SUMO_TAG_MEANDATA_LANE, edgeIDs);
110 // parse edges
111 const auto attributes = parseAttributes(SUMO_TAG_MEANDATA_LANE, writtenAttributes);
112 // check if meanData edge exists
114 return false;
116 return false;
117 } else if ((period != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_PERIOD, period, true)) {
118 return false;
119 } else if ((begin != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_BEGIN, begin, true)) {
120 return false;
121 } else if ((end != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_END, end, true)) {
122 return false;
123 } else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_MAX_TRAVELTIME, maxTravelTime, true)) {
124 return false;
125 } else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_MIN_SAMPLES, minSamples, true)) {
126 return false;
127 } else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_HALTING_SPEED_THRESHOLD, speedThreshold, true)) {
128 return false;
129 } else if (!checkExcludeEmpty(SUMO_TAG_MEANDATA_EDGE, id, excludeEmpty)) {
130 return false;
131 } else if ((edges.size() == edgeIDs.size()) && (attributes.size() == writtenAttributes.size())) {
132 GNEMeanData* edgeMeanData = new GNEMeanData(SUMO_TAG_MEANDATA_LANE, id, myNet, myFileBucket, file, type, period, begin, end,
133 trackVehicles, attributes, aggregate, edgeIDs, edgeFile, excludeEmpty, withInternal,
134 detectPersons, minSamples, maxTravelTime, vTypes, speedThreshold);
135 if (myAllowUndoRedo) {
136 myNet->getUndoList()->begin(edgeMeanData, TL("add meanDataLane"));
137 myNet->getUndoList()->add(new GNEChange_MeanData(edgeMeanData, true), true);
138 myNet->getUndoList()->end();
139 } else {
140 myNet->getAttributeCarriers()->insertMeanData(edgeMeanData);
141 edgeMeanData->incRef("buildEdgeMeanData");
142 }
143 return true;
144 } else {
145 return false;
146 }
147}
148
149
150std::vector<GNEEdge*>
151GNEMeanDataHandler::parseEdges(const SumoXMLTag tag, const std::vector<std::string>& edgeIDs) {
152 std::vector<GNEEdge*> edges;
153 for (const auto& edgeID : edgeIDs) {
154 GNEEdge* edge = myNet->getAttributeCarriers()->retrieveEdge(edgeID, false);
155 // empty edges aren't allowed. If edge is empty, write error, clear edges and stop
156 if (edge == nullptr) {
157 writeError(TLF("Could not build % in netedit", toString(tag)) + std::string("; ") + TL("Edge doesn't exist."));
158 edges.clear();
159 return edges;
160 } else {
161 edges.push_back(edge);
162 }
163 }
164 return edges;
165}
166
167
168std::vector<SumoXMLAttr>
169GNEMeanDataHandler::parseAttributes(const SumoXMLTag tag, const std::vector<std::string>& attrStrs) {
170 std::vector<SumoXMLAttr> attrs;
171 for (const auto& attrStr : attrStrs) {
172 if (SUMOXMLDefinitions::Attrs.hasString(attrStr)) {
173 attrs.push_back(static_cast<SumoXMLAttr>(SUMOXMLDefinitions::Attrs.get(attrStr)));
174 } else {
175 writeError(TLF("Could not build % in netedit", toString(tag)) + std::string("; ") + TLF("Attribute '%' doesn't exist.", attrStr));
176 attrs.clear();
177 return attrs;
178 }
179 }
180 return attrs;
181}
182
183
184bool
186 // retrieve meanData element
187 auto meanDataElement = myNet->getAttributeCarriers()->retrieveMeanData(tag, id, false);
188 // if meanData exist, check if overwrite (delete)
189 if (meanDataElement) {
191 // delete meanData element (and all of their childrens)
192 myNet->deleteMeanData(meanDataElement, myNet->getUndoList());
193 } else if (myRemainElements) {
194 // duplicated dataset
195 return writeWarningDuplicated(tag, meanDataElement->getID(), meanDataElement->getTagProperty()->getTag());
196 } else {
197 // open overwrite dialog
198 GNEOverwriteElement overwriteElementDialog(this, meanDataElement);
199 // continue depending of result
200 if (overwriteElementDialog.getResult() == GNEOverwriteElement::Result::ACCEPT) {
201 // delete meanData element (and all of their childrens)
202 myNet->deleteMeanData(meanDataElement, myNet->getUndoList());
203 } else if (overwriteElementDialog.getResult() == GNEOverwriteElement::Result::CANCEL) {
204 // duplicated demand
205 return writeWarningDuplicated(tag, meanDataElement->getID(), meanDataElement->getTagProperty()->getTag());
206 } else {
207 return false;
208 }
209 }
210 }
211 return true;
212}
213
214
215bool
216GNEMeanDataHandler::checkExcludeEmpty(const SumoXMLTag tag, const std::string& id, const std::string& excludeEmpty) {
217 if (GNEAttributeCarrier::canParse<bool>(excludeEmpty)) {
218 return true;
219 } else if (excludeEmpty == SUMOXMLDefinitions::ExcludeEmptys.getString(ExcludeEmpty::DEFAULTS)) {
220 return true;
221 } else {
222 return writeError(TLF("Could not build % with ID '%' in netedit; Invalid value '%' for %.", toString(tag), id, excludeEmpty, toString(SUMO_ATTR_EXCLUDE_EMPTY)));
223 }
224}
225
226/****************************************************************************/
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
FileBucket * myFileBucket
fileBucket
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
GNEApplicationWindowHelper::FileBucketHandler * getFileBucketHandler() const
get file bucket handler
Result getResult() const
get result to indicate if this dialog was closed accepting or rejecting changes
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
GNEMeanData * retrieveMeanData(SumoXMLTag type, const std::string &id, bool hardFail=true) const
Returns the named meanData.
void insertMeanData(GNEMeanData *meanData)
Insert a meanData element in container.
GNEEdge * retrieveEdge(const std::string &id, bool hardFail=true) const
get edge by id
void deleteMeanData(GNEMeanData *meanData, GNEUndoList *undoList)
remove generic data
Definition GNENet.cpp:852
GNENetHelper::AttributeCarriers * getAttributeCarriers() const
get all attribute carriers used in this net
Definition GNENet.cpp:174
GNEApplicationWindow * getGNEApplicationWindow() const
get tag properties database
Definition GNENet.cpp:138
GNEUndoList * getUndoList() const
get undo list(used for simplify code)
Definition GNENet.cpp:156
void incRef(const std::string &debugMsg="")
Increase reference.
void end()
End undo command sub-group. If the sub-group is still empty, it will be deleted; otherwise,...
void begin(GUIIcon icon, const std::string &description)
Begin undo command sub-group with current supermode. This begins a new group of commands that are tre...
void add(GNEChange *command, bool doit=false, bool merge=true)
Add new command, executing it if desired. The new command will be merged with the previous command if...
static StringBijection< ExcludeEmpty > ExcludeEmptys
exclude empty values
static SequentialStringBijection Attrs
The names of SUMO-XML attributes for use in netbuild.