Eclipse SUMO - Simulation of Urban MObility
GNEAttributeCarrier.h
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 // Abstract Base class for gui objects which carry attributes
19 /****************************************************************************/
20 #pragma once
21 #include <config.h>
22 
23 #include <fx.h>
25 
26 #include "GNETagProperties.h"
27 
28 
29 // ===========================================================================
30 // class declarations
31 // ===========================================================================
32 class GNENet;
33 class GNEUndoList;
34 class GUIGlObject;
36 class GNELane;
37 class GNEEdge;
38 
39 // ===========================================================================
40 // class definitions
41 // ===========================================================================
49 
51  friend class GNEChange_Attribute;
53 
54 public:
55 
60  GNEAttributeCarrier(const SumoXMLTag tag, GNENet* net);
61 
63  virtual ~GNEAttributeCarrier();
64 
66  GNENet* getNet() const;
67 
69  void selectAttributeCarrier(const bool changeFlag = true);
70 
72  void unselectAttributeCarrier(const bool changeFlag = true);
73 
75  bool isAttributeCarrierSelected() const;
76 
78  bool drawUsingSelectColor() const;
79 
83  virtual const std::string& getID() const = 0;
84 
86  virtual GUIGlObject* getGUIGlObject() = 0;
87 
89  virtual void updateGeometry() = 0;
90 
92 
95  /* @brief method for getting the Attribute of an XML key
96  * @param[in] key The attribute key
97  * @return string with the value associated to key
98  */
99  virtual std::string getAttribute(SumoXMLAttr key) const = 0;
100 
101  /* @brief method for setting the attribute and letting the object perform additional changes
102  * @param[in] key The attribute key
103  * @param[in] value The new value
104  * @param[in] undoList The undoList on which to register changes
105  */
106  virtual void setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) = 0;
107 
108  /* @brief method for check if new value for certain attribute is valid
109  * @param[in] key The attribute key
110  * @param[in] value The new value
111  */
112  virtual bool isValid(SumoXMLAttr key, const std::string& value) = 0;
113 
114  /* @brief method for enable attribute
115  * @param[in] key The attribute key
116  * @param[in] undoList The undoList on which to register changes
117  * @note certain attributes can be only enabled, and can produce the disabling of other attributes
118  */
119  virtual void enableAttribute(SumoXMLAttr key, GNEUndoList* undoList) = 0;
120 
121  /* @brief method for disable attribute
122  * @param[in] key The attribute key
123  * @param[in] undoList The undoList on which to register changes
124  * @note certain attributes can be only enabled, and can produce the disabling of other attributes
125  */
126  virtual void disableAttribute(SumoXMLAttr key, GNEUndoList* undoList) = 0;
127 
128  /* @brief method for check if the value for certain attribute is set
129  * @param[in] key The attribute key
130  */
131  virtual bool isAttributeEnabled(SumoXMLAttr key) const = 0;
132 
134  virtual std::string getPopUpID() const = 0;
135 
137  virtual std::string getHierarchyName() const = 0;
138 
142  virtual const std::map<std::string, std::string>& getACParametersMap() const = 0;
143 
145  template<typename T>
146  T getACParameters() const;
147 
149  void setACParameters(const std::string& parameters, GNEUndoList* undoList);
150 
152  void setACParameters(const std::vector<std::pair<std::string, std::string> >& parameters, GNEUndoList* undoList);
153 
155  void setACParameters(const std::map<std::string, std::string>& parameters, GNEUndoList* undoList);
156 
158  void addACParameters(const std::string& key, const std::string& attribute, GNEUndoList* undoList);
159 
161  void removeACParametersKeys(const std::vector<std::string>& keepKeys, GNEUndoList* undoList);
162 
164 
165  /* @brief method for return an alternative value for disabled attributes. Used only in GNEFrames
166  * @param[in] key The attribute key
167  */
169 
173  static const std::string FEATURE_LOADED;
174 
176  static const std::string FEATURE_GUESSED;
177 
179  static const std::string FEATURE_MODIFIED;
180 
182  static const std::string FEATURE_APPROVED;
184 
186  static const size_t MAXNUMBEROFATTRIBUTES;
187 
189  static const double INVALID_POSITION;
190 
192  virtual std::string getAttributeForSelection(SumoXMLAttr key) const;
193 
195  const std::string& getTagStr() const;
196 
198  const GNETagProperties& getTagProperty() const;
199 
201  FXIcon* getIcon() const;
202 
204  static const GNETagProperties& getTagProperties(SumoXMLTag tag);
205 
207  static std::vector<SumoXMLTag> allowedTags(const bool onlyDrawables);
208 
210  static std::vector<std::pair<SumoXMLTag, const std::string> > getAllowedTagsByCategory(const int tagPropertyCategory, const bool onlyDrawables);
211 
213  template<typename T>
214  static bool canParse(const std::string& string) {
215  try {
216  GNEAttributeCarrier::parse<T>(string);
217  } catch (NumberFormatException&) {
218  return false;
219  } catch (TimeFormatException&) {
220  return false;
221  } catch (EmptyData&) {
222  return false;
223  } catch (BoolFormatException&) {
224  return false;
225  }
226  return true;
227  }
228 
230  template<typename T>
231  static T parse(const std::string& string);
232 
234  template<typename T>
235  static bool canParse(GNENet* net, const std::string& value, bool report) {
236  try {
237  parse<T>(net, value);
238  } catch (FormatException& exception) {
239  if (report) {
240  WRITE_WARNING(exception.what())
241  }
242  return false;
243  }
244  return true;
245  }
246 
248  template<typename T>
249  static T parse(GNENet* net, const std::string& value);
250 
252  template<typename T>
253  static std::string parseIDs(const std::vector<T>& ACs);
254 
256  static bool lanesConsecutives(const std::vector<GNELane*>& lanes);
257 
259  template <typename T>
260  static T parseAttributeFromXML(const SUMOSAXAttributes& attrs, const std::string& objectID, const SumoXMLTag tag, const SumoXMLAttr attribute, bool& abort) {
261  bool parsedOk = true;
262  // declare string values
263  std::string defaultValue, parsedAttribute, warningMessage;
264  // obtain tag properties
265  const auto& tagProperties = getTagProperties(tag);
266  // first check if attribute is deprecated
267  if (tagProperties.isAttributeDeprecated(attribute)) {
268  // show warning if deprecateda ttribute is in the SUMOSAXAttributes
269  if (attrs.hasAttribute(attribute)) {
270  WRITE_WARNING("Attribute " + toString(attribute) + "' of " + tagProperties.getTagStr() + " is deprecated and will not be loaded.");
271  }
272  // return a dummy value
273  return parse<T>("");
274  }
275  // now check if we're obtaining attribute of an object with an already parsed ID
276  if (objectID != "") {
277  warningMessage = tagProperties.getTagStr() + " with ID '" + objectID + "'";
278  } else {
279  warningMessage = tagProperties.getTagStr();
280  }
281  // check if we're parsing block movement
282  if (attribute == GNE_ATTR_BLOCK_MOVEMENT) {
283  // first check if we can parse
284  if (tagProperties.canBlockMovement()) {
285  // First check if attribute can be parsed to bool
286  parsedAttribute = attrs.get<std::string>(attribute, objectID.c_str(), parsedOk, false);
287  // check that sucesfully parsed attribute can be converted to type double
288  if (!canParse<bool>(parsedAttribute)) {
289  abort = true;
290  // return default value
291  return parse<T>("0");
292  } else {
293  // return readed value
294  return parse<T>(parsedAttribute);
295  }
296  } else {
297  throw ProcessError("Trying to parsing block movement attribute in an AC that cannot be moved");
298  }
299  }
300  // now check if we're parsing a GEO Attribute
301  if (tagProperties.hasGEOPosition() && ((attribute == SUMO_ATTR_LON) || (attribute == SUMO_ATTR_LAT))) {
302  // first check if GEO Attribute is defined
303  if (attrs.hasAttribute(attribute)) {
304  // First check if attribute can be parsed to string
305  parsedAttribute = attrs.get<std::string>(attribute, objectID.c_str(), parsedOk, false);
306  // check that sucesfully parsed attribute can be converted to type double
307  if (!canParse<double>(parsedAttribute)) {
308  WRITE_WARNING("Format of GEO attribute '" + toString(attribute) + "' of " +
309  warningMessage + " is invalid; Cannot be parsed to float; " + tagProperties.getTagStr() + " cannot be created");
310  // return default value
311  return parse<T>("0");
312  } else {
313  // return readed value
314  return parse<T>(parsedAttribute);
315  }
316  }
317  parsedOk = false;
318  // return default value
319  return parse<T>("0");
320  }
321  // obtain attribute properties (Only for improving efficiency)
322  const auto& attrProperties = tagProperties.getAttributeProperties(attribute);
323  // set a special default value for numerical and boolean attributes (To avoid errors parsing)
324  if (attrProperties.isNumerical() || attrProperties.isBool()) {
325  defaultValue = "0";
326  } else if (attrProperties.isColor()) {
327  defaultValue = "black";
328  } else if (attrProperties.isposition()) {
329  defaultValue = "0,0";
330  }
331  // first check that attribute exists in XML
332  if (attrs.hasAttribute(attribute)) {
333  // First check if attribute can be parsed to string
334  parsedAttribute = attrs.get<std::string>(attribute, objectID.c_str(), parsedOk, false);
335  // check parsed attribute
336  if (!checkParsedAttribute(tagProperties, attrProperties, attribute, defaultValue, parsedAttribute, warningMessage)) {
337  abort = true;
338  }
339  } else if (tagProperties.canMaskXYZPositions() && (attribute == SUMO_ATTR_POSITION)) {
340  // obtain masked position attribute
341  if (!parseMaskedPositionAttribute(attrs, objectID, tagProperties, attrProperties, parsedAttribute, warningMessage)) {
342  abort = true;
343  }
344  } else {
345  // if attribute is optional and has a default value, obtain it. In other case, abort.
346  if (attrProperties.isOptional()) {
347  parsedAttribute = attrProperties.getDefaultValue();
348  } else {
349  WRITE_WARNING("Essential " + attrProperties.getDescription() + " attribute '" + toString(attribute) + "' of " +
350  warningMessage + " is missing; " + tagProperties.getTagStr() + " cannot be created");
351  // abort parsing (and creation) of element
352  abort = true;
353  // set default value (To avoid errors in parse<T>(parsedAttribute))
354  parsedAttribute = defaultValue;
355  }
356  }
357  // return parsed attribute
358  return parse<T>(parsedAttribute);
359  }
360 
361 protected:
364 
367 
370 
373 
374 private:
376  virtual void setAttribute(SumoXMLAttr key, const std::string& value) = 0;
377 
379  virtual void setEnabledAttribute(const int enabledAttributes) = 0;
380 
382  static void fillAttributeCarriers();
383 
385  static void fillNetworkElements();
386 
388  static void fillAdditionals();
389 
391  static void fillShapes();
392 
394  static void fillTAZElements();
395 
397  static void fillDemandElements();
398 
400  static void fillVehicleElements();
401 
403  static void fillStopElements();
404 
406  static void fillPersonElements();
407 
409  static void fillPersonPlanTrips();
410 
412  static void fillPersonPlanWalks();
413 
415  static void fillPersonPlanRides();
416 
418  static void fillPersonPlanEdgeEdge(SumoXMLTag currentTag);
419 
421  static void fillPersonPlanEdgeBusStop(SumoXMLTag currentTag);
422 
424  static void fillPersonPlanEdgeStop(SumoXMLTag currentTag);
425 
427  static void fillPersonPlanBusStopEdge(SumoXMLTag currentTag);
428 
430  static void fillPersonPlanBusStopBusStop(SumoXMLTag currentTag);
431 
433  static void fillPersonPlanBusStopStop(SumoXMLTag currentTag);
434 
436  static void fillPersonPlanStopEdge(SumoXMLTag currentTag);
437 
439  static void fillPersonPlanStopBusStop(SumoXMLTag currentTag);
440 
442  static void fillPersonPlanStopStop(SumoXMLTag currentTag);
443 
445  static void fillPersonTripAttributes(SumoXMLTag currentTag);
446 
448  static void fillRideAttributes(SumoXMLTag currentTag);
449 
451  static void fillPersonStopElements();
452 
454  static void fillCommonVehicleAttributes(SumoXMLTag currentTag);
455 
457  static void fillCommonFlowAttributes(SumoXMLTag currentTag, const bool forVehicles);
458 
460  static void fillCarFollowingModelAttributes(SumoXMLTag currentTag);
461 
463  static void fillJunctionModelAttributes(SumoXMLTag currentTag);
464 
466  static void fillLaneChangingModelAttributes(SumoXMLTag currentTag);
467 
469  static void fillCommonPersonAttributes(SumoXMLTag currentTag);
470 
472  static void fillCommonStopAttributes(SumoXMLTag currentTag, const bool parking);
473 
475  static void fillDataElements();
476 
478  static bool checkParsedAttribute(const GNETagProperties& tagProperties, const GNEAttributeProperties& attrProperties, const SumoXMLAttr attribute,
479  std::string& defaultValue, std::string& parsedAttribute, std::string& warningMessage);
480 
482  static bool parseMaskedPositionAttribute(const SUMOSAXAttributes& attrs, const std::string& objectID, const GNETagProperties& tagProperties,
483  const GNEAttributeProperties& attrProperties, std::string& parsedAttribute, std::string& warningMessage);
484 
486  static std::map<SumoXMLTag, GNETagProperties> myTagProperties;
487 
490 
493 };
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:276
SumoXMLTag
Numbers representing SUMO-XML - element names.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ SUMO_ATTR_LON
@ GNE_ATTR_BLOCK_MOVEMENT
block movement of a graphic element
@ SUMO_ATTR_LAT
@ SUMO_ATTR_POSITION
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
GNEAttributeCarrier(const GNEAttributeCarrier &)=delete
Invalidated copy constructor.
static void fillPersonPlanBusStopEdge(SumoXMLTag currentTag)
fill person plan busStop -> edge
virtual std::string getAttributeForSelection(SumoXMLAttr key) const
method for getting the attribute in the context of object selection
bool isAttributeCarrierSelected() const
check if attribute carrier is selected
virtual void setEnabledAttribute(const int enabledAttributes)=0
method for enabling the attribute and nothing else (used in GNEChange_EnableAttribute)
static void fillCommonStopAttributes(SumoXMLTag currentTag, const bool parking)
fill stop person attributes (used by stops and personStps)
GNEAttributeCarrier(const SumoXMLTag tag, GNENet *net)
Constructor.
static void fillPersonPlanBusStopStop(SumoXMLTag currentTag)
fill person plan busStop -> stop
static void fillPersonPlanBusStopBusStop(SumoXMLTag currentTag)
fill person plan busStop -> edge
bool mySelected
boolean to check if this AC is selected (instead of GUIGlObjectStorage)
static void fillVehicleElements()
fill vehicle elements
static void fillDemandElements()
fill demand elements
static void fillPersonPlanEdgeEdge(SumoXMLTag currentTag)
fill person plan edge -> edge
static GNETagProperties dummyTagProperty
dummy TagProperty used for reference some elements (for Example, dummyEdge)
static void fillPersonElements()
fill person elements
void setACParameters(const std::string &parameters, GNEUndoList *undoList)
set parameters (string)
static void fillDataElements()
fill Data elements
static void fillPersonPlanRides()
fill person plan rides
static void fillAdditionals()
fill additional elements
static void fillPersonPlanStopBusStop(SumoXMLTag currentTag)
fill person plan stop -> edge
static void fillLaneChangingModelAttributes(SumoXMLTag currentTag)
fill Junction Model Attributes of Vehicle/Person Types
virtual const std::string & getID() const =0
return ID of object
virtual void setAttribute(SumoXMLAttr key, const std::string &value)=0
method for setting the attribute and nothing else (used in GNEChange_Attribute)
static void fillAttributeCarriers()
fill Attribute Carriers
static const std::string FEATURE_LOADED
static void fillCommonPersonAttributes(SumoXMLTag currentTag)
fill common person attributes (used by person and personFlows)
virtual std::string getPopUpID() const =0
get PopPup ID (Used in AC Hierarchy)
static void fillNetworkElements()
fill network elements
virtual void enableAttribute(SumoXMLAttr key, GNEUndoList *undoList)=0
static void fillPersonPlanEdgeStop(SumoXMLTag currentTag)
fill person plan edge -> stop
static void fillPersonStopElements()
fill personStop elements
static const std::string FEATURE_APPROVED
feature has been approved but not changed (i.e. after being reguessed)
static T parse(const std::string &string)
parses a value of type T from string (used for basic types: int, double, bool, etc....
std::string getAlternativeValueForDisabledAttributes(SumoXMLAttr key) const
static T parse(GNENet *net, const std::string &value)
parses a complex value of type T from string (use for list of edges, list of lanes,...
static const GNETagProperties & getTagProperties(SumoXMLTag tag)
get Tag Properties
void removeACParametersKeys(const std::vector< std::string > &keepKeys, GNEUndoList *undoList)
remove keys
const std::string & getTagStr() const
get tag assigned to this object in string format
static void fillPersonPlanEdgeBusStop(SumoXMLTag currentTag)
fill person plan edge -> edge
static const std::string FEATURE_GUESSED
feature has been reguessed (may still be unchanged be we can't tell (yet)
static void fillStopElements()
fill stop elements
virtual GUIGlObject * getGUIGlObject()=0
get GUIGlObject associated with this AttributeCarrier
const GNETagProperties & getTagProperty() const
get Tag Property assigned to this object
static bool parseMaskedPositionAttribute(const SUMOSAXAttributes &attrs, const std::string &objectID, const GNETagProperties &tagProperties, const GNEAttributeProperties &attrProperties, std::string &parsedAttribute, std::string &warningMessage)
parse and check masked (note: This function is only to improve legilibility)
static void fillPersonTripAttributes(SumoXMLTag currentTag)
fill specific person trip attributes
void unselectAttributeCarrier(const bool changeFlag=true)
unselect attribute carrier using GUIGlobalSelection
bool drawUsingSelectColor() const
check if attribute carrier must be drawn using selecting color.
static bool canParse(GNENet *net, const std::string &value, bool report)
true if a value of type T can be parsed from string
static void fillPersonPlanStopStop(SumoXMLTag currentTag)
fill person plan stop -> stop
static void fillCommonVehicleAttributes(SumoXMLTag currentTag)
fill common vehicle attributes (used by vehicles, trips, routeFlows and flows)
void addACParameters(const std::string &key, const std::string &attribute, GNEUndoList *undoList)
add (or update attribute) key and attribute
FXIcon * getIcon() const
get FXIcon associated to this AC
static T parseAttributeFromXML(const SUMOSAXAttributes &attrs, const std::string &objectID, const SumoXMLTag tag, const SumoXMLAttr attribute, bool &abort)
Parse attribute from XML and show warnings if there are problems parsing it.
virtual const std::map< std::string, std::string > & getACParametersMap() const =0
static bool canParse(const std::string &string)
true if a value of type T can be parsed from string
virtual void disableAttribute(SumoXMLAttr key, GNEUndoList *undoList)=0
GNEAttributeCarrier & operator=(const GNEAttributeCarrier &src)=delete
Invalidated assignment operator.
virtual bool isAttributeEnabled(SumoXMLAttr key) const =0
static bool lanesConsecutives(const std::vector< GNELane * > &lanes)
check if lanes are consecutives
static void fillCommonFlowAttributes(SumoXMLTag currentTag, const bool forVehicles)
fill common flow attributes (used by flows, routeFlows and personFlows)
static void fillPersonPlanStopEdge(SumoXMLTag currentTag)
fill person plan stop -> edge
static std::vector< std::pair< SumoXMLTag, const std::string > > getAllowedTagsByCategory(const int tagPropertyCategory, const bool onlyDrawables)
get tags of all editable element types using TagProperty Type (NetworkEditMode::NETWORKELEMENT,...
static void fillPersonPlanWalks()
fill person plan walks
static void fillTAZElements()
fill TAZ elements
static const double INVALID_POSITION
invalid double position
virtual bool isValid(SumoXMLAttr key, const std::string &value)=0
static bool checkParsedAttribute(const GNETagProperties &tagProperties, const GNEAttributeProperties &attrProperties, const SumoXMLAttr attribute, std::string &defaultValue, std::string &parsedAttribute, std::string &warningMessage)
parse and check attribute (note: This function is only to improve legilibility)
GNENet * myNet
pointer to net
virtual std::string getHierarchyName() const =0
get Hierarchy Name (Used in AC Hierarchy)
static void fillJunctionModelAttributes(SumoXMLTag currentTag)
fill Junction Model Attributes of Vehicle/Person Types
static void fillShapes()
fill shape elements
GNENet * getNet() const
get pointer to net
static void fillPersonPlanTrips()
fill person plan trips
static std::string parseIDs(const std::vector< T > &ACs)
parses a list of specific Attribute Carriers into a string of IDs
void selectAttributeCarrier(const bool changeFlag=true)
select attribute carrier using GUIGlobalSelection
static const std::string FEATURE_MODIFIED
feature has been manually modified (implies approval)
static std::vector< SumoXMLTag > allowedTags(const bool onlyDrawables)
get tags of all editable element types
static void fillCarFollowingModelAttributes(SumoXMLTag currentTag)
fill Car Following Model of Vehicle/Person Types
virtual void updateGeometry()=0
update pre-computed geometry information
virtual void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)=0
T getACParameters() const
get parameters
static void fillRideAttributes(SumoXMLTag currentTag)
fill specific ride attributes
virtual ~GNEAttributeCarrier()
Destructor.
virtual std::string getAttribute(SumoXMLAttr key) const =0
const GNETagProperties & myTagProperty
the xml tag to which this attribute carrier corresponds
static std::map< SumoXMLTag, GNETagProperties > myTagProperties
map with the tags properties
static const size_t MAXNUMBEROFATTRIBUTES
max number of attributes allowed for every tag
the function-object for an editing operation (abstract base)
the function-object for an editing operation (abstract base)
A road/street connecting two junctions (netedit-version)
Definition: GNEEdge.h:49
This lane is powered by an underlying GNEEdge and basically knows how to draw itself.
Definition: GNELane.h:45
A NBNetBuilder extended by visualisation and editing capabilities.
Definition: GNENet.h:40
Encapsulated SAX-Attributes.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.