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 #endif // DEBUG
116 }
117 
118 
119 void
120 GNEAttributeProperties::setDiscreteValues(const std::vector<std::string>& discreteValues) {
121  if (isDiscrete()) {
122  myDiscreteValues = discreteValues;
123  } else {
124  throw FormatException("AttributeProperty doesn't support discrete values");
125  }
126 }
127 
128 
129 void
131  if (isActivatable()) {
132  myDefaultActivated = value;
133  } else {
134  throw FormatException("AttributeProperty doesn't support default activated");
135  }
136 }
137 
138 
139 void
141  if (hasAttrSynonym()) {
142  myAttrSynonym = synonym;
143  } else {
144  throw FormatException("AttributeProperty doesn't support synonyms");
145  }
146 }
147 
148 
149 void
150 GNEAttributeProperties::setRange(const double minimum, const double maximum) {
151  if (hasAttrRange()) {
152  myMinimumRange = minimum;
153  myMaximumRange = maximum;
154  // check that given range is valid
156  throw FormatException("empty range");
157  } else if ((myMinimumRange == 0) && (myMaximumRange == 0)) {
158  throw FormatException("non-defined range");
159  } else if ((myMaximumRange - myMinimumRange) <= 0) {
160  throw FormatException("invalid range");
161  }
162  } else {
163  throw FormatException("AttributeProperty doesn't support ranges");
164  }
165 }
166 
167 
168 void
170  myTagPropertyParent = tagPropertyParent;
171 }
172 
173 
176  return myAttribute;
177 }
178 
179 
180 const std::string&
182  return myAttrStr;
183 }
184 
185 
186 const GNETagProperties&
188  return *myTagPropertyParent;
189 }
190 
191 
192 int
194  for (auto i = myTagPropertyParent->begin(); i != myTagPropertyParent->end(); i++) {
195  if (i->getAttr() == myAttribute) {
196  return (int)(i - myTagPropertyParent->begin());
197  }
198  }
199  throw ProcessError("Attribute wasn't found in myTagPropertyParent");
200 }
201 
202 
203 const std::string&
205  return myDefinition;
206 }
207 
208 
209 const std::string&
211  return myDefaultValue;
212 }
213 
214 
215 bool
217  return myDefaultActivated;
218 }
219 
220 
221 std::string
223  std::string pre;
224  std::string type;
225  std::string plural;
226  std::string last;
227  // pre type
228  if ((myAttributeProperty & LIST) != 0) {
229  pre += "list of ";
230  if ((myAttributeProperty & VCLASS) != 0) {
231  plural = "es";
232  } else {
233  plural = "s";
234  }
235  }
236  if ((myAttributeProperty & POSITIVE) != 0) {
237  pre += "non-negative ";
238  }
239  if ((myAttributeProperty & DISCRETE) != 0) {
240  pre += "discrete ";
241  }
242  if ((myAttributeProperty & UNIQUE) != 0) {
243  pre += "unique ";
244  }
245  // type
246  if ((myAttributeProperty & INT) != 0) {
247  type = "integer";
248  }
249  if ((myAttributeProperty & FLOAT) != 0) {
250  type = "float";
251  }
252  if ((myAttributeProperty & SUMOTIME) != 0) {
253  type = "SUMOTime";
254  }
255  if ((myAttributeProperty & BOOL) != 0) {
256  type = "boolean";
257  }
258  if ((myAttributeProperty & STRING) != 0) {
259  type = "string";
260  }
261  if ((myAttributeProperty & POSITION) != 0) {
262  type = "position";
263  }
264  if ((myAttributeProperty & COLOR) != 0) {
265  type = "color";
266  }
267  if ((myAttributeProperty & VCLASS) != 0) {
268  type = "vClass";
269  }
270  if ((myAttributeProperty & FILENAME) != 0) {
271  type = "filename";
272  }
273  if ((myAttributeProperty & PROBABILITY) != 0) {
274  type = "probability";
275  last = "[0, 1]";
276  }
277  if ((myAttributeProperty & ANGLE) != 0) {
278  type = "angle";
279  last = "[0, 360]";
280  }
281  return pre + type + plural + last;
282 }
283 
284 
285 const std::vector<std::string>&
287  return myDiscreteValues;
288 }
289 
290 
293  if (hasAttrSynonym()) {
294  return myAttrSynonym;
295  } else {
296  throw ProcessError("Attr doesn't support synonym");
297  }
298 }
299 
300 
301 double
303  if (hasAttrRange()) {
304  return myMinimumRange;
305  } else {
306  throw ProcessError("Attr doesn't support range");
307  }
308 }
309 
310 
311 double
313  if (hasAttrRange()) {
314  return myMaximumRange;
315  } else {
316  throw ProcessError("Attr doesn't support range");
317  }
318 }
319 
320 
321 bool
323  return (myAttributeProperty & DEFAULTVALUE) != 0;
324 }
325 
326 
327 bool
329  return (myAttributeProperty & SYNONYM) != 0;
330 }
331 
332 bool
334  return (myAttributeProperty & RANGE) != 0;
335 }
336 
337 
338 bool
340  return (myAttributeProperty & INT) != 0;
341 }
342 
343 
344 bool
346  return (myAttributeProperty & FLOAT) != 0;
347 }
348 
349 
350 bool
352  return (myAttributeProperty & SUMOTIME) != 0;
353 }
354 
355 
356 bool
358  return (myAttributeProperty & BOOL) != 0;
359 }
360 
361 
362 bool
364  return (myAttributeProperty & STRING) != 0;
365 }
366 
367 
368 bool
370  return (myAttributeProperty & POSITION) != 0;
371 }
372 
373 
374 bool
376  return (myAttributeProperty & PROBABILITY) != 0;
377 }
378 
379 
380 bool
382  return (myAttributeProperty & (INT | FLOAT | SUMOTIME)) != 0;
383 }
384 
385 
386 bool
388  return (myAttributeProperty & POSITIVE) != 0;
389 }
390 
391 
392 bool
394  return (myAttributeProperty & COLOR) != 0;
395 }
396 
397 
398 bool
400  return (myAttributeProperty & VTYPE) != 0;
401 }
402 
403 
404 bool
406  return (myAttributeProperty & FILENAME) != 0;
407 }
408 
409 
410 bool
412  return (myAttributeProperty & VCLASS) != 0;
413 }
414 
415 
416 bool
418  return ((myAttributeProperty & LIST) != 0) && ((myAttributeProperty & VCLASS) != 0);
419 }
420 
421 
422 bool
424  return (myAttributeProperty & LIST) != 0;
425 }
426 
427 
428 bool
430  return (myAttributeProperty & SECUENCIAL) != 0;
431 }
432 
433 
434 bool
436  return (myAttributeProperty & UNIQUE) != 0;
437 }
438 
439 
440 bool
442  return (myAttributeProperty & DISCRETE) != 0;
443 }
444 
445 
446 bool
448  return (myAttributeProperty & EXTENDED) != 0;
449 }
450 
451 
452 bool
454  return (myAttributeProperty & UPDATEGEOMETRY) != 0;
455 }
456 
457 
458 bool
460  return (myAttributeProperty & ACTIVATABLE) != 0;
461 }
462 
463 
464 bool
466  return (myAttributeProperty & FLOWDEFINITION) != 0;
467 }
468 
469 
470 bool
472  return (myAttributeProperty & AUTOMATICID) != 0;
473 }
474 
475 /****************************************************************************/
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ SUMO_ATTR_NOTHING
invalid attribute
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
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)