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>
29 #include <netedit/GNEViewParent.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 
50 void
51 GNEDataSet::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 
68 void
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 
88 bool
89 GNEDataSet::AttributeColors::exist(const std::string& attribute) const {
90  return (myMinMaxValue.count(attribute) > 0);
91 }
92 
93 
94 double
95 GNEDataSet::AttributeColors::getMinValue(const std::string& attribute) const {
96  return myMinMaxValue.at(attribute).first;
97 }
98 
99 
100 double
101 GNEDataSet::AttributeColors::getMaxValue(const std::string& attribute) const {
102  return myMinMaxValue.at(attribute).second;
103 }
104 
105 
106 void
108  myMinMaxValue.clear();
109 }
110 
111 // ---------------------------------------------------------------------------
112 // GNEDataSet - methods
113 // ---------------------------------------------------------------------------
114 
115 GNEDataSet::GNEDataSet(GNENet* net, const std::string dataSetID) :
117  myDataSetID(dataSetID) {
118 }
119 
120 
122 
123 
126  return nullptr;
127 }
128 
129 
130 const std::string&
132  return myDataSetID;
133 }
134 
135 
138  return nullptr;
139 }
140 
141 
142 void
144  // first update attribute colors in data interval childrens
145  for (const auto& interval : myDataIntervalChildren) {
146  interval.second->updateAttributeColors();
147  }
148  // continue with data sets containers
151  // iterate over all data interval children
152  for (const auto& interval : myDataIntervalChildren) {
153  myAllAttributeColors.updateAllValues(interval.second->getAllAttributeColors());
154  }
155  // iterate over specificdata interval children
156  for (const auto& interval : myDataIntervalChildren) {
157  for (const auto& specificAttributeColor : interval.second->getSpecificAttributeColors()) {
158  mySpecificAttributeColors[specificAttributeColor.first].updateAllValues(specificAttributeColor.second);
159  }
160  }
161 }
162 
163 
166  return myAllAttributeColors;
167 }
168 
169 
170 const std::map<SumoXMLTag, GNEDataSet::AttributeColors>&
173 }
174 
175 
176 void
178  // nothing to update
179 }
180 
181 
182 Position
184  return Position(0, 0);
185 }
186 
187 
188 void
190  // iterate over intervals
191  for (const auto& interval : myDataIntervalChildren) {
192  // open device
193  device.openTag(SUMO_TAG_INTERVAL);
194  // write ID
195  device.writeAttr(SUMO_ATTR_ID, getID());
196  // write begin
197  device.writeAttr(SUMO_ATTR_BEGIN, interval.second->getAttribute(SUMO_ATTR_BEGIN));
198  // write end
199  device.writeAttr(SUMO_ATTR_END, interval.second->getAttribute(SUMO_ATTR_END));
200  // iterate over interval generic datas
201  for (const auto& genericData : interval.second->getGenericDataChildren()) {
202  // write generic data
203  genericData->writeGenericData(device);
204  }
205  // close device
206  device.closeTag();
207  }
208 }
209 
210 
211 void
213  // check that dataInterval wasn't previously inserted
214  if (myDataIntervalChildren.count(dataInterval->getAttributeDouble(SUMO_ATTR_BEGIN)) == 0) {
215  // add data interval child
216  myDataIntervalChildren[dataInterval->getAttributeDouble(SUMO_ATTR_BEGIN)] = dataInterval;
217  // add reference in attributeCarriers
219  } else {
220  throw ProcessError("DataInterval was already inserted");
221  }
222 }
223 
224 
225 void
227  // check that dataInterval was previously inserted
228  if (myDataIntervalChildren.count(dataInterval->getAttributeDouble(SUMO_ATTR_BEGIN)) == 1) {
229  // remove data interval child
231  // remove it from inspected elements and HierarchicalElementTree
234  // remove reference from attributeCarriers
236  } else {
237  throw ProcessError("DataInterval wasn't previously inserted");
238  }
239 }
240 
241 
242 bool
244  for (const auto& interval : myDataIntervalChildren) {
245  if (interval.second == dataInterval) {
246  return true;
247  }
248  }
249  return false;
250 }
251 
252 void
253 GNEDataSet::updateDataIntervalBegin(const double oldBegin) {
254  // check that dataInterval was previously inserted
255  if (myDataIntervalChildren.count(oldBegin) == 1) {
256  // get data interval
257  GNEDataInterval* dataInterval = myDataIntervalChildren.at(oldBegin);
258  // insert again using new begin
259  myDataIntervalChildren[dataInterval->getAttributeDouble(SUMO_ATTR_BEGIN)] = dataInterval;
260  } else {
261  throw ProcessError("DataInterval wasn't previously inserted");
262  }
263 }
264 
265 
266 bool
267 GNEDataSet::checkNewInterval(const double newBegin, const double newEnd) {
268  return checkNewInterval(myDataIntervalChildren, newBegin, newEnd);
269 }
270 
271 
272 bool
273 GNEDataSet::checkNewBeginEnd(const GNEDataInterval* dataInterval, const double newBegin, const double newEnd) {
274  // make a copy of myDataIntervalChildren without dataInterval, and check checkNewInterval
275  std::map<const double, GNEDataInterval*> copyOfDataIntervalMap;
276  for (const auto& element : myDataIntervalChildren) {
277  if (element.second != dataInterval) {
278  copyOfDataIntervalMap.insert(element);
279  }
280  }
281  return checkNewInterval(copyOfDataIntervalMap, newBegin, newEnd);
282 }
283 
284 
286 GNEDataSet::retrieveInterval(const double begin, const double end) const {
287  if (myDataIntervalChildren.count(begin) == 0) {
288  return nullptr;
289  } else if (myDataIntervalChildren.at(begin)->getAttributeDouble(SUMO_ATTR_END) != end) {
290  return nullptr;
291  } else {
292  return myDataIntervalChildren.at(begin);
293  }
294 }
295 
296 
297 const std::map<const double, GNEDataInterval*>&
299  return myDataIntervalChildren;
300 }
301 
302 
303 std::string
305  switch (key) {
306  case SUMO_ATTR_ID:
307  return myDataSetID;
308  default:
309  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
310  }
311 }
312 
313 
314 double
316  throw InvalidArgument(getTagStr() + " doesn't have a double attribute of type '" + toString(key) + "'");
317 }
318 
319 
320 void
321 GNEDataSet::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
322  switch (key) {
323  case SUMO_ATTR_ID:
324  undoList->changeAttribute(new GNEChange_Attribute(this, key, value));
325  break;
326  default:
327  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
328  }
329 }
330 
331 
332 bool
333 GNEDataSet::isValid(SumoXMLAttr key, const std::string& value) {
334  switch (key) {
335  case SUMO_ATTR_ID:
336  if (SUMOXMLDefinitions::isValidNetID(value) && (myNet->getAttributeCarriers()->retrieveDataSet(value, false) == nullptr)) {
337  return true;
338  } else {
339  return false;
340  }
341  default:
342  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
343  }
344 }
345 
346 
347 void
349  // nothing to enable
350 }
351 
352 
353 void
355  // nothing to disable
356 }
357 
358 
359 bool
361  return true;
362 }
363 
364 
365 bool
367  return false;
368 }
369 
370 
371 std::string
373  return getTagStr();
374 }
375 
376 
377 std::string
379  return getTagStr() + ": " + myDataSetID;
380 }
381 
382 
383 const std::map<std::string, std::string>&
385  return getParametersMap();
386 }
387 
388 
389 void
390 GNEDataSet::setAttribute(SumoXMLAttr key, const std::string& value) {
391  switch (key) {
392  case SUMO_ATTR_ID:
393  myDataSetID = value;
394  // update all intervals
395  for (const auto& interval : myDataIntervalChildren) {
396  interval.second->updateGenericDataIDs();
397  }
398  break;
399  default:
400  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
401  }
402 }
403 
404 
405 void
406 GNEDataSet::toogleAttribute(SumoXMLAttr /*key*/, const bool /*value*/, const int /*previousParameters*/) {
407  throw InvalidArgument("Nothing to enable");
408 }
409 
410 
411 bool
412 GNEDataSet::checkNewInterval(const std::map<const double, GNEDataInterval*>& dataIntervalMap, const double newBegin, const double newEnd) {
413  if (dataIntervalMap.empty()) {
414  return true;
415  } else {
416  // declare first and last element
417  const auto itFirstElement = dataIntervalMap.begin();
418  const auto itLastElement = dataIntervalMap.rbegin();
419  if (newBegin > newEnd) {
420  return false;
421  } else if (dataIntervalMap.count(newBegin) == 1) {
422  return false;
423  } else if (newBegin < itFirstElement->first) {
424  return (newEnd <= itFirstElement->first);
425  } else if (newBegin > itLastElement->first) {
426  return (newBegin >= itLastElement->second->getAttributeDouble(SUMO_ATTR_END));
427  } else {
428  // iterate over myDataIntervalChildren
429  for (auto it = itFirstElement; it != dataIntervalMap.end(); it++) {
430  if (newBegin < it->first) {
431  // obtain previous edge
432  auto itPrevious = it;
433  itPrevious--;
434  // check overlapping with end
435  if (itPrevious->second->getAttributeDouble(SUMO_ATTR_END) < newBegin) {
436  return true;
437  }
438  }
439  }
440  }
441  return false;
442  }
443 }
444 
445 /****************************************************************************/
@ 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
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 belongs to GNENet but has influency in the simulation.
double getAttributeDouble(SumoXMLAttr key) const
@bief 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:321
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
Definition: GNEDataSet.cpp:333
Position getPositionInView() const
Returns element position in view.
Definition: GNEDataSet.cpp:183
GUIGlObject * getGUIGlObject()
get GUIGlObject associated with this AttributeCarrier
Definition: GNEDataSet.cpp:137
void writeDataSet(OutputDevice &device) const
write data set
Definition: GNEDataSet.cpp:189
std::string myDataSetID
dataSet ID
Definition: GNEDataSet.h:207
bool dataIntervalChildrenExist(GNEDataInterval *dataInterval) const
check if given data interval exist
Definition: GNEDataSet.cpp:243
std::string getAttribute(SumoXMLAttr key) const
Definition: GNEDataSet.cpp:304
std::map< const double, GNEDataInterval * > myDataIntervalChildren
map with dataIntervals children sorted by begin
Definition: GNEDataSet.h:210
const std::string & getID() const
get ID
Definition: GNEDataSet.cpp:131
void removeDataIntervalChild(GNEDataInterval *dataInterval)
add data interval child
Definition: GNEDataSet.cpp:226
bool isAttributeComputed(SumoXMLAttr key) const
Definition: GNEDataSet.cpp:366
std::string getPopUpID() const
get PopPup ID (Used in AC Hierarchy)
Definition: GNEDataSet.cpp:372
GNEDataInterval * retrieveInterval(const double begin, const double end) const
return interval
Definition: GNEDataSet.cpp:286
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:378
~GNEDataSet()
Destructor.
Definition: GNEDataSet.cpp:121
const GNEDataSet::AttributeColors & getAllAttributeColors() const
all attribute colors
Definition: GNEDataSet.cpp:165
void updateAttributeColors()
update attribute colors deprecated
Definition: GNEDataSet.cpp:143
double getAttributeDouble(SumoXMLAttr key) const
Definition: GNEDataSet.cpp:315
std::map< SumoXMLTag, GNEDataSet::AttributeColors > mySpecificAttributeColors
specific attribute colors
Definition: GNEDataSet.h:216
bool isAttributeEnabled(SumoXMLAttr key) const
Definition: GNEDataSet.cpp:360
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:273
GNEDataSet::AttributeColors myAllAttributeColors
all attribute colors
Definition: GNEDataSet.h:213
void enableAttribute(SumoXMLAttr key, GNEUndoList *undoList)
Definition: GNEDataSet.cpp:348
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:171
const std::map< const double, GNEDataInterval * > & getDataIntervalChildren() const
get data interval children
Definition: GNEDataSet.cpp:298
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:267
void updateGeometry()
update pre-computed geometry information
Definition: GNEDataSet.cpp:177
void disableAttribute(SumoXMLAttr key, GNEUndoList *undoList)
Definition: GNEDataSet.cpp:354
const std::map< std::string, std::string > & getACParametersMap() const
get parameters map
Definition: GNEDataSet.cpp:384
void toogleAttribute(SumoXMLAttr key, const bool value, const int previousParameters)
method for enable or disable the attribute and nothing else (used in GNEChange_EnableAttribute)
Definition: GNEDataSet.cpp:406
void addDataIntervalChild(GNEDataInterval *dataInterval)
add data interval child
Definition: GNEDataSet.cpp:212
void updateDataIntervalBegin(const double oldBegin)
update data interval begin
Definition: GNEDataSet.cpp:253
void removeCurrentEditedAttributeCarrier(const GNEAttributeCarrier *HE)
if given AttributeCarrier is the same of myHE, set it as nullptr
GNEFrameModules::HierarchicalElementTree * getHierarchicalElementTree() const
get HierarchicalElementTree 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:125
GNEViewNet * getViewNet() const
get view net
Definition: GNENet.cpp:1964
void changeAttribute(GNEChange_Attribute *change)
special method for change attributes, avoid empty changes, always execute
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 & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:248
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
const std::map< std::string, std::string > & 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