SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00009 // A SUMO-compliant built logic for a traffic light 00010 /****************************************************************************/ 00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00013 /****************************************************************************/ 00014 // 00015 // This file is part of SUMO. 00016 // SUMO is free software: you can redistribute it and/or modify 00017 // it under the terms of the GNU General Public License as published by 00018 // the Free Software Foundation, either version 3 of the License, or 00019 // (at your option) any later version. 00020 // 00021 /****************************************************************************/ 00022 00023 00024 // =========================================================================== 00025 // included modules 00026 // =========================================================================== 00027 #ifdef _MSC_VER 00028 #include <windows_config.h> 00029 #else 00030 #include <config.h> 00031 #endif 00032 00033 #include <vector> 00034 #include <bitset> 00035 #include <utility> 00036 #include <string> 00037 #include <sstream> 00038 #include <cassert> 00039 #include "NBEdge.h" 00040 #include "NBEdgeCont.h" 00041 #include "NBTrafficLightLogic.h" 00042 #include <utils/options/OptionsCont.h> 00043 #include <utils/options/Option.h> 00044 #include <utils/common/ToString.h> 00045 #include <utils/common/StringTokenizer.h> 00046 #include <utils/iodevices/OutputDevice.h> 00047 00048 #ifdef CHECK_MEMORY_LEAKS 00049 #include <foreign/nvwa/debug_new.h> 00050 #endif // CHECK_MEMORY_LEAKS 00051 00052 00053 // =========================================================================== 00054 // static members 00055 // =========================================================================== 00056 const char NBTrafficLightLogic::allowedStatesInitializer[] = {LINKSTATE_TL_GREEN_MAJOR, 00057 LINKSTATE_TL_GREEN_MINOR, 00058 LINKSTATE_TL_RED, 00059 LINKSTATE_TL_YELLOW_MAJOR, 00060 LINKSTATE_TL_YELLOW_MINOR, 00061 LINKSTATE_TL_OFF_BLINKING, 00062 LINKSTATE_TL_OFF_NOSIGNAL 00063 }; 00064 00065 const std::string NBTrafficLightLogic::ALLOWED_STATES(NBTrafficLightLogic::allowedStatesInitializer); 00066 00067 // =========================================================================== 00068 // member method definitions 00069 // =========================================================================== 00070 NBTrafficLightLogic::NBTrafficLightLogic(const std::string& id, 00071 const std::string& subid, unsigned int noLinks) : 00072 Named(id), myNumLinks(noLinks), mySubID(subid), 00073 myOffset(0) {} 00074 00075 NBTrafficLightLogic::NBTrafficLightLogic(const NBTrafficLightLogic* logic) : 00076 Named(logic->getID()), 00077 myNumLinks(logic->myNumLinks), 00078 mySubID(logic->getProgramID()), 00079 myOffset(logic->getOffset()), 00080 myPhases(logic->myPhases.begin(), logic->myPhases.end()) {} 00081 00082 00083 NBTrafficLightLogic::~NBTrafficLightLogic() {} 00084 00085 00086 void 00087 NBTrafficLightLogic::addStep(SUMOTime duration, const std::string& state, int index) { 00088 // check state size 00089 if (myNumLinks == 0) { 00090 // initialize 00091 myNumLinks = (unsigned int)state.size(); 00092 } else if (state.size() != myNumLinks) { 00093 throw ProcessError("When adding phase: state length of " + toString(state.size()) + 00094 " does not match declared number of links " + toString(myNumLinks)); 00095 } 00096 // check state contents 00097 const size_t illegal = state.find_first_not_of(ALLOWED_STATES); 00098 if (std::string::npos != illegal) { 00099 throw ProcessError("When adding phase: illegal character '" + toString(state[illegal]) + "' in state"); 00100 } 00101 // interpret index 00102 if (index < 0 || index >= (int)myPhases.size()) { 00103 // insert at the end 00104 index = (int)myPhases.size(); 00105 } 00106 myPhases.insert(myPhases.begin() + index, PhaseDefinition(duration, state)); 00107 } 00108 00109 00110 void 00111 NBTrafficLightLogic::deletePhase(unsigned int index) { 00112 if (index >= myPhases.size()) { 00113 throw InvalidArgument("Index " + toString(index) + " out of range for logic with " 00114 + toString(myPhases.size()) + " phases."); 00115 } 00116 myPhases.erase(myPhases.begin() + index); 00117 } 00118 00119 00120 void 00121 NBTrafficLightLogic::resetPhases() { 00122 myNumLinks = 0; 00123 myPhases.clear(); 00124 } 00125 00126 00127 SUMOTime 00128 NBTrafficLightLogic::getDuration() const { 00129 SUMOTime duration = 0; 00130 for (PhaseDefinitionVector::const_iterator i = myPhases.begin(); i != myPhases.end(); ++i) { 00131 duration += (*i).duration; 00132 } 00133 return duration; 00134 } 00135 00136 00137 void 00138 NBTrafficLightLogic::closeBuilding() { 00139 for (unsigned int i = 0; i < myPhases.size() - 1;) { 00140 if (myPhases[i].state != myPhases[i + 1].state) { 00141 ++i; 00142 continue; 00143 } 00144 myPhases[i].duration += myPhases[i + 1].duration; 00145 myPhases.erase(myPhases.begin() + i + 1); 00146 } 00147 } 00148 00149 00150 void 00151 NBTrafficLightLogic::setPhaseState(unsigned int phaseIndex, int tlIndex, LinkState linkState) { 00152 assert(phaseIndex < myPhases.size()); 00153 std::string& phaseState = myPhases[phaseIndex].state; 00154 assert(tlIndex < phaseState.size()); 00155 phaseState[tlIndex] = (char)linkState; 00156 } 00157 00158 00159 void 00160 NBTrafficLightLogic::setPhaseDuration(unsigned int phaseIndex, SUMOTime duration) { 00161 assert(phaseIndex < myPhases.size()); 00162 myPhases[phaseIndex].duration = duration; 00163 } 00164 00165 00166 /****************************************************************************/ 00167