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-2020 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  myAttrSynonym(SUMO_ATTR_NOTHING),
42  myMinimumRange(0),
43  myMaximumRange(0) {}
44 
45 
46 GNEAttributeProperties::GNEAttributeProperties(const SumoXMLAttr attribute, const int attributeProperty, const std::string& definition, std::string defaultValue) :
47  myAttribute(attribute),
48  myTagPropertyParent(nullptr),
49  myAttrStr(toString(attribute)),
50  myAttributeProperty(attributeProperty),
51  myDefinition(definition),
52  myDefaultValue(defaultValue),
53  myAttrSynonym(SUMO_ATTR_NOTHING),
54  myMinimumRange(0),
55  myMaximumRange(0) {
56  // empty definition aren't valid
57  if (definition.empty()) {
58  throw FormatException("Missing definition for AttributeProperty '" + toString(attribute) + "'");
59  }
60  // if default value isn't empty, but attribute doesn't support default values, throw exception.
61  if (!defaultValue.empty() && !(attributeProperty & DEFAULTVALUESTATIC)) {
62  throw FormatException("AttributeProperty for '" + toString(attribute) + "' doesn't support default values");
63  }
64  // default value cannot be static and mutables at the same time
65  if ((attributeProperty & DEFAULTVALUESTATIC) && (attributeProperty & DEFAULTVALUEMUTABLE)) {
66  throw FormatException("Default value for attribute '" + toString(attribute) + "' cannot be static and mutable at the same time");
67  }
68  // Attributes that can write optionally their values in XML must have either a static or a mutable efault value
69  if ((attributeProperty & XMLOPTIONAL) && !((attributeProperty & DEFAULTVALUESTATIC) || (attributeProperty & DEFAULTVALUEMUTABLE))) {
70  throw FormatException("Attribute '" + toString(attribute) + "' requires a either static or mutable default value");
71  }
72  // Attributes cannot be flowdefinition and enabilitablet at the same time
73  if ((attributeProperty & FLOWDEFINITION) && (attributeProperty & ACTIVATABLE)) {
74  throw FormatException("Attribute '" + toString(attribute) + "' cannot be flowdefinition and activatable at the same time");
75  }
76 }
77 
78 
80 
81 
82 void
84  // check that positive attributes correspond only to a int, floats or SUMOTimes
85  if (isPositive() && !(isInt() || isFloat() || isSUMOTime())) {
86  throw FormatException("Only int, floats or SUMOTimes can be positive");
87  }
88  // check that secuential attributes correspond to a list
89  if (isSecuential() && !isList()) {
90  throw FormatException("Secuential property only is compatible with list properties");
91  }
92  // check that synonym attribute isn't nothing
94  throw FormatException("synonym attribute cannot be nothing");
95  }
96  // check that ranges are valid
97  if (hasAttrRange()) {
99  throw FormatException("empty range");
100  } else if ((myMinimumRange == 0) && (myMaximumRange == 0)) {
101  throw FormatException("non-defined range");
102  } else if ((myMaximumRange - myMinimumRange) <= 0) {
103  throw FormatException("invalid range");
104  }
105  }
106  // check that positive attributes correspond only to a int, floats or SUMOTimes
108  throw FormatException("if attribute is optional, must have either a static or dynamic default value");
109  }
110 }
111 
112 
113 void
114 GNEAttributeProperties::setDiscreteValues(const std::vector<std::string>& discreteValues) {
115  if (isDiscrete()) {
116  myDiscreteValues = discreteValues;
117  } else {
118  throw FormatException("AttributeProperty doesn't support discrete values values");
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 std::string
201  std::string pre;
202  std::string type;
203  std::string plural;
204  std::string last;
205  // pre type
206  if ((myAttributeProperty & LIST) != 0) {
207  pre += "list of ";
208  if ((myAttributeProperty & VCLASS) != 0) {
209  plural = "es";
210  } else {
211  plural = "s";
212  }
213  }
214  if ((myAttributeProperty & POSITIVE) != 0) {
215  pre += "positive ";
216  }
217  if ((myAttributeProperty & DISCRETE) != 0) {
218  pre += "discrete ";
219  }
220  if ((myAttributeProperty & XMLOPTIONAL) != 0) {
221  pre += "optional ";
222  }
223  if ((myAttributeProperty & UNIQUE) != 0) {
224  pre += "unique ";
225  }
226  if ((myAttributeProperty & VCLASSES) != 0) {
227  pre += "vclasses ";
228  }
229  // type
230  if ((myAttributeProperty & INT) != 0) {
231  type = "integer";
232  }
233  if ((myAttributeProperty & FLOAT) != 0) {
234  type = "float";
235  }
236  if ((myAttributeProperty & SUMOTIME) != 0) {
237  type = "SUMOTime";
238  }
239  if ((myAttributeProperty & BOOL) != 0) {
240  type = "boolean";
241  }
242  if ((myAttributeProperty & STRING) != 0) {
243  type = "string";
244  }
245  if ((myAttributeProperty & POSITION) != 0) {
246  type = "position";
247  }
248  if ((myAttributeProperty & COLOR) != 0) {
249  type = "color";
250  }
251  if ((myAttributeProperty & VCLASS) != 0) {
252  type = "VClass";
253  }
254  if ((myAttributeProperty & FILENAME) != 0) {
255  type = "filename";
256  }
257  if ((myAttributeProperty & PROBABILITY) != 0) {
258  type = "probability";
259  last = "[0, 1]";
260  }
261  if ((myAttributeProperty & ANGLE) != 0) {
262  type = "angle";
263  last = "[0, 360]";
264  }
265  return pre + type + plural + last;
266 }
267 
268 
269 const std::vector<std::string>&
271  return myDiscreteValues;
272 }
273 
274 
277  if (hasAttrSynonym()) {
278  return myAttrSynonym;
279  } else {
280  throw ProcessError("Attr doesn't support synonym");
281  }
282 }
283 
284 
285 double
287  if (hasAttrRange()) {
288  return myMinimumRange;
289  } else {
290  throw ProcessError("Attr doesn't support range");
291  }
292 }
293 
294 
295 double
297  if (hasAttrRange()) {
298  return myMaximumRange;
299  } else {
300  throw ProcessError("Attr doesn't support range");
301  }
302 }
303 
304 
305 bool
307  return (myAttributeProperty & DEFAULTVALUESTATIC) != 0;
308 }
309 
310 
311 bool
314 }
315 
316 
317 bool
319  return (myAttributeProperty & SYNONYM) != 0;
320 }
321 
322 bool
324  return (myAttributeProperty & RANGE) != 0;
325 }
326 
327 
328 bool
330  return (myAttributeProperty & INT) != 0;
331 }
332 
333 
334 bool
336  return (myAttributeProperty & FLOAT) != 0;
337 }
338 
339 
340 bool
342  return (myAttributeProperty & SUMOTIME) != 0;
343 }
344 
345 
346 bool
348  return (myAttributeProperty & BOOL) != 0;
349 }
350 
351 
352 bool
354  return (myAttributeProperty & STRING) != 0;
355 }
356 
357 
358 bool
360  return (myAttributeProperty & POSITION) != 0;
361 }
362 
363 
364 bool
366  return (myAttributeProperty & PROBABILITY) != 0;
367 }
368 
369 
370 bool
372  return (myAttributeProperty & (INT | FLOAT | SUMOTIME)) != 0;
373 }
374 
375 
376 bool
378  return (myAttributeProperty & POSITIVE) != 0;
379 }
380 
381 
382 bool
384  return (myAttributeProperty & COLOR) != 0;
385 }
386 
387 
388 bool
390  return (myAttributeProperty & FILENAME) != 0;
391 }
392 
393 
394 bool
396  return (myAttributeProperty & VCLASS) != 0;
397 }
398 
399 
400 bool
402  return ((myAttributeProperty & LIST) != 0) && ((myAttributeProperty & VCLASS) != 0);
403 }
404 
405 
406 bool
408  return (myAttributeProperty & LIST) != 0;
409 }
410 
411 
412 bool
414  return (myAttributeProperty & SECUENCIAL) != 0;
415 }
416 
417 
418 bool
420  return (myAttributeProperty & UNIQUE) != 0;
421 }
422 
423 
424 bool
426  return (myAttributeProperty & XMLOPTIONAL) != 0;
427 }
428 
429 bool
431  return (myAttributeProperty & DISCRETE) != 0;
432 }
433 
434 
435 bool
437  return (myAttributeProperty & VCLASSES) != 0;
438 }
439 
440 
441 bool
443  return (myAttributeProperty & EXTENDED) != 0;
444 }
445 
446 
447 bool
449  return (myAttributeProperty & UPDATEGEOMETRY) != 0;
450 }
451 
452 
453 bool
455  return (myAttributeProperty & ACTIVATABLE) != 0;
456 }
457 
458 
459 bool
461  return (myAttributeProperty & COMPLEX) != 0;
462 }
463 
464 
465 bool
467  return (myAttributeProperty & FLOWDEFINITION) != 0;
468 }
469 
470 /****************************************************************************/
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ SUMO_ATTR_NOTHING
invalid attribute
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
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
bool hasMutableDefaultValue() const
return true if attribute owns a mutable default value
std::string getDescription() const
return a description of attribute
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 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
bool isOptional() const
return true if atribute is optional (it will be written in XML only if his value is different of defa...
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 isComplex() const
return true if atribute is complex
bool hasAttrSynonym() const
return true if Attr correspond to an element that will be written in XML with another name
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 hasStaticDefaultValue() const
return true if attribute owns a static default value
bool isExtended() const
return true if atribute is extended
void checkAttributeIntegrity()
check Attribute integrity (For example, throw an exception if tag has a Float default value,...
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 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 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)
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)