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-2023 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
27#include "GNETagProperties.h"
28
29#include "GNEAttributeCarrier.h"
30
31
32// ===========================================================================
33// method definitions
34// ===========================================================================
35
37
38
39GNEAttributeProperties::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
68void
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
119void
120GNEAttributeProperties::setDiscreteValues(const std::vector<std::string>& discreteValues, bool showAll) {
121 if (isDiscrete()) {
122 myDiscreteValues = discreteValues;
123 myShowAllDiscreteValues = showAll;
124 } else {
125 throw FormatException("AttributeProperty doesn't support discrete values");
126 }
127}
128
129
130void
132 if (isActivatable()) {
133 myDefaultActivated = value;
134 } else {
135 throw FormatException("AttributeProperty doesn't support default activated");
136 }
137}
138
139
140void
142 if (hasAttrSynonym()) {
143 myAttrSynonym = synonym;
144 } else {
145 throw FormatException("AttributeProperty doesn't support synonyms");
146 }
147}
148
149
150void
151GNEAttributeProperties::setRange(const double minimum, const double maximum) {
152 if (hasAttrRange()) {
153 myMinimumRange = minimum;
154 myMaximumRange = maximum;
155 // check that given range is valid
157 throw FormatException("empty range");
158 } else if ((myMinimumRange == 0) && (myMaximumRange == 0)) {
159 throw FormatException("non-defined range");
160 } else if ((myMaximumRange - myMinimumRange) <= 0) {
161 throw FormatException("invalid range");
162 }
163 } else {
164 throw FormatException("AttributeProperty doesn't support ranges");
165 }
166}
167
168
169void
173
174
179
180
181const std::string&
185
186
187const GNETagProperties&
191
192
193int
195 for (auto i = myTagPropertyParent->begin(); i != myTagPropertyParent->end(); i++) {
196 if (i->getAttr() == myAttribute) {
197 return (int)(i - myTagPropertyParent->begin());
198 }
199 }
200 throw ProcessError("Attribute wasn't found in myTagPropertyParent");
201}
202
203
204const std::string&
208
209
210const std::string&
214
215
216bool
220
221
222std::string
224 std::string pre;
225 std::string type;
226 std::string plural;
227 std::string last;
228 // pre type
229 if ((myAttributeProperty & LIST) != 0) {
230 pre += "list of ";
231 if ((myAttributeProperty & VCLASS) != 0) {
232 plural = "es";
233 } else {
234 plural = "s";
235 }
236 }
237 if ((myAttributeProperty & POSITIVE) != 0) {
238 pre += "non-negative ";
239 }
240 if ((myAttributeProperty & DISCRETE) != 0) {
241 pre += "discrete ";
242 }
243 if ((myAttributeProperty & UNIQUE) != 0) {
244 pre += "unique ";
245 }
246 // type
247 if ((myAttributeProperty & INT) != 0) {
248 type = "integer";
249 }
250 if ((myAttributeProperty & FLOAT) != 0) {
251 type = "float";
252 }
253 if ((myAttributeProperty & SUMOTIME) != 0) {
254 type = "SUMOTime";
255 }
256 if ((myAttributeProperty & BOOL) != 0) {
257 type = "boolean";
258 }
259 if ((myAttributeProperty & STRING) != 0) {
260 type = "string";
261 }
262 if ((myAttributeProperty & POSITION) != 0) {
263 type = "position";
264 }
265 if ((myAttributeProperty & COLOR) != 0) {
266 type = "color";
267 }
268 if ((myAttributeProperty & VCLASS) != 0) {
269 type = "vClass";
270 }
271 if ((myAttributeProperty & FILENAME) != 0) {
272 type = "filename";
273 }
274 if ((myAttributeProperty & PROBABILITY) != 0) {
275 type = "probability";
276 last = "[0, 1]";
277 }
278 if ((myAttributeProperty & ANGLE) != 0) {
279 type = "angle";
280 last = "[0, 360]";
281 }
282 return pre + type + plural + last;
283}
284
285
286const std::vector<std::string>&
290
291
294 if (hasAttrSynonym()) {
295 return myAttrSynonym;
296 } else {
297 throw ProcessError("Attr doesn't support synonym");
298 }
299}
300
301
302double
304 if (hasAttrRange()) {
305 return myMinimumRange;
306 } else {
307 throw ProcessError("Attr doesn't support range");
308 }
309}
310
311
312double
314 if (hasAttrRange()) {
315 return myMaximumRange;
316 } else {
317 throw ProcessError("Attr doesn't support range");
318 }
319}
320
321
322bool
326
327
328bool
332
333bool
337
338
339bool
341 return (myAttributeProperty & INT) != 0;
342}
343
344
345bool
349
350
351bool
355
356
357bool
359 return (myAttributeProperty & BOOL) != 0;
360}
361
362
363bool
367
368
369bool
373
374
375bool
379
380
381bool
385
386
387bool
391
392
393bool
397
398
399bool
403
404
405bool
409
410
411bool
415
416
417bool
421
422
423bool
425 return (myAttributeProperty & LIST) != 0;
426}
427
428
429bool
433
434
435bool
439
440
441bool
445
446
447bool
451
452
453bool
457
458
459bool
463
464
465bool
469
470
471bool
475
476
477bool
481
482
483bool
487
488/****************************************************************************/
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 setSynonym(const SumoXMLAttr synonym)
set synonim
double myMinimumRange
minimun Range
bool isSVCPermission() const
return true if attribute is a VehicleClass
bool showAllDiscreteValues() const
show all discrete values in the comboBox
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
bool myShowAllDiscreteValues
show all discrete values in ComboBox
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 isVClasses() const
return true if attribute is a list of VClasses
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(...)
void setDiscreteValues(const std::vector< std::string > &discreteValues, bool showAll)
set discrete values
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)