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 break;
173 }
174}
175
176
177bool
178GNEDemandElementFlow::isValidFlowAttribute(GNEDemandElement* flowElement, SumoXMLAttr key, const std::string& value) {
179 // declare string error
180 std::string error;
181 switch (key) {
182 case SUMO_ATTR_DEPART:
183 case SUMO_ATTR_BEGIN: {
184 SUMOTime dummyDepart;
185 DepartDefinition dummyDepartProcedure;
186 parseDepart(value, flowElement->getTagProperty()->getTagStr(), id, dummyDepart, dummyDepartProcedure, error);
187 // if error is empty, given value is valid
188 return error.empty();
189 }
190 case SUMO_ATTR_END:
191 if (GNEAttributeCarrier::canParse<SUMOTime>(value)) {
192 return (GNEAttributeCarrier::parse<SUMOTime>(value) >= 0);
193 } else {
194 return false;
195 }
199 case SUMO_ATTR_PERIOD:
200 case GNE_ATTR_POISSON:
201 if (GNEAttributeCarrier::canParse<double>(value)) {
202 return (GNEAttributeCarrier::parse<double>(value) > 0);
203 } else {
204 return false;
205 }
206 case SUMO_ATTR_PROB:
207 if (GNEAttributeCarrier::canParse<double>(value)) {
208 const double prob = GNEAttributeCarrier::parse<double>(value);
209 return ((prob >= 0) && (prob <= 1));
210 } else {
211 return false;
212 }
213 case SUMO_ATTR_NUMBER:
214 if (GNEAttributeCarrier::canParse<int>(value)) {
215 return (GNEAttributeCarrier::parse<int>(value) >= 0);
216 } else {
217 return false;
218 }
221 const auto& flowValues = flowElement->getTagProperty()->getAttributeProperties(key)->getDiscreteValues();
222 if (std::find(flowValues.begin(), flowValues.end(), value) != flowValues.end()) {
223 return true;
224 } else {
225 return false;
226 }
227 }
228 default:
229 return flowElement->isCommonAttributeValid(key, value);
230 }
231}
232
233
234void
236 switch (key) {
237 case SUMO_ATTR_END:
238 case SUMO_ATTR_NUMBER:
242 case SUMO_ATTR_PERIOD:
243 case GNE_ATTR_POISSON:
244 case SUMO_ATTR_PROB:
245 undoList->add(new GNEChange_ToggleAttribute(flowElement, key, true), true);
246 return;
247 default:
248 throw InvalidArgument(flowElement->getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
249 }
250}
251
252
253void
255 switch (key) {
256 case SUMO_ATTR_END:
257 case SUMO_ATTR_NUMBER:
261 case SUMO_ATTR_PERIOD:
262 case GNE_ATTR_POISSON:
263 case SUMO_ATTR_PROB:
264 undoList->add(new GNEChange_ToggleAttribute(flowElement, key, false), true);
265 return;
266 default:
267 throw InvalidArgument(flowElement->getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
268 }
269}
270
271
272bool
274 switch (key) {
275 case SUMO_ATTR_END:
276 return (parametersSet & VEHPARS_END_SET) != 0;
277 case SUMO_ATTR_NUMBER:
278 return (parametersSet & VEHPARS_NUMBER_SET) != 0;
282 return (parametersSet & VEHPARS_VPH_SET) != 0;
283 case SUMO_ATTR_PERIOD:
284 return (parametersSet & VEHPARS_PERIOD_SET) != 0;
285 case GNE_ATTR_POISSON:
286 return (parametersSet & VEHPARS_POISSON_SET) != 0;
287 case SUMO_ATTR_PROB:
288 return (parametersSet & VEHPARS_PROB_SET) != 0;
291 default:
292 return true;
293 }
294}
295
296
297void
298GNEDemandElementFlow::setFlowAttribute(GNEDemandElement* flowElement, SumoXMLAttr key, const std::string& value) {
299 // declare string error
300 std::string error;
301 switch (key) {
302 case SUMO_ATTR_DEPART:
303 case SUMO_ATTR_BEGIN: {
304 parseDepart(value, flowElement->getTagProperty()->getTagStr(), id, depart, departProcedure, error);
305 break;
306 }
307 case SUMO_ATTR_END:
308 repetitionEnd = string2time(value);
309 break;
313 repetitionOffset = TIME2STEPS(3600 / GNEAttributeCarrier::parse<double>(value));
314 poissonRate = GNEAttributeCarrier::parse<double>(value) / 3600;
315 break;
316 case SUMO_ATTR_PERIOD:
319 break;
320 case GNE_ATTR_POISSON:
321 poissonRate = GNEAttributeCarrier::parse<double>(value);
323 break;
324 case SUMO_ATTR_PROB:
325 repetitionProbability = GNEAttributeCarrier::parse<double>(value);
326 break;
327 case SUMO_ATTR_NUMBER:
328 repetitionNumber = GNEAttributeCarrier::parse<int>(value);
329 break;
331 if (value == (toString(SUMO_ATTR_END) + "-" + toString(SUMO_ATTR_NUMBER))) {
334 // in this special case, disable other spacing
339 } else {
340 // if previously end-number was enabled, enable perHour
343 }
344 if (value == toString(SUMO_ATTR_END)) {
347 } else if (value == toString(SUMO_ATTR_NUMBER)) {
350 }
351 }
352 break;
354 if ((value == toString(SUMO_ATTR_VEHSPERHOUR)) ||
361 } else if (value == toString(SUMO_ATTR_PERIOD)) {
366 } else if (value == toString(GNE_ATTR_POISSON)) {
371 } else if (value == toString(SUMO_ATTR_PROB)) {
376 }
377 break;
378 default:
379 flowElement->setCommonAttribute(key, value);
380 break;
381 }
382}
383
384
385void
386GNEDemandElementFlow::toggleFlowAttribute(const SumoXMLAttr attribute, const bool value) {
387 // modify parameters depending of given Flow attribute
388 if (value) {
389 switch (attribute) {
390 case SUMO_ATTR_END:
392 break;
393 case SUMO_ATTR_NUMBER:
395 break;
400 break;
401 case SUMO_ATTR_PERIOD:
403 break;
404 case GNE_ATTR_POISSON:
406 break;
407 case SUMO_ATTR_PROB:
409 break;
410 default:
411 break;
412 }
413 } else {
414 switch (attribute) {
415 case SUMO_ATTR_END:
416 parametersSet &= ~VEHPARS_END_SET;
417 break;
418 case SUMO_ATTR_NUMBER:
419 parametersSet &= ~VEHPARS_NUMBER_SET;
420 break;
424 parametersSet &= ~VEHPARS_VPH_SET;
425 break;
426 case SUMO_ATTR_PERIOD:
427 parametersSet &= ~VEHPARS_PERIOD_SET;
428 break;
429 case GNE_ATTR_POISSON:
430 parametersSet &= ~VEHPARS_POISSON_SET;
431 break;
432 case SUMO_ATTR_PROB:
433 parametersSet &= ~VEHPARS_PROB_SET;
434 break;
435 default:
436 break;
437 }
438 }
439}
440
441
442void
444 // first check that this demand element is a flow
445 if (flowElement->getTagProperty()->isFlow()) {
446 // end
447 if ((parametersSet & VEHPARS_END_SET) == 0) {
449 }
450 // number
451 if ((parametersSet & VEHPARS_NUMBER_SET) == 0) {
453 }
454 // vehicles/person/container per hour
455 if (((parametersSet & VEHPARS_PERIOD_SET) == 0) &&
457 ((parametersSet & VEHPARS_VPH_SET) == 0)) {
459 }
460 // probability
461 if ((parametersSet & VEHPARS_PROB_SET) == 0) {
463 }
464 // poisson
465 if (repetitionOffset < 0) {
469 } else {
471 }
472 }
473}
474
475/****************************************************************************/
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