Eclipse SUMO - Simulation of Urban MObility
MSMeanData.h
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 /****************************************************************************/
19 // Data collector for edges/lanes
20 /****************************************************************************/
21 #pragma once
22 #include <config.h>
23 
24 #include <vector>
25 #include <set>
26 #include <list>
27 #include <limits>
30 #include <utils/common/SUMOTime.h>
31 
32 
33 // ===========================================================================
34 // class declarations
35 // ===========================================================================
36 class OutputDevice;
37 class MSEdge;
38 class MSLane;
39 class SUMOTrafficObject;
40 
41 typedef std::vector<MSEdge*> MSEdgeVector;
42 
43 // ===========================================================================
44 // class definitions
45 // ===========================================================================
58 public:
66  class MeanDataValues : public MSMoveReminder {
67  public:
69  MeanDataValues(MSLane* const lane, const double length, const bool doAdd, const MSMeanData* const parent);
70 
72  virtual ~MeanDataValues();
73 
74 
77  virtual void reset(bool afterWrite = false) = 0;
78 
83  virtual void addTo(MeanDataValues& val) const = 0;
84 
85 
94  virtual bool notifyEnter(SUMOTrafficObject& veh, MSMoveReminder::Notification reason, const MSLane* enteredLane = 0);
95 
96 
110  bool notifyMove(SUMOTrafficObject& veh, double oldPos,
111  double newPos, double newSpeed);
112 
113 
122  virtual bool notifyLeave(SUMOTrafficObject& veh, double lastPos,
123  MSMoveReminder::Notification reason, const MSLane* enteredLane = 0);
124 
125 
130  virtual bool isEmpty() const;
131 
132 
135  virtual void update();
136 
144  virtual void write(OutputDevice& dev, long long int attributeMask, const SUMOTime period,
145  const double numLanes, const double defaultTravelTime,
146  const int numVehicles = -1) const = 0;
147 
151  virtual double getSamples() const;
152 
156  double getTravelledDistance() const {
157  return travelledDistance;
158  }
159 
161  template <class T>
162  static void checkWriteAttribute(OutputDevice& dev, long long int attributeMask, const SumoXMLAttr attr, const T& val) {
163  if (attributeMask == 0 || attributeMask & ((long long int)1 << attr)) {
164  dev.writeAttr(attr, val);
165  }
166  }
167 
168  protected:
170  const MSMeanData* const myParent;
171 
173  const double myLaneLength;
174 
179 
183 
184  };
185 
186 
192  public:
194  MeanDataValueTracker(MSLane* const lane, const double length,
195  const MSMeanData* const parent);
196 
198  virtual ~MeanDataValueTracker();
199 
202  void reset(bool afterWrite);
203 
208  void addTo(MSMeanData::MeanDataValues& val) const;
209 
212 
216  void notifyMoveInternal(const SUMOTrafficObject& veh, const double frontOnLane, const double timeOnLane, const double meanSpeedFrontOnLane, const double meanSpeedVehicleOnLane, const double travelledDistanceFrontOnLane, const double travelledDistanceVehicleOnLane, const double meanLengthOnLane);
217 
218 
228  bool notifyLeave(SUMOTrafficObject& veh, double lastPos, MSMoveReminder::Notification reason, const MSLane* enteredLane = 0);
229 
230 
242  bool notifyEnter(SUMOTrafficObject& veh, MSMoveReminder::Notification reason, const MSLane* enteredLane = 0);
244 
245  bool isEmpty() const;
246 
254  void write(OutputDevice& dev, long long int attributeMask, const SUMOTime period,
255  const double numLanes, const double defaultTravelTime,
256  const int numVehicles = -1) const;
257 
258  int getNumReady() const;
259 
260  void clearFirst();
261 
262  double getSamples() const;
263 
264  private:
265  class TrackerEntry {
266  public:
269  : myNumVehicleEntered(0), myNumVehicleLeft(0), myValues(values) {}
270 
272  virtual ~TrackerEntry() {
273  delete myValues;
274  }
275 
278 
281 
284  };
285 
287  std::map<const SUMOTrafficObject*, TrackerEntry*> myTrackedData;
288 
290  std::list<TrackerEntry*> myCurrentData;
291 
292  };
293 
294 
295 public:
311  MSMeanData(const std::string& id,
312  const SUMOTime dumpBegin, const SUMOTime dumpEnd,
313  const bool useLanes, const bool withEmpty,
314  const bool printDefaults, const bool withInternal,
315  const bool trackVehicles, const int detectPersons,
316  const double minSamples,
317  const double maxTravelTime,
318  const std::string& vTypes,
319  const std::string& writeAttributes);
320 
321 
323  virtual ~MSMeanData();
324 
327  void init();
328 
331 
346  void writeXMLOutput(OutputDevice& dev, SUMOTime startTime, SUMOTime stopTime);
347 
354  virtual void writeXMLDetectorProlog(OutputDevice& dev) const;
356 
359  virtual void detectorUpdate(const SUMOTime step);
360 
361  double getMinSamples() const {
362  return myMinSamples;
363  }
364 
365  double getMaxTravelTime() const {
366  return myMaxTravelTime;
367  }
368 
369  bool isEdgeData() const {
370  return myAmEdgeBased;
371  }
372 
373 
374 protected:
380  virtual MSMeanData::MeanDataValues* createValues(MSLane* const lane, const double length, const bool doAdd) const = 0;
381 
387  void resetOnly(SUMOTime stopTime);
388 
393  virtual std::string getEdgeID(const MSEdge* const edge);
394 
409  void writeEdge(OutputDevice& dev, const std::vector<MeanDataValues*>& edgeValues,
410  MSEdge* edge, SUMOTime startTime, SUMOTime stopTime);
411 
418  virtual void openInterval(OutputDevice& dev, const SUMOTime startTime, const SUMOTime stopTime);
419 
429  virtual bool writePrefix(OutputDevice& dev, const MeanDataValues& values,
430  const SumoXMLTag tag, const std::string id) const;
431 
432 protected:
434  const double myMinSamples;
435 
437  const double myMaxTravelTime;
438 
440  std::vector<std::vector<MeanDataValues*> > myMeasures;
441 
443  const bool myDumpEmpty;
444 
445 private:
446  static long long int initWrittenAttributes(const std::string writeAttributes, const std::string& id);
447 
449  const bool myAmEdgeBased;
450 
453 
456 
458  const bool myPrintDefaults;
459 
461  const bool myDumpInternal;
462 
464  const bool myTrackVehicles;
465 
467  const long long int myWrittenAttributes;
468 
470  std::list< std::pair<SUMOTime, SUMOTime> > myPendingIntervals;
471 
472 private:
475 
478 
479 };
std::vector< MSEdge * > MSEdgeVector
Definition: MSEdge.h:73
std::vector< MSEdge * > MSEdgeVector
Definition: MSMeanData.h:39
long long int SUMOTime
Definition: SUMOTime.h:31
SumoXMLTag
Numbers representing SUMO-XML - element names.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
Base of value-generating classes (detectors)
A road/street connecting two junctions.
Definition: MSEdge.h:77
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
Definition: MSMeanData.h:265
MeanDataValues * myValues
The number of vehicles which left in the current interval.
Definition: MSMeanData.h:283
virtual ~TrackerEntry()
Constructor.
Definition: MSMeanData.h:272
int myNumVehicleEntered
The number of vehicles which entered in the current interval.
Definition: MSMeanData.h:277
TrackerEntry(MeanDataValues *const values)
Constructor.
Definition: MSMeanData.h:268
int myNumVehicleLeft
The number of vehicles which left in the current interval.
Definition: MSMeanData.h:280
Data structure for mean (aggregated) edge/lane values for tracked vehicles.
Definition: MSMeanData.h:191
void write(OutputDevice &dev, long long int attributeMask, const SUMOTime period, const double numLanes, const double defaultTravelTime, const int numVehicles=-1) const
Writes output values into the given stream.
Definition: MSMeanData.cpp:373
double getSamples() const
Returns the number of collected sample seconds.
Definition: MSMeanData.cpp:400
MeanDataValueTracker(MSLane *const lane, const double length, const MSMeanData *const parent)
Constructor.
Definition: MSMeanData.cpp:285
bool notifyLeave(SUMOTrafficObject &veh, double lastPos, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Called if the vehicle leaves the reminder's lane.
Definition: MSMeanData.cpp:334
void addTo(MSMeanData::MeanDataValues &val) const
Add the values of this to the given one and store them there.
Definition: MSMeanData.cpp:322
void notifyMoveInternal(const SUMOTrafficObject &veh, const double frontOnLane, const double timeOnLane, const double meanSpeedFrontOnLane, const double meanSpeedVehicleOnLane, const double travelledDistanceFrontOnLane, const double travelledDistanceVehicleOnLane, const double meanLengthOnLane)
Internal notification about the vehicle moves.
Definition: MSMeanData.cpp:328
virtual ~MeanDataValueTracker()
Destructor.
Definition: MSMeanData.cpp:293
bool notifyEnter(SUMOTrafficObject &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Computes current values and adds them to their sums.
Definition: MSMeanData.cpp:343
std::list< TrackerEntry * > myCurrentData
The currently active meandata "intervals".
Definition: MSMeanData.h:290
bool isEmpty() const
Returns whether any data was collected.
Definition: MSMeanData.cpp:367
void reset(bool afterWrite)
Resets values so they may be used for the next interval.
Definition: MSMeanData.cpp:310
std::map< const SUMOTrafficObject *, TrackerEntry * > myTrackedData
The map of vehicles to data entries.
Definition: MSMeanData.h:287
Data structure for mean (aggregated) edge/lane values.
Definition: MSMeanData.h:66
static void checkWriteAttribute(OutputDevice &dev, long long int attributeMask, const SumoXMLAttr attr, const T &val)
write attribute if it passed the attribute mask check
Definition: MSMeanData.h:162
virtual void addTo(MeanDataValues &val) const =0
Add the values of this to the given one and store them there.
virtual bool notifyLeave(SUMOTrafficObject &veh, double lastPos, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Called if the vehicle leaves the reminder's lane.
Definition: MSMeanData.cpp:257
MeanDataValues(MSLane *const lane, const double length, const bool doAdd, const MSMeanData *const parent)
Constructor.
Definition: MSMeanData.cpp:57
double getTravelledDistance() const
Returns the total travelled distance.
Definition: MSMeanData.h:156
virtual void update()
Called if a per timestep update is needed. Default does nothing.
Definition: MSMeanData.cpp:272
double travelledDistance
The sum of the distances the vehicles travelled.
Definition: MSMeanData.h:181
virtual bool isEmpty() const
Returns whether any data was collected.
Definition: MSMeanData.cpp:266
virtual void write(OutputDevice &dev, long long int attributeMask, const SUMOTime period, const double numLanes, const double defaultTravelTime, const int numVehicles=-1) const =0
Writes output values into the given stream.
bool notifyMove(SUMOTrafficObject &veh, double oldPos, double newPos, double newSpeed)
Checks whether the reminder still has to be notified about the vehicle moves.
Definition: MSMeanData.cpp:84
const double myLaneLength
The length of the lane / edge the data collector is on.
Definition: MSMeanData.h:173
virtual ~MeanDataValues()
Destructor.
Definition: MSMeanData.cpp:67
virtual void reset(bool afterWrite=false)=0
Resets values so they may be used for the next interval.
virtual bool notifyEnter(SUMOTrafficObject &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Called if the vehicle enters the reminder's lane.
Definition: MSMeanData.cpp:72
const MSMeanData *const myParent
The meandata parent.
Definition: MSMeanData.h:170
virtual double getSamples() const
Returns the number of collected sample seconds.
Definition: MSMeanData.cpp:277
Data collector for edges/lanes.
Definition: MSMeanData.h:57
double getMaxTravelTime() const
Definition: MSMeanData.h:365
void writeXMLOutput(OutputDevice &dev, SUMOTime startTime, SUMOTime stopTime)
Writes collected values into the given stream.
Definition: MSMeanData.cpp:603
void writeEdge(OutputDevice &dev, const std::vector< MeanDataValues * > &edgeValues, MSEdge *edge, SUMOTime startTime, SUMOTime stopTime)
Writes edge values into the given stream.
Definition: MSMeanData.cpp:518
const bool myDumpInternal
Whether internal lanes/edges shall be written.
Definition: MSMeanData.h:461
const SUMOTime myDumpBegin
The first and the last time step to write information (-1 indicates always)
Definition: MSMeanData.h:452
virtual void detectorUpdate(const SUMOTime step)
Updates the detector.
Definition: MSMeanData.cpp:649
bool isEdgeData() const
Definition: MSMeanData.h:369
virtual ~MSMeanData()
Destructor.
Definition: MSMeanData.cpp:478
double getMinSamples() const
Definition: MSMeanData.h:361
virtual MSMeanData::MeanDataValues * createValues(MSLane *const lane, const double length, const bool doAdd) const =0
Create an instance of MeanDataValues.
void init()
Adds the value collectors to all relevant edges.
Definition: MSMeanData.cpp:433
const double myMinSamples
the minimum sample seconds
Definition: MSMeanData.h:434
void resetOnly(SUMOTime stopTime)
Resets network value in order to allow processing of the next interval.
Definition: MSMeanData.cpp:488
const long long int myWrittenAttributes
bit mask for checking attributes to be written
Definition: MSMeanData.h:467
const bool myPrintDefaults
Whether empty lanes/edges shall be written.
Definition: MSMeanData.h:458
MSMeanData(const MSMeanData &)
Invalidated copy constructor.
const bool myAmEdgeBased
Information whether the output shall be edge-based (not lane-based)
Definition: MSMeanData.h:449
const double myMaxTravelTime
the maximum travel time to write
Definition: MSMeanData.h:437
const SUMOTime myDumpEnd
Definition: MSMeanData.h:452
std::list< std::pair< SUMOTime, SUMOTime > > myPendingIntervals
The intervals for which output still has to be generated (only in the tracking case)
Definition: MSMeanData.h:470
virtual std::string getEdgeID(const MSEdge *const edge)
Return the relevant edge id.
Definition: MSMeanData.cpp:512
static long long int initWrittenAttributes(const std::string writeAttributes, const std::string &id)
Definition: MSMeanData.cpp:656
virtual void writeXMLDetectorProlog(OutputDevice &dev) const
Opens the XML-output using "netstats" as root element.
Definition: MSMeanData.cpp:643
const bool myDumpEmpty
Whether empty lanes/edges shall be written.
Definition: MSMeanData.h:443
MSEdgeVector myEdges
The corresponding first edges.
Definition: MSMeanData.h:455
virtual void openInterval(OutputDevice &dev, const SUMOTime startTime, const SUMOTime stopTime)
Writes the interval opener.
Definition: MSMeanData.cpp:584
MSMeanData(const std::string &id, const SUMOTime dumpBegin, const SUMOTime dumpEnd, const bool useLanes, const bool withEmpty, const bool printDefaults, const bool withInternal, const bool trackVehicles, const int detectPersons, const double minSamples, const double maxTravelTime, const std::string &vTypes, const std::string &writeAttributes)
Constructor.
Definition: MSMeanData.cpp:408
std::vector< std::vector< MeanDataValues * > > myMeasures
Value collectors; sorted by edge, then by lane.
Definition: MSMeanData.h:440
virtual bool writePrefix(OutputDevice &dev, const MeanDataValues &values, const SumoXMLTag tag, const std::string id) const
Checks for emptiness and writes prefix into the given stream.
Definition: MSMeanData.cpp:591
const bool myTrackVehicles
Whether vehicles are tracked.
Definition: MSMeanData.h:464
MSMeanData & operator=(const MSMeanData &)
Invalidated assignment operator.
Something on a lane to be noticed about vehicle movement.
Notification
Definition of a vehicle state.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:60
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:239
Representation of a vehicle, person, or container.