SUMO - Simulation of Urban MObility

Command_SaveTLSSwitches.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Writes information about the green durations of a tls
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright (C) 2001-2011 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 // included modules
00021 // ===========================================================================
00022 #ifdef _MSC_VER
00023 #include <windows_config.h>
00024 #else
00025 #include <config.h>
00026 #endif
00027 
00028 #include "Command_SaveTLSSwitches.h"
00029 #include <microsim/traffic_lights/MSTrafficLightLogic.h>
00030 #include <microsim/MSEventControl.h>
00031 #include <microsim/MSNet.h>
00032 #include <microsim/MSLink.h>
00033 #include <microsim/MSLane.h>
00034 #include <utils/common/UtilExceptions.h>
00035 #include <utils/common/MsgHandler.h>
00036 #include <utils/iodevices/OutputDevice.h>
00037 
00038 #ifdef CHECK_MEMORY_LEAKS
00039 #include <foreign/nvwa/debug_new.h>
00040 #endif // CHECK_MEMORY_LEAKS
00041 
00042 
00043 // ===========================================================================
00044 // method definitions
00045 // ===========================================================================
00046 Command_SaveTLSSwitches::Command_SaveTLSSwitches(const MSTLLogicControl::TLSLogicVariants& logics,
00047         OutputDevice& od)
00048     : myOutputDevice(od), myLogics(logics) {
00049     MSNet::getInstance()->getEndOfTimestepEvents().addEvent(this, 0, MSEventControl::ADAPT_AFTER_EXECUTION);
00050     myOutputDevice.writeXMLHeader("tls-switches");
00051 }
00052 
00053 
00054 Command_SaveTLSSwitches::~Command_SaveTLSSwitches() {
00055 }
00056 
00057 
00058 SUMOTime
00059 Command_SaveTLSSwitches::execute(SUMOTime currentTime) {
00060     MSTrafficLightLogic* light = myLogics.getActive();
00061     const MSTrafficLightLogic::LinkVectorVector& links = light->getLinks();
00062     const std::string& state = light->getCurrentPhaseDef().getState();
00063     for (unsigned int i=0; i<(unsigned int) links.size(); i++) {
00064         if (state[i]==LINKSTATE_TL_GREEN_MAJOR||state[i]==LINKSTATE_TL_GREEN_MINOR) {
00065             if (myPreviousLinkStates.find(i)==myPreviousLinkStates.end()) {
00066                 // was not saved before
00067                 myPreviousLinkStates[i] = currentTime;
00068                 continue;
00069             }
00070         } else {
00071             if (myPreviousLinkStates.find(i)==myPreviousLinkStates.end()) {
00072                 // was not yet green
00073                 continue;
00074             }
00075             const MSTrafficLightLogic::LinkVector& currLinks = links[i];
00076             const MSTrafficLightLogic::LaneVector& currLanes = light->getLanesAt(i);
00077             SUMOTime lastOn = myPreviousLinkStates[i];
00078             for (int j=0; j<(int) currLinks.size(); j++) {
00079                 MSLink* link = currLinks[j];
00080                 myOutputDevice << "   <tlsSwitch id=\"" << light->getID()
00081                                << "\" programID=\"" << light->getProgramID()
00082                                << "\" fromLane=\"" << currLanes[j]->getID()
00083                                << "\" toLane=\"" << link->getLane()->getID()
00084                                << "\" begin=\"" << time2string(lastOn)
00085                                << "\" end=\"" << time2string(currentTime)
00086                                << "\" duration=\"" << time2string(currentTime-lastOn)
00087                                << "\"/>\n";
00088             }
00089             myPreviousLinkStates.erase(myPreviousLinkStates.find(i));
00090         }
00091     }
00092     return DELTA_T;
00093 }
00094 
00095 
00096 
00097 /****************************************************************************/
00098 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines