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-2016 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 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // CHECK_MEMORY_LEAKS
46 
47 
48 // ===========================================================================
49 // member method definitions
50 // ===========================================================================
52 }
53 
54 
56  for (std::map<SumoXMLTag, NamedObjectCont<MSDetectorFileOutput*> >::iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
57  (*i).second.clear();
58  }
59  for (std::vector<MSMeanData*>::const_iterator i = myMeanData.begin(); i != myMeanData.end(); ++i) {
60  delete *i;
61  }
62 }
63 
64 
65 void
67  // flush the last values
68  writeOutput(step, true);
69  // [...] files are closed on another place [...]
70  myIntervals.clear();
71 }
72 
73 
74 void
75 MSDetectorControl::add(SumoXMLTag type, MSDetectorFileOutput* d, const std::string& device, SUMOTime splInterval, SUMOTime begin) {
76  if (!myDetectors[type].add(d->getID(), d)) {
77  throw ProcessError(toString(type) + " detector '" + d->getID() + "' could not be build (declared twice?).");
78  }
79  addDetectorAndInterval(d, &OutputDevice::getDevice(device), splInterval, begin);
80 }
81 
82 
83 
84 void
86  if (!myDetectors[type].add(d->getID(), d)) {
87  throw ProcessError(toString(type) + " detector '" + d->getID() + "' could not be build (declared twice?).");
88  }
89 }
90 
91 
92 
93 void
94 MSDetectorControl::add(MSMeanData* mn, const std::string& device,
95  SUMOTime frequency, SUMOTime begin) {
96  myMeanData.push_back(mn);
97  addDetectorAndInterval(mn, &OutputDevice::getDevice(device), frequency, begin);
98  if (begin == string2time(OptionsCont::getOptions().getString("begin"))) {
99  mn->init();
100  }
101 }
102 
103 
104 const std::vector<SumoXMLTag>
106  std::vector<SumoXMLTag> result;
107  for (std::map<SumoXMLTag, NamedObjectCont<MSDetectorFileOutput*> >::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
108  result.push_back(i->first);
109  }
110  return result;
111 }
112 
113 
116  if (myDetectors.find(type) == myDetectors.end()) {
117  return myEmptyContainer;
118  }
119  return myDetectors.find(type)->second;
120 }
121 
122 
123 void
125  for (std::map<SumoXMLTag, NamedObjectCont<MSDetectorFileOutput*> >::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
126  const std::map<std::string, MSDetectorFileOutput*>& dets = getTypedDetectors((*i).first).getMyMap();
127  for (std::map<std::string, MSDetectorFileOutput*>::const_iterator j = dets.begin(); j != dets.end(); ++j) {
128  (*j).second->detectorUpdate(step);
129  }
130  }
131  for (std::vector<MSMeanData*>::const_iterator i = myMeanData.begin(); i != myMeanData.end(); ++i) {
132  (*i)->detectorUpdate(step);
133  }
134 }
135 
136 
137 void
139  for (Intervals::iterator i = myIntervals.begin(); i != myIntervals.end(); ++i) {
140  IntervalsKey interval = (*i).first;
141  if (myLastCalls[interval] + interval.first <= step || (closing && myLastCalls[interval] < step)) {
142  DetectorFileVec dfVec = (*i).second;
143  SUMOTime startTime = myLastCalls[interval];
144  // check whether at the end the output was already generated
145  for (DetectorFileVec::iterator it = dfVec.begin(); it != dfVec.end(); ++it) {
146  MSDetectorFileOutput* det = it->first;
147  det->writeXMLOutput(*(it->second), startTime, step);
148  }
149  myLastCalls[interval] = step;
150  }
151  }
152 }
153 
154 
155 void
157  OutputDevice* device,
158  SUMOTime interval,
159  SUMOTime begin) {
160  if (begin == -1) {
161  begin = string2time(OptionsCont::getOptions().getString("begin"));
162  }
163  IntervalsKey key = std::make_pair(interval, begin);
164  Intervals::iterator it = myIntervals.find(key);
165  // Add command for given key only once to MSEventControl...
166  if (it == myIntervals.end()) {
167  DetectorFileVec detAndFileVec;
168  detAndFileVec.push_back(std::make_pair(det, device));
169  myIntervals.insert(std::make_pair(key, detAndFileVec));
170  myLastCalls[key] = begin;
171  } else {
172  DetectorFileVec& detAndFileVec = it->second;
173  if (find_if(detAndFileVec.begin(), detAndFileVec.end(), bind2nd(detectorEquals(), det)) == detAndFileVec.end()) {
174  detAndFileVec.push_back(std::make_pair(det, device));
175  } else {
176  // detector already in container. Don't add several times
177  WRITE_WARNING("MSDetectorControl::addDetectorAndInterval: detector already in container. Ignoring.");
178  return;
179  }
180  }
181  det->writeXMLDetectorProlog(*device);
182 }
183 
184 
185 
186 /****************************************************************************/
187 
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.
long long int SUMOTime
Definition: SUMOTime.h:43
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:69
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...
const IDMap & getMyMap() const
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::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
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
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:332