Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
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#include "GNEAttributeCarrier.h"
22#include "GNETagProperties.h"
24
25// ===========================================================================
26// method definitions
27// ===========================================================================
28
30
31
32GNEAttributeProperties::GNEAttributeProperties(const SumoXMLAttr attribute, const int attributeProperty, const std::string& definition, std::string defaultValue) :
33 myAttribute(attribute),
34 myTagPropertyParent(nullptr),
35 myAttrStr(toString(attribute)),
36 myAttributeProperty(attributeProperty),
37 myDefinition(definition),
38 myDefaultValue(defaultValue),
39 myDefaultActivated(false),
40 myAttrSynonym(SUMO_ATTR_NOTHING),
41 myMinimumRange(0),
42 myMaximumRange(0) {
43 // empty definition aren't valid
44 if (definition.empty()) {
45 throw FormatException("Missing definition for AttributeProperty '" + toString(attribute) + "'");
46 }
47 // if default value isn't empty, but attribute doesn't support default values, throw exception.
48 if (!defaultValue.empty() && !(attributeProperty & DEFAULTVALUE)) {
49 throw FormatException("AttributeProperty for '" + toString(attribute) + "' doesn't support default values");
50 }
51 // Attributes cannot be flowdefinition and enabilitablet at the same time
52 if ((attributeProperty & FLOW) && (attributeProperty & ACTIVATABLE)) {
53 throw FormatException("Attribute '" + toString(attribute) + "' cannot be flowdefinition and activatable at the same time");
54 }
55}
56
57
59
60
61void
63 // check integrity only in debug mode
64#ifdef DEBUG
65 // check that default values can be parsed (only in debug mode)
66 if (hasDefaultValue()) {
67 if (isInt() && !GNEAttributeCarrier::canParse<int>(myDefaultValue)) {
68 throw FormatException("Default value cannot be parsed to int");
69 }
70 if (isFloat() && !GNEAttributeCarrier::canParse<double>(myDefaultValue)) {
71 throw FormatException("Default value cannot be parsed to float");
72 }
73 if (isSUMOTime() && !GNEAttributeCarrier::canParse<SUMOTime>(myDefaultValue)) {
74 throw FormatException("Default value cannot be parsed to SUMOTime");
75 }
76 if (isBool() && !GNEAttributeCarrier::canParse<bool>(myDefaultValue)) {
77 throw FormatException("Default value cannot be parsed to bool");
78 }
79 if (isPosition() && (myDefaultValue.size() > 0) && !GNEAttributeCarrier::canParse<Position>(myDefaultValue)) {
80 throw FormatException("Default value cannot be parsed to position");
81 }
82 if (isColor() && !GNEAttributeCarrier::canParse<RGBColor>(myDefaultValue)) {
83 throw FormatException("Default value cannot be parsed to color");
84 }
85 }
86 // check that positive attributes correspond only to a int, floats or SUMOTimes
87 if (isPositive() && !(isInt() || isFloat() || isSUMOTime())) {
88 throw FormatException("Only int, floats or SUMOTimes can be positive");
89 }
90 // check that secuential attributes correspond to a list
91 if (isSecuential() && !isList()) {
92 throw FormatException("Secuential property only is compatible with list properties");
93 }
94 // check that synonym attribute isn't nothing
96 throw FormatException("synonym attribute cannot be nothing");
97 }
98 // check that ranges are valid
99 if (hasAttrRange()) {
101 throw FormatException("empty range");
102 } else if ((myMinimumRange == 0) && (myMaximumRange == 0)) {
103 throw FormatException("non-defined range");
104 } else if ((myMaximumRange - myMinimumRange) <= 0) {
105 throw FormatException("invalid range");
106 }
107 }
108 // check that unique attributes aren't copyables
109 if (isUnique() && isCopyable()) {
110 throw FormatException("Unique attributes aren't copyables");
111 }
112#endif // DEBUG
113}
114
115
116void
117GNEAttributeProperties::setDiscreteValues(const std::vector<std::string>& discreteValues) {
118 if (isDiscrete()) {
119 myDiscreteValues = discreteValues;
120 } else {
121 throw FormatException("AttributeProperty doesn't support discrete values");
122 }
123}
124
125
126void
128 if (isActivatable()) {
129 myDefaultActivated = value;
130 } else {
131 throw FormatException("AttributeProperty doesn't support default activated");
132 }
133}
134
135
136void
138 if (hasAttrSynonym()) {
139 myAttrSynonym = synonym;
140 } else {
141 throw FormatException("AttributeProperty doesn't support synonyms");
142 }
143}
144
145
146void
147GNEAttributeProperties::setRange(const double minimum, const double maximum) {
148 if (hasAttrRange()) {
149 myMinimumRange = minimum;
150 myMaximumRange = maximum;
151 // check that given range is valid
153 throw FormatException("empty range");
154 } else if ((myMinimumRange == 0) && (myMaximumRange == 0)) {
155 throw FormatException("non-defined range");
156 } else if ((myMaximumRange - myMinimumRange) <= 0) {
157 throw FormatException("invalid range");
158 }
159 } else {
160 throw FormatException("AttributeProperty doesn't support ranges");
161 }
162}
163
164
165void
169
170
175
176
177const std::string&
181
182
183const GNETagProperties&
187
188
189int
191 for (auto i = myTagPropertyParent->begin(); i != myTagPropertyParent->end(); i++) {
192 if (i->getAttr() == myAttribute) {
193 return (int)(i - myTagPropertyParent->begin());
194 }
195 }
196 throw ProcessError("Attribute wasn't found in myTagPropertyParent");
197}
198
199
200const std::string&
204
205
206const std::string&
210
211
212bool
216
217
218std::string
220 std::string pre;
221 std::string type;
222 std::string plural;
223 std::string last;
224 // pre type
225 if ((myAttributeProperty & LIST) != 0) {
226 pre += "list of ";
227 if ((myAttributeProperty & VCLASS) != 0) {
228 plural = "es";
229 } else {
230 plural = "s";
231 }
232 }
233 if ((myAttributeProperty & POSITIVE) != 0) {
234 pre += "non-negative ";
235 }
236 if ((myAttributeProperty & DISCRETE) != 0) {
237 pre += "discrete ";
238 }
239 if ((myAttributeProperty & UNIQUE) != 0) {
240 pre += "unique ";
241 }
242 // type
243 if ((myAttributeProperty & INT) != 0) {
244 type = "integer";
245 }
246 if ((myAttributeProperty & FLOAT) != 0) {
247 type = "float";
248 }
249 if ((myAttributeProperty & SUMOTIME) != 0) {
250 type = "SUMOTime";
251 }
252 if ((myAttributeProperty & BOOL) != 0) {
253 type = "boolean";
254 }
255 if ((myAttributeProperty & STRING) != 0) {
256 type = "string";
257 }
258 if ((myAttributeProperty & POSITION) != 0) {
259 type = "position";
260 }
261 if ((myAttributeProperty & COLOR) != 0) {
262 type = "color";
263 }
264 if ((myAttributeProperty & VCLASS) != 0) {
265 type = "vClass";
266 }
267 if ((myAttributeProperty & FILENAME) != 0) {
268 type = "filename";
269 }
270 if ((myAttributeProperty & PROBABILITY) != 0) {
271 type = "probability";
272 last = "[0, 1]";
273 }
274 if ((myAttributeProperty & ANGLE) != 0) {
275 type = "angle";
276 last = "[0, 360]";
277 }
278 return pre + type + plural + last;
279}
280
281
282const std::vector<std::string>&
286
287
290 if (hasAttrSynonym()) {
291 return myAttrSynonym;
292 } else {
293 throw ProcessError("Attr doesn't support synonym");
294 }
295}
296
297
298double
300 if (hasAttrRange()) {
301 return myMinimumRange;
302 } else {
303 throw ProcessError("Attr doesn't support range");
304 }
305}
306
307
308double
310 if (hasAttrRange()) {
311 return myMaximumRange;
312 } else {
313 throw ProcessError("Attr doesn't support range");
314 }
315}
316
317
318bool
322
323
324bool
328
329bool
333
334
335bool
337 return (myAttributeProperty & INT) != 0;
338}
339
340
341bool
345
346
347bool
351
352
353bool
355 return (myAttributeProperty & BOOL) != 0;
356}
357
358
359bool
363
364
365bool
369
370
371bool
375
376
377bool
381
382
383bool
387
388
389bool
393
394
395bool
399
400
401bool
405
406
407bool
411
412
413bool
417
418
419bool
421 return (myAttributeProperty & LIST) != 0;
422}
423
424
425bool
429
430
431bool
435
436
437bool
441
442
443bool
447
448
449bool
453
454
455bool
459
460
461bool
463 return (myAttributeProperty & FLOW) != 0;
464}
465
466
467bool
471
472
473bool
477
478
479bool
483
484
485bool
487 return (myAttributeProperty & GEO) != 0;
488}
489
490bool
494
495/****************************************************************************/
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 isGEO() const
return true if attribute is GEO
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 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 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,...
bool isNetedit() const
return true if attribute is exclusive of netedit
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 isFlow() const
return true if attribute is part of a flow definition
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 isAlwaysEnabled() const
return true if attribute is always enabled
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)