Eclipse SUMO - Simulation of Urban MObility
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
28#include <netedit/GNEViewNet.h>
29#include <netedit/GNENet.h>
30#include <netedit/GNEUndoList.h>
32
33#include "GNEDataHandler.h"
34
35// ===========================================================================
36// member method definitions
37// ===========================================================================
38
39GNEDataHandler::GNEDataHandler(GNENet* net, const std::string& file, const bool allowUndoRedo, const bool overwrite) :
40 DataHandler(file),
41 myNet(net),
42 myAllowUndoRedo(allowUndoRedo),
43 myOverwrite(overwrite) {
44}
45
46
48
49
50bool
52 // nothing to do
53 return true;
54}
55
56
57bool
58GNEDataHandler::buildDataSet(const std::string& id) {
59 // first check if dataSet exist
61 return false;
62 } else if (!checkDuplicatedDataSet(id)) {
63 return false;
64 } else {
65 GNEDataSet* dataSet = new GNEDataSet(id, myNet, myFilename);
66 if (myAllowUndoRedo) {
67 myNet->getViewNet()->getUndoList()->begin(dataSet, TL("add data set"));
68 myNet->getViewNet()->getUndoList()->add(new GNEChange_DataSet(dataSet, true), true);
70 } else {
71 // insert dataSet without allowing undo/redo
73 dataSet->incRef("buildDataSet");
74 }
75 return true;
76 }
77}
78
79
80bool
82 const std::string& dataSetID, const double begin, const double end) {
83 // get dataSet
84 GNEDataSet* dataSet = myNet->getAttributeCarriers()->retrieveDataSet(dataSetID, false);
85 // first check if dataSet exist
86 if (dataSet == nullptr) {
87 // create dataset AND data interval
88 dataSet = new GNEDataSet(dataSetID, myNet, myFilename);
89 GNEDataInterval* dataInterval = new GNEDataInterval(dataSet, begin, end);
90 if (myAllowUndoRedo) {
91 myNet->getViewNet()->getUndoList()->begin(dataInterval, TL("add data set and data interval"));
92 myNet->getViewNet()->getUndoList()->add(new GNEChange_DataSet(dataSet, true), true);
93 myNet->getViewNet()->getUndoList()->add(new GNEChange_DataInterval(dataInterval, true), true);
95 } else {
96 // insert dataSet allowing undo/redo
98 dataSet->incRef("buildDataInterval");
99 // insert dataInterval without allowing undo/redo
100 dataSet->addDataIntervalChild(dataInterval);
101 dataInterval->incRef("buildDataInterval");
102 }
103 return true;
104 } else if (dataSet->retrieveInterval(begin, end) == nullptr) {
105 GNEDataInterval* dataInterval = new GNEDataInterval(dataSet, begin, end);
106 if (myAllowUndoRedo) {
107 myNet->getViewNet()->getUndoList()->begin(dataInterval, TL("add data interval"));
108 myNet->getViewNet()->getUndoList()->add(new GNEChange_DataInterval(dataInterval, true), true);
110 } else {
111 // insert dataInterval without allowing undo/redo
112 dataSet->addDataIntervalChild(dataInterval);
113 dataInterval->incRef("buildDataInterval");
114 }
115 return true;
116 } else {
117 return false;
118 }
119}
120
121
122bool
123GNEDataHandler::buildEdgeData(const CommonXMLStructure::SumoBaseObject* sumoBaseObject, const std::string& edgeID,
124 const Parameterised::Map& parameters) {
125 // get dataSet
127 if (dataSet != nullptr) {
128 // get interval
129 GNEDataInterval* dataInterval = dataSet->retrieveInterval(
132 if (dataInterval != nullptr) {
133 // get data
134 GNEEdge* edge = myNet->getAttributeCarriers()->retrieveEdge(edgeID, false);
135 if (edge) {
136 GNEGenericData* edgeData = new GNEEdgeData(dataInterval, edge, parameters);
137 if (myAllowUndoRedo) {
138 myNet->getViewNet()->getUndoList()->begin(edgeData, TL("add edge rel"));
139 myNet->getViewNet()->getUndoList()->add(new GNEChange_GenericData(edgeData, true), true);
141 } else {
142 dataInterval->addGenericDataChild(edgeData);
143 edge->addChildElement(edgeData);
144 edgeData->incRef("buildEdgeData");
145 }
146 return true;
147 } else {
149 }
150 } else {
152 }
153 } else {
155 }
156}
157
158
159bool
160GNEDataHandler::buildEdgeRelationData(const CommonXMLStructure::SumoBaseObject* sumoBaseObject, const std::string& fromEdgeID,
161 const std::string& toEdgeID, const Parameterised::Map& parameters) {
162 // get dataSet
164 if (dataSet != nullptr) {
165 // get interval
166 GNEDataInterval* dataInterval = dataSet->retrieveInterval(
169 if (dataInterval != nullptr) {
170 // get data
171 GNEEdge* const fromEdge = myNet->getAttributeCarriers()->retrieveEdge(fromEdgeID, false);
172 GNEEdge* const toEdge = myNet->getAttributeCarriers()->retrieveEdge(toEdgeID, false);
173 if (fromEdge == nullptr) {
175 } else if (toEdge == nullptr) {
177 } else {
178 // avoid duplicated edgeRel in the same interval
179 if (dataInterval->edgeRelExists(fromEdge, toEdge)) {
180 return writeError(TLF("There is already a edgeRel defined between '%' and '%'.", fromEdgeID, toEdgeID));
181 } else {
182 GNEGenericData* edgeData = new GNEEdgeRelData(dataInterval, fromEdge, toEdge, parameters);
183 if (myAllowUndoRedo) {
184 myNet->getViewNet()->getUndoList()->begin(edgeData, TL("add edge rel"));
185 myNet->getViewNet()->getUndoList()->add(new GNEChange_GenericData(edgeData, true), true);
187 } else {
188 dataInterval->addGenericDataChild(edgeData);
189 fromEdge->addChildElement(edgeData);
190 toEdge->addChildElement(edgeData);
191 edgeData->incRef("buildEdgeRelationData");
192 }
193 return true;
194 }
195 }
196 } else {
198 }
199 } else {
201 }
202}
203
204
205bool
206GNEDataHandler::buildTAZRelationData(const CommonXMLStructure::SumoBaseObject* sumoBaseObject, const std::string& fromTAZID,
207 const std::string& toTAZID, const Parameterised::Map& parameters) {
208 // get dataSet
210 if (dataSet != nullptr) {
211 // get interval
212 GNEDataInterval* dataInterval = dataSet->retrieveInterval(
215 if (dataInterval != nullptr) {
216 // get from TAZs
219 if (fromTAZ == nullptr) {
221 } else if (toTAZ == nullptr) {
223 } else if ((fromTAZ != toTAZ) && dataInterval->TAZRelExists(fromTAZ, toTAZ)) {
224 return writeError(TLF("There is already a TAZ rel defined between '%' and '%'.", fromTAZID, toTAZID));
225 } else if ((fromTAZ == toTAZ) && dataInterval->TAZRelExists(fromTAZ)) {
226 return writeError(TLF("There is already a TAZ rel defined in '%'.", toTAZID));
227 } else if (fromTAZ == toTAZ) {
228 GNEGenericData* edgeData = new GNETAZRelData(dataInterval, fromTAZ, parameters);
229 if (myAllowUndoRedo) {
230 myNet->getViewNet()->getUndoList()->begin(edgeData, TL("add TAZ rel"));
231 myNet->getViewNet()->getUndoList()->add(new GNEChange_GenericData(edgeData, true), true);
233 } else {
234 dataInterval->addGenericDataChild(edgeData);
235 fromTAZ->addChildElement(edgeData);
236 edgeData->incRef("buildTAZRelationData");
237 }
238 return true;
239 } else {
240 GNEGenericData* edgeData = new GNETAZRelData(dataInterval, fromTAZ, toTAZ, parameters);
241 if (myAllowUndoRedo) {
242 myNet->getViewNet()->getUndoList()->begin(edgeData, TL("add TAZ rel"));
243 myNet->getViewNet()->getUndoList()->add(new GNEChange_GenericData(edgeData, true), true);
245 } else {
246 dataInterval->addGenericDataChild(edgeData);
247 fromTAZ->addChildElement(edgeData);
248 toTAZ->addChildElement(edgeData);
249 edgeData->incRef("buildTAZRelationData");
250 }
251 return true;
252 }
253 } else {
255 }
256 } else {
258 }
259}
260
261
262bool
264 // retrieve data set
265 auto dataSet = myNet->getAttributeCarriers()->retrieveDataSet(id, false);
266 // if demand exist, check if overwrite (delete)
267 if (dataSet) {
268 if (!myAllowUndoRedo) {
269 // only overwrite if allow undo-redo
271 } else if (myOverwrite) {
272 // delete demand element (and all of their childrens)
274 } else {
275 // duplicated dataSet
277 }
278 }
279 return true;
280}
281
282/****************************************************************************/
#define TL(string)
Definition MsgHandler.h:305
#define TLF(string,...)
Definition MsgHandler.h:307
@ 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 writeErrorInvalidParent(const SumoXMLTag tag, const std::string &id, const SumoXMLTag parentTag, const std::string &parentID)
write error "invalid parent element" giving ids of current and parent 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
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
~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.
const bool myOverwrite
check if overwrite
bool checkDuplicatedDataSet(const std::string &id)
check if given ID correspond to a duplicated dataSet
bool postParserTasks()
run post parser tasks
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
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
An Element which don't belong to GNENet but has influence in the simulation.
Definition GNEEdgeData.h:32
A road/street connecting two junctions (netedit-version)
Definition GNEEdge.h:53
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
A NBNetBuilder extended by visualisation and editing capabilities.
Definition GNENet.h:42
GNENetHelper::AttributeCarriers * getAttributeCarriers() const
get all attribute carriers used in this net
Definition GNENet.cpp:146
void deleteDataSet(GNEDataSet *dataSet, GNEUndoList *undoList)
remove data set
Definition GNENet.cpp:774
GNEViewNet * getViewNet() const
get view net
Definition GNENet.cpp:2194
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...
GNEUndoList * getUndoList() const
get the undoList object
std::map< std::string, std::string > Map
parameters map