SUMO - Simulation of Urban MObility
GNECalibratorFlow.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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
17 //
18 /****************************************************************************/
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #ifdef _MSC_VER
24 #include <windows_config.h>
25 #else
26 #include <config.h>
27 #endif
28 
29 #include <string>
30 #include <iostream>
31 #include <utility>
35 #include <utils/common/ToString.h>
36 #include <utils/geom/GeomHelper.h>
42 #include <utils/gui/div/GLHelper.h>
46 
47 #include "GNEViewNet.h"
48 #include "GNENet.h"
49 #include "GNECalibratorFlow.h"
51 #include "GNECalibratorRoute.h"
52 #include "GNECalibrator.h"
53 #include "GNECalibratorDialog.h"
54 #include "GNEChange_Attribute.h"
55 #include "GNEUndoList.h"
56 
57 
58 // ===========================================================================
59 // member method definitions
60 // ===========================================================================
61 
62 
65  myCalibratorParent(calibratorDialog->getEditedCalibrator()),
66  myFlowID(calibratorDialog->getEditedCalibrator()->getViewNet()->getNet()->generateCalibratorFlowID()),
67  myVehicleType(calibratorDialog->getEditedCalibrator()->getCalibratorVehicleTypes().front()),
68  myRoute(calibratorDialog->getEditedCalibrator()->getCalibratorRoutes().front()),
69  myColor(getDefaultValue<RGBColor>(SUMO_TAG_FLOW, SUMO_ATTR_COLOR)),
70  myDepartLane(getDefaultValue<std::string>(SUMO_TAG_FLOW, SUMO_ATTR_DEPARTLANE)),
71  myDepartPos(getDefaultValue<std::string>(SUMO_TAG_FLOW, SUMO_ATTR_DEPARTPOS)),
72  myDepartSpeed(getDefaultValue<std::string>(SUMO_TAG_FLOW, SUMO_ATTR_DEPARTSPEED)),
73  myArrivalLane(getDefaultValue<std::string>(SUMO_TAG_FLOW, SUMO_ATTR_ARRIVALLANE)),
74  myArrivalPos(getDefaultValue<std::string>(SUMO_TAG_FLOW, SUMO_ATTR_ARRIVALPOS)),
75  myArrivalSpeed(getDefaultValue<std::string>(SUMO_TAG_FLOW, SUMO_ATTR_ARRIVALSPEED)),
76  myLine(getDefaultValue<std::string>(SUMO_TAG_FLOW, SUMO_ATTR_LINE)),
77  myPersonNumber(getDefaultValue<int>(SUMO_TAG_FLOW, SUMO_ATTR_PERSON_NUMBER)),
78  myContainerNumber(getDefaultValue<int>(SUMO_TAG_FLOW, SUMO_ATTR_CONTAINER_NUMBER)),
79  myReroute(getDefaultValue<bool>(SUMO_TAG_FLOW, SUMO_ATTR_CONTAINER_NUMBER)),
80  myDepartPosLat(getDefaultValue<std::string>(SUMO_TAG_FLOW, SUMO_ATTR_DEPARTPOS_LAT)),
81  myArrivalPosLat(getDefaultValue<std::string>(SUMO_TAG_FLOW, SUMO_ATTR_ARRIVALPOS_LAT)),
82  myBegin(getDefaultValue<double>(SUMO_TAG_FLOW, SUMO_ATTR_BEGIN)),
83  myEnd(getDefaultValue<double>(SUMO_TAG_FLOW, SUMO_ATTR_END)),
84  myVehsPerHour(getDefaultValue<double>(SUMO_TAG_FLOW, SUMO_ATTR_VEHSPERHOUR)),
85  myPeriod(getDefaultValue<double>(SUMO_TAG_FLOW, SUMO_ATTR_PERIOD)),
86  myProbability(getDefaultValue<double>(SUMO_TAG_FLOW, SUMO_ATTR_PROB)),
87  myNumber(getDefaultValue<int>(SUMO_TAG_FLOW, SUMO_ATTR_NUMBER)),
88  myFlowType(GNE_CALIBRATORFLOW_VEHSPERHOUR) {}
89 
90 
91 GNECalibratorFlow::GNECalibratorFlow(GNECalibrator* calibratorParent, const std::string& flowID, GNECalibratorVehicleType* vehicleType, GNECalibratorRoute* route,
92  const RGBColor& color, const std::string& departLane, const std::string& departPos, const std::string& departSpeed, const std::string& arrivalLane,
93  const std::string& arrivalPos, const std::string& arrivalSpeed, const std::string& line, int personNumber, int containerNumber, bool reroute,
94  const std::string& departPosLat, const std::string& arrivalPosLat, double begin, double end, double vehsPerHour, double period, double probability,
95  int number, GNECalibratorFlow::TypeOfFlow flowType) :
97  myCalibratorParent(calibratorParent),
98  myFlowID(flowID),
99  myVehicleType(vehicleType),
100  myRoute(route),
101  myColor(color),
102  myDepartLane(departLane),
103  myDepartPos(departPos),
104  myDepartSpeed(departSpeed),
105  myArrivalLane(arrivalLane),
106  myArrivalPos(arrivalPos),
107  myArrivalSpeed(arrivalSpeed),
108  myLine(line),
109  myPersonNumber(personNumber),
110  myContainerNumber(containerNumber),
111  myReroute(reroute),
112  myDepartPosLat(departPosLat),
113  myArrivalPosLat(arrivalPosLat),
114  myBegin(begin),
115  myEnd(end),
116  myVehsPerHour(vehsPerHour),
117  myPeriod(period),
118  myProbability(probability),
119  myNumber(number),
120  myFlowType(flowType) {
121 }
122 
123 
125 
126 
127 void
129  // Open flow tag
130  device.openTag(getTag());
131  // Write begin
133  // Write end
134  device.writeAttr(SUMO_ATTR_END, myEnd);
135  // Write type
137  // Write route
139  // Write color
141  // Write depart lane
143  // Write depart pos
145  // Write depart speed
147  // Write arrival lane
149  // Write arrival pos
151  // Write arrival speed
153  // Write line
155  // Write person number
157  // Write container number
159  // Write reroute
161  // Write departPosLat
163  // Write arrivalPosLat
165  // Write number
167  // Write type of flow
169  // write period
172  // write vehs per hour
175  // write probability
177  }
178  // Close flow tag
179  device.closeTag();
180 }
181 
182 
185  return myCalibratorParent;
186 }
187 
188 
191  return myFlowType;
192 }
193 
194 
195 void
197  myFlowType = type;
198 }
199 
200 
201 std::string
203  switch (key) {
204  case SUMO_ATTR_ID:
205  return myFlowID;
206  case SUMO_ATTR_TYPE:
207  return myVehicleType->getID();
208  case SUMO_ATTR_ROUTE:
209  return myRoute->getID();
210  case SUMO_ATTR_COLOR:
211  return toString(myColor);
212  case SUMO_ATTR_BEGIN:
213  return toString(myBegin);
214  case SUMO_ATTR_END:
215  return toString(myEnd);
217  return toString(myVehsPerHour);
218  case SUMO_ATTR_PERIOD:
219  return toString(myPeriod);
220  case SUMO_ATTR_PROB:
221  return toString(myProbability);
222  case SUMO_ATTR_NUMBER:
223  return toString(myNumber);
225  return myDepartLane;
226  case SUMO_ATTR_DEPARTPOS:
227  return myDepartPos;
229  return myDepartSpeed;
231  return myArrivalLane;
233  return myArrivalPos;
235  return myArrivalSpeed;
236  case SUMO_ATTR_LINE:
237  return myLine;
239  return toString(myPersonNumber);
241  return toString(myContainerNumber);
242  case SUMO_ATTR_REROUTE:
243  return toString(myReroute);
245  return myDepartPosLat;
247  return myArrivalPosLat;
248  default:
249  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
250  }
251 }
252 
253 
254 void
255 GNECalibratorFlow::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
256  if (value == getAttribute(key)) {
257  return; //avoid needless changes, later logic relies on the fact that attributes have changed
258  }
259  switch (key) {
260  case SUMO_ATTR_ID:
261  case SUMO_ATTR_TYPE:
262  case SUMO_ATTR_ROUTE:
263  case SUMO_ATTR_COLOR:
264  case SUMO_ATTR_BEGIN:
265  case SUMO_ATTR_END:
267  case SUMO_ATTR_PERIOD:
268  case SUMO_ATTR_PROB:
269  case SUMO_ATTR_NUMBER:
271  case SUMO_ATTR_DEPARTPOS:
276  case SUMO_ATTR_LINE:
279  case SUMO_ATTR_REROUTE:
282  undoList->p_add(new GNEChange_Attribute(this, key, value));
283  break;
284  default:
285  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
286  }
287 }
288 
289 
290 bool
291 GNECalibratorFlow::isValid(SumoXMLAttr key, const std::string& value) {
292  switch (key) {
293  case SUMO_ATTR_ID:
294  return isValidID(value) && (myCalibratorParent->getViewNet()->getNet()->retrieveCalibratorFlow(value, false) == NULL);
295  case SUMO_ATTR_TYPE:
296  return isValidID(value) && (myCalibratorParent->getViewNet()->getNet()->retrieveCalibratorVehicleType(value, false) != NULL);
297  case SUMO_ATTR_ROUTE:
298  return isValidID(value) && (myCalibratorParent->getViewNet()->getNet()->retrieveCalibratorRoute(value, false) != NULL);
299  case SUMO_ATTR_COLOR:
300  return canParse<RGBColor>(value);
301  case SUMO_ATTR_BEGIN:
302  return canParse<double>(value) && (parse<double>(value) >= 0);
303  case SUMO_ATTR_END:
304  return canParse<double>(value) && (parse<double>(value) >= 0);
306  return canParse<double>(value) && (parse<double>(value) >= 0);
307  case SUMO_ATTR_PERIOD:
308  return canParse<double>(value) && (parse<double>(value) >= 0);
309  case SUMO_ATTR_PROB:
310  return canParse<double>(value) && (parse<double>(value) >= 0) && (parse<double>(value) <= 1);
311  case SUMO_ATTR_NUMBER:
312  return canParse<int>(value) && parse<int>(value) >= 0;
314  if ((value == "random") || (value == "free") || (value == "allowed") || (value == "best") || (value == "first")) {
315  return true;
316  } else {
317  return (myCalibratorParent->getViewNet()->getNet()->retrieveLane(value, false) != NULL);
318  }
319  case SUMO_ATTR_DEPARTPOS:
320  if ((value == "random") || (value == "free") || (value == "random_free") || (value == "base") || (value == "last")) {
321  return true;
322  } else {
323  return canParse<double>(value);
324  }
326  if ((value == "random") || (value == "max")) {
327  return true;
328  } else {
329  return canParse<double>(value);
330  }
332  if (value == "current") {
333  return true;
334  } else {
335  return (myCalibratorParent->getViewNet()->getNet()->retrieveLane(value, false) != NULL);
336  }
338  if ((value == "random") || (value == "max")) {
339  return true;
340  } else {
341  return canParse<double>(value);
342  }
344  if (value == "current") {
345  return true;
346  } else {
347  return canParse<double>(value);
348  }
349  case SUMO_ATTR_LINE:
350  return true;
352  return canParse<int>(value) && parse<int>(value) >= 0;
354  return canParse<int>(value) && parse<int>(value) >= 0;
355  case SUMO_ATTR_REROUTE:
356  return canParse<bool>(value);
358  if ((value == "left") || (value == "right") || (value == "center") || (value == "compact") || (value == "nice") || (value == "arbitrary")) {
359  return true;
360  } else {
361  return false;
362  }
364  if ((value == "") || (value == "left") || (value == "right") || (value == "center")) {
365  return true;
366  } else {
367  return canParse<double>(value);
368  }
369  default:
370  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
371  }
372 }
373 
374 // ===========================================================================
375 // private
376 // ===========================================================================
377 
378 void
379 GNECalibratorFlow::setAttribute(SumoXMLAttr key, const std::string& value) {
380  switch (key) {
381  case SUMO_ATTR_ID: {
382  std::string oldID = myFlowID;
383  myFlowID = value;
385  break;
386  }
387  case SUMO_ATTR_TYPE:
389  break;
390  case SUMO_ATTR_ROUTE:
392  break;
393  case SUMO_ATTR_COLOR:
394  myColor = parse<RGBColor>(value);
395  break;
396  case SUMO_ATTR_BEGIN:
397  myBegin = parse<double>(value);
398  break;
399  case SUMO_ATTR_END:
400  myEnd = parse<double>(value);
401  break;
403  myVehsPerHour = parse<double>(value);
404  break;
405  case SUMO_ATTR_PERIOD:
406  myPeriod = parse<double>(value);
407  break;
408  case SUMO_ATTR_PROB:
409  myProbability = parse<double>(value);
410  break;
411  case SUMO_ATTR_NUMBER:
412  myNumber = parse<int>(value);
413  break;
415  myDepartLane = value;
416  break;
417  case SUMO_ATTR_DEPARTPOS:
418  myDepartPos = value;
419  break;
421  myDepartSpeed = value;
422  break;
424  myArrivalLane = value;
425  break;
427  myArrivalPos = value;
428  break;
430  myArrivalSpeed = value;
431  break;
432  case SUMO_ATTR_LINE:
433  myLine = value;
434  break;
436  myPersonNumber = parse<int>(value);
437  break;
439  myContainerNumber = parse<int>(value);
440  break;
441  case SUMO_ATTR_REROUTE:
442  myReroute = parse<bool>(value);
443  break;
445  myDepartPosLat = value;
446  break;
448  myArrivalPosLat = value;
449  break;
450  default:
451  throw InvalidArgument(toString(getTag()) + " doesn't have an attribute of type '" + toString(key) + "'");
452  }
453 }
454 
455 /****************************************************************************/
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:260
RGBColor myColor
color of flow
a flow definition (used by router)
~GNECalibratorFlow()
destructor
bool isValid(SumoXMLAttr key, const std::string &value)
std::string myLine
line of bus/container stop
std::string myDepartPosLat
departPosLat
weights: time range begin
void setFlowType(GNECalibratorFlow::TypeOfFlow type)
set type of flow
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
double myPeriod
period
int myNumber
number of flow
bool myReroute
reroute
GNECalibratorFlow(GNECalibratorDialog *calibratorDialog)
constructor (used only in GNECalibratorDialog)
std::string myArrivalPos
arrival pos
double myEnd
time step end
std::string myArrivalPosLat
std::string getAttribute(SumoXMLAttr key) const
inherited from GNEAttributeCarrier
int myPersonNumber
number of person
void p_add(GNEChange_Attribute *cmd)
special method, avoid empty changes, always execute
std::string myDepartPos
depart position
static bool isValidID(const std::string &value)
true if value is a valid sumo ID
TypeOfFlow
type of flow
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
std::string myDepartLane
depart lane
std::string myArrivalLane
arrival lane
friend class GNEChange_Attribute
declare friend class
double myVehsPerHour
flows per hour
const std::string getID() const
function to support debugging
std::string myFlowID
ID of flow.
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
GNECalibratorRoute * retrieveCalibratorRoute(const std::string &id, bool hardFail=true) const
Returns the named calibrator route.
Definition: GNENet.cpp:1800
int myContainerNumber
number of container
GNECalibrator * getCalibratorParent() const
get pointer to calibrator parent
Dialog for edit calibrators.
void changeCalibratorFlowID(GNECalibratorFlow *flow, const std::string &oldID)
change Calibrator Flow ID
Definition: GNENet.cpp:1900
GNEViewNet * getViewNet() const
Returns a pointer to GNEViewNet in which additional element is located.
double myProbability
probability
GNECalibrator * myCalibratorParent
pointer to calibrator parent
std::string myArrivalSpeed
arrival speed
weights: time range end
std::string myDepartSpeed
depart speed
GNECalibratorVehicleType * retrieveCalibratorVehicleType(const std::string &id, bool hardFail=true) const
Returns the named calibrator vehicle type.
Definition: GNENet.cpp:1816
GNENet * getNet() const
get the net object
TypeOfFlow myFlowType
type of flow
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
bool closeTag()
Closes the most recently opened tag.
GNECalibratorFlow::TypeOfFlow getFlowType() const
get type of flow
GNECalibratorVehicleType * myVehicleType
type of flow
void writeFlow(OutputDevice &device)
write Flow values into a XML
GNELane * retrieveLane(const std::string &id, bool failHard=true, bool checkVolatileChange=false)
get lane by id
Definition: GNENet.cpp:1000
A color information.
GNECalibratorRoute * myRoute
route in which this flow is used
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
GNECalibratorFlow * retrieveCalibratorFlow(const std::string &id, bool hardFail=true) const
Returns the named calibrator flow.
Definition: GNENet.cpp:1832
SumoXMLTag getTag() const
get XML Tag assigned to this object