SUMO - Simulation of Urban MObility

MSDetectorControl.cpp

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