Eclipse SUMO - Simulation of Urban MObility
GNEAttributeProperties.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-2024 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 tag properties used in GNEAttributeCarrier
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 
26 #include "GNEAttributeProperties.h"
27 #include "GNETagProperties.h"
28 
29 #include "GNEAttributeCarrier.h"
30 
31 
32 // ===========================================================================
33 // method definitions
34 // ===========================================================================
35 
37 
38 
39 GNEAttributeProperties::GNEAttributeProperties(const SumoXMLAttr attribute, const int attributeProperty, const std::string& definition, std::string defaultValue) :
40  myAttribute(attribute),
41  myTagPropertyParent(nullptr),
42  myAttrStr(toString(attribute)),
43  myAttributeProperty(attributeProperty),
44  myDefinition(definition),
45  myDefaultValue(defaultValue),
46  myDefaultActivated(false),
47  myAttrSynonym(SUMO_ATTR_NOTHING),
48  myMinimumRange(0),
49  myMaximumRange(0) {
50  // empty definition aren't valid
51  if (definition.empty()) {
52  throw FormatException("Missing definition for AttributeProperty '" + toString(attribute) + "'");
53  }
54  // if default value isn't empty, but attribute doesn't support default values, throw exception.
55  if (!defaultValue.empty() && !(attributeProperty & DEFAULTVALUE)) {
56  throw FormatException("AttributeProperty for '" + toString(attribute) + "' doesn't support default values");
57  }
58  // Attributes cannot be flowdefinition and enabilitablet at the same time
59  if ((attributeProperty & FLOWDEFINITION) && (attributeProperty & ACTIVATABLE)) {
60  throw FormatException("Attribute '" + toString(attribute) + "' cannot be flowdefinition and activatable at the same time");
61  }
62 }
63 
64 
66 
67 
68 void
70  // check integrity only in debug mode
71 #ifdef DEBUG
72  // check that default values can be parsed (only in debug mode)
73  if (hasDefaultValue()) {
74  if (isInt() && !GNEAttributeCarrier::canParse<int>(myDefaultValue)) {
75  throw FormatException("Default value cannot be parsed to int");
76  }
77  if (isFloat() && !GNEAttributeCarrier::canParse<double>(myDefaultValue)) {
78  throw FormatException("Default value cannot be parsed to float");
79  }
80  if (isSUMOTime() && !GNEAttributeCarrier::canParse<SUMOTime>(myDefaultValue)) {
81  throw FormatException("Default value cannot be parsed to SUMOTime");
82  }
83  if (isBool() && !GNEAttributeCarrier::canParse<bool>(myDefaultValue)) {
84  throw FormatException("Default value cannot be parsed to bool");
85  }
86  if (isPosition() && (myDefaultValue.size() > 0) && !GNEAttributeCarrier::canParse<Position>(myDefaultValue)) {
87  throw FormatException("Default value cannot be parsed to position");
88  }
89  if (isColor() && !GNEAttributeCarrier::canParse<RGBColor>(myDefaultValue)) {
90  throw FormatException("Default value cannot be parsed to color");
91  }
92  }
93  // check that positive attributes correspond only to a int, floats or SUMOTimes
94  if (isPositive() && !(isInt() || isFloat() || isSUMOTime())) {
95  throw FormatException("Only int, floats or SUMOTimes can be positive");
96  }
97  // check that secuential attributes correspond to a list
98  if (isSecuential() && !isList()) {
99  throw FormatException("Secuential property only is compatible with list properties");
100  }
101  // check that synonym attribute isn't nothing
103  throw FormatException("synonym attribute cannot be nothing");
104  }
105  // check that ranges are valid
106  if (hasAttrRange()) {
108  throw FormatException("empty range");
109  } else if ((myMinimumRange == 0) && (myMaximumRange == 0)) {
110  throw FormatException("non-defined range");
111  } else if ((myMaximumRange - myMinimumRange) <= 0) {
112  throw FormatException("invalid range");
113  }
114  }
115  // check that unique attributes aren't copyables
116  if (isUnique() && isCopyable()) {
117  throw FormatException("Unique attributes aren't copyables");
118  }
119 #endif // DEBUG
120 }
121 
122 
123 void
124 GNEAttributeProperties::setDiscreteValues(const std::vector<std::string>& discreteValues) {
125  if (isDiscrete()) {
126  myDiscreteValues = discreteValues;
127  } else {
128  throw FormatException("AttributeProperty doesn't support discrete values");
129  }
130 }
131 
132 
133 void
135  if (isActivatable()) {
136  myDefaultActivated = value;
137  } else {
138  throw FormatException("AttributeProperty doesn't support default activated");
139  }
140 }
141 
142 
143 void
145  if (hasAttrSynonym()) {
146  myAttrSynonym = synonym;
147  } else {
148  throw FormatException("AttributeProperty doesn't support synonyms");
149  }
150 }
151 
152 
153 void
154 GNEAttributeProperties::setRange(const double minimum, const double maximum) {
155  if (hasAttrRange()) {
156  myMinimumRange = minimum;
157  myMaximumRange = maximum;
158  // check that given range is valid
160  throw FormatException("empty range");
161  } else if ((myMinimumRange == 0) && (myMaximumRange == 0)) {
162  throw FormatException("non-defined range");
163  } else if ((myMaximumRange - myMinimumRange) <= 0) {
164  throw FormatException("invalid range");
165  }
166  } else {
167  throw FormatException("AttributeProperty doesn't support ranges");
168  }
169 }
170 
171 
172 void
174  myTagPropertyParent = tagPropertyParent;
175 }
176 
177 
180  return myAttribute;
181 }
182 
183 
184 const std::string&
186  return myAttrStr;
187 }
188 
189 
190 const GNETagProperties&
192  return *myTagPropertyParent;
193 }
194 
195 
196 int
198  for (auto i = myTagPropertyParent->begin(); i != myTagPropertyParent->end(); i++) {
199  if (i->getAttr() == myAttribute) {
200  return (int)(i - myTagPropertyParent->begin());
201  }
202  }
203  throw ProcessError("Attribute wasn't found in myTagPropertyParent");
204 }
205 
206 
207 const std::string&
209  return myDefinition;
210 }
211 
212 
213 const std::string&
215  return myDefaultValue;
216 }
217 
218 
219 bool
221  return myDefaultActivated;
222 }
223 
224 
225 std::string
227  std::string pre;
228  std::string type;
229  std::string plural;
230  std::string last;
231  // pre type
232  if ((myAttributeProperty & LIST) != 0) {
233  pre += "list of ";
234  if ((myAttributeProperty & VCLASS) != 0) {
235  plural = "es";
236  } else {
237  plural = "s";
238  }
239  }
240  if ((myAttributeProperty & POSITIVE) != 0) {
241  pre += "non-negative ";
242  }
243  if ((myAttributeProperty & DISCRETE) != 0) {
244  pre += "discrete ";
245  }
246  if ((myAttributeProperty & UNIQUE) != 0) {
247  pre += "unique ";
248  }
249  // type
250  if ((myAttributeProperty & INT) != 0) {
251  type = "integer";
252  }
253  if ((myAttributeProperty & FLOAT) != 0) {
254  type = "float";
255  }
256  if ((myAttributeProperty & SUMOTIME) != 0) {
257  type = "SUMOTime";
258  }
259  if ((myAttributeProperty & BOOL) != 0) {
260  type = "boolean";
261  }
262  if ((myAttributeProperty & STRING) != 0) {
263  type = "string";
264  }
265  if ((myAttributeProperty & POSITION) != 0) {
266  type = "position";
267  }
268  if ((myAttributeProperty & COLOR) != 0) {
269  type = "color";
270  }
271  if ((myAttributeProperty & VCLASS) != 0) {
272  type = "vClass";
273  }
274  if ((myAttributeProperty & FILENAME) != 0) {
275  type = "filename";
276  }
277  if ((myAttributeProperty & PROBABILITY) != 0) {
278  type = "probability";
279  last = "[0, 1]";
280  }
281  if ((myAttributeProperty & ANGLE) != 0) {
282  type = "angle";
283  last = "[0, 360]";
284  }
285  return pre + type + plural + last;
286 }
287 
288 
289 const std::vector<std::string>&
291  return myDiscreteValues;
292 }
293 
294 
297  if (hasAttrSynonym()) {
298  return myAttrSynonym;
299  } else {
300  throw ProcessError("Attr doesn't support synonym");
301  }
302 }
303 
304 
305 double
307  if (hasAttrRange()) {
308  return myMinimumRange;
309  } else {
310  throw ProcessError("Attr doesn't support range");
311  }
312 }
313 
314 
315 double
317  if (hasAttrRange()) {
318  return myMaximumRange;
319  } else {
320  throw ProcessError("Attr doesn't support range");
321  }
322 }
323 
324 
325 bool
327  return (myAttributeProperty & DEFAULTVALUE) != 0;
328 }
329 
330 
331 bool
333  return (myAttributeProperty & SYNONYM) != 0;
334 }
335 
336 bool
338  return (myAttributeProperty & RANGE) != 0;
339 }
340 
341 
342 bool
344  return (myAttributeProperty & INT) != 0;
345 }
346 
347 
348 bool
350  return (myAttributeProperty & FLOAT) != 0;
351 }
352 
353 
354 bool
356  return (myAttributeProperty & SUMOTIME) != 0;
357 }
358 
359 
360 bool
362  return (myAttributeProperty & BOOL) != 0;
363 }
364 
365 
366 bool
368  return (myAttributeProperty & STRING) != 0;
369 }
370 
371 
372 bool
374  return (myAttributeProperty & POSITION) != 0;
375 }
376 
377 
378 bool
380  return (myAttributeProperty & PROBABILITY) != 0;
381 }
382 
383 
384 bool
386  return (myAttributeProperty & (INT | FLOAT | SUMOTIME)) != 0;
387 }
388 
389 
390 bool
392  return (myAttributeProperty & POSITIVE) != 0;
393 }
394 
395 
396 bool
398  return (myAttributeProperty & COLOR) != 0;
399 }
400 
401 
402 bool
404  return (myAttributeProperty & VTYPE) != 0;
405 }
406 
407 
408 bool
410  return (myAttributeProperty & FILENAME) != 0;
411 }
412 
413 
414 bool
416  return (myAttributeProperty & VCLASS) != 0;
417 }
418 
419 
420 bool
422  return ((myAttributeProperty & LIST) != 0) && ((myAttributeProperty & VCLASS) != 0);
423 }
424 
425 
426 bool
428  return (myAttributeProperty & LIST) != 0;
429 }
430 
431 
432 bool
434  return (myAttributeProperty & SECUENCIAL) != 0;
435 }
436 
437 
438 bool
440  return (myAttributeProperty & UNIQUE) != 0;
441 }
442 
443 
444 bool
446  return (myAttributeProperty & DISCRETE) != 0;
447 }
448 
449 
450 bool
452  return (myAttributeProperty & EXTENDED) != 0;
453 }
454 
455 
456 bool
458  return (myAttributeProperty & UPDATEGEOMETRY) != 0;
459 }
460 
461 
462 bool
464  return (myAttributeProperty & ACTIVATABLE) != 0;
465 }
466 
467 
468 bool
470  return (myAttributeProperty & FLOWDEFINITION) != 0;
471 }
472 
473 
474 bool
476  return (myAttributeProperty & AUTOMATICID) != 0;
477 }
478 
479 
480 bool
482  return (myAttributeProperty & COPYABLE) != 0;
483 }
484 
485 /****************************************************************************/
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ SUMO_ATTR_NOTHING
invalid attribute, must be the last one
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
double getMaximumRange() const
get maximum range
bool isVClass() const
return true if attribute is a VehicleClass
bool isProbability() const
return true if attribute is a probability
bool isColor() const
return true if attribute is a color
void setTagPropertyParent(GNETagProperties *tagPropertyParent)
set tag property parent
void setDiscreteValues(const std::vector< std::string > &discreteValues)
set discrete values
void setSynonym(const SumoXMLAttr synonym)
set synonim
double myMinimumRange
minimun Range
bool isSVCPermission() const
return true if attribute is a VehicleClass
int getPositionListed() const
get position in list (used in frames for listing attributes with certain sort)
bool isBool() const
return true if attribute is boolean
const std::string & getAttrStr() const
get XML Attribute
std::string getDescription() const
return a description of attribute
bool myDefaultActivated
default activated (by default false)
bool isFlowDefinition() const
return true if attribute is part of a flow definition
bool isPosition() const
return true if attribute is a position
bool hasAttrRange() const
return true if Attr correspond to an element that only accept a range of values
bool getDefaultActivated() const
get default active value
bool isList() const
return true if attribute is a list
SumoXMLAttr myAttrSynonym
Attribute written in XML (If is SUMO_ATTR_NOTHING), original Attribute will be written)
bool isNumerical() const
return true if attribute is numerical (int or float)
bool isInt() const
return true if attribute is an integer
bool isDiscrete() const
return true if attribute is discrete
void setDefaultActivated(const bool value)
set default activated value
GNEAttributeProperties()
default constructor
bool isCopyable() const
return true if attribute ID is copyable
const std::string & getDefaultValue() const
get default value
const std::string & getDefinition() const
get default value
double getMinimumRange() const
get minimum range
bool hasAttrSynonym() const
return true if Attr correspond to an element that will be written in XML with another name
bool isVType() const
return true if attribute is a VType or vTypeDistribution
int myAttributeProperty
Property of attribute.
bool isUnique() const
return true if attribute is unique
double myMaximumRange
maxium Range
bool isString() const
return true if attribute is a string
bool isExtended() const
return true if attribute is extended
std::vector< std::string > myDiscreteValues
discrete values that can take this Attribute (by default empty)
SumoXMLAttr getAttrSynonym() const
get tag synonym
bool isFloat() const
return true if attribute is a float
void checkAttributeIntegrity() const
check Attribute integrity (For example, throw an exception if tag has a Float default value,...
void setRange(const double minimum, const double maximum)
set range
GNETagProperties * myTagPropertyParent
pointer to tagProperty parent
SumoXMLAttr myAttribute
XML Attribute.
std::string myDefinition
text with a definition of attribute
bool isSUMOTime() const
return true if attribute is a SUMOTime
std::string myDefaultValue
default value (by default empty)
bool hasDefaultValue() const
return true if attribute owns a default value
bool isActivatable() const
return true if attribute is activatable
bool requireUpdateGeometry() const
return true if attribute requires a update geometry in setAttribute(...)
bool isPositive() const
return true if attribute is positive
const std::vector< std::string > & getDiscreteValues() const
get discrete values
bool isFilename() const
return true if attribute is a filename
SumoXMLAttr getAttr() const
get XML Attribute
bool isSecuential() const
return true if attribute is sequential
const GNETagProperties & getTagPropertyParent() const
get reference to tagProperty parent
std::string myAttrStr
string with the Attribute in text format (to avoid unnecesaries toStrings(...) calls)
bool hasAutomaticID() const
return true if attribute ID can generate an automatic ID
std::vector< GNEAttributeProperties >::const_iterator end() const
get end of attribute values (used for iterate)
std::vector< GNEAttributeProperties >::const_iterator begin() const
get begin of attribute values (used for iterate)