Eclipse SUMO - Simulation of Urban MObility
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
23#include <netedit/GNEViewNet.h>
24#include <netedit/GNENet.h>
25#include <netedit/GNEUndoList.h>
27
28#include "GNEMeanDataHandler.h"
29
30// ===========================================================================
31// member method definitions
32// ===========================================================================
33
34GNEMeanDataHandler::GNEMeanDataHandler(GNENet* net, const std::string& filename, const bool allowUndoRedo, const bool overwrite) :
35 MeanDataHandler(filename),
36 myNet(net),
37 myAllowUndoRedo(allowUndoRedo),
38 myOverwrite(overwrite) {
39}
40
41
43
44
45bool
47 // nothing to do
48 return true;
49}
50
51
52bool
53GNEMeanDataHandler::buildEdgeMeanData(const CommonXMLStructure::SumoBaseObject* /*sumoBaseObject*/, const std::string& id,
54 const std::string& file, SUMOTime period, SUMOTime begin, SUMOTime end, const bool trackVehicles,
55 const std::vector<std::string>& writtenAttributes, const bool aggregate, const std::vector<std::string>& edgeIDs,
56 const std::string& edgeFile, std::string excludeEmpty, const bool withInternal,
57 const std::vector<std::string>& detectPersons, const double minSamples, const double maxTravelTime,
58 const std::vector<std::string>& vTypes, const double speedThreshold) {
59 // parse attributes
60 const auto edges = parseEdges(SUMO_TAG_MEANDATA_EDGE, edgeIDs);
61 // parse edges
62 const auto attributes = parseAttributes(SUMO_TAG_MEANDATA_EDGE, writtenAttributes);
63 // check if meanData edge exists
65 return false;
67 return false;
68 } else if ((period != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_PERIOD, period, true)) {
69 return false;
70 } else if ((begin != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_BEGIN, begin, true)) {
71 return false;
72 } else if ((end != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_END, end, true)) {
73 return false;
74 } else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_MAX_TRAVELTIME, maxTravelTime, true)) {
75 return false;
76 } else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_MIN_SAMPLES, minSamples, true)) {
77 return false;
78 } else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_HALTING_SPEED_THRESHOLD, speedThreshold, true)) {
79 return false;
80 } else if (!checkExcludeEmpty(SUMO_TAG_MEANDATA_EDGE, id, excludeEmpty)) {
81 return false;
82 } else if ((edges.size() == edgeIDs.size()) && (attributes.size() == writtenAttributes.size())) {
83 GNEMeanData* edgeMeanData = new GNEMeanData(SUMO_TAG_MEANDATA_EDGE, id, myNet, myFilename, file, period, begin, end,
84 trackVehicles, attributes, aggregate, edgeIDs, edgeFile, excludeEmpty, withInternal,
85 detectPersons, minSamples, maxTravelTime, vTypes, speedThreshold);
86 if (myAllowUndoRedo) {
87 myNet->getViewNet()->getUndoList()->begin(edgeMeanData, TL("add meanDataEdge"));
88 myNet->getViewNet()->getUndoList()->add(new GNEChange_MeanData(edgeMeanData, true), true);
90 } else {
92 edgeMeanData->incRef("buildEdgeMeanData");
93 }
94 return true;
95 } else {
96 return false;
97 }
98}
99
100
101bool
102GNEMeanDataHandler::buildLaneMeanData(const CommonXMLStructure::SumoBaseObject* /*sumoBaseObject*/, const std::string& id,
103 const std::string& file, SUMOTime period, SUMOTime begin, SUMOTime end, const bool trackVehicles,
104 const std::vector<std::string>& writtenAttributes, const bool aggregate, const std::vector<std::string>& edgeIDs,
105 const std::string& edgeFile, std::string excludeEmpty, const bool withInternal,
106 const std::vector<std::string>& detectPersons, const double minSamples, const double maxTravelTime,
107 const std::vector<std::string>& vTypes, 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, myFilename, file, period, begin, end,
133 trackVehicles, attributes, aggregate, edgeIDs, edgeFile, excludeEmpty, withInternal,
134 detectPersons, minSamples, maxTravelTime, vTypes, speedThreshold);
135 if (myAllowUndoRedo) {
136 myNet->getViewNet()->getUndoList()->begin(edgeMeanData, TL("add meanDataLane"));
137 myNet->getViewNet()->getUndoList()->add(new GNEChange_MeanData(edgeMeanData, true), true);
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) {
190 if (!myAllowUndoRedo) {
191 // only overwrite if allow undo-redo
193 } else if (myOverwrite) {
194 // delete meanData element (and all of their childrens)
195 myNet->deleteMeanData(meanDataElement, myNet->getViewNet()->getUndoList());
196 } else {
197 // duplicated demand
199 }
200 }
201 return true;
202}
203
204
205bool
206GNEMeanDataHandler::checkExcludeEmpty(const SumoXMLTag tag, const std::string& id, const std::string& excludeEmpty) {
207 if (GNEAttributeCarrier::canParse<bool>(excludeEmpty)) {
208 return true;
209 } else if (excludeEmpty == SUMOXMLDefinitions::ExcludeEmptys.getString(ExcludeEmpty::DEFAULTS)) {
210 return true;
211 } else {
212 return writeError(TLF("Could not build % with ID '%' in netedit; Invalid value '%' for %.", toString(tag), id, excludeEmpty, toString(SUMO_ATTR_EXCLUDE_EMPTY)));
213 }
214}
215
216/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define TL(string)
Definition MsgHandler.h:301
#define TLF(string,...)
Definition MsgHandler.h:303
#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
@ SUMO_TAG_DATASET
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 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
A road/street connecting two junctions (netedit-version)
Definition GNEEdge.h:53
bool buildLaneMeanData(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &id, const std::string &file, SUMOTime period, SUMOTime begin, SUMOTime end, const bool trackVehicles, const std::vector< std::string > &writtenAttributes, const bool aggregate, const std::vector< std::string > &edgeIDs, const std::string &edgeFile, 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.
bool postParserTasks()
run post parser tasks
std::vector< SumoXMLAttr > parseAttributes(const SumoXMLTag tag, const std::vector< std::string > &attrStrs)
parse attributes
bool buildEdgeMeanData(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &id, const std::string &file, SUMOTime period, SUMOTime begin, SUMOTime end, const bool trackVehicles, const std::vector< std::string > &writtenAttributes, const bool aggregate, const std::vector< std::string > &edgeIDs, const std::string &edgeFile, 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)
bool checkExcludeEmpty(const SumoXMLTag tag, const std::string &id, const std::string &excludeEmpty)
check if given excludeEmpty is valid
const bool myOverwrite
check if overwrite
GNEMeanDataHandler()=delete
invalidate default onstructor
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
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
A NBNetBuilder extended by visualisation and editing capabilities.
Definition GNENet.h:42
void deleteMeanData(GNEMeanData *meanData, GNEUndoList *undoList)
remove generic data
Definition GNENet.cpp:830
GNENetHelper::AttributeCarriers * getAttributeCarriers() const
get all attribute carriers used in this net
Definition GNENet.cpp:146
GNEViewNet * getViewNet() const
get view net
Definition GNENet.cpp:2194
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...
GNEUndoList * getUndoList() const
get the undoList object
The XML-Handler for network loading.
static StringBijection< ExcludeEmpty > ExcludeEmptys
exclude empty values
static SequentialStringBijection Attrs
The names of SUMO-XML attributes for use in netbuild.