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.org/sumo
3 // Copyright (C) 2001-2022 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 
30 // ===========================================================================
31 // method definitions
32 // ===========================================================================
33 
35  myAttribute(SUMO_ATTR_NOTHING),
36  myTagPropertyParent(nullptr),
37  myAttrStr(toString(SUMO_ATTR_NOTHING)),
38  myAttributeProperty(STRING),
39  myDefinition(""),
40  myDefaultValue(""),
41  myDefaultActivated(false),
42  myAttrSynonym(SUMO_ATTR_NOTHING),
43  myMinimumRange(0),
44  myMaximumRange(0) {}
45 
46 
47 GNEAttributeProperties::GNEAttributeProperties(const SumoXMLAttr attribute, const int attributeProperty, const std::string& definition, std::string defaultValue) :
48  myAttribute(attribute),
49  myTagPropertyParent(nullptr),
50  myAttrStr(toString(attribute)),
51  myAttributeProperty(attributeProperty),
52  myDefinition(definition),
53  myDefaultValue(defaultValue),
54  myDefaultActivated(false),
55  myAttrSynonym(SUMO_ATTR_NOTHING),
56  myMinimumRange(0),
57  myMaximumRange(0) {
58  // empty definition aren't valid
59  if (definition.empty()) {
60  throw FormatException("Missing definition for AttributeProperty '" + toString(attribute) + "'");
61  }
62  // if default value isn't empty, but attribute doesn't support default values, throw exception.
63  if (!defaultValue.empty() && !(attributeProperty & DEFAULTVALUE)) {
64  throw FormatException("AttributeProperty for '" + toString(attribute) + "' doesn't support default values");
65  }
66  // Attributes cannot be flowdefinition and enabilitablet at the same time
67  if ((attributeProperty & FLOWDEFINITION) && (attributeProperty & ACTIVATABLE)) {
68  throw FormatException("Attribute '" + toString(attribute) + "' cannot be flowdefinition and activatable at the same time");
69  }
70 }
71 
72 
74 
75 
76 void
78  // check that positive attributes correspond only to a int, floats or SUMOTimes
79  if (isPositive() && !(isInt() || isFloat() || isSUMOTime())) {
80  throw FormatException("Only int, floats or SUMOTimes can be positive");
81  }
82  // check that secuential attributes correspond to a list
83  if (isSecuential() && !isList()) {
84  throw FormatException("Secuential property only is compatible with list properties");
85  }
86  // check that synonym attribute isn't nothing
88  throw FormatException("synonym attribute cannot be nothing");
89  }
90  // check that ranges are valid
91  if (hasAttrRange()) {
93  throw FormatException("empty range");
94  } else if ((myMinimumRange == 0) && (myMaximumRange == 0)) {
95  throw FormatException("non-defined range");
96  } else if ((myMaximumRange - myMinimumRange) <= 0) {
97  throw FormatException("invalid range");
98  }
99  }
100 }
101 
102 
103 void
104 GNEAttributeProperties::setDiscreteValues(const std::vector<std::string>& discreteValues) {
105  if (isDiscrete()) {
106  myDiscreteValues = discreteValues;
107  } else {
108  throw FormatException("AttributeProperty doesn't support discrete values");
109  }
110 }
111 
112 
113 void
115  if (isActivatable()) {
116  myDefaultActivated = value;
117  } else {
118  throw FormatException("AttributeProperty doesn't support default activated");
119  }
120 }
121 
122 
123 void
125  if (hasAttrSynonym()) {
126  myAttrSynonym = synonym;
127  } else {
128  throw FormatException("AttributeProperty doesn't support synonyms");
129  }
130 }
131 
132 
133 void
134 GNEAttributeProperties::setRange(const double minimum, const double maximum) {
135  if (hasAttrRange()) {
136  myMinimumRange = minimum;
137  myMaximumRange = maximum;
138  // check that given range is valid
140  throw FormatException("empty range");
141  } else if ((myMinimumRange == 0) && (myMaximumRange == 0)) {
142  throw FormatException("non-defined range");
143  } else if ((myMaximumRange - myMinimumRange) <= 0) {
144  throw FormatException("invalid range");
145  }
146  } else {
147  throw FormatException("AttributeProperty doesn't support ranges");
148  }
149 }
150 
151 
152 void
154  myTagPropertyParent = tagPropertyParent;
155 }
156 
157 
160  return myAttribute;
161 }
162 
163 
164 const std::string&
166  return myAttrStr;
167 }
168 
169 
170 const GNETagProperties&
172  return *myTagPropertyParent;
173 }
174 
175 
176 int
178  for (auto i = myTagPropertyParent->begin(); i != myTagPropertyParent->end(); i++) {
179  if (i->getAttr() == myAttribute) {
180  return (int)(i - myTagPropertyParent->begin());
181  }
182  }
183  throw ProcessError("Attribute wasn't found in myTagPropertyParent");
184 }
185 
186 
187 const std::string&
189  return myDefinition;
190 }
191 
192 
193 const std::string&
195  return myDefaultValue;
196 }
197 
198 
199 bool
201  return myDefaultActivated;
202 }
203 
204 
205 std::string
207  std::string pre;
208  std::string type;
209  std::string plural;
210  std::string last;
211  // pre type
212  if ((myAttributeProperty & LIST) != 0) {
213  pre += "list of ";
214  if ((myAttributeProperty & VCLASS) != 0) {
215  plural = "es";
216  } else {
217  plural = "s";
218  }
219  }
220  if ((myAttributeProperty & POSITIVE) != 0) {
221  pre += "positive ";
222  }
223  if ((myAttributeProperty & DISCRETE) != 0) {
224  pre += "discrete ";
225  }
226  if ((myAttributeProperty & UNIQUE) != 0) {
227  pre += "unique ";
228  }
229  if ((myAttributeProperty & VCLASSES) != 0) {
230  pre += "vclasses ";
231  }
232  // type
233  if ((myAttributeProperty & INT) != 0) {
234  type = "integer";
235  }
236  if ((myAttributeProperty & FLOAT) != 0) {
237  type = "float";
238  }
239  if ((myAttributeProperty & SUMOTIME) != 0) {
240  type = "SUMOTime";
241  }
242  if ((myAttributeProperty & BOOL) != 0) {
243  type = "boolean";
244  }
245  if ((myAttributeProperty & STRING) != 0) {
246  type = "string";
247  }
248  if ((myAttributeProperty & POSITION) != 0) {
249  type = "position";
250  }
251  if ((myAttributeProperty & COLOR) != 0) {
252  type = "color";
253  }
254  if ((myAttributeProperty & VCLASS) != 0) {
255  type = "VClass";
256  }
257  if ((myAttributeProperty & FILENAME) != 0) {
258  type = "filename";
259  }
260  if ((myAttributeProperty & PROBABILITY) != 0) {
261  type = "probability";
262  last = "[0, 1]";
263  }
264  if ((myAttributeProperty & ANGLE) != 0) {
265  type = "angle";
266  last = "[0, 360]";
267  }
268  return pre + type + plural + last;
269 }
270 
271 
272 const std::vector<std::string>&
274  return myDiscreteValues;
275 }
276 
277 
280  if (hasAttrSynonym()) {
281  return myAttrSynonym;
282  } else {
283  throw ProcessError("Attr doesn't support synonym");
284  }
285 }
286 
287 
288 double
290  if (hasAttrRange()) {
291  return myMinimumRange;
292  } else {
293  throw ProcessError("Attr doesn't support range");
294  }
295 }
296 
297 
298 double
300  if (hasAttrRange()) {
301  return myMaximumRange;
302  } else {
303  throw ProcessError("Attr doesn't support range");
304  }
305 }
306 
307 
308 bool
310  return (myAttributeProperty & DEFAULTVALUE) != 0;
311 }
312 
313 
314 bool
316  return (myAttributeProperty & SYNONYM) != 0;
317 }
318 
319 bool
321  return (myAttributeProperty & RANGE) != 0;
322 }
323 
324 
325 bool
327  return (myAttributeProperty & INT) != 0;
328 }
329 
330 
331 bool
333  return (myAttributeProperty & FLOAT) != 0;
334 }
335 
336 
337 bool
339  return (myAttributeProperty & SUMOTIME) != 0;
340 }
341 
342 
343 bool
345  return (myAttributeProperty & BOOL) != 0;
346 }
347 
348 
349 bool
351  return (myAttributeProperty & STRING) != 0;
352 }
353 
354 
355 bool
357  return (myAttributeProperty & POSITION) != 0;
358 }
359 
360 
361 bool
363  return (myAttributeProperty & PROBABILITY) != 0;
364 }
365 
366 
367 bool
369  return (myAttributeProperty & (INT | FLOAT | SUMOTIME)) != 0;
370 }
371 
372 
373 bool
375  return (myAttributeProperty & POSITIVE) != 0;
376 }
377 
378 
379 bool
381  return (myAttributeProperty & COLOR) != 0;
382 }
383 
384 
385 bool
387  return (myAttributeProperty & VTYPE) != 0;
388 }
389 
390 
391 bool
393  return (myAttributeProperty & FILENAME) != 0;
394 }
395 
396 
397 bool
399  return (myAttributeProperty & VCLASS) != 0;
400 }
401 
402 
403 bool
405  return ((myAttributeProperty & LIST) != 0) && ((myAttributeProperty & VCLASS) != 0);
406 }
407 
408 
409 bool
411  return (myAttributeProperty & LIST) != 0;
412 }
413 
414 
415 bool
417  return (myAttributeProperty & SECUENCIAL) != 0;
418 }
419 
420 
421 bool
423  return (myAttributeProperty & UNIQUE) != 0;
424 }
425 
426 
427 bool
429  return (myAttributeProperty & DISCRETE) != 0;
430 }
431 
432 
433 bool
435  return (myAttributeProperty & VCLASSES) != 0;
436 }
437 
438 
439 bool
441  return (myAttributeProperty & EXTENDED) != 0;
442 }
443 
444 
445 bool
447  return (myAttributeProperty & UPDATEGEOMETRY) != 0;
448 }
449 
450 
451 bool
453  return (myAttributeProperty & ACTIVATABLE) != 0;
454 }
455 
456 
457 bool
459  return (myAttributeProperty & FLOWDEFINITION) != 0;
460 }
461 
462 
463 bool
465  return (myAttributeProperty & AUTOMATICID) != 0;
466 }
467 
468 /****************************************************************************/
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 atribute is a VehicleClass
bool isProbability() const
return true if atribute is a probability
bool isColor() const
return true if atribute 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 atribute 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 atribute 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 atribute is part of a flow definition
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 atribute 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 atribute is numerical (int or float)
bool isInt() const
return true if atribute is an integer
bool isDiscrete() const
return true if atribute 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 atribute is a VType or vTypeDistribution
int myAttributeProperty
Property of attribute.
bool isUnique() const
return true if atribute is unique
double myMaximumRange
maxium Range
bool isString() const
return true if atribute is a string
bool isExtended() const
return true if atribute 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 atribute 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 atribute is a list of VClasses
bool isSUMOTime() const
return true if atribute is a SUMOTime
std::string myDefaultValue
default value (by default empty)
bool isposition() const
return true if atribute is a position
bool hasDefaultValue() const
return true if attribute owns a default value
bool isActivatable() const
return true if atribute is activatable
bool requireUpdateGeometry() const
return true if atribute requires a update geometry in setAttribute(...)
bool isPositive() const
return true if atribute is positive
const std::vector< std::string > & getDiscreteValues() const
get discrete values
bool isFilename() const
return true if atribute is a filename
SumoXMLAttr getAttr() const
get XML Attribute
bool isSecuential() const
return true if atribute 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)