SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-sim.org/
15 // Copyright (C) 2001-2014 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 HAVE_INTERNAL
44 #include <mesosim/MEInductLoop.h>
45 #endif
46 
47 #ifdef CHECK_MEMORY_LEAKS
48 #include <foreign/nvwa/debug_new.h>
49 #endif // CHECK_MEMORY_LEAKS
50 
51 
52 // ===========================================================================
53 // member method definitions
54 // ===========================================================================
56 }
57 
58 
60  for (std::map<SumoXMLTag, NamedObjectCont<MSDetectorFileOutput*> >::iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
61  (*i).second.clear();
62  }
63  for (std::vector<MSMeanData*>::const_iterator i = myMeanData.begin(); i != myMeanData.end(); ++i) {
64  delete *i;
65  }
66 }
67 
68 
69 void
71  // flush the last values
72  writeOutput(step, true);
73  // [...] files are closed on another place [...]
74  myIntervals.clear();
75 }
76 
77 
78 void
79 MSDetectorControl::add(SumoXMLTag type, MSDetectorFileOutput* d, const std::string& device, int splInterval, SUMOTime begin) {
80  if (!myDetectors[type].add(d->getID(), d)) {
81  throw ProcessError(toString(type) + " detector '" + d->getID() + "' could not be build (declared twice?).");
82  }
83  addDetectorAndInterval(d, &OutputDevice::getDevice(device), splInterval, begin);
84 }
85 
86 
87 
88 void
90  if (!myDetectors[type].add(d->getID(), d)) {
91  throw ProcessError(toString(type) + " detector '" + d->getID() + "' could not be build (declared twice?).");
92  }
93 }
94 
95 
96 
97 void
98 MSDetectorControl::add(MSMeanData* mn, const std::string& device,
99  SUMOTime frequency, SUMOTime begin) {
100  myMeanData.push_back(mn);
101  addDetectorAndInterval(mn, &OutputDevice::getDevice(device), frequency, begin);
102  if (begin == string2time(OptionsCont::getOptions().getString("begin"))) {
103  mn->init();
104  }
105 }
106 
107 
108 const std::vector<SumoXMLTag>
110  std::vector<SumoXMLTag> result;
111  for (std::map<SumoXMLTag, NamedObjectCont<MSDetectorFileOutput*> >::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
112  result.push_back(i->first);
113  }
114  return result;
115 }
116 
117 
120  if (myDetectors.find(type) == myDetectors.end()) {
121  return myEmptyContainer;
122  }
123  return myDetectors.find(type)->second;
124 }
125 
126 
127 void
129  for (std::map<SumoXMLTag, NamedObjectCont<MSDetectorFileOutput*> >::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
130  const std::map<std::string, MSDetectorFileOutput*>& dets = getTypedDetectors((*i).first).getMyMap();
131  for (std::map<std::string, MSDetectorFileOutput*>::const_iterator j = dets.begin(); j != dets.end(); ++j) {
132  (*j).second->detectorUpdate(step);
133  }
134  }
135  for (std::vector<MSMeanData*>::const_iterator i = myMeanData.begin(); i != myMeanData.end(); ++i) {
136  (*i)->detectorUpdate(step);
137  }
138 }
139 
140 
141 void
143  for (Intervals::iterator i = myIntervals.begin(); i != myIntervals.end(); ++i) {
144  IntervalsKey interval = (*i).first;
145  if (myLastCalls[interval] + interval.first <= step || (closing && myLastCalls[interval] < step)) {
146  DetectorFileVec dfVec = (*i).second;
147  SUMOTime startTime = myLastCalls[interval];
148  // check whether at the end the output was already generated
149  for (DetectorFileVec::iterator it = dfVec.begin(); it != dfVec.end(); ++it) {
150  MSDetectorFileOutput* det = it->first;
151  det->writeXMLOutput(*(it->second), startTime, step);
152  }
153  myLastCalls[interval] = step;
154  }
155  }
156 }
157 
158 
159 void
161  OutputDevice* device,
162  SUMOTime interval,
163  SUMOTime begin) {
164  if (begin == -1) {
165  begin = string2time(OptionsCont::getOptions().getString("begin"));
166  }
167  IntervalsKey key = std::make_pair(interval, begin);
168  Intervals::iterator it = myIntervals.find(key);
169  // Add command for given key only once to MSEventControl...
170  if (it == myIntervals.end()) {
171  DetectorFileVec detAndFileVec;
172  detAndFileVec.push_back(std::make_pair(det, device));
173  myIntervals.insert(std::make_pair(key, detAndFileVec));
174  myLastCalls[key] = begin;
175  } else {
176  DetectorFileVec& detAndFileVec = it->second;
177  if (find_if(detAndFileVec.begin(), detAndFileVec.end(), bind2nd(detectorEquals(), det)) == detAndFileVec.end()) {
178  detAndFileVec.push_back(std::make_pair(det, device));
179  } else {
180  // detector already in container. Don't add several times
181  WRITE_WARNING("MSDetectorControl::addDetectorAndInterval: detector already in container. Ignoring.");
182  return;
183  }
184  }
185  det->writeXMLDetectorProlog(*device);
186 }
187 
188 
189 
190 /****************************************************************************/
191 
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.
Returns true if detectors are equal.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:196
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
virtual void writeXMLDetectorProlog(OutputDevice &dev) const =0
Open the XML-output.
const std::string & getID() const
Returns the id.
Definition: Named.h:60
const NamedObjectCont< MSDetectorFileOutput * > & getTypedDetectors(SumoXMLTag type) const
Returns the list of detectors of the given type.
NamedObjectCont< MSDetectorFileOutput * > myEmptyContainer
An empty container to return in getTypedDetectors() if no detectors of the asked type exist...
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:48
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:52
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 (.
const IDMap & getMyMap() const
const std::vector< SumoXMLTag > getAvailableTypes() const
Returns the list of available detector types.
void close(SUMOTime step)
Closes the detector outputs.
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
~MSDetectorControl()
Destructor.
void add(SumoXMLTag type, MSDetectorFileOutput *d, const std::string &device, int splInterval, SUMOTime begin=-1)
Adds a detector/output combination into the containers.
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
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.
Base of value-generating classes (detectors)
void init()
Adds the value collectors to all relevant edges.
Definition: MSMeanData.cpp:270