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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
19 // Detectors container; responsible for string and output generation
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <iostream>
29 #include "MSDetectorControl.h"
30 #include "MSMeanData_Net.h"
32 #include <utils/options/Option.h>
34 
35 
36 // ===========================================================================
37 // member method definitions
38 // ===========================================================================
40 }
41 
42 
44  for (std::map<SumoXMLTag, NamedObjectCont<MSDetectorFileOutput*> >::iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
45  (*i).second.clear();
46  }
47  for (std::vector<MSMeanData*>::const_iterator i = myMeanData.begin(); i != myMeanData.end(); ++i) {
48  delete *i;
49  }
50 }
51 
52 
53 void
55  // flush the last values
56  writeOutput(step, true);
57  // [...] files are closed on another place [...]
58  myIntervals.clear();
59 }
60 
61 
62 void
63 MSDetectorControl::add(SumoXMLTag type, MSDetectorFileOutput* d, const std::string& device, SUMOTime splInterval, SUMOTime begin) {
64  if (!myDetectors[type].add(d->getID(), d)) {
65  throw ProcessError(toString(type) + " detector '" + d->getID() + "' could not be build (declared twice?).");
66  }
67  addDetectorAndInterval(d, &OutputDevice::getDevice(device), splInterval, begin);
68 }
69 
70 
71 
72 void
74  if (!myDetectors[type].add(d->getID(), d)) {
75  throw ProcessError(toString(type) + " detector '" + d->getID() + "' could not be build (declared twice?).");
76  }
77 }
78 
79 
80 
81 void
82 MSDetectorControl::add(MSMeanData* mn, const std::string& device,
83  SUMOTime frequency, SUMOTime begin) {
84  myMeanData.push_back(mn);
85  addDetectorAndInterval(mn, &OutputDevice::getDevice(device), frequency, begin);
86  if (begin == string2time(OptionsCont::getOptions().getString("begin"))) {
87  mn->init();
88  }
89 }
90 
91 
92 const std::vector<SumoXMLTag>
94  std::vector<SumoXMLTag> result;
95  for (std::map<SumoXMLTag, NamedObjectCont<MSDetectorFileOutput*> >::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
96  result.push_back(i->first);
97  }
98  return result;
99 }
100 
101 
104  if (myDetectors.find(type) == myDetectors.end()) {
105  return myEmptyContainer;
106  }
107  return myDetectors.find(type)->second;
108 }
109 
110 
111 void
113  for (const auto& i : myDetectors) {
114  for (const auto& j : getTypedDetectors(i.first)) {
115  j.second->detectorUpdate(step);
116  }
117  }
118  for (MSMeanData* const i : myMeanData) {
119  i->detectorUpdate(step);
120  }
121 }
122 
123 
124 void
126  for (Intervals::iterator i = myIntervals.begin(); i != myIntervals.end(); ++i) {
127  IntervalsKey interval = (*i).first;
128  if (myLastCalls[interval] + interval.first <= step || (closing && myLastCalls[interval] < step)) {
129  DetectorFileVec dfVec = (*i).second;
130  SUMOTime startTime = myLastCalls[interval];
131  // check whether at the end the output was already generated
132  for (DetectorFileVec::iterator it = dfVec.begin(); it != dfVec.end(); ++it) {
133  MSDetectorFileOutput* det = it->first;
134  det->writeXMLOutput(*(it->second), startTime, step);
135  }
136  myLastCalls[interval] = step;
137  }
138  }
139 }
140 
141 
142 void
144  OutputDevice* device,
145  SUMOTime interval,
146  SUMOTime begin) {
147  if (begin == -1) {
148  begin = string2time(OptionsCont::getOptions().getString("begin"));
149  }
150  IntervalsKey key = std::make_pair(interval, begin);
151  Intervals::iterator it = myIntervals.find(key);
152  // Add command for given key only once to MSEventControl...
153  if (it == myIntervals.end()) {
154  DetectorFileVec detAndFileVec;
155  detAndFileVec.push_back(std::make_pair(det, device));
156  myIntervals.insert(std::make_pair(key, detAndFileVec));
157  myLastCalls[key] = begin;
158  } else {
159  DetectorFileVec& detAndFileVec = it->second;
160  if (find_if(detAndFileVec.begin(), detAndFileVec.end(), bind2nd(detectorEquals(), det)) == detAndFileVec.end()) {
161  detAndFileVec.push_back(std::make_pair(det, device));
162  } else {
163  // detector already in container. Don't add several times
164  WRITE_WARNING("MSDetectorControl::addDetectorAndInterval: detector already in container. Ignoring.");
165  return;
166  }
167  }
168  det->writeXMLDetectorProlog(*device);
169 }
170 
171 
172 
173 /****************************************************************************/
174 
MSDetectorFileOutput::writeXMLDetectorProlog
virtual void writeXMLDetectorProlog(OutputDevice &dev) const =0
Open the XML-output.
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:275
MSDetectorFileOutput
Base of value-generating classes (detectors)
Definition: MSDetectorFileOutput.h:63
MSDetectorControl.h
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:63
OptionsCont.h
MsgHandler.h
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:57
MSDetectorControl::MSDetectorControl
MSDetectorControl()
Constructor.
Definition: MSDetectorControl.cpp:39
MSDetectorControl::myEmptyContainer
NamedObjectCont< MSDetectorFileOutput * > myEmptyContainer
An empty container to return in getTypedDetectors() if no detectors of the asked type exist.
Definition: MSDetectorControl.h:230
MSDetectorControl::DetectorFileVec
std::vector< DetectorFilePair > DetectorFileVec
Container holding DetectorFilePair (with the same interval).
Definition: MSDetectorControl.h:188
MSDetectorControl::close
void close(SUMOTime step)
Closes the detector outputs.
Definition: MSDetectorControl.cpp:54
MSDetectorControl::detectorEquals
Returns true if detectors are equal.
Definition: MSDetectorControl.h:208
MSMeanData_Net.h
SumoXMLTag
SumoXMLTag
Numbers representing SUMO-XML - element names.
Definition: SUMOXMLDefinitions.h:41
MSDetectorControl::getAvailableTypes
const std::vector< SumoXMLTag > getAvailableTypes() const
Returns the list of available detector types.
Definition: MSDetectorControl.cpp:93
MSDetectorControl::~MSDetectorControl
~MSDetectorControl()
Destructor.
Definition: MSDetectorControl.cpp:43
MSDetectorControl::writeOutput
void writeOutput(SUMOTime step, bool closing)
Writes the output to be generated within the given time step.
Definition: MSDetectorControl.cpp:125
MSMeanData
Data collector for edges/lanes.
Definition: MSMeanData.h:59
NamedObjectCont< MSDetectorFileOutput * >
MSDetectorControl::myLastCalls
std::map< IntervalsKey, SUMOTime > myLastCalls
The map that holds the last call for each sample interval.
Definition: MSDetectorControl.h:224
MSDetectorControl::getTypedDetectors
const NamedObjectCont< MSDetectorFileOutput * > & getTypedDetectors(SumoXMLTag type) const
Returns the list of detectors of the given type.
Definition: MSDetectorControl.cpp:103
MSDetectorFileOutput::writeXMLOutput
virtual void writeXMLOutput(OutputDevice &dev, SUMOTime startTime, SUMOTime stopTime)=0
Write the generated output to the given device.
ProcessError
Definition: UtilExceptions.h:39
MSDetectorControl::addDetectorAndInterval
void addDetectorAndInterval(MSDetectorFileOutput *det, OutputDevice *device, SUMOTime interval, SUMOTime begin=-1)
Adds one of the detectors as a new MSDetectorFileOutput.
Definition: MSDetectorControl.cpp:143
MSMeanData::init
void init()
Adds the value collectors to all relevant edges.
Definition: MSMeanData.cpp:430
string2time
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:44
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
MSDetectorControl::myIntervals
Intervals myIntervals
Map that hold DetectorFileVec for given intervals.
Definition: MSDetectorControl.h:221
OutputDevice::getDevice
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
Definition: OutputDevice.cpp:54
Option.h
MSDetectorControl::add
void add(SumoXMLTag type, MSDetectorFileOutput *d, const std::string &device, SUMOTime splInterval, SUMOTime begin=-1)
Adds a detector/output combination into the containers.
Definition: MSDetectorControl.cpp:63
config.h
MSDetectorControl::myDetectors
std::map< SumoXMLTag, NamedObjectCont< MSDetectorFileOutput * > > myDetectors
The detectors map, first by detector type, then using NamedObjectCont (.
Definition: MSDetectorControl.h:217
MSDetectorControl::IntervalsKey
std::pair< SUMOTime, SUMOTime > IntervalsKey
Definition of the interval key.
Definition: MSDetectorControl.h:191
MSDetectorControl::myMeanData
std::vector< MSMeanData * > myMeanData
List of harmonoise detectors.
Definition: MSDetectorControl.h:227
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:76
MSDetectorControl::updateDetectors
void updateDetectors(const SUMOTime step)
Computes detector values.
Definition: MSDetectorControl.cpp:112