SUMO - Simulation of Urban MObility
MSDetectorControl.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00012 // Detectors container; responsible for string and output generation
00013 /****************************************************************************/
00014 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00015 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00016 /****************************************************************************/
00017 //
00018 //   This file is part of SUMO.
00019 //   SUMO is free software: you can redistribute it and/or modify
00020 //   it under the terms of the GNU General Public License as published by
00021 //   the Free Software Foundation, either version 3 of the License, or
00022 //   (at your option) any later version.
00023 //
00024 /****************************************************************************/
00025 
00026 
00027 // ===========================================================================
00028 // included modules
00029 // ===========================================================================
00030 #ifdef _MSC_VER
00031 #include <windows_config.h>
00032 #else
00033 #include <config.h>
00034 #endif
00035 
00036 #include <iostream>
00037 #include "MSDetectorControl.h"
00038 #include "MSMeanData_Net.h"
00039 #include <utils/options/OptionsCont.h>
00040 #include <utils/options/Option.h>
00041 #include <utils/common/MsgHandler.h>
00042 
00043 #ifdef HAVE_MESOSIM
00044 #include <mesosim/MEInductLoop.h>
00045 #endif
00046 
00047 #ifdef CHECK_MEMORY_LEAKS
00048 #include <foreign/nvwa/debug_new.h>
00049 #endif // CHECK_MEMORY_LEAKS
00050 
00051 
00052 // ===========================================================================
00053 // member method definitions
00054 // ===========================================================================
00055 MSDetectorControl::MSDetectorControl() {
00056 }
00057 
00058 
00059 MSDetectorControl::~MSDetectorControl() {
00060     for (std::map<SumoXMLTag, NamedObjectCont<MSDetectorFileOutput*> >::iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
00061         (*i).second.clear();
00062     }
00063     for (std::vector<MSMeanData*>::const_iterator i = myMeanData.begin(); i != myMeanData.end(); ++i) {
00064         delete *i;
00065     }
00066 }
00067 
00068 
00069 void
00070 MSDetectorControl::close(SUMOTime step) {
00071     // flush the last values
00072     writeOutput(step, true);
00073     // [...] files are closed on another place [...]
00074     myIntervals.clear();
00075 }
00076 
00077 
00078 void
00079 MSDetectorControl::add(SumoXMLTag type, MSDetectorFileOutput* d, OutputDevice& device, int splInterval, SUMOTime begin) {
00080     if (myDetectors.find(type) == myDetectors.end()) {
00081         myDetectors[type] = NamedObjectCont<MSDetectorFileOutput*>();
00082     }
00083     NamedObjectCont<MSDetectorFileOutput*> &m = myDetectors.find(type)->second;
00084     // insert object into dictionary
00085     if (! m.add(d->getID(), d)) {
00086         throw ProcessError(toString(type) + " detector '" + d->getID() + "' could not be build (declared twice?).");
00087     }
00088     addDetectorAndInterval(d, &device, splInterval, begin);
00089 }
00090 
00091 
00092 
00093 void
00094 MSDetectorControl::add(SumoXMLTag type, MSDetectorFileOutput* d) {
00095     if (myDetectors.find(type) == myDetectors.end()) {
00096         myDetectors[type] = NamedObjectCont<MSDetectorFileOutput*>();
00097     }
00098     NamedObjectCont<MSDetectorFileOutput*> &m = myDetectors.find(type)->second;
00099     // insert object into dictionary
00100     if (! m.add(d->getID(), d)) {
00101         throw ProcessError(toString(type) + " detector '" + d->getID() + "' could not be build (declared twice?).");
00102     }
00103 }
00104 
00105 
00106 
00107 void
00108 MSDetectorControl::add(MSMeanData* mn, OutputDevice& device,
00109                        SUMOTime frequency, SUMOTime begin) {
00110     myMeanData.push_back(mn);
00111     addDetectorAndInterval(mn, &device, frequency, begin);
00112     if (begin == string2time(OptionsCont::getOptions().getString("begin"))) {
00113         mn->init();
00114     }
00115 }
00116 
00117 
00118 const NamedObjectCont<MSDetectorFileOutput*> &
00119 MSDetectorControl::getTypedDetectors(SumoXMLTag type) const {
00120     if (myDetectors.find(type) == myDetectors.end()) {
00121         return myEmptyContainer;//myDetectors[type] = NamedObjectCont<MSDetectorFileOutput*>();
00122     }
00123     return myDetectors.find(type)->second;
00124 }
00125 
00126 
00127 void
00128 MSDetectorControl::updateDetectors(const SUMOTime step) {
00129     for (std::map<SumoXMLTag, NamedObjectCont<MSDetectorFileOutput*> >::const_iterator i = myDetectors.begin(); i != myDetectors.end(); ++i) {
00130         const std::map<std::string, MSDetectorFileOutput*> &dets = getTypedDetectors((*i).first).getMyMap();
00131         for (std::map<std::string, MSDetectorFileOutput*>::const_iterator j = dets.begin(); j != dets.end(); ++j) {
00132             (*j).second->detectorUpdate(step);
00133         }
00134     }
00135     for (std::vector<MSMeanData*>::const_iterator i = myMeanData.begin(); i != myMeanData.end(); ++i) {
00136         (*i)->detectorUpdate(step);
00137     }
00138 }
00139 
00140 
00141 void
00142 MSDetectorControl::writeOutput(SUMOTime step, bool closing) {
00143     for (Intervals::iterator i = myIntervals.begin(); i != myIntervals.end(); ++i) {
00144         IntervalsKey interval = (*i).first;
00145         if (myLastCalls[interval] + interval.first <= step || (closing && myLastCalls[interval] < step)) {
00146             DetectorFileVec dfVec = (*i).second;
00147             SUMOTime startTime = myLastCalls[interval];
00148             // check whether at the end the output was already generated
00149             for (DetectorFileVec::iterator it = dfVec.begin(); it != dfVec.end(); ++it) {
00150                 MSDetectorFileOutput* det = it->first;
00151                 det->writeXMLOutput(*(it->second), startTime, step);
00152             }
00153             myLastCalls[interval] = step;
00154         }
00155     }
00156 }
00157 
00158 
00159 void
00160 MSDetectorControl::addDetectorAndInterval(MSDetectorFileOutput* det,
00161         OutputDevice* device,
00162         SUMOTime interval,
00163         SUMOTime begin) {
00164     if (begin == -1) {
00165         begin = string2time(OptionsCont::getOptions().getString("begin"));
00166     }
00167     IntervalsKey key = std::make_pair(interval, begin);
00168     Intervals::iterator it = myIntervals.find(key);
00169     // Add command for given key only once to MSEventControl...
00170     if (it == myIntervals.end()) {
00171         DetectorFileVec detAndFileVec;
00172         detAndFileVec.push_back(std::make_pair(det, device));
00173         myIntervals.insert(std::make_pair(key, detAndFileVec));
00174         myLastCalls[key] = begin;
00175     } else {
00176         DetectorFileVec& detAndFileVec = it->second;
00177         if (find_if(detAndFileVec.begin(), detAndFileVec.end(), bind2nd(detectorEquals(), det)) == detAndFileVec.end()) {
00178             detAndFileVec.push_back(std::make_pair(det, device));
00179         } else {
00180             // detector already in container. Don't add several times
00181             WRITE_WARNING("MSDetectorControl::addDetectorAndInterval: detector already in container. Ignoring.");
00182             return;
00183         }
00184     }
00185     det->writeXMLDetectorProlog(*device);
00186 }
00187 
00188 
00189 
00190 /****************************************************************************/
00191 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines