Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GNEDemandElementFlow.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-2025 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// An auxiliar, asbtract class for flow elements (vehicles, person and containers)
19/****************************************************************************/
20
22#include <netedit/GNEUndoList.h>
26
28
29// ===========================================================================
30// member method definitions
31// ===========================================================================
32
34 // set default flow attributes
35 setDefaultFlowAttributes(flowElement);
36}
37
38
40 SUMOVehicleParameter(vehicleParameters) {
41 // set default flow attributes
42 setDefaultFlowAttributes(flowElement);
43}
44
45
47
48
49void
50GNEDemandElementFlow::drawFlowLabel(const Position& position, const double rotation, const double width,
51 const double length, const double exaggeration) const {
52 // declare contour width
53 const double contourWidth = (0.05 * exaggeration);
54 // Push matrix
56 // Traslate to bot
57 glTranslated(position.x(), position.y(), GLO_VEHICLELABELS);
58 // glTranslated(position.x(), position.y(), GLO_ROUTE + getType() + 0.1 + GLO_PERSONFLOW + 0.1);
59 glRotated(rotation, 0, 0, -1);
60 glTranslated(-1 * ((width * 0.5 * exaggeration) + (0.35 * exaggeration)) - 0.05, 0, 0);
61 // draw external box
63 GLHelper::drawBoxLine(Position(), Position(), 0, (length * exaggeration), 0.3 * exaggeration);
64 // draw internal box
65 glTranslated(0, 0, 0.1);
67 GLHelper::drawBoxLine(Position(0, -contourWidth), Position(0, -contourWidth), 0, (length * exaggeration) - (contourWidth * 2), (0.3 * exaggeration) - contourWidth);
68 // draw stack label
69 GLHelper::drawText("Flow", Position(0, length * exaggeration * -0.5), (.1 * exaggeration), (0.6 * exaggeration), RGBColor::BLACK, 90, 0, -1);
70 // pop draw matrix
72}
73
74
75std::string
77 switch (key) {
79 case SUMO_ATTR_BEGIN:
81 return "triggered";
83 return "containerTriggered";
85 return "now";
87 return "split";
89 return "begin";
90 } else {
91 return time2string(depart);
92 }
93 case SUMO_ATTR_END:
101 case GNE_ATTR_POISSON:
103 case SUMO_ATTR_PROB:
105 case SUMO_ATTR_NUMBER:
111 } else {
112 return toString(SUMO_ATTR_END);
113 }
116 } else {
117 return "invalid terminate";
118 }
129 return toString(SUMO_ATTR_PROB);
132 } else {
133 return "invalid flow spacing";
134 }
135 default:
136 return flowElement->getCommonAttribute(key);
137 }
138}
139
140
141double
143 switch (key) {
144 case SUMO_ATTR_DEPART:
145 case SUMO_ATTR_BEGIN:
146 return STEPS2TIME(depart);
147 default:
148 throw InvalidArgument("Flow doesn't have a double attribute of type '" + toString(key) + "'");
149 }
150}
151
152
153void
154GNEDemandElementFlow::setFlowAttribute(GNEDemandElement* flowElement, SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
155 switch (key) {
156 case SUMO_ATTR_DEPART:
157 case SUMO_ATTR_BEGIN:
158 case SUMO_ATTR_END:
159 case SUMO_ATTR_NUMBER:
163 case SUMO_ATTR_PERIOD:
164 case GNE_ATTR_POISSON:
165 case SUMO_ATTR_PROB:
168 GNEChange_Attribute::changeAttribute(flowElement, key, value, undoList);
169 break;
170 default:
171 return flowElement->setCommonAttribute(key, value, undoList);
172 }
173}
174
175
176bool
177GNEDemandElementFlow::isValidFlowAttribute(GNEDemandElement* flowElement, SumoXMLAttr key, const std::string& value) {
178 // declare string error
179 std::string error;
180 switch (key) {
181 case SUMO_ATTR_DEPART:
182 case SUMO_ATTR_BEGIN: {
183 SUMOTime dummyDepart;
184 DepartDefinition dummyDepartProcedure;
185 parseDepart(value, flowElement->getTagProperty()->getTagStr(), id, dummyDepart, dummyDepartProcedure, error);
186 // if error is empty, given value is valid
187 return error.empty();
188 }
189 case SUMO_ATTR_END:
190 if (GNEAttributeCarrier::canParse<SUMOTime>(value)) {
191 return (GNEAttributeCarrier::parse<SUMOTime>(value) >= 0);
192 } else {
193 return false;
194 }
198 case SUMO_ATTR_PERIOD:
199 case GNE_ATTR_POISSON:
200 if (GNEAttributeCarrier::canParse<double>(value)) {
201 return (GNEAttributeCarrier::parse<double>(value) > 0);
202 } else {
203 return false;
204 }
205 case SUMO_ATTR_PROB:
206 if (GNEAttributeCarrier::canParse<double>(value)) {
207 const double prob = GNEAttributeCarrier::parse<double>(value);
208 return ((prob >= 0) && (prob <= 1));
209 } else {
210 return false;
211 }
212 case SUMO_ATTR_NUMBER:
213 if (GNEAttributeCarrier::canParse<int>(value)) {
214 return (GNEAttributeCarrier::parse<int>(value) >= 0);
215 } else {
216 return false;
217 }
220 const auto& flowValues = flowElement->getTagProperty()->getAttributeProperties(key)->getDiscreteValues();
221 if (std::find(flowValues.begin(), flowValues.end(), value) != flowValues.end()) {
222 return true;
223 } else {
224 return false;
225 }
226 }
227 default:
228 return flowElement->isCommonAttributeValid(key, value);
229 }
230}
231
232
233void
235 switch (key) {
236 case SUMO_ATTR_END:
237 case SUMO_ATTR_NUMBER:
241 case SUMO_ATTR_PERIOD:
242 case GNE_ATTR_POISSON:
243 case SUMO_ATTR_PROB:
244 undoList->add(new GNEChange_ToggleAttribute(flowElement, key, true), true);
245 return;
246 default:
247 throw InvalidArgument(flowElement->getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
248 }
249}
250
251
252void
254 switch (key) {
255 case SUMO_ATTR_END:
256 case SUMO_ATTR_NUMBER:
260 case SUMO_ATTR_PERIOD:
261 case GNE_ATTR_POISSON:
262 case SUMO_ATTR_PROB:
263 undoList->add(new GNEChange_ToggleAttribute(flowElement, key, false), true);
264 return;
265 default:
266 throw InvalidArgument(flowElement->getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
267 }
268}
269
270
271bool
273 switch (key) {
274 case SUMO_ATTR_END:
275 return (parametersSet & VEHPARS_END_SET) != 0;
276 case SUMO_ATTR_NUMBER:
277 return (parametersSet & VEHPARS_NUMBER_SET) != 0;
281 return (parametersSet & VEHPARS_VPH_SET) != 0;
282 case SUMO_ATTR_PERIOD:
283 return (parametersSet & VEHPARS_PERIOD_SET) != 0;
284 case GNE_ATTR_POISSON:
285 return (parametersSet & VEHPARS_POISSON_SET) != 0;
286 case SUMO_ATTR_PROB:
287 return (parametersSet & VEHPARS_PROB_SET) != 0;
290 default:
291 return true;
292 }
293}
294
295
296void
297GNEDemandElementFlow::setFlowAttribute(GNEDemandElement* flowElement, SumoXMLAttr key, const std::string& value) {
298 // declare string error
299 std::string error;
300 switch (key) {
301 case SUMO_ATTR_DEPART:
302 case SUMO_ATTR_BEGIN: {
303 parseDepart(value, flowElement->getTagProperty()->getTagStr(), id, depart, departProcedure, error);
304 break;
305 }
306 case SUMO_ATTR_END:
307 repetitionEnd = string2time(value);
308 break;
312 repetitionOffset = TIME2STEPS(3600 / GNEAttributeCarrier::parse<double>(value));
313 poissonRate = GNEAttributeCarrier::parse<double>(value) / 3600;
314 break;
315 case SUMO_ATTR_PERIOD:
318 break;
319 case GNE_ATTR_POISSON:
320 poissonRate = GNEAttributeCarrier::parse<double>(value);
322 break;
323 case SUMO_ATTR_PROB:
324 repetitionProbability = GNEAttributeCarrier::parse<double>(value);
325 break;
326 case SUMO_ATTR_NUMBER:
327 repetitionNumber = GNEAttributeCarrier::parse<int>(value);
328 break;
330 if (value == (toString(SUMO_ATTR_END) + "-" + toString(SUMO_ATTR_NUMBER))) {
333 // in this special case, disable other spacing
338 } else {
339 // if previously end-number was enabled, enable perHour
342 }
343 if (value == toString(SUMO_ATTR_END)) {
346 } else if (value == toString(SUMO_ATTR_NUMBER)) {
349 }
350 }
351 break;
353 if ((value == toString(SUMO_ATTR_VEHSPERHOUR)) ||
360 } else if (value == toString(SUMO_ATTR_PERIOD)) {
365 } else if (value == toString(GNE_ATTR_POISSON)) {
370 } else if (value == toString(SUMO_ATTR_PROB)) {
375 }
376 break;
377 default:
378 flowElement->setCommonAttribute(key, value);
379 break;
380 }
381}
382
383
384void
385GNEDemandElementFlow::toggleFlowAttribute(const SumoXMLAttr attribute, const bool value) {
386 // modify parameters depending of given Flow attribute
387 if (value) {
388 switch (attribute) {
389 case SUMO_ATTR_END:
391 break;
392 case SUMO_ATTR_NUMBER:
394 break;
399 break;
400 case SUMO_ATTR_PERIOD:
402 break;
403 case GNE_ATTR_POISSON:
405 break;
406 case SUMO_ATTR_PROB:
408 break;
409 default:
410 break;
411 }
412 } else {
413 switch (attribute) {
414 case SUMO_ATTR_END:
415 parametersSet &= ~VEHPARS_END_SET;
416 break;
417 case SUMO_ATTR_NUMBER:
418 parametersSet &= ~VEHPARS_NUMBER_SET;
419 break;
423 parametersSet &= ~VEHPARS_VPH_SET;
424 break;
425 case SUMO_ATTR_PERIOD:
426 parametersSet &= ~VEHPARS_PERIOD_SET;
427 break;
428 case GNE_ATTR_POISSON:
429 parametersSet &= ~VEHPARS_POISSON_SET;
430 break;
431 case SUMO_ATTR_PROB:
432 parametersSet &= ~VEHPARS_PROB_SET;
433 break;
434 default:
435 break;
436 }
437 }
438}
439
440
441void
443 // first check that this demand element is a flow
444 if (flowElement->getTagProperty()->isFlow()) {
445 // end
446 if ((parametersSet & VEHPARS_END_SET) == 0) {
448 }
449 // number
450 if ((parametersSet & VEHPARS_NUMBER_SET) == 0) {
452 }
453 // vehicles/person/container per hour
454 if (((parametersSet & VEHPARS_PERIOD_SET) == 0) &&
456 ((parametersSet & VEHPARS_VPH_SET) == 0)) {
458 }
459 // probability
460 if ((parametersSet & VEHPARS_PROB_SET) == 0) {
462 }
463 // poisson
464 if (repetitionOffset < 0) {
468 } else {
470 }
471 }
472}
473
474/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
@ GLO_VEHICLELABELS
stack and flow labels (used in netedit)
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition SUMOTime.cpp:46
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:91
#define STEPS2TIME(x)
Definition SUMOTime.h:55
#define TIME2STEPS(x)
Definition SUMOTime.h:57
const long long int VEHPARS_END_SET
const long long int VEHPARS_PERIOD_SET
const long long int VEHPARS_POISSON_SET
const long long int VEHPARS_PROB_SET
const long long int VEHPARS_VPH_SET
const long long int VEHPARS_NUMBER_SET
DepartDefinition
Possible ways to depart.
@ BEGIN
The departure is at simulation start.
@ NOW
The vehicle is discarded if emission fails (not fully implemented yet)
@ SPLIT
The departure is triggered by a train split.
@ CONTAINER_TRIGGERED
The departure is container triggered.
@ TRIGGERED
The departure is person triggered.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ SUMO_ATTR_NUMBER
@ SUMO_ATTR_DEPART
@ SUMO_ATTR_VEHSPERHOUR
@ SUMO_ATTR_BEGIN
weights: time range begin
@ GNE_ATTR_POISSON
poisson definition (used in flow)
@ SUMO_ATTR_CONTAINERSPERHOUR
@ SUMO_ATTR_PERIOD
@ SUMO_ATTR_END
weights: time range end
@ SUMO_ATTR_PROB
@ GNE_ATTR_FLOW_SPACING
flow spacing
@ GNE_ATTR_FLOW_TERMINATE
flow terminating
@ SUMO_ATTR_PERSONSPERHOUR
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition GLHelper.cpp:649
static void popMatrix()
pop matrix
Definition GLHelper.cpp:131
static void drawBoxLine(const Position &beg, double rot, double visLength, double width, double offset=0)
Draws a thick line.
Definition GLHelper.cpp:296
static void pushMatrix()
push matrix
Definition GLHelper.cpp:118
static void drawText(const std::string &text, const Position &pos, const double layer, const double size, const RGBColor &col=RGBColor::BLACK, const double angle=0, const int align=0, double width=-1)
Definition GLHelper.cpp:742
void setCommonAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
const std::string & getTagStr() const
get tag assigned to this object in string format
const GNETagProperties * getTagProperty() const
get tagProperty associated with this Attribute Carrier
bool isCommonAttributeValid(SumoXMLAttr key, const std::string &value) const
std::string getCommonAttribute(SumoXMLAttr key) const
static void changeAttribute(GNEAttributeCarrier *AC, SumoXMLAttr key, const std::string &value, GNEUndoList *undoList, const bool force=false)
change attribute
the function-object for an editing operation (abstract base)
void toggleFlowAttribute(const SumoXMLAttr attribute, const bool value)
toggle flow parameters (used in toggleAttribute(...) function of vehicles, persons and containers
void drawFlowLabel(const Position &position, const double rotation, const double width, const double length, const double exaggeration) const
draw flow label
std::string getFlowAttribute(const GNEDemandElement *flowElement, SumoXMLAttr key) const
inherited from GNEAttributeCarrier and adapted to GNEDemandElementFlow
void disableFlowAttribute(GNEDemandElement *flowElement, SumoXMLAttr key, GNEUndoList *undoList)
bool isFlowAttributeEnabled(SumoXMLAttr key) const
double getFlowAttributeDouble(SumoXMLAttr key) const
GNEDemandElementFlow(GNEDemandElement *flowElement)
constructor
bool isValidFlowAttribute(GNEDemandElement *flowElement, SumoXMLAttr key, const std::string &value)
void enableFlowAttribute(GNEDemandElement *flowElement, SumoXMLAttr key, GNEUndoList *undoList)
void setFlowAttribute(GNEDemandElement *flowElement, SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
void setDefaultFlowAttributes(GNEDemandElement *flowElement)
set flow default attributes
bool isFlow() const
return true if tag correspond to a flow element
const std::string & getTagStr() const
get Tag vinculated with this attribute Property in String Format (used to avoid multiple calls to toS...
const std::vector< const GNEAttributeProperties * > & getAttributeProperties() const
get all attribute properties
bool hasAttribute(SumoXMLAttr attr) const
check if current TagProperties owns the attribute "attr"
const std::string & getDefaultStringValue(SumoXMLAttr attr) const
default values
void add(GNEChange *command, bool doit=false, bool merge=true)
Add new command, executing it if desired. The new command will be merged with the previous command if...
A point in 2D or 3D with translation and scaling methods.
Definition Position.h:37
double x() const
Returns the x-position.
Definition Position.h:52
double y() const
Returns the y-position.
Definition Position.h:57
static const RGBColor GREY
Definition RGBColor.h:197
static const RGBColor CYAN
Definition RGBColor.h:192
static const RGBColor BLACK
Definition RGBColor.h:196
Structure representing possible vehicle parameter.
double repetitionProbability
The probability for emitting a vehicle per second.
SUMOTime repetitionOffset
The time offset between vehicle reinsertions.
long long int parametersSet
Information for the router which parameter were set, TraCI may modify this (when changing color)
double poissonRate
The rate for emitting vehicles with a poisson distribution.
SUMOTime repetitionEnd
The time at which the flow ends (only needed when using repetitionProbability)
static bool parseDepart(const std::string &val, const std::string &element, const std::string &id, SUMOTime &depart, DepartDefinition &dd, std::string &error, const std::string &attr="departure")
Validates a given depart value.
DepartDefinition departProcedure
Information how the vehicle shall choose the depart time.
static std::string adjustDecimalValue(double value, int precision)
write with maximum precision if needed but remove trailing zeros