SUMO - Simulation of Urban MObility

NBTrafficLightLogic.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // A SUMO-compliant built logic for a traffic light
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 <vector>
00031 #include <bitset>
00032 #include <utility>
00033 #include <string>
00034 #include <sstream>
00035 #include <cassert>
00036 #include "NBEdge.h"
00037 #include "NBEdgeCont.h"
00038 #include "NBTrafficLightLogic.h"
00039 #include <utils/options/OptionsCont.h>
00040 #include <utils/options/Option.h>
00041 #include <utils/common/ToString.h>
00042 #include <utils/common/StringTokenizer.h>
00043 #include <utils/iodevices/OutputDevice.h>
00044 
00045 #ifdef CHECK_MEMORY_LEAKS
00046 #include <foreign/nvwa/debug_new.h>
00047 #endif // CHECK_MEMORY_LEAKS
00048 
00049 
00050 // ===========================================================================
00051 // static members
00052 // ===========================================================================
00053 const char NBTrafficLightLogic::allowedStatesInitializer[] = {LINKSTATE_TL_GREEN_MAJOR,
00054         LINKSTATE_TL_GREEN_MINOR,
00055         LINKSTATE_TL_RED,
00056         LINKSTATE_TL_YELLOW_MAJOR,
00057         LINKSTATE_TL_YELLOW_MINOR,
00058         LINKSTATE_TL_OFF_BLINKING,
00059         LINKSTATE_TL_OFF_NOSIGNAL
00060                                                              };
00061 
00062 const std::string NBTrafficLightLogic::ALLOWED_STATES(NBTrafficLightLogic::allowedStatesInitializer);
00063 
00064 // ===========================================================================
00065 // member method definitions
00066 // ===========================================================================
00067 NBTrafficLightLogic::NBTrafficLightLogic(const std::string& id,
00068         const std::string& subid, unsigned int noLinks) throw() :
00069     Named(id), myNumLinks(noLinks), mySubID(subid),
00070     myOffset(0) {}
00071 
00072 NBTrafficLightLogic::NBTrafficLightLogic(const NBTrafficLightLogic* logic) :
00073     Named(logic->getID()),
00074     myNumLinks(logic->myNumLinks),
00075     mySubID(logic->getProgramID()),
00076     myOffset(logic->getOffset()),
00077     myPhases(logic->myPhases.begin(), logic->myPhases.end()) {}
00078 
00079 
00080 NBTrafficLightLogic::~NBTrafficLightLogic() throw() {}
00081 
00082 
00083 void
00084 NBTrafficLightLogic::addStep(SUMOTime duration, const std::string& state, int index) {
00085     // check state size
00086     if (myNumLinks == 0) {
00087         // initialize
00088         myNumLinks = (unsigned int)state.size();
00089     } else if (state.size() != myNumLinks) {
00090         throw ProcessError("When adding phase: state length of " + toString(state.size()) +
00091                            " does not match declared number of links " + toString(myNumLinks));
00092     }
00093     // check state contents
00094     const size_t illegal = state.find_first_not_of(ALLOWED_STATES);
00095     if (std::string::npos != illegal) {
00096         throw ProcessError("When adding phase: illegal character '" + toString(state[illegal]) + "' in state");
00097     }
00098     // interpret index
00099     if (index < 0 || index >= (int)myPhases.size()) {
00100         // insert at the end
00101         index = (int)myPhases.size();
00102     }
00103     myPhases.insert(myPhases.begin() + index, PhaseDefinition(duration, state));
00104 }
00105 
00106 
00107 void
00108 NBTrafficLightLogic::deletePhase(unsigned int index) {
00109     if (index >= myPhases.size()) {
00110         throw InvalidArgument("Index " + toString(index) + " out of range for logic with "
00111                               + toString(myPhases.size()) + " phases.");
00112     }
00113     myPhases.erase(myPhases.begin() + index);
00114 }
00115 
00116 
00117 void
00118 NBTrafficLightLogic::resetPhases() {
00119     myNumLinks = 0;
00120     myPhases.clear();
00121 }
00122 
00123 
00124 SUMOTime
00125 NBTrafficLightLogic::getDuration() const throw() {
00126     SUMOTime duration = 0;
00127     for (PhaseDefinitionVector::const_iterator i=myPhases.begin(); i!=myPhases.end(); ++i) {
00128         duration += (*i).duration;
00129     }
00130     return duration;
00131 }
00132 
00133 
00134 void
00135 NBTrafficLightLogic::closeBuilding() throw() {
00136     for (unsigned int i=0; i<myPhases.size()-1;) {
00137         if (myPhases[i].state!=myPhases[i+1].state) {
00138             ++i;
00139             continue;
00140         }
00141         myPhases[i].duration += myPhases[i+1].duration;
00142         myPhases.erase(myPhases.begin()+i+1);
00143     }
00144 }
00145 
00146 
00147 void
00148 NBTrafficLightLogic::setPhaseState(unsigned int phaseIndex, int tlIndex, LinkState linkState) {
00149     assert(phaseIndex < myPhases.size());
00150     std::string& phaseState = myPhases[phaseIndex].state;
00151     assert(tlIndex < phaseState.size());
00152     phaseState[tlIndex] = (char)linkState;
00153 }
00154 
00155 
00156 void
00157 NBTrafficLightLogic::setPhaseDuration(unsigned int phaseIndex, SUMOTime duration) {
00158     assert(phaseIndex < myPhases.size());
00159     myPhases[phaseIndex].duration = duration;
00160 }
00161 
00162 
00163 /****************************************************************************/
00164 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines