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>
32#include "MSMeanData_Net.h"
33#include "MSDetectorControl.h"
34
35
36// ===========================================================================
37// member method definitions
38// ===========================================================================
40}
41
42
44 for (auto i = myDetectors.begin(); i != myDetectors.end(); ++i) {
45 (*i).second.clear();
46 }
47 for (auto item : myMeanData) {
48 for (MSMeanData* md : item.second) {
49 delete md;
50 }
51 }
52 myMeanData.clear();
53}
54
55
56void
58 // flush the last values
59 writeOutput(step, true);
60 // [...] files are closed on another place [...]
61 myIntervals.clear();
62}
63
64
65void
66MSDetectorControl::add(SumoXMLTag type, MSDetectorFileOutput* d, const std::string& device, SUMOTime interval, SUMOTime begin) {
67 if (!myDetectors[type].add(d->getID(), d)) {
68 throw ProcessError(toString(type) + " detector '" + d->getID() + "' could not be build (declared twice?).");
69 }
70 addDetectorAndInterval(d, &OutputDevice::getDevice(device), interval, begin);
71}
72
73
74void
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
82void
83MSDetectorControl::add(MSMeanData* md, const std::string& device,
84 SUMOTime frequency, SUMOTime begin) {
85 myMeanData[md->getID()].push_back(md);
86 addDetectorAndInterval(md, &OutputDevice::getDevice(device), frequency, begin);
87 if (begin <= string2time(OptionsCont::getOptions().getString("begin"))) {
88 md->init();
89 }
90 MSGlobals::gHaveEmissions |= typeid(*md) == typeid(MSMeanData_Emissions);
91}
92
93
94const 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
113void
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
128void
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
146void
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(TL("MSDetectorControl::addDetectorAndInterval: detector already in container. Ignoring."));
177 return;
178 }
179 }
180 det->writeXMLDetectorProlog(*device);
181}
182
183void
185 for (const auto& i : myDetectors) {
186 for (const auto& j : getTypedDetectors(i.first)) {
187 j.second->clearState(step);
188 }
189 }
190}
191
192/****************************************************************************/
long long int SUMOTime
Definition: GUI.h:36
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:265
#define TL(string)
Definition: MsgHandler.h:282
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:45
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(SUMOTime step)
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.
static bool gHaveEmissions
Whether emission output of some type is needed (files or GUI)
Definition: MSGlobals.h:177
Emission data collector for edges/lanes.
Data collector for edges/lanes.
Definition: MSMeanData.h:57
virtual void detectorUpdate(const SUMOTime step)
Updates the detector.
Definition: MSMeanData.cpp:721
void init()
Adds the value collectors to all relevant edges.
Definition: MSMeanData.cpp:443
const std::string & getID() const
Returns the id.
Definition: Named.h:74
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:59
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
static OutputDevice & getDevice(const std::string &name, bool usePrefix=true)
Returns the described OutputDevice.