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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
22 // Detectors container; responsible for string and output generation
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <iostream>
36 #include "MSDetectorControl.h"
37 #include "MSMeanData_Net.h"
39 #include <utils/options/Option.h>
41 
42 
43 // ===========================================================================
44 // member method definitions
45 // ===========================================================================
47 }
48 
49 
51  for (std::map<SumoXMLTag, NamedObjectCont<MSDetectorFileOutput*> >::iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
52  (*i).second.clear();
53  }
54  for (std::vector<MSMeanData*>::const_iterator i = myMeanData.begin(); i != myMeanData.end(); ++i) {
55  delete *i;
56  }
57 }
58 
59 
60 void
62  // flush the last values
63  writeOutput(step, true);
64  // [...] files are closed on another place [...]
65  myIntervals.clear();
66 }
67 
68 
69 void
70 MSDetectorControl::add(SumoXMLTag type, MSDetectorFileOutput* d, const std::string& device, SUMOTime splInterval, SUMOTime begin) {
71  if (!myDetectors[type].add(d->getID(), d)) {
72  throw ProcessError(toString(type) + " detector '" + d->getID() + "' could not be build (declared twice?).");
73  }
74  addDetectorAndInterval(d, &OutputDevice::getDevice(device), splInterval, begin);
75 }
76 
77 
78 
79 void
81  if (!myDetectors[type].add(d->getID(), d)) {
82  throw ProcessError(toString(type) + " detector '" + d->getID() + "' could not be build (declared twice?).");
83  }
84 }
85 
86 
87 
88 void
89 MSDetectorControl::add(MSMeanData* mn, const std::string& device,
90  SUMOTime frequency, SUMOTime begin) {
91  myMeanData.push_back(mn);
92  addDetectorAndInterval(mn, &OutputDevice::getDevice(device), frequency, begin);
93  if (begin == string2time(OptionsCont::getOptions().getString("begin"))) {
94  mn->init();
95  }
96 }
97 
98 
99 const std::vector<SumoXMLTag>
101  std::vector<SumoXMLTag> result;
102  for (std::map<SumoXMLTag, NamedObjectCont<MSDetectorFileOutput*> >::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
103  result.push_back(i->first);
104  }
105  return result;
106 }
107 
108 
111  if (myDetectors.find(type) == myDetectors.end()) {
112  return myEmptyContainer;
113  }
114  return myDetectors.find(type)->second;
115 }
116 
117 
118 void
120  for (const auto& i : myDetectors) {
121  for (const auto& j : getTypedDetectors(i.first)) {
122  j.second->detectorUpdate(step);
123  }
124  }
125  for (MSMeanData* const i : myMeanData) {
126  i->detectorUpdate(step);
127  }
128 }
129 
130 
131 void
133  for (Intervals::iterator i = myIntervals.begin(); i != myIntervals.end(); ++i) {
134  IntervalsKey interval = (*i).first;
135  if (myLastCalls[interval] + interval.first <= step || (closing && myLastCalls[interval] < step)) {
136  DetectorFileVec dfVec = (*i).second;
137  SUMOTime startTime = myLastCalls[interval];
138  // check whether at the end the output was already generated
139  for (DetectorFileVec::iterator it = dfVec.begin(); it != dfVec.end(); ++it) {
140  MSDetectorFileOutput* det = it->first;
141  det->writeXMLOutput(*(it->second), startTime, step);
142  }
143  myLastCalls[interval] = step;
144  }
145  }
146 }
147 
148 
149 void
151  OutputDevice* device,
152  SUMOTime interval,
153  SUMOTime begin) {
154  if (begin == -1) {
155  begin = string2time(OptionsCont::getOptions().getString("begin"));
156  }
157  IntervalsKey key = std::make_pair(interval, begin);
158  Intervals::iterator it = myIntervals.find(key);
159  // Add command for given key only once to MSEventControl...
160  if (it == myIntervals.end()) {
161  DetectorFileVec detAndFileVec;
162  detAndFileVec.push_back(std::make_pair(det, device));
163  myIntervals.insert(std::make_pair(key, detAndFileVec));
164  myLastCalls[key] = begin;
165  } else {
166  DetectorFileVec& detAndFileVec = it->second;
167  if (find_if(detAndFileVec.begin(), detAndFileVec.end(), bind2nd(detectorEquals(), det)) == detAndFileVec.end()) {
168  detAndFileVec.push_back(std::make_pair(det, device));
169  } else {
170  // detector already in container. Don't add several times
171  WRITE_WARNING("MSDetectorControl::addDetectorAndInterval: detector already in container. Ignoring.");
172  return;
173  }
174  }
175  det->writeXMLDetectorProlog(*device);
176 }
177 
178 
179 
180 /****************************************************************************/
181 
std::vector< DetectorFilePair > DetectorFileVec
Container holding DetectorFilePair (with the same interval).
Data collector for edges/lanes.
Definition: MSMeanData.h:66
SumoXMLTag
Numbers representing SUMO-XML - element names.
std::vector< MSMeanData * > myMeanData
List of harmonoise detectors.
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:74
A map of named object pointers.
Returns true if detectors are equal.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:64
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:55
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:46
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:70
long long int SUMOTime
Definition: TraCIDefs.h:51
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:432