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-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/****************************************************************************/
18// Builds meanData objects for netedit
19/****************************************************************************/
20
21// ===========================================================================
22// included modules
23// ===========================================================================
24#include <config.h>
25
26
29#include <netedit/GNEViewNet.h>
30#include <netedit/GNENet.h>
31#include <netedit/GNEUndoList.h>
33
34#include "GNEMeanDataHandler.h"
35
36
37// ===========================================================================
38// member method definitions
39// ===========================================================================
40
41GNEMeanDataHandler::GNEMeanDataHandler(GNENet* net, const bool allowUndoRedo, const bool overwrite) :
42 myNet(net),
43 myAllowUndoRedo(allowUndoRedo),
44 myOverwrite(overwrite) {
45}
46
47
49
50
51bool
53 // nothing to do
54 return true;
55}
56
57
58bool
59GNEMeanDataHandler::buildEdgeMeanData(const CommonXMLStructure::SumoBaseObject* /*sumoBaseObject*/, const std::string& id,
60 const std::string& file, SUMOTime period, SUMOTime begin, SUMOTime end, const bool trackVehicles,
61 const std::vector<std::string>& writtenAttributes, const bool aggregate, const std::vector<std::string>& edgeIDs,
62 const std::string& edgeFile, std::string excludeEmpty, const bool withInternal,
63 const std::vector<std::string>& detectPersons, const double minSamples, const double maxTravelTime,
64 const std::vector<std::string>& vTypes, const double speedThreshold) {
65 // parse attributes
66 const auto edges = parseEdges(SUMO_TAG_MEANDATA_EDGE, edgeIDs);
67 // parse edges
68 const auto attributes = parseAttributes(SUMO_TAG_MEANDATA_EDGE, writtenAttributes);
69 // check if meanData edge exists
71 return false;
73 return false;
74 } else if ((edges.size() == edgeIDs.size()) && (attributes.size() == writtenAttributes.size())) {
75 GNEMeanData* edgeMeanData = new GNEMeanData(myNet, SUMO_TAG_MEANDATA_EDGE, id, file, period, begin, end,
76 trackVehicles, attributes, aggregate, edgeIDs, edgeFile, excludeEmpty, withInternal,
77 detectPersons, minSamples, maxTravelTime, vTypes, speedThreshold);
78 if (myAllowUndoRedo) {
79 myNet->getViewNet()->getUndoList()->begin(edgeMeanData, TL("add meanDataEdge"));
80 myNet->getViewNet()->getUndoList()->add(new GNEChange_MeanData(edgeMeanData, true), true);
82 } else {
84 edgeMeanData->incRef("buildEdgeMeanData");
85 }
86 return true;
87 } else {
88 return false;
89 }
90}
91
92
93bool
94GNEMeanDataHandler::buildLaneMeanData(const CommonXMLStructure::SumoBaseObject* /*sumoBaseObject*/, const std::string& id,
95 const std::string& file, SUMOTime period, SUMOTime begin, SUMOTime end, const bool trackVehicles,
96 const std::vector<std::string>& writtenAttributes, const bool aggregate, const std::vector<std::string>& edgeIDs,
97 const std::string& edgeFile, std::string excludeEmpty, const bool withInternal,
98 const std::vector<std::string>& detectPersons, const double minSamples, const double maxTravelTime,
99 const std::vector<std::string>& vTypes, const double speedThreshold) {
100 // parse attributes
101 const auto edges = parseEdges(SUMO_TAG_MEANDATA_LANE, edgeIDs);
102 // parse edges
103 const auto attributes = parseAttributes(SUMO_TAG_MEANDATA_LANE, writtenAttributes);
104 // check if meanData edge exists
106 return false;
108 return false;
109 } else if ((edges.size() == edgeIDs.size()) && (attributes.size() == writtenAttributes.size())) {
110 GNEMeanData* edgeMeanData = new GNEMeanData(myNet, SUMO_TAG_MEANDATA_LANE, id, file, period, begin, end,
111 trackVehicles, attributes, aggregate, edgeIDs, edgeFile, excludeEmpty, withInternal,
112 detectPersons, minSamples, maxTravelTime, vTypes, speedThreshold);
113 if (myAllowUndoRedo) {
114 myNet->getViewNet()->getUndoList()->begin(edgeMeanData, TL("add meanDataLane"));
115 myNet->getViewNet()->getUndoList()->add(new GNEChange_MeanData(edgeMeanData, true), true);
117 } else {
118 myNet->getAttributeCarriers()->insertMeanData(edgeMeanData);
119 edgeMeanData->incRef("buildEdgeMeanData");
120 }
121 return true;
122 } else {
123 return false;
124 }
125}
126
127
128std::vector<GNEEdge*>
129GNEMeanDataHandler::parseEdges(const SumoXMLTag tag, const std::vector<std::string>& edgeIDs) {
130 std::vector<GNEEdge*> edges;
131 for (const auto& edgeID : edgeIDs) {
132 GNEEdge* edge = myNet->getAttributeCarriers()->retrieveEdge(edgeID, false);
133 // empty edges aren't allowed. If edge is empty, write error, clear edges and stop
134 if (edge == nullptr) {
135 writeError(TLF("Could not build % in netedit", toString(tag)) + std::string("; ") + TL("Edge doesn't exist."));
136 edges.clear();
137 return edges;
138 } else {
139 edges.push_back(edge);
140 }
141 }
142 return edges;
143}
144
145
146std::vector<SumoXMLAttr>
147GNEMeanDataHandler::parseAttributes(const SumoXMLTag tag, const std::vector<std::string>& attrStrs) {
148 std::vector<SumoXMLAttr> attrs;
149 for (const auto& attrStr : attrStrs) {
150 if (SUMOXMLDefinitions::Attrs.hasString(attrStr)) {
151 attrs.push_back(static_cast<SumoXMLAttr>(SUMOXMLDefinitions::Attrs.get(attrStr)));
152 } else {
153 writeError(TLF("Could not build % in netedit", toString(tag)) + std::string("; ") + TLF("Attribute '%' doesn't exist.", attrStr));
154 attrs.clear();
155 return attrs;
156 }
157 }
158 return attrs;
159}
160
161
162bool
164 // retrieve meanData element
165 auto meanDataElement = myNet->getAttributeCarriers()->retrieveMeanData(tag, id, false);
166 // if meanData exist, check if overwrite (delete)
167 if (meanDataElement) {
168 if (!myAllowUndoRedo) {
169 // only overwrite if allow undo-redo
171 } else if (myOverwrite) {
172 // delete meanData element (and all of their childrens)
173 myNet->deleteMeanData(meanDataElement, myNet->getViewNet()->getUndoList());
174 } else {
175 // duplicated demand
177 }
178 }
179 return true;
180}
181
182/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define TL(string)
Definition MsgHandler.h:315
#define TLF(string,...)
Definition MsgHandler.h:317
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.
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 writeErrorDuplicated(const SumoXMLTag tag, const std::string &id, const SumoXMLTag checkedTag)
write error "duplicated additional"
bool checkValidAdditionalID(const SumoXMLTag tag, const std::string &value)
check if the given additional ID is valid
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.
GNEMeanDataHandler(GNENet *net, const bool allowUndoRedo, const bool overwrite)
Constructor.
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)
const bool myOverwrite
check if overwrite
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
An Element which don't belong to GNENet but has influence in the simulation.
Definition GNEMeanData.h:33
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:779
GNENetHelper::AttributeCarriers * getAttributeCarriers() const
get all attribute carriers used in this net
Definition GNENet.cpp:127
GNEViewNet * getViewNet() const
get view net
Definition GNENet.cpp:2163
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
static SequentialStringBijection Attrs
The names of SUMO-XML attributes for use in netbuild.