MSTrafficLightLogic.cpp
Go to the documentation of this file.00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029
00030 #include <string>
00031 #include <iostream>
00032 #include <map>
00033 #include <microsim/MSLink.h>
00034 #include <microsim/MSLane.h>
00035 #include "MSTrafficLightLogic.h"
00036 #include <microsim/MSEventControl.h>
00037 #include "MSTLLogicControl.h"
00038 #include <microsim/MSJunctionLogic.h>
00039
00040 #ifdef CHECK_MEMORY_LEAKS
00041 #include <foreign/nvwa/debug_new.h>
00042 #endif // CHECK_MEMORY_LEAKS
00043
00044
00045
00046
00047
00048
00049
00050
00051 MSTrafficLightLogic::SwitchCommand::SwitchCommand(MSTLLogicControl &tlcontrol,
00052 MSTrafficLightLogic *tlLogic, SUMOTime nextSwitch) throw()
00053 : myTLControl(tlcontrol), myTLLogic(tlLogic),
00054 myAssumedNextSwitch(nextSwitch), myAmValid(true) {}
00055
00056
00057 MSTrafficLightLogic::SwitchCommand::~SwitchCommand() throw() {}
00058
00059
00060
00061 SUMOTime
00062 MSTrafficLightLogic::SwitchCommand::execute(SUMOTime) throw(ProcessError) {
00063
00064 if (!myAmValid) {
00065 return 0;
00066 }
00067
00068 bool isActive = myTLControl.isActive(myTLLogic);
00069 size_t step1 = myTLLogic->getCurrentPhaseIndex();
00070 SUMOTime next = myTLLogic->trySwitch(isActive);
00071 size_t step2 = myTLLogic->getCurrentPhaseIndex();
00072 if (step1!=step2) {
00073 if (isActive) {
00074
00075 const MSTLLogicControl::TLSLogicVariants &vars = myTLControl.get(myTLLogic->getID());
00076
00077 myTLLogic->setLinkPriorities();
00078
00079 vars.executeOnSwitchActions();
00080 }
00081 }
00082 myAssumedNextSwitch += next;
00083 return next;
00084 }
00085
00086
00087 void
00088 MSTrafficLightLogic::SwitchCommand::deschedule(MSTrafficLightLogic *tlLogic) throw() {
00089 if (tlLogic==myTLLogic) {
00090 myAmValid = false;
00091 myAssumedNextSwitch = -1;
00092 }
00093 }
00094
00095
00096
00097
00098
00099 MSTrafficLightLogic::MSTrafficLightLogic(MSTLLogicControl &tlcontrol,
00100 const std::string &id, const std::string &subid,
00101 SUMOTime delay) throw()
00102 : myID(id), mySubID(subid), myCurrentDurationIncrement(-1),
00103 myDefaultCycleTime(0) {
00104 mySwitchCommand = new SwitchCommand(tlcontrol, this, delay);
00105 MSNet::getInstance()->getBeginOfTimestepEvents().addEvent(
00106 mySwitchCommand, delay, MSEventControl::NO_CHANGE);
00107 }
00108
00109
00110 void
00111 MSTrafficLightLogic::init(NLDetectorBuilder &) throw(ProcessError) {
00112 }
00113
00114
00115 MSTrafficLightLogic::~MSTrafficLightLogic() throw() {
00116 mySwitchCommand->deschedule(this);
00117 }
00118
00119
00120
00121 void
00122 MSTrafficLightLogic::addLink(MSLink *link, MSLane *lane, unsigned int pos) throw() {
00123
00124 myLinks.reserve(pos+1);
00125 while (myLinks.size()<=pos) {
00126 myLinks.push_back(LinkVector());
00127 }
00128 myLinks[pos].push_back(link);
00129
00130 myLanes.reserve(pos+1);
00131 while (myLanes.size()<=pos) {
00132 myLanes.push_back(LaneVector());
00133 }
00134 myLanes[pos].push_back(lane);
00135 }
00136
00137
00138 void
00139 MSTrafficLightLogic::adaptLinkInformationFrom(const MSTrafficLightLogic &logic) throw() {
00140 myLinks = logic.myLinks;
00141 myLanes = logic.myLanes;
00142 }
00143
00144
00145 std::map<MSLink*, std::pair<MSLink::LinkState, bool> >
00146 MSTrafficLightLogic::collectLinkStates() const throw() {
00147 std::map<MSLink*, std::pair<MSLink::LinkState, bool> > ret;
00148 for (LinkVectorVector::const_iterator i1=myLinks.begin(); i1!=myLinks.end(); ++i1) {
00149 const LinkVector &l = (*i1);
00150 for (LinkVector::const_iterator i2=l.begin(); i2!=l.end(); ++i2) {
00151 ret[*i2] = std::make_pair((*i2)->getState(), (*i2)->havePriority());
00152 }
00153 }
00154 return ret;
00155 }
00156
00157
00158 void
00159 MSTrafficLightLogic::resetLinkStates(const std::map<MSLink*, std::pair<MSLink::LinkState, bool> > &vals) const throw() {
00160 for (LinkVectorVector::const_iterator i1=myLinks.begin(); i1!=myLinks.end(); ++i1) {
00161 const LinkVector &l = (*i1);
00162 for (LinkVector::const_iterator i2=l.begin(); i2!=l.end(); ++i2) {
00163 assert(vals.find(*i2)!=vals.end());
00164 const std::pair<MSLink::LinkState, bool> &lvals = vals.find(*i2)->second;
00165 (*i2)->setTLState(lvals.first);
00166 (*i2)->setPriority(lvals.second);
00167 }
00168 }
00169 }
00170
00171
00172
00173 int
00174 MSTrafficLightLogic::getLinkIndex(const MSLink * const link) const throw() {
00175 int index = 0;
00176 for (LinkVectorVector::const_iterator i1=myLinks.begin(); i1!=myLinks.end(); ++i1, ++index) {
00177 const LinkVector &l = (*i1);
00178 for (LinkVector::const_iterator i2=l.begin(); i2!=l.end(); ++i2) {
00179 if ((*i2)==link) {
00180 return index;
00181 }
00182 }
00183 }
00184 return -1;
00185 }
00186
00187
00188
00189
00190 SUMOTime
00191 MSTrafficLightLogic::getNextSwitchTime() const throw() {
00192 return mySwitchCommand!=0 ? mySwitchCommand->getNextSwitchTime() : -1;
00193 }
00194
00195
00196
00197 void
00198 MSTrafficLightLogic::addOverridingDuration(SUMOTime duration) throw() {
00199 myOverridingTimes.push_back(duration);
00200 }
00201
00202
00203 void
00204 MSTrafficLightLogic::setCurrentDurationIncrement(SUMOTime delay) throw() {
00205 myCurrentDurationIncrement = delay;
00206 }
00207
00208
00209
00210
00211
00212 void
00213 MSTrafficLightLogic::setParameter(const std::map<std::string, std::string> ¶ms) throw() {
00214 myParameter = params;
00215 }
00216
00217
00218 std::string
00219 MSTrafficLightLogic::getParameterValue(const std::string &key) const throw() {
00220 if (myParameter.find(key)==myParameter.end()) {
00221 return "";
00222 }
00223 return myParameter.find(key)->second;
00224 }
00225
00226
00227
00228