Eclipse SUMO - Simulation of Urban MObility
MSDetectorControl.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-2022 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 /****************************************************************************/
23 // Detectors container; responsible for string and output generation
24 /****************************************************************************/
25 #include <config.h>
26 
27 #include <iostream>
28 #include "MSDetectorControl.h"
29 #include "MSMeanData_Net.h"
31 #include <utils/options/Option.h>
33 
34 
35 // ===========================================================================
36 // member method definitions
37 // ===========================================================================
39 }
40 
41 
43  for (auto i = myDetectors.begin(); i != myDetectors.end(); ++i) {
44  (*i).second.clear();
45  }
46  for (auto item : myMeanData) {
47  for (MSMeanData* md : item.second) {
48  delete md;
49  }
50  }
51  myMeanData.clear();
52 }
53 
54 
55 void
57  // flush the last values
58  writeOutput(step, true);
59  // [...] files are closed on another place [...]
60  myIntervals.clear();
61 }
62 
63 
64 void
65 MSDetectorControl::add(SumoXMLTag type, MSDetectorFileOutput* d, const std::string& device, SUMOTime interval, SUMOTime begin) {
66  if (!myDetectors[type].add(d->getID(), d)) {
67  throw ProcessError(toString(type) + " detector '" + d->getID() + "' could not be build (declared twice?).");
68  }
69  addDetectorAndInterval(d, &OutputDevice::getDevice(device), interval, begin);
70 }
71 
72 
73 
74 void
76  if (!myDetectors[type].add(d->getID(), d)) {
77  throw ProcessError(toString(type) + " detector '" + d->getID() + "' could not be build (declared twice?).");
78  }
79 }
80 
81 
82 
83 void
84 MSDetectorControl::add(MSMeanData* md, const std::string& device,
85  SUMOTime frequency, SUMOTime begin) {
86  myMeanData[md->getID()].push_back(md);
87  addDetectorAndInterval(md, &OutputDevice::getDevice(device), frequency, begin);
88  if (begin <= string2time(OptionsCont::getOptions().getString("begin"))) {
89  md->init();
90  }
91 }
92 
93 
94 const std::vector<SumoXMLTag>
96  std::vector<SumoXMLTag> result;
97  for (std::map<SumoXMLTag, NamedObjectCont<MSDetectorFileOutput*> >::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
98  result.push_back(i->first);
99  }
100  return result;
101 }
102 
103 
106  if (myDetectors.find(type) == myDetectors.end()) {
107  return myEmptyContainer;
108  }
109  return myDetectors.find(type)->second;
110 }
111 
112 
113 void
115  for (const auto& i : myDetectors) {
116  for (const auto& j : getTypedDetectors(i.first)) {
117  j.second->detectorUpdate(step);
118  }
119  }
120  for (auto item : myMeanData) {
121  for (MSMeanData* md : item.second) {
122  md->detectorUpdate(step);
123  }
124  }
125 }
126 
127 
128 void
130  for (Intervals::iterator i = myIntervals.begin(); i != myIntervals.end(); ++i) {
131  IntervalsKey interval = (*i).first;
132  if (myLastCalls[interval] + interval.first <= step || (closing && myLastCalls[interval] < step)) {
133  DetectorFileVec dfVec = (*i).second;
134  SUMOTime startTime = myLastCalls[interval];
135  // check whether at the end the output was already generated
136  for (DetectorFileVec::iterator it = dfVec.begin(); it != dfVec.end(); ++it) {
137  MSDetectorFileOutput* det = it->first;
138  det->writeXMLOutput(*(it->second), startTime, step);
139  }
140  myLastCalls[interval] = step;
141  }
142  }
143 }
144 
145 
146 void
148  OutputDevice* device,
149  SUMOTime interval,
150  SUMOTime begin) {
151  const SUMOTime simBegin = string2time(OptionsCont::getOptions().getString("begin"));
152  if (begin == -1) {
153  begin = simBegin;
154  }
155  IntervalsKey key = std::make_pair(interval, begin);
156  Intervals::iterator it = myIntervals.find(key);
157  // Add command for given key only once to MSEventControl...
158  if (it == myIntervals.end()) {
159  DetectorFileVec detAndFileVec;
160  detAndFileVec.push_back(std::make_pair(det, device));
161  myIntervals.insert(std::make_pair(key, detAndFileVec));
162  SUMOTime lastCall = begin;
163  if (begin < simBegin) {
164  SUMOTime divRest = (simBegin - begin) % interval;
165  lastCall = simBegin - divRest;
166  }
167  myLastCalls[key] = lastCall;
168  } else {
169  DetectorFileVec& detAndFileVec = it->second;
170  if (find_if(detAndFileVec.begin(), detAndFileVec.end(), [&](const DetectorFilePair & pair) {
171  return pair.first == det;
172  }) == detAndFileVec.end()) {
173  detAndFileVec.push_back(std::make_pair(det, device));
174  } else {
175  // detector already in container. Don't add several times
176  WRITE_WARNING("MSDetectorControl::addDetectorAndInterval: detector already in container. Ignoring.");
177  return;
178  }
179  }
180  det->writeXMLDetectorProlog(*device);
181 }
182 
183 void
185  for (const auto& i : myDetectors) {
186  for (const auto& j : getTypedDetectors(i.first)) {
187  j.second->clearState();
188  }
189  }
190 }
191 
192 /****************************************************************************/
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:280
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:45
long long int SUMOTime
Definition: SUMOTime.h:32
SumoXMLTag
Numbers representing SUMO-XML - element names.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
void writeOutput(SUMOTime step, bool closing)
Writes the output to be generated within the given time step.
std::pair< SUMOTime, SUMOTime > IntervalsKey
Definition of the interval key (interval, begin)
const NamedObjectCont< MSDetectorFileOutput * > & getTypedDetectors(SumoXMLTag type) const
Returns the list of detectors of the given type.
std::map< IntervalsKey, SUMOTime > myLastCalls
The map that holds the last call for each sample interval.
Intervals myIntervals
Map that hold DetectorFileVec for given intervals.
void clearState()
Remove all vehicles before quick-loading state.
~MSDetectorControl()
Destructor.
void updateDetectors(const SUMOTime step)
Computes detector values.
void add(SumoXMLTag type, MSDetectorFileOutput *d, const std::string &device, SUMOTime interval, SUMOTime begin=-1)
Adds a detector/output combination into the containers.
std::vector< DetectorFilePair > DetectorFileVec
Container holding DetectorFilePair (with the same interval).
std::map< std::string, std::vector< MSMeanData * > > myMeanData
List of meanData detectors.
void close(SUMOTime step)
Closes the detector outputs.
const std::vector< SumoXMLTag > getAvailableTypes() const
Returns the list of available detector types.
MSDetectorControl()
Constructor.
std::pair< MSDetectorFileOutput *, OutputDevice * > DetectorFilePair
A pair of a Detector with it's associated file-stream.
NamedObjectCont< MSDetectorFileOutput * > myEmptyContainer
An empty container to return in getTypedDetectors() if no detectors of the asked type exist.
std::map< SumoXMLTag, NamedObjectCont< MSDetectorFileOutput * > > myDetectors
The detectors map, first by detector type, then using NamedObjectCont (.
void addDetectorAndInterval(MSDetectorFileOutput *det, OutputDevice *device, SUMOTime interval, SUMOTime begin=-1)
Adds one of the detectors as a new MSDetectorFileOutput.
Base of value-generating classes (detectors)
virtual void writeXMLDetectorProlog(OutputDevice &dev) const =0
Open the XML-output.
virtual void writeXMLOutput(OutputDevice &dev, SUMOTime startTime, SUMOTime stopTime)=0
Write the generated output to the given device.
Data collector for edges/lanes.
Definition: MSMeanData.h:57
virtual void detectorUpdate(const SUMOTime step)
Updates the detector.
Definition: MSMeanData.cpp:663
void init()
Adds the value collectors to all relevant edges.
Definition: MSMeanData.cpp:438
const std::string & getID() const
Returns the id.
Definition: Named.h:74
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.