MSLaneSpeedTrigger.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Changes the speed allowed on a set of lanes
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 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 <string>
00031 #include <utils/common/MsgHandler.h>
00032 #include <utils/common/WrappingCommand.h>
00033 #include <microsim/MSLane.h>
00034 #include <utils/xml/SUMOXMLDefinitions.h>
00035 #include <utils/common/UtilExceptions.h>
00036 #include "MSLaneSpeedTrigger.h"
00037 #include <utils/xml/XMLSubSys.h>
00038 #include <utils/common/TplConvert.h>
00039 #include <microsim/MSEventControl.h>
00040 
00041 #ifdef HAVE_MESOSIM
00042 #include <microsim/MSGlobals.h>
00043 #include <mesosim/MELoop.h>
00044 #include <mesosim/MESegment.h>
00045 #endif
00046 
00047 #ifdef CHECK_MEMORY_LEAKS
00048 #include <foreign/nvwa/debug_new.h>
00049 #endif // CHECK_MEMORY_LEAKS
00050 
00051 
00052 // ===========================================================================
00053 // method definitions
00054 // ===========================================================================
00055 MSLaneSpeedTrigger::MSLaneSpeedTrigger(const std::string &id,
00056                                        const std::vector<MSLane*> &destLanes,
00057                                        const std::string &file) throw(ProcessError)
00058         : MSTrigger(id), SUMOSAXHandler(file),
00059         myDestLanes(destLanes), myAmOverriding(false), myDidInit(false) {
00060     myCurrentSpeed = destLanes[0]->getMaxSpeed();
00061     if (file != "") {
00062         if (!XMLSubSys::runParser(*this, file)) {
00063             throw ProcessError();
00064         }
00065         if (!myDidInit) {
00066             init();
00067         }
00068     }
00069 }
00070 
00071 void
00072 MSLaneSpeedTrigger::init() throw(ProcessError) {
00073     // set it to the right value
00074     // assert there is at least one
00075     if (myLoadedSpeeds.size()==0) {
00076         myLoadedSpeeds.push_back(std::make_pair(100000, myCurrentSpeed));
00077     }
00078     // set the process to the begin
00079     myCurrentEntry = myLoadedSpeeds.begin();
00080     // pass previous time steps
00081     while ((*myCurrentEntry).first<MSNet::getInstance()->getCurrentTimeStep()&&myCurrentEntry!=myLoadedSpeeds.end()) {
00082         processCommand(true, MSNet::getInstance()->getCurrentTimeStep());
00083     }
00084 
00085     // add the processing to the event handler
00086     MSNet::getInstance()->getBeginOfTimestepEvents().addEvent(
00087         new WrappingCommand<MSLaneSpeedTrigger>(this, &MSLaneSpeedTrigger::execute),
00088         (*myCurrentEntry).first, MSEventControl::NO_CHANGE);
00089     myDidInit = true;
00090 }
00091 
00092 
00093 MSLaneSpeedTrigger::~MSLaneSpeedTrigger() throw() {}
00094 
00095 
00096 SUMOTime
00097 MSLaneSpeedTrigger::execute(SUMOTime currentTime) throw(ProcessError) {
00098     return processCommand(true, currentTime);
00099 }
00100 
00101 
00102 SUMOTime
00103 MSLaneSpeedTrigger::processCommand(bool move2next, SUMOTime currentTime) {
00104     std::vector<MSLane*>::iterator i;
00105     const SUMOReal speed = getCurrentSpeed();
00106     for (i=myDestLanes.begin(); i!=myDestLanes.end(); ++i) {
00107 #ifdef HAVE_MESOSIM
00108         if (MSGlobals::gUseMesoSim) {
00109             MESegment *first = MSGlobals::gMesoNet->getSegmentForEdge((*i)->getEdge());
00110             while (first!=0) {
00111                 MSGlobals::gMesoNet->setSpeed(first, speed, currentTime);
00112                 first = first->getNextSegment();
00113             }
00114             continue;
00115         }
00116 #endif
00117         (*i)->setMaxSpeed(speed);
00118     }
00119     if (!move2next) {
00120         // changed from the gui
00121         return 0;
00122     }
00123     if (myCurrentEntry!=myLoadedSpeeds.end()) {
00124         ++myCurrentEntry;
00125     }
00126     if (myCurrentEntry!=myLoadedSpeeds.end()) {
00127         return ((*myCurrentEntry).first)-((*(myCurrentEntry-1)).first);
00128     } else {
00129         return 0;
00130     }
00131 }
00132 
00133 
00134 void
00135 MSLaneSpeedTrigger::myStartElement(SumoXMLTag element,
00136                                    const SUMOSAXAttributes &attrs) throw(ProcessError) {
00137     // check whether the correct tag is read
00138     if (element!=SUMO_TAG_STEP) {
00139         return;
00140     }
00141     // extract the values
00142     bool ok = true;
00143     SUMOTime next = attrs.getSUMOTimeReporting(SUMO_ATTR_TIME, "vss/step", getID().c_str(), ok);
00144     SUMOReal speed = attrs.getOptSUMORealReporting(SUMO_ATTR_SPEED, "vss/step", getID().c_str(), ok, -1);
00145     // check the values
00146     if (next<0) {
00147         MsgHandler::getErrorInstance()->inform("Wrong time in MSLaneSpeedTrigger in file '" + getFileName() + "'.");
00148         return;
00149     }
00150     if (speed<0) {
00151         MsgHandler::getErrorInstance()->inform("Wrong speed in MSLaneSpeedTrigger in file '" + getFileName() + "'.");
00152         return;
00153     }
00154     // set the values for the next step as they are valid
00155     myLoadedSpeeds.push_back(std::make_pair(next, speed));
00156 }
00157 
00158 
00159 void
00160 MSLaneSpeedTrigger::myEndElement(SumoXMLTag element) throw(ProcessError) {
00161     if (element==SUMO_TAG_VSS && !myDidInit) {
00162         init();
00163     }
00164 }
00165 
00166 
00167 SUMOReal
00168 MSLaneSpeedTrigger::getDefaultSpeed() const {
00169     return myDefaultSpeed;
00170 }
00171 
00172 
00173 void
00174 MSLaneSpeedTrigger::setOverriding(bool val) {
00175     myAmOverriding = val;
00176     processCommand(false, MSNet::getInstance()->getCurrentTimeStep());
00177 }
00178 
00179 
00180 void
00181 MSLaneSpeedTrigger::setOverridingValue(SUMOReal val) {
00182     mySpeedOverrideValue = val;
00183     processCommand(false, MSNet::getInstance()->getCurrentTimeStep());
00184 }
00185 
00186 
00187 SUMOReal
00188 MSLaneSpeedTrigger::getLoadedSpeed() {
00189     if (myCurrentEntry!=myLoadedSpeeds.begin()) {
00190         return (*(myCurrentEntry-1)).second;
00191     } else {
00192         return (*myCurrentEntry).second;
00193     }
00194 }
00195 
00196 
00197 SUMOReal
00198 MSLaneSpeedTrigger::getCurrentSpeed() const {
00199     if (myAmOverriding) {
00200         return mySpeedOverrideValue;
00201     } else {
00202         // ok, maybe the first shall not yet be the valid one
00203         if (myCurrentEntry==myLoadedSpeeds.begin()&&(*myCurrentEntry).first>MSNet::getInstance()->getCurrentTimeStep()) {
00204             return myDefaultSpeed;
00205         }
00206         // try the loaded
00207         if (myCurrentEntry!=myLoadedSpeeds.end()&&(*myCurrentEntry).first<=MSNet::getInstance()->getCurrentTimeStep()) {
00208             return (*myCurrentEntry).second;
00209         } else {
00210             return (*(myCurrentEntry-1)).second;
00211         }
00212     }
00213 }
00214 
00215 
00216 /****************************************************************************/
00217 

Generated on Wed May 5 00:06:31 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6