SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00007 // A complete traffic light logic loaded from a sumo-net. (opted to reimplement 00008 // since NBLoadedTLDef is quite vissim specific) 00009 /****************************************************************************/ 00010 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00011 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00012 /****************************************************************************/ 00013 // 00014 // This file is part of SUMO. 00015 // SUMO is free software: you can redistribute it and/or modify 00016 // it under the terms of the GNU General Public License as published by 00017 // the Free Software Foundation, either version 3 of the License, or 00018 // (at your option) any later version. 00019 // 00020 /****************************************************************************/ 00021 00022 // =========================================================================== 00023 // included modules 00024 // =========================================================================== 00025 #ifdef _MSC_VER 00026 #include <windows_config.h> 00027 #else 00028 #include <config.h> 00029 #endif 00030 00031 #include <vector> 00032 #include <set> 00033 #include <cassert> 00034 #include <iterator> 00035 #include <utils/common/MsgHandler.h> 00036 #include <utils/common/ToString.h> 00037 #include <utils/options/OptionsCont.h> 00038 #include "NBTrafficLightLogic.h" 00039 #include "NBTrafficLightDefinition.h" 00040 #include "NBLoadedSUMOTLDef.h" 00041 #include "NBNode.h" 00042 00043 #ifdef CHECK_MEMORY_LEAKS 00044 #include <foreign/nvwa/debug_new.h> 00045 #endif // CHECK_MEMORY_LEAKS 00046 00047 // =========================================================================== 00048 // method definitions 00049 // =========================================================================== 00050 00051 NBLoadedSUMOTLDef::NBLoadedSUMOTLDef(const std::string& id, const std::string& programID, SUMOTime offset) : 00052 NBTrafficLightDefinition(id, programID), 00053 myTLLogic(0) { 00054 myTLLogic = new NBTrafficLightLogic(id, programID, 0); 00055 myTLLogic->setOffset(offset); 00056 } 00057 00058 00059 NBLoadedSUMOTLDef::NBLoadedSUMOTLDef(NBTrafficLightDefinition* def, NBTrafficLightLogic* logic) : 00060 NBTrafficLightDefinition(def->getID(), def->getProgramID()), 00061 myTLLogic(new NBTrafficLightLogic(logic)), 00062 myOriginalNodes(def->getNodes().begin(), def->getNodes().end()) { 00063 myControlledLinks = def->getControlledLinks(); 00064 } 00065 00066 00067 NBLoadedSUMOTLDef::~NBLoadedSUMOTLDef() { 00068 delete myTLLogic; 00069 } 00070 00071 00072 NBTrafficLightLogic* 00073 NBLoadedSUMOTLDef::myCompute(const NBEdgeCont& ec, unsigned int brakingTime) { 00074 // @todo what to do with those parameters? 00075 UNUSED_PARAMETER(ec); 00076 UNUSED_PARAMETER(brakingTime); 00077 myTLLogic->closeBuilding(); 00078 return new NBTrafficLightLogic(myTLLogic); 00079 } 00080 00081 00082 void 00083 NBLoadedSUMOTLDef::addConnection(NBEdge* from, NBEdge* to, int fromLane, int toLane, int linkIndex) { 00084 assert(myTLLogic->getNumLinks() > 0); // logic should be loaded by now 00085 if (linkIndex >= (int)myTLLogic->getNumLinks()) { 00086 WRITE_ERROR("Invalid linkIndex " + toString(linkIndex) + " for traffic light '" + getID() + 00087 "' with " + toString(myTLLogic->getNumLinks()) + " links."); 00088 return; 00089 } 00090 NBConnection conn(from, fromLane, to, toLane, linkIndex); 00091 // avoid duplicates 00092 remove_if(myControlledLinks.begin(), myControlledLinks.end(), connection_equal(conn)); 00093 myControlledLinks.push_back(conn); 00094 addNode(from->getToNode()); 00095 addNode(to->getFromNode()); 00096 myOriginalNodes.insert(from->getToNode()); 00097 myOriginalNodes.insert(to->getFromNode()); 00098 // added connections are definitely controlled. make sure none are removed because they lie within the tl 00099 myControlledInnerEdges.insert(from->getID()); 00100 // set this information now so that it can be used while loading diffs 00101 from->setControllingTLInformation(conn, getID()); 00102 } 00103 00104 00105 void 00106 NBLoadedSUMOTLDef::setTLControllingInformation(const NBEdgeCont&) const { 00107 setTLControllingInformation(); 00108 } 00109 00110 00111 void 00112 NBLoadedSUMOTLDef::setTLControllingInformation() const { 00113 // if nodes have been removed our links may have been invalidated as well 00114 // since no logic will be built anyway there is no reason to inform any edges 00115 if (amInvalid()) { 00116 return; 00117 } 00118 // set the information about the link's positions within the tl into the 00119 // edges the links are starting at, respectively 00120 for (NBConnectionVector::const_iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) { 00121 const NBConnection& c = *it; 00122 assert(c.getTLIndex() < myTLLogic->getNumLinks()); 00123 NBEdge* edge = c.getFrom(); 00124 edge->setControllingTLInformation(c, getID()); 00125 } 00126 } 00127 00128 00129 void 00130 NBLoadedSUMOTLDef::remapRemoved(NBEdge*, const EdgeVector&, const EdgeVector&) {} 00131 00132 00133 void 00134 NBLoadedSUMOTLDef::replaceRemoved(NBEdge*, int, NBEdge*, int) {} 00135 00136 00137 void 00138 NBLoadedSUMOTLDef::addPhase(SUMOTime duration, const std::string& state) { 00139 myTLLogic->addStep(duration, state); 00140 } 00141 00142 00143 bool 00144 NBLoadedSUMOTLDef::amInvalid() const { 00145 if (myControlledLinks.size() == 0) { 00146 return true; 00147 } 00148 // make sure that myControlledNodes are the original nodes 00149 if (myControlledNodes.size() != myOriginalNodes.size()) { 00150 return true; 00151 } 00152 for (std::vector<NBNode*>::const_iterator i = myControlledNodes.begin(); i != myControlledNodes.end(); i++) { 00153 if (myOriginalNodes.count(*i) != 1) { 00154 return true; 00155 } 00156 } 00157 return false; 00158 } 00159 00160 00161 void 00162 NBLoadedSUMOTLDef::removeConnection(const NBConnection& conn, bool reconstruct) { 00163 NBConnectionVector::iterator it = find(myControlledLinks.begin(), myControlledLinks.end(), conn); 00164 if (it == myControlledLinks.end()) { 00165 throw ProcessError("Attempt to remove nonexistant connection"); 00166 } 00167 const int removed = conn.getTLIndex(); 00168 // remove the connection 00169 myControlledLinks.erase(it); 00170 if (reconstruct) { 00171 // updating the edge is only needed for immediate use in NETEDIT. 00172 // It may conflict with loading diffs 00173 conn.getFrom()->setControllingTLInformation(conn, ""); 00174 // shift link numbers down so there is no gap 00175 for (NBConnectionVector::iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) { 00176 NBConnection& c = *it; 00177 if (c.getTLIndex() > removed) { 00178 c.setTLIndex(c.getTLIndex() - 1); 00179 } 00180 } 00181 // update controlling information with new link numbers 00182 setTLControllingInformation(); 00183 // rebuild the logic 00184 const std::vector<NBTrafficLightLogic::PhaseDefinition> phases = myTLLogic->getPhases(); 00185 NBTrafficLightLogic* newLogic = new NBTrafficLightLogic(getID(), getProgramID(), 0); 00186 newLogic->setOffset(myTLLogic->getOffset()); 00187 for (std::vector<NBTrafficLightLogic::PhaseDefinition>::const_iterator it = phases.begin(); it != phases.end(); it++) { 00188 std::string newState = it->state; 00189 newState.erase(newState.begin() + removed); 00190 newLogic->addStep(it->duration, newState); 00191 } 00192 delete myTLLogic; 00193 myTLLogic = newLogic; 00194 } 00195 } 00196 00197 /****************************************************************************/ 00198