SUMO - Simulation of Urban MObility
MSDetectorControl.cpp
Go to the documentation of this file.
1 /****************************************************************************/
12 // Detectors container; responsible for string and output generation
13 /****************************************************************************/
14 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
15 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
16 /****************************************************************************/
17 //
18 // This file is part of SUMO.
19 // SUMO is free software: you can redistribute it and/or modify
20 // it under the terms of the GNU General Public License as published by
21 // the Free Software Foundation, either version 3 of the License, or
22 // (at your option) any later version.
23 //
24 /****************************************************************************/
25 
26 
27 // ===========================================================================
28 // included modules
29 // ===========================================================================
30 #ifdef _MSC_VER
31 #include <windows_config.h>
32 #else
33 #include <config.h>
34 #endif
35 
36 #include <iostream>
37 #include "MSDetectorControl.h"
38 #include "MSMeanData_Net.h"
40 #include <utils/options/Option.h>
42 
43 
44 // ===========================================================================
45 // member method definitions
46 // ===========================================================================
48 }
49 
50 
52  for (std::map<SumoXMLTag, NamedObjectCont<MSDetectorFileOutput*> >::iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
53  (*i).second.clear();
54  }
55  for (std::vector<MSMeanData*>::const_iterator i = myMeanData.begin(); i != myMeanData.end(); ++i) {
56  delete *i;
57  }
58 }
59 
60 
61 void
63  // flush the last values
64  writeOutput(step, true);
65  // [...] files are closed on another place [...]
66  myIntervals.clear();
67 }
68 
69 
70 void
71 MSDetectorControl::add(SumoXMLTag type, MSDetectorFileOutput* d, const std::string& device, SUMOTime splInterval, SUMOTime begin) {
72  if (!myDetectors[type].add(d->getID(), d)) {
73  throw ProcessError(toString(type) + " detector '" + d->getID() + "' could not be build (declared twice?).");
74  }
75  addDetectorAndInterval(d, &OutputDevice::getDevice(device), splInterval, begin);
76 }
77 
78 
79 
80 void
82  if (!myDetectors[type].add(d->getID(), d)) {
83  throw ProcessError(toString(type) + " detector '" + d->getID() + "' could not be build (declared twice?).");
84  }
85 }
86 
87 
88 
89 void
90 MSDetectorControl::add(MSMeanData* mn, const std::string& device,
91  SUMOTime frequency, SUMOTime begin) {
92  myMeanData.push_back(mn);
93  addDetectorAndInterval(mn, &OutputDevice::getDevice(device), frequency, begin);
94  if (begin == string2time(OptionsCont::getOptions().getString("begin"))) {
95  mn->init();
96  }
97 }
98 
99 
100 const std::vector<SumoXMLTag>
102  std::vector<SumoXMLTag> result;
103  for (std::map<SumoXMLTag, NamedObjectCont<MSDetectorFileOutput*> >::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
104  result.push_back(i->first);
105  }
106  return result;
107 }
108 
109 
112  if (myDetectors.find(type) == myDetectors.end()) {
113  return myEmptyContainer;
114  }
115  return myDetectors.find(type)->second;
116 }
117 
118 
119 void
121  for (std::map<SumoXMLTag, NamedObjectCont<MSDetectorFileOutput*> >::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
122  const std::map<std::string, MSDetectorFileOutput*>& dets = getTypedDetectors((*i).first).getMyMap();
123  for (std::map<std::string, MSDetectorFileOutput*>::const_iterator j = dets.begin(); j != dets.end(); ++j) {
124  (*j).second->detectorUpdate(step);
125  }
126  }
127  for (std::vector<MSMeanData*>::const_iterator i = myMeanData.begin(); i != myMeanData.end(); ++i) {
128  (*i)->detectorUpdate(step);
129  }
130 }
131 
132 
133 void
135  for (Intervals::iterator i = myIntervals.begin(); i != myIntervals.end(); ++i) {
136  IntervalsKey interval = (*i).first;
137  if (myLastCalls[interval] + interval.first <= step || (closing && myLastCalls[interval] < step)) {
138  DetectorFileVec dfVec = (*i).second;
139  SUMOTime startTime = myLastCalls[interval];
140  // check whether at the end the output was already generated
141  for (DetectorFileVec::iterator it = dfVec.begin(); it != dfVec.end(); ++it) {
142  MSDetectorFileOutput* det = it->first;
143  det->writeXMLOutput(*(it->second), startTime, step);
144  }
145  myLastCalls[interval] = step;
146  }
147  }
148 }
149 
150 
151 void
153  OutputDevice* device,
154  SUMOTime interval,
155  SUMOTime begin) {
156  if (begin == -1) {
157  begin = string2time(OptionsCont::getOptions().getString("begin"));
158  }
159  IntervalsKey key = std::make_pair(interval, begin);
160  Intervals::iterator it = myIntervals.find(key);
161  // Add command for given key only once to MSEventControl...
162  if (it == myIntervals.end()) {
163  DetectorFileVec detAndFileVec;
164  detAndFileVec.push_back(std::make_pair(det, device));
165  myIntervals.insert(std::make_pair(key, detAndFileVec));
166  myLastCalls[key] = begin;
167  } else {
168  DetectorFileVec& detAndFileVec = it->second;
169  if (find_if(detAndFileVec.begin(), detAndFileVec.end(), bind2nd(detectorEquals(), det)) == detAndFileVec.end()) {
170  detAndFileVec.push_back(std::make_pair(det, device));
171  } else {
172  // detector already in container. Don't add several times
173  WRITE_WARNING("MSDetectorControl::addDetectorAndInterval: detector already in container. Ignoring.");
174  return;
175  }
176  }
177  det->writeXMLDetectorProlog(*device);
178 }
179 
180 
181 
182 /****************************************************************************/
183 
std::vector< DetectorFilePair > DetectorFileVec
Container holding DetectorFilePair (with the same interval).
Data collector for edges/lanes.
Definition: MSMeanData.h:67
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:66
A map of named object pointers.
Returns true if detectors are equal.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
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:56
const IDMap & getMyMap() const
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:47
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:71
long long int SUMOTime
Definition: TraCIDefs.h:52
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:328