Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GNEDataHandler.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 data objects for netedit
19/****************************************************************************/
20
30#include <netedit/GNENet.h>
32#include <netedit/GNEUndoList.h>
33#include <netedit/GNEViewNet.h>
35
36#include "GNEDataHandler.h"
37
38// ===========================================================================
39// member method definitions
40// ===========================================================================
41
42GNEDataHandler::GNEDataHandler(GNENet* net, FileBucket* fileBucket, const bool allowUndoRedo) :
43 DataHandler(fileBucket),
44 myNet(net),
45 myAllowUndoRedo(allowUndoRedo) {
46}
47
48
50 // update options based in current buckets
52}
53
54
55bool
56GNEDataHandler::buildDataSet(const std::string& id) {
57 // first check if dataSet exist
59 return false;
60 } else if (!checkDuplicatedDataSet(id)) {
61 return false;
62 } else {
63 GNEDataSet* dataSet = new GNEDataSet(id, myNet, myFileBucket);
64 if (myAllowUndoRedo) {
65 myNet->getUndoList()->begin(dataSet, TL("add data set"));
66 myNet->getUndoList()->add(new GNEChange_DataSet(dataSet, true), true);
67 myNet->getUndoList()->end();
68 } else {
69 // insert dataSet without allowing undo/redo
71 dataSet->incRef("buildDataSet");
72 }
73 return true;
74 }
75}
76
77
78bool
80 const std::string& dataSetID, const double begin, const double end) {
81 // get dataSet
82 GNEDataSet* dataSet = myNet->getAttributeCarriers()->retrieveDataSet(dataSetID, false);
83 // first check if dataSet exist
84 if (dataSet == nullptr) {
85 // create dataset AND data interval
86 dataSet = new GNEDataSet(dataSetID, myNet, myFileBucket);
87 GNEDataInterval* dataInterval = new GNEDataInterval(dataSet, begin, end);
88 if (myAllowUndoRedo) {
89 myNet->getUndoList()->begin(dataInterval, TL("add data set and data interval"));
90 myNet->getUndoList()->add(new GNEChange_DataSet(dataSet, true), true);
91 myNet->getUndoList()->add(new GNEChange_DataInterval(dataInterval, true), true);
92 myNet->getUndoList()->end();
93 } else {
94 // insert dataSet allowing undo/redo
96 dataSet->incRef("buildDataInterval");
97 // insert dataInterval without allowing undo/redo
98 dataSet->addDataIntervalChild(dataInterval);
99 dataInterval->incRef("buildDataInterval");
100 }
101 return true;
102 } else if (dataSet->retrieveInterval(begin, end) == nullptr) {
103 GNEDataInterval* dataInterval = new GNEDataInterval(dataSet, begin, end);
104 if (myAllowUndoRedo) {
105 myNet->getUndoList()->begin(dataInterval, TL("add data interval"));
106 myNet->getUndoList()->add(new GNEChange_DataInterval(dataInterval, true), true);
107 myNet->getUndoList()->end();
108 } else {
109 // insert dataInterval without allowing undo/redo
110 dataSet->addDataIntervalChild(dataInterval);
111 dataInterval->incRef("buildDataInterval");
112 }
113 return true;
114 } else {
115 return false;
116 }
117}
118
119
120bool
121GNEDataHandler::buildEdgeData(const CommonXMLStructure::SumoBaseObject* sumoBaseObject, const std::string& edgeID,
122 const Parameterised::Map& parameters) {
123 // get dataSet
125 if (dataSet) {
126 // get interval
127 GNEDataInterval* dataInterval = dataSet->retrieveInterval(
130 if (dataInterval) {
131 // get data
132 GNEEdge* edge = myNet->getAttributeCarriers()->retrieveEdge(edgeID, false);
133 if (edge == nullptr) {
135 } else if (dataInterval->edgeRelSingleExists(edge)) {
136 return writeError(TLF("There is already a edgeRel defined in edge '%'.", edge));
137 } else {
138 // create edge data
139 GNEGenericData* edgeData = new GNEEdgeData(dataInterval, edge, parameters);
140 if (myAllowUndoRedo) {
141 myNet->getUndoList()->begin(edgeData, TL("add edge rel"));
142 myNet->getUndoList()->add(new GNEChange_GenericData(edgeData, true), true);
143 myNet->getUndoList()->end();
144 } else {
145 dataInterval->addGenericDataChild(edgeData);
146 edge->addChildElement(edgeData);
147 edgeData->incRef("buildEdgeData");
148 }
149 return true;
150 }
151 } else {
153 }
154 } else {
156 }
157}
158
159
160bool
161GNEDataHandler::buildEdgeRelationData(const CommonXMLStructure::SumoBaseObject* sumoBaseObject, const std::string& fromEdgeID,
162 const std::string& toEdgeID, const Parameterised::Map& parameters) {
163 // get dataSet
165 if (dataSet != nullptr) {
166 // get interval
167 GNEDataInterval* dataInterval = dataSet->retrieveInterval(
170 if (dataInterval != nullptr) {
171 // get data
172 GNEEdge* const fromEdge = myNet->getAttributeCarriers()->retrieveEdge(fromEdgeID, false);
173 GNEEdge* const toEdge = myNet->getAttributeCarriers()->retrieveEdge(toEdgeID, false);
174 if (fromEdge == nullptr) {
176 } else if (toEdge == nullptr) {
178 } else if (dataInterval->edgeRelExists(fromEdge, toEdge)) {
179 return writeError(TLF("There is already a edgeRel defined between '%' and '%'.", fromEdgeID, toEdgeID));
180 } else {
181 GNEGenericData* edgeData = new GNEEdgeRelData(dataInterval, fromEdge, toEdge, parameters);
182 if (myAllowUndoRedo) {
183 myNet->getUndoList()->begin(edgeData, TL("add edge rel"));
184 myNet->getUndoList()->add(new GNEChange_GenericData(edgeData, true), true);
185 myNet->getUndoList()->end();
186 } else {
187 dataInterval->addGenericDataChild(edgeData);
188 fromEdge->addChildElement(edgeData);
189 toEdge->addChildElement(edgeData);
190 edgeData->incRef("buildEdgeRelationData");
191 }
192 return true;
193 }
194 } else {
196 }
197 } else {
199 }
200}
201
202
203bool
204GNEDataHandler::buildTAZRelationData(const CommonXMLStructure::SumoBaseObject* sumoBaseObject, const std::string& fromTAZID,
205 const std::string& toTAZID, const Parameterised::Map& parameters) {
206 // get dataSet
208 if (dataSet != nullptr) {
209 // get interval
210 GNEDataInterval* dataInterval = dataSet->retrieveInterval(
213 if (dataInterval != nullptr) {
214 // get from TAZs
217 if (fromTAZ == nullptr) {
219 } else if (toTAZ == nullptr) {
221 } else if ((fromTAZ != toTAZ) && dataInterval->TAZRelExists(fromTAZ, toTAZ)) {
222 return writeError(TLF("There is already a TAZ rel defined between '%' and '%'.", fromTAZID, toTAZID));
223 } else if ((fromTAZ == toTAZ) && dataInterval->TAZRelExists(fromTAZ)) {
224 return writeError(TLF("There is already a TAZ rel defined in '%'.", toTAZID));
225 } else if (fromTAZ == toTAZ) {
226 GNEGenericData* edgeData = new GNETAZRelData(dataInterval, fromTAZ, parameters);
227 if (myAllowUndoRedo) {
228 myNet->getUndoList()->begin(edgeData, TL("add TAZ rel"));
229 myNet->getUndoList()->add(new GNEChange_GenericData(edgeData, true), true);
230 myNet->getUndoList()->end();
231 } else {
232 dataInterval->addGenericDataChild(edgeData);
233 fromTAZ->addChildElement(edgeData);
234 edgeData->incRef("buildTAZRelationData");
235 }
236 return true;
237 } else {
238 GNEGenericData* edgeData = new GNETAZRelData(dataInterval, fromTAZ, toTAZ, parameters);
239 if (myAllowUndoRedo) {
240 myNet->getUndoList()->begin(edgeData, TL("add TAZ rel"));
241 myNet->getUndoList()->add(new GNEChange_GenericData(edgeData, true), true);
242 myNet->getUndoList()->end();
243 } else {
244 dataInterval->addGenericDataChild(edgeData);
245 fromTAZ->addChildElement(edgeData);
246 toTAZ->addChildElement(edgeData);
247 edgeData->incRef("buildTAZRelationData");
248 }
249 return true;
250 }
251 } else {
253 }
254 } else {
256 }
257}
258
259
260bool
262 // retrieve data set
263 auto dataSet = myNet->getAttributeCarriers()->retrieveDataSet(id, false);
264 // if demand exist, check if overwrite (delete)
265 if (dataSet) {
267 // delete data element (and all of their childrens)
268 myNet->deleteDataSet(dataSet, myNet->getUndoList());
269 } else if (myRemainElements) {
270 // duplicated dataset
272 } else {
273 // open overwrite dialog
274 GNEOverwriteElement overwriteElementDialog(this, dataSet);
275 // continue depending of result
276 if (overwriteElementDialog.getResult() == GNEOverwriteElement::Result::ACCEPT) {
277 // delete data element (and all of their childrens)
278 myNet->deleteDataSet(dataSet, myNet->getUndoList());
279 } else if (overwriteElementDialog.getResult() == GNEOverwriteElement::Result::CANCEL) {
280 // duplicated dataset
282 } else {
283 return false;
284 }
285 }
286 }
287 return true;
288}
289
290/****************************************************************************/
#define TL(string)
Definition MsgHandler.h:304
#define TLF(string,...)
Definition MsgHandler.h:306
@ SUMO_TAG_EDGEREL
a relation between two edges
@ SUMO_TAG_DATAINTERVAL
@ SUMO_TAG_TAZ
a traffic assignment zone
@ GNE_TAG_EDGEREL_SINGLE
@ SUMO_TAG_DATASET
@ SUMO_TAG_TAZREL
a relation between two TAZs
@ SUMO_TAG_EDGE
begin/end of the description of an edge
@ SUMO_ATTR_BEGIN
weights: time range begin
@ SUMO_ATTR_END
weights: time range end
@ SUMO_ATTR_ID
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 writeErrorInvalidParent(const SumoXMLTag tag, const std::string &id, const std::vector< SumoXMLTag > parentTags, const std::string &parentID)
write error "invalid parent element" giving ids of current and parent 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 myRemainElements
remain elements
SumoBaseObject * getParentSumoBaseObject() const
get pointer to mySumoBaseObjectParent SumoBaseObject (if is null, then is the root)
double getDoubleAttribute(const SumoXMLAttr attr) const
get double attribute
const std::string & getStringAttribute(const SumoXMLAttr attr) const
get string attribute
The XML-Handler for network loading.
Definition DataHandler.h:36
GNEApplicationWindowHelper::FileBucketHandler * getFileBucketHandler() const
get file bucket handler
~GNEDataHandler()
Destructor.
bool buildDataInterval(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &dataSetID, const double begin, const double end)
Builds DataInterval.
bool buildTAZRelationData(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &fromTAZID, const std::string &toTAZID, const Parameterised::Map &parameters)
Builds TAZRelationData.
bool buildDataSet(const std::string &id)
Builds DataSet (exclusive of netedit)
const bool myAllowUndoRedo
allow undo/redo
bool buildEdgeRelationData(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &fromEdgeID, const std::string &toEdgeID, const Parameterised::Map &parameters)
Builds edgeRelationData.
bool buildEdgeData(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &edgeID, const Parameterised::Map &parameters)
Builds edgeData.
bool checkDuplicatedDataSet(const std::string &id)
check if given ID correspond to a duplicated dataSet
GNENet * myNet
pointer to GNENet
GNEDataHandler()=delete
invalidate default constructor
bool edgeRelExists(const GNEEdge *fromEdge, const GNEEdge *toEdge) const
check if there is already a edgeRel defined between two edges
bool TAZRelExists(const GNEAdditional *TAZ) const
check if there is already a TAZRel defined in one TAZ
bool edgeRelSingleExists(const GNEEdge *edge) const
check if there is already a edgeRel single defined in the given edge
void addGenericDataChild(GNEGenericData *genericData)
add generic data child
GNEDataInterval * retrieveInterval(const double begin, const double end) const
return interval
void addDataIntervalChild(GNEDataInterval *dataInterval)
add data interval child
Result getResult() const
get result to indicate if this dialog was closed accepting or rejecting changes
An Element which don't belong to GNENet but has influence in the simulation.
Definition GNEEdgeData.h:32
An Element which don't belong to GNENet but has influence in the simulation.
void addChildElement(ChildType *element)
add child without updating parent (ONLY used if we're creating elements without undo-redo)
void insertDataSet(GNEDataSet *dataSet)
Insert a data set in container.
GNEAdditional * retrieveAdditional(SumoXMLTag type, const std::string &id, bool hardFail=true) const
Returns the named additional.
GNEDataSet * retrieveDataSet(const std::string &id, bool hardFail=true) const
Returns the named data set.
GNEEdge * retrieveEdge(const std::string &id, bool hardFail=true) const
get edge by id
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
void deleteDataSet(GNEDataSet *dataSet, GNEUndoList *undoList)
remove data set
Definition GNENet.cpp:796
GNEUndoList * getUndoList() const
get undo list(used for simplify code)
Definition GNENet.cpp:156
void incRef(const std::string &debugMsg="")
Increase reference.
An Element which don't belong to GNENet but has influence in the simulation.
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...
std::map< std::string, std::string > Map
parameters map