00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029
00030 #include <iostream>
00031 #include "MSDetectorControl.h"
00032 #include "MSMeanData_Net.h"
00033 #include <microsim/output/MSE2Collector.h>
00034 #include <microsim/output/MS_E2_ZS_CollectorOverLanes.h>
00035 #include <microsim/output/MSE3Collector.h>
00036 #include <microsim/output/MSInductLoop.h>
00037 #include <microsim/output/MSMeanData_Harmonoise.h>
00038 #include <utils/options/OptionsCont.h>
00039 #include <utils/options/Option.h>
00040
00041 #ifdef _MESSAGES
00042 #include <microsim/output/MSMsgInductLoop.h>
00043 #endif
00044
00045 #ifdef HAVE_MESOSIM
00046 #include <mesosim/MEInductLoop.h>
00047 #endif
00048
00049 #ifdef CHECK_MEMORY_LEAKS
00050 #include <foreign/nvwa/debug_new.h>
00051 #endif // CHECK_MEMORY_LEAKS
00052
00053
00054
00055
00056
00057 MSDetectorControl::MSDetectorControl() throw() {
00058 }
00059
00060
00061 MSDetectorControl::~MSDetectorControl() throw() {
00062 #ifdef HAVE_MESOSIM
00063 myMesoLoops.clear();
00064 #endif
00065 myLoops.clear();
00066 #ifdef _MESSAGES
00067 #ifdef _DEBUG
00068 std::cout << "MSDetectorControl: clearing myMsgLoops" << std::endl;
00069 #endif
00070 myMsgLoops.clear();
00071 #endif
00072 myE2Detectors.clear();
00073 myE3Detectors.clear();
00074 myE2OverLanesDetectors.clear();
00075 myRouteProbeDetectors.clear();
00076 }
00077
00078
00079 void
00080 MSDetectorControl::close(SUMOTime step) throw(IOError) {
00081
00082 writeOutput(step, true);
00083
00084 myIntervals.clear();
00085 }
00086
00087
00088 #ifdef _MESSAGES
00089 void
00090 MSDetectorControl::add(MSMsgInductLoop *msgl, OutputDevice& device, int splInterval) throw(ProcessError) {
00091 #ifdef _DEBUG
00092 std::cout << "adding MSMsgInductLoop..." << std::endl;
00093 #endif
00094 if (! myMsgLoops.add(msgl->getID(), msgl)) {
00095 throw ProcessError("message induct loop '" + msgl->getID() + "' could not be build;"
00096 + "\n (declared twice?)");
00097 }
00098 addDetectorAndInterval(msgl, &device, splInterval);
00099 }
00100 #endif
00101
00102
00103 void
00104 MSDetectorControl::add(MSInductLoop *il, OutputDevice& device, int splInterval) throw(ProcessError) {
00105
00106 if (! myLoops.add(il->getID(), il)) {
00107 throw ProcessError("induct loop '" + il->getID() + "' could not be build;\n (declared twice?)");
00108 }
00109 addDetectorAndInterval(il, &device, splInterval);
00110 }
00111
00112
00113 void
00114 MSDetectorControl::add(MSE2Collector *e2, OutputDevice& device, int splInterval) throw(ProcessError) {
00115
00116 if (! myE2Detectors.add(e2->getID(), e2)) {
00117 throw ProcessError("e2-detector '" + e2->getID() + "' could not be build;\n (declared twice?)");
00118 }
00119 addDetectorAndInterval(e2, &device, splInterval);
00120 }
00121
00122
00123 void
00124 MSDetectorControl::add(MS_E2_ZS_CollectorOverLanes *e2ol, OutputDevice& device, int splInterval) throw(ProcessError) {
00125
00126 if (! myE2OverLanesDetectors.add(e2ol->getID(), e2ol)) {
00127 throw ProcessError("e2-overlanes-detector '" + e2ol->getID() + "' could not be build;\n (declared twice?)");
00128 }
00129 addDetectorAndInterval(e2ol, &device, splInterval);
00130 }
00131
00132
00133 void
00134 MSDetectorControl::add(MSE2Collector *e2) throw(ProcessError) {
00135
00136 if (! myE2Detectors.add(e2->getID(), e2)) {
00137 throw ProcessError("e2-detector '" + e2->getID() + "' could not be build;\n (declared twice?)");
00138 }
00139 }
00140
00141
00142 void
00143 MSDetectorControl::add(MS_E2_ZS_CollectorOverLanes *e2ol) throw(ProcessError) {
00144
00145 if (! myE2OverLanesDetectors.add(e2ol->getID(), e2ol)) {
00146 throw ProcessError("e2-overlanes-detector '" + e2ol->getID() + "' could not be build;\n (declared twice?)");
00147 }
00148 }
00149
00150
00151 void
00152 MSDetectorControl::add(MSMeanData_Harmonoise *mn) throw() {
00153 myHarmonoiseDetectors.push_back(mn);
00154 }
00155
00156
00157 void
00158 MSDetectorControl::add(MSE3Collector *e3, OutputDevice& device, int splInterval) throw(ProcessError) {
00159
00160 if (! myE3Detectors.add(e3->getID(), e3)) {
00161 throw ProcessError("e3-detector '" + e3->getID() + "' could not be build;\n (declared twice?)");
00162 }
00163 addDetectorAndInterval(e3, &device, splInterval);
00164 }
00165
00166
00167 void
00168 MSDetectorControl::add(MSRouteProbe *vp, OutputDevice& device,
00169 SUMOTime frequency, SUMOTime begin) throw(ProcessError) {
00170
00171 if (! myRouteProbeDetectors.add(vp->getID(), vp)) {
00172 throw ProcessError("routeprobe '" + vp->getID() + "' could not be build;\n (declared twice?)");
00173 }
00174 addDetectorAndInterval(vp, &device, frequency, begin);
00175 }
00176
00177
00178
00179
00180 #ifdef HAVE_MESOSIM
00181 void
00182 MSDetectorControl::add(MEInductLoop *meil, OutputDevice& device, int splInterval) throw(ProcessError) {
00183
00184 if (! myMesoLoops.add(meil->getID(), meil)) {
00185 throw ProcessError("meso-induct loop '" + meil->getID() + "' could not be build;"
00186 + "\n (declared twice?)");
00187 }
00188 addDetectorAndInterval(meil, &device, splInterval);
00189 }
00190 #endif
00191
00192
00193 void
00194 MSDetectorControl::updateDetectors(SUMOTime step) throw() {
00195
00196
00197 const std::vector<MSE2Collector*> &e2s = myE2Detectors.buildAndGetStaticVector();
00198 for (E2Vect::const_iterator i=e2s.begin(); i!=e2s.end(); ++i) {
00199 (*i)->update(step);
00200 }
00201
00202 const std::vector<MSE3Collector*> &e3s = myE3Detectors.buildAndGetStaticVector();
00203 for (E3Vect::const_iterator i=e3s.begin(); i!=e3s.end(); ++i) {
00204 (*i)->update(step);
00205 }
00206
00207
00208
00209 for (std::vector<MSMeanData_Harmonoise*>::const_iterator i=myHarmonoiseDetectors.begin(); i!=myHarmonoiseDetectors.end(); ++i) {
00210 (*i)->update();
00211 }
00212 }
00213
00214 void
00215 MSDetectorControl::writeOutput(SUMOTime step, bool closing) throw(IOError) {
00216 for (Intervals::iterator i=myIntervals.begin(); i!=myIntervals.end(); ++i) {
00217 IntervalsKey interval = (*i).first;
00218 if (myLastCalls[interval] + interval.first <= step || (closing && myLastCalls[interval] < step)) {
00219 DetectorFileVec dfVec = (*i).second;
00220 SUMOTime startTime = myLastCalls[interval];
00221
00222 for (DetectorFileVec::iterator it = dfVec.begin(); it!=dfVec.end(); ++it) {
00223 MSDetectorFileOutput* det = it->first;
00224 det->writeXMLOutput(*(it->second), startTime, step);
00225 }
00226 myLastCalls[interval] = step;
00227 }
00228 }
00229 }
00230
00231
00232 void
00233 MSDetectorControl::addDetectorAndInterval(MSDetectorFileOutput* det,
00234 OutputDevice *device,
00235 SUMOTime interval,
00236 SUMOTime begin) throw() {
00237 if (begin == -1) {
00238 begin = string2time(OptionsCont::getOptions().getString("begin"));
00239 }
00240 IntervalsKey key = std::make_pair(interval, begin);
00241 Intervals::iterator it = myIntervals.find(key);
00242
00243 if (it == myIntervals.end()) {
00244 DetectorFileVec detAndFileVec;
00245 detAndFileVec.push_back(std::make_pair(det, device));
00246 myIntervals.insert(std::make_pair(key, detAndFileVec));
00247 myLastCalls[key] = begin;
00248 } else {
00249 DetectorFileVec& detAndFileVec = it->second;
00250 if (find_if(detAndFileVec.begin(), detAndFileVec.end(), bind2nd(detectorEquals(), det)) == detAndFileVec.end()) {
00251 detAndFileVec.push_back(std::make_pair(det, device));
00252 } else {
00253
00254 WRITE_WARNING("MSDetector2File::addDetectorAndInterval: detector already in container. Ignoring.");
00255 return;
00256 }
00257 }
00258 det->writeXMLDetectorProlog(*device);
00259 }
00260
00261
00262
00263
00264
00265
00266