SUMO - Simulation of Urban MObility

NBLoadedSUMOTLDef.cpp

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