NBTrafficLightLogicCont.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // A container for traffic light definitions and built programs
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 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 #include <map>
00030 #include <string>
00031 #include <algorithm>
00032 #include <utils/common/MsgHandler.h>
00033 #include <utils/common/ToString.h>
00034 #include <utils/iodevices/OutputDevice.h>
00035 #include <utils/options/OptionsCont.h>
00036 #include "NBTrafficLightLogic.h"
00037 #include "NBTrafficLightLogicCont.h"
00038 
00039 #ifdef CHECK_MEMORY_LEAKS
00040 #include <foreign/nvwa/debug_new.h>
00041 #endif // CHECK_MEMORY_LEAKS
00042 
00043 
00044 // ===========================================================================
00045 // method definitions
00046 // ===========================================================================
00047 NBTrafficLightLogicCont::NBTrafficLightLogicCont() throw() {}
00048 
00049 
00050 NBTrafficLightLogicCont::~NBTrafficLightLogicCont() throw() {
00051     clear();
00052 }
00053 
00054 
00055 void
00056 NBTrafficLightLogicCont::applyOptions(OptionsCont &oc) throw() {
00057     // check whether any offsets shall be manipulated by setting
00058     //  them to half of the duration
00059     if (oc.isSet("tl-logics.half-offset")) {
00060         myHalfOffsetTLS = oc.getStringVector("tl-logics.half-offset");
00061     }
00062     // check whether any offsets shall be manipulated by setting
00063     //  them to a quarter of the duration
00064     if (oc.isSet("tl-logics.quarter-offset")) {
00065         myQuarterOffsetTLS = oc.getStringVector("tl-logics.quarter-offset");
00066     }
00067 }
00068 
00069 
00070 bool
00071 NBTrafficLightLogicCont::insert(NBTrafficLightDefinition *logic) throw() {
00072     DefinitionContType::iterator i=myDefinitions.find(logic->getID());
00073     if (i!=myDefinitions.end()) {
00074         return false;
00075     }
00076     myDefinitions[logic->getID()] = logic;
00077     return true;
00078 }
00079 
00080 
00081 bool
00082 NBTrafficLightLogicCont::remove(const std::string &id) throw() {
00083     DefinitionContType::iterator i=myDefinitions.find(id);
00084     if (i==myDefinitions.end()) {
00085         return false;
00086     }
00087     delete(*i).second;
00088     myDefinitions.erase(i);
00089     return true;
00090 }
00091 
00092 
00093 void
00094 NBTrafficLightLogicCont::computeLogics(NBEdgeCont &ec, OptionsCont &oc) throw() {
00095     unsigned int no = 0;
00096     for (DefinitionContType::iterator i=myDefinitions.begin(); i!=myDefinitions.end(); i++) {
00097         std::string id = (*i).first;
00098         if (myComputed.find(id)!=myComputed.end()) {
00099             WRITE_WARNING("Traffic light '" + id + "' was already built.");
00100             continue;
00101         }
00102         // build program
00103         NBTrafficLightDefinition *def = (*i).second;
00104         NBTrafficLightLogic *built = def->compute(ec, oc);
00105         if (built==0) {
00106             WRITE_WARNING("Could not build traffic lights '" + id + "'");
00107             continue;
00108         }
00109         // compute offset
00110         SUMOTime T = built->getDuration();
00111         if (find(myHalfOffsetTLS.begin(), myHalfOffsetTLS.end(), id)!=myHalfOffsetTLS.end()) {
00112             built->setOffset((SUMOTime)(T/2.));
00113         }
00114         if (find(myQuarterOffsetTLS.begin(), myQuarterOffsetTLS.end(), id)!=myQuarterOffsetTLS.end()) {
00115             built->setOffset((SUMOTime)(T/4.));
00116         }
00117         // and insert the result after computation
00118         myComputed[(*i).first] = built;
00119         no++;
00120     }
00121     WRITE_MESSAGE(toString<int>(no) + " traffic light(s) computed.");
00122 }
00123 
00124 
00125 void
00126 NBTrafficLightLogicCont::writeXML(OutputDevice &into) throw(IOError) {
00127     for (ComputedContType::iterator i=myComputed.begin(); i!=myComputed.end(); i++) {
00128         (*i).second->writeXML(into);
00129     }
00130     into << "\n";
00131 }
00132 
00133 
00134 void
00135 NBTrafficLightLogicCont::clear() throw() {
00136     for (ComputedContType::iterator i=myComputed.begin(); i!=myComputed.end(); ++i) {
00137         delete(*i).second;
00138     }
00139     myComputed.clear();
00140     for (DefinitionContType::iterator i=myDefinitions.begin(); i!=myDefinitions.end(); ++i) {
00141         delete(*i).second;
00142     }
00143     myDefinitions.clear();
00144 }
00145 
00146 
00147 void
00148 NBTrafficLightLogicCont::remapRemoved(NBEdge *removed, const EdgeVector &incoming,
00149                                       const EdgeVector &outgoing) throw() {
00150     for (DefinitionContType::iterator i=myDefinitions.begin(); i!=myDefinitions.end(); i++) {
00151         (*i).second->remapRemoved(removed, incoming, outgoing);
00152     }
00153 }
00154 
00155 
00156 void
00157 NBTrafficLightLogicCont::replaceRemoved(NBEdge *removed, int removedLane,
00158                                         NBEdge *by, int byLane) throw() {
00159     for (DefinitionContType::iterator i=myDefinitions.begin(); i!=myDefinitions.end(); i++) {
00160         (*i).second->replaceRemoved(removed, removedLane, by, byLane);
00161     }
00162 }
00163 
00164 
00165 NBTrafficLightDefinition *
00166 NBTrafficLightLogicCont::getDefinition(const std::string &id) const throw() {
00167     DefinitionContType::const_iterator i=myDefinitions.find(id);
00168     if (i!=myDefinitions.end()) {
00169         return (*i).second;
00170     }
00171     return 0;
00172 }
00173 
00174 
00175 void
00176 NBTrafficLightLogicCont::setTLControllingInformation(const NBEdgeCont &ec) throw() {
00177     DefinitionContType::iterator i;
00178     // set the information about all participants, first
00179     for (i=myDefinitions.begin(); i!=myDefinitions.end(); i++) {
00180         (*i).second->setParticipantsInformation();
00181     }
00182     // insert the information about the tl-controlling
00183     for (i=myDefinitions.begin(); i!=myDefinitions.end(); i++) {
00184         (*i).second->setTLControllingInformation(ec);
00185     }
00186 }
00187 
00188 
00189 
00190 /****************************************************************************/
00191 

Generated on Wed May 5 00:06:33 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6