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