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.sourceforge.net/
15 // Copyright (C) 2001-2012 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, OutputDevice& device, int splInterval, SUMOTime begin) {
80  if (myDetectors.find(type) == myDetectors.end()) {
82  }
84  // insert object into dictionary
85  if (! m.add(d->getID(), d)) {
86  throw ProcessError(toString(type) + " detector '" + d->getID() + "' could not be build (declared twice?).");
87  }
88  addDetectorAndInterval(d, &device, splInterval, begin);
89 }
90 
91 
92 
93 void
95  if (myDetectors.find(type) == myDetectors.end()) {
97  }
99  // insert object into dictionary
100  if (! m.add(d->getID(), d)) {
101  throw ProcessError(toString(type) + " detector '" + d->getID() + "' could not be build (declared twice?).");
102  }
103 }
104 
105 
106 
107 void
109  SUMOTime frequency, SUMOTime begin) {
110  myMeanData.push_back(mn);
111  addDetectorAndInterval(mn, &device, frequency, begin);
112  if (begin == string2time(OptionsCont::getOptions().getString("begin"))) {
113  mn->init();
114  }
115 }
116 
117 
120  if (myDetectors.find(type) == myDetectors.end()) {
121  return myEmptyContainer;//myDetectors[type] = NamedObjectCont<MSDetectorFileOutput*>();
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