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