Eclipse SUMO - Simulation of Urban MObility
GNEDataSet.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3// Copyright (C) 2001-2022 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// A abstract class for data sets
19/****************************************************************************/
20
21
22// ===========================================================================
23// included modules
24// ===========================================================================
25#include <config.h>
26
27#include <netedit/GNENet.h>
28#include <netedit/GNEViewNet.h>
30#include <netedit/GNEUndoList.h>
33
34#include "GNEDataSet.h"
35#include "GNEDataInterval.h"
36
37
38// ===========================================================================
39// member method definitions
40// ===========================================================================
41
42// ---------------------------------------------------------------------------
43// GNEDataSet::AttributeColors - methods
44// ---------------------------------------------------------------------------
45
47}
48
49
50void
51GNEDataSet::AttributeColors::updateValues(const std::string& attribute, const double value) {
52 // check if exist
53 if (myMinMaxValue.count(attribute) == 0) {
54 myMinMaxValue[attribute] = std::make_pair(value, value);
55 } else {
56 // update min value
57 if (value < myMinMaxValue.at(attribute).first) {
58 myMinMaxValue.at(attribute).first = value;
59 }
60 // update max value
61 if (value > myMinMaxValue.at(attribute).second) {
62 myMinMaxValue.at(attribute).second = value;
63 }
64 }
65}
66
67
68void
70 // iterate over map
71 for (const auto& attributeColor : attributeColors.myMinMaxValue) {
72 if (myMinMaxValue.count(attributeColor.first) == 0) {
73 myMinMaxValue[attributeColor.first] = attributeColor.second;
74 } else {
75 // update min value
76 if (attributeColor.second.first < myMinMaxValue.at(attributeColor.first).first) {
77 myMinMaxValue.at(attributeColor.first).first = attributeColor.second.first;
78 }
79 // update max value
80 if (attributeColor.second.second > myMinMaxValue.at(attributeColor.first).second) {
81 myMinMaxValue.at(attributeColor.first).second = attributeColor.second.second;
82 }
83 }
84 }
85}
86
87
88bool
89GNEDataSet::AttributeColors::exist(const std::string& attribute) const {
90 return (myMinMaxValue.count(attribute) > 0);
91}
92
93
94double
95GNEDataSet::AttributeColors::getMinValue(const std::string& attribute) const {
96 return myMinMaxValue.at(attribute).first;
97}
98
99
100double
101GNEDataSet::AttributeColors::getMaxValue(const std::string& attribute) const {
102 return myMinMaxValue.at(attribute).second;
103}
104
105
106void
108 myMinMaxValue.clear();
109}
110
111// ---------------------------------------------------------------------------
112// GNEDataSet - methods
113// ---------------------------------------------------------------------------
114
115GNEDataSet::GNEDataSet(GNENet* net, const std::string dataSetID) :
117 myDataSetID(dataSetID) {
118}
119
120
122
123
126 return nullptr;
127}
128
129
132 return nullptr;
133}
134
135
136void
138 // first update attribute colors in data interval childrens
139 for (const auto& interval : myDataIntervalChildren) {
140 interval.second->updateAttributeColors();
141 }
142 // continue with data sets containers
145 // iterate over all data interval children
146 for (const auto& interval : myDataIntervalChildren) {
147 myAllAttributeColors.updateAllValues(interval.second->getAllAttributeColors());
148 }
149 // iterate over specificdata interval children
150 for (const auto& interval : myDataIntervalChildren) {
151 for (const auto& specificAttributeColor : interval.second->getSpecificAttributeColors()) {
152 mySpecificAttributeColors[specificAttributeColor.first].updateAllValues(specificAttributeColor.second);
153 }
154 }
155}
156
157
161}
162
163
164const std::map<SumoXMLTag, GNEDataSet::AttributeColors>&
167}
168
169
170void
172 // nothing to update
173}
174
175
178 return Position(0, 0);
179}
180
181
182void
184 // iterate over intervals
185 for (const auto& interval : myDataIntervalChildren) {
186 // open device
188 // write ID
189 device.writeAttr(SUMO_ATTR_ID, getID());
190 // write begin
191 device.writeAttr(SUMO_ATTR_BEGIN, interval.second->getAttribute(SUMO_ATTR_BEGIN));
192 // write end
193 device.writeAttr(SUMO_ATTR_END, interval.second->getAttribute(SUMO_ATTR_END));
194 // iterate over interval generic datas
195 for (const auto& genericData : interval.second->getGenericDataChildren()) {
196 // write generic data
197 genericData->writeGenericData(device);
198 }
199 // close device
200 device.closeTag();
201 }
202}
203
204
205void
207 // check that dataInterval wasn't previously inserted
208 if (myDataIntervalChildren.count(dataInterval->getAttributeDouble(SUMO_ATTR_BEGIN)) == 0) {
209 // add data interval child
210 myDataIntervalChildren[dataInterval->getAttributeDouble(SUMO_ATTR_BEGIN)] = dataInterval;
211 // add reference in attributeCarriers
213 } else {
214 throw ProcessError("DataInterval was already inserted");
215 }
216}
217
218
219void
221 // check that dataInterval was previously inserted
222 if (myDataIntervalChildren.count(dataInterval->getAttributeDouble(SUMO_ATTR_BEGIN)) == 1) {
223 // remove data interval child
225 // remove it from inspected elements and GNEElementTree
228 // remove reference from attributeCarriers
230 } else {
231 throw ProcessError("DataInterval wasn't previously inserted");
232 }
233}
234
235
236bool
238 for (const auto& interval : myDataIntervalChildren) {
239 if (interval.second == dataInterval) {
240 return true;
241 }
242 }
243 return false;
244}
245
246void
248 // check that dataInterval was previously inserted
249 if (myDataIntervalChildren.count(oldBegin) == 1) {
250 // get data interval
251 GNEDataInterval* dataInterval = myDataIntervalChildren.at(oldBegin);
252 // insert again using new begin
253 myDataIntervalChildren[dataInterval->getAttributeDouble(SUMO_ATTR_BEGIN)] = dataInterval;
254 } else {
255 throw ProcessError("DataInterval wasn't previously inserted");
256 }
257}
258
259
260bool
261GNEDataSet::checkNewInterval(const double newBegin, const double newEnd) {
262 return checkNewInterval(myDataIntervalChildren, newBegin, newEnd);
263}
264
265
266bool
267GNEDataSet::checkNewBeginEnd(const GNEDataInterval* dataInterval, const double newBegin, const double newEnd) {
268 // make a copy of myDataIntervalChildren without dataInterval, and check checkNewInterval
269 std::map<const double, GNEDataInterval*> copyOfDataIntervalMap;
270 for (const auto& element : myDataIntervalChildren) {
271 if (element.second != dataInterval) {
272 copyOfDataIntervalMap.insert(element);
273 }
274 }
275 return checkNewInterval(copyOfDataIntervalMap, newBegin, newEnd);
276}
277
278
280GNEDataSet::retrieveInterval(const double begin, const double end) const {
281 if (myDataIntervalChildren.count(begin) == 0) {
282 return nullptr;
283 } else if (myDataIntervalChildren.at(begin)->getAttributeDouble(SUMO_ATTR_END) != end) {
284 return nullptr;
285 } else {
286 return myDataIntervalChildren.at(begin);
287 }
288}
289
290
291const std::map<const double, GNEDataInterval*>&
294}
295
296
297std::string
299 switch (key) {
300 case SUMO_ATTR_ID:
301 return myDataSetID;
302 default:
303 throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
304 }
305}
306
307
308double
310 throw InvalidArgument(getTagStr() + " doesn't have a double attribute of type '" + toString(key) + "'");
311}
312
313
314void
315GNEDataSet::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
316 switch (key) {
317 case SUMO_ATTR_ID:
318 undoList->changeAttribute(new GNEChange_Attribute(this, key, value));
319 break;
320 default:
321 throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
322 }
323}
324
325
326bool
327GNEDataSet::isValid(SumoXMLAttr key, const std::string& value) {
328 switch (key) {
329 case SUMO_ATTR_ID:
330 if (SUMOXMLDefinitions::isValidNetID(value) && (myNet->getAttributeCarriers()->retrieveDataSet(value, false) == nullptr)) {
331 return true;
332 } else {
333 return false;
334 }
335 default:
336 throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
337 }
338}
339
340
341std::string
343 return getTagStr();
344}
345
346
347std::string
349 return getTagStr() + ": " + myDataSetID;
350}
351
352
355 return getParametersMap();
356}
357
358
359void
360GNEDataSet::setAttribute(SumoXMLAttr key, const std::string& value) {
361 switch (key) {
362 case SUMO_ATTR_ID:
363 myDataSetID = value;
364 // update all intervals
365 for (const auto& interval : myDataIntervalChildren) {
366 interval.second->updateGenericDataIDs();
367 }
368 break;
369 default:
370 throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
371 }
372 // mark interval toolbar for update
374}
375
376
377bool
378GNEDataSet::checkNewInterval(const std::map<const double, GNEDataInterval*>& dataIntervalMap, const double newBegin, const double newEnd) {
379 if (dataIntervalMap.empty()) {
380 return true;
381 } else {
382 // declare first and last element
383 const auto itFirstElement = dataIntervalMap.begin();
384 const auto itLastElement = dataIntervalMap.rbegin();
385 if (newBegin > newEnd) {
386 return false;
387 } else if (dataIntervalMap.count(newBegin) == 1) {
388 return false;
389 } else if (newBegin < itFirstElement->first) {
390 return (newEnd <= itFirstElement->first);
391 } else if (newBegin > itLastElement->first) {
392 return (newBegin >= itLastElement->second->getAttributeDouble(SUMO_ATTR_END));
393 } else {
394 // iterate over myDataIntervalChildren
395 for (auto it = itFirstElement; it != dataIntervalMap.end(); it++) {
396 if (newBegin < it->first) {
397 // obtain previous edge
398 auto itPrevious = it;
399 itPrevious--;
400 // check overlapping with end
401 if (itPrevious->second->getAttributeDouble(SUMO_ATTR_END) < newBegin) {
402 return true;
403 }
404 }
405 }
406 }
407 return false;
408 }
409}
410
411/****************************************************************************/
@ SUMO_TAG_INTERVAL
an aggreagated-output interval
@ SUMO_TAG_DATASET
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ SUMO_ATTR_BEGIN
weights: time range begin
@ SUMO_ATTR_END
weights: time range end
@ SUMO_ATTR_ID
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
const std::string getID() const
get ID (all Attribute Carriers have one)
friend class GNEChange_Attribute
declare friend class
const std::string & getTagStr() const
get tag assigned to this object in string format
GNENet * myNet
pointer to net
An Element which don't belong to GNENet but has influence in the simulation.
double getAttributeDouble(SumoXMLAttr key) const
attribute colors
Definition: GNEDataSet.h:47
std::map< std::string, std::pair< double, double > > myMinMaxValue
map with the minimum and maximum value
Definition: GNEDataSet.h:73
void updateAllValues(const AttributeColors &attributeColors)
update value for all attributes
Definition: GNEDataSet.cpp:69
bool exist(const std::string &attribute) const
check if given attribute exist (needed for non-double attributes)
Definition: GNEDataSet.cpp:89
double getMaxValue(const std::string &attribute) const
get maximum value
Definition: GNEDataSet.cpp:101
AttributeColors()
default constructor
Definition: GNEDataSet.cpp:46
double getMinValue(const std::string &attribute) const
get minimum value
Definition: GNEDataSet.cpp:95
void clear()
clear AttributeColors
Definition: GNEDataSet.cpp:107
void updateValues(const std::string &attribute, const double value)
update value for an specific attribute
Definition: GNEDataSet.cpp:51
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
method for setting the attribute and letting the object perform data element changes
Definition: GNEDataSet.cpp:315
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
Definition: GNEDataSet.cpp:327
Position getPositionInView() const
Returns element position in view.
Definition: GNEDataSet.cpp:177
GUIGlObject * getGUIGlObject()
get GUIGlObject associated with this AttributeCarrier
Definition: GNEDataSet.cpp:131
void writeDataSet(OutputDevice &device) const
write data set
Definition: GNEDataSet.cpp:183
std::string myDataSetID
dataSet ID
Definition: GNEDataSet.h:180
bool dataIntervalChildrenExist(GNEDataInterval *dataInterval) const
check if given data interval exist
Definition: GNEDataSet.cpp:237
std::string getAttribute(SumoXMLAttr key) const
Definition: GNEDataSet.cpp:298
std::map< const double, GNEDataInterval * > myDataIntervalChildren
map with dataIntervals children sorted by begin
Definition: GNEDataSet.h:183
void removeDataIntervalChild(GNEDataInterval *dataInterval)
add data interval child
Definition: GNEDataSet.cpp:220
std::string getPopUpID() const
get PopPup ID (Used in AC Hierarchy)
Definition: GNEDataSet.cpp:342
GNEDataInterval * retrieveInterval(const double begin, const double end) const
return interval
Definition: GNEDataSet.cpp:280
GNEHierarchicalElement * getHierarchicalElement()
get GNEHierarchicalElement associated with this AttributeCarrier
Definition: GNEDataSet.cpp:125
std::string getHierarchyName() const
get Hierarchy Name (Used in AC Hierarchy)
Definition: GNEDataSet.cpp:348
~GNEDataSet()
Destructor.
Definition: GNEDataSet.cpp:121
const Parameterised::Map & getACParametersMap() const
get parameters map
Definition: GNEDataSet.cpp:354
const GNEDataSet::AttributeColors & getAllAttributeColors() const
all attribute colors
Definition: GNEDataSet.cpp:159
void updateAttributeColors()
update attribute colors deprecated
Definition: GNEDataSet.cpp:137
double getAttributeDouble(SumoXMLAttr key) const
Definition: GNEDataSet.cpp:309
std::map< SumoXMLTag, GNEDataSet::AttributeColors > mySpecificAttributeColors
specific attribute colors
Definition: GNEDataSet.h:189
bool checkNewBeginEnd(const GNEDataInterval *dataInterval, const double newBegin, const double newEnd)
check if new begin or end for given GNEDataInterval is given
Definition: GNEDataSet.cpp:267
GNEDataSet::AttributeColors myAllAttributeColors
all attribute colors
Definition: GNEDataSet.h:186
GNEDataSet(GNENet *net, const std::string dataSetID)
Constructor.
Definition: GNEDataSet.cpp:115
const std::map< SumoXMLTag, GNEDataSet::AttributeColors > & getSpecificAttributeColors() const
specific attribute colors
Definition: GNEDataSet.cpp:165
const std::map< const double, GNEDataInterval * > & getDataIntervalChildren() const
get data interval children
Definition: GNEDataSet.cpp:292
bool checkNewInterval(const double newBegin, const double newEnd)
check if a new GNEDataInterval with the given begin and end can be inserted in current GNEDataSet
Definition: GNEDataSet.cpp:261
void updateGeometry()
update pre-computed geometry information
Definition: GNEDataSet.cpp:171
void addDataIntervalChild(GNEDataInterval *dataInterval)
add data interval child
Definition: GNEDataSet.cpp:206
void updateDataIntervalBegin(const double oldBegin)
update data interval begin
Definition: GNEDataSet.cpp:247
void removeCurrentEditedAttributeCarrier(const GNEAttributeCarrier *HE)
if given AttributeCarrier is the same of myHE, set it as nullptr
GNEElementTree * getHierarchicalElementTree() const
get GNEElementTree modul
GNEDataSet * retrieveDataSet(const std::string &id, bool hardFail=true) const
Returns the named data set.
void insertDataInterval(GNEDataInterval *dataInterval)
insert data interval
void deleteDataInterval(GNEDataInterval *dataInterval)
delete data interval
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:132
GNEViewNet * getViewNet() const
get view net
Definition: GNENet.cpp:1987
void changeAttribute(GNEChange_Attribute *change)
special method for change attributes, avoid empty changes, always execute
GNEViewNetHelper::IntervalBar & getIntervalBar()
get interval bar
GNEViewParent * getViewParent() const
get the net object
void removeFromAttributeCarrierInspected(const GNEAttributeCarrier *AC)
remove given AC of list of inspected Attribute Carriers
GNEInspectorFrame * getInspectorFrame() const
get frame for inspect elements
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:251
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
std::map< std::string, std::string > Map
parameters map
Definition: Parameterised.h:45
const Parameterised::Map & getParametersMap() const
Returns the inner key/value map.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:37
static bool isValidNetID(const std::string &value)
whether the given string is a valid id for a network element