SUMO - Simulation of Urban MObility
Command_SaveTLSSwitches.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // Writes information about the green durations of a tls
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
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 "Command_SaveTLSSwitches.h"
00032 #include <microsim/traffic_lights/MSTrafficLightLogic.h>
00033 #include <microsim/MSEventControl.h>
00034 #include <microsim/MSNet.h>
00035 #include <microsim/MSLink.h>
00036 #include <microsim/MSLane.h>
00037 #include <utils/common/UtilExceptions.h>
00038 #include <utils/common/MsgHandler.h>
00039 #include <utils/iodevices/OutputDevice.h>
00040 
00041 #ifdef CHECK_MEMORY_LEAKS
00042 #include <foreign/nvwa/debug_new.h>
00043 #endif // CHECK_MEMORY_LEAKS
00044 
00045 
00046 // ===========================================================================
00047 // method definitions
00048 // ===========================================================================
00049 Command_SaveTLSSwitches::Command_SaveTLSSwitches(const MSTLLogicControl::TLSLogicVariants& logics,
00050         OutputDevice& od)
00051     : myOutputDevice(od), myLogics(logics) {
00052     MSNet::getInstance()->getEndOfTimestepEvents().addEvent(this, 0, MSEventControl::ADAPT_AFTER_EXECUTION);
00053     myOutputDevice.writeXMLHeader("tls-switches");
00054 }
00055 
00056 
00057 Command_SaveTLSSwitches::~Command_SaveTLSSwitches() {
00058 }
00059 
00060 
00061 SUMOTime
00062 Command_SaveTLSSwitches::execute(SUMOTime currentTime) {
00063     MSTrafficLightLogic* light = myLogics.getActive();
00064     const MSTrafficLightLogic::LinkVectorVector& links = light->getLinks();
00065     const std::string& state = light->getCurrentPhaseDef().getState();
00066     for (unsigned int i = 0; i < (unsigned int) links.size(); i++) {
00067         if (state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR) {
00068             if (myPreviousLinkStates.find(i) == myPreviousLinkStates.end()) {
00069                 // was not saved before
00070                 myPreviousLinkStates[i] = currentTime;
00071                 continue;
00072             }
00073         } else {
00074             if (myPreviousLinkStates.find(i) == myPreviousLinkStates.end()) {
00075                 // was not yet green
00076                 continue;
00077             }
00078             const MSTrafficLightLogic::LinkVector& currLinks = links[i];
00079             const MSTrafficLightLogic::LaneVector& currLanes = light->getLanesAt(i);
00080             SUMOTime lastOn = myPreviousLinkStates[i];
00081             for (int j = 0; j < (int) currLinks.size(); j++) {
00082                 MSLink* link = currLinks[j];
00083                 myOutputDevice << "   <tlsSwitch id=\"" << light->getID()
00084                                << "\" programID=\"" << light->getProgramID()
00085                                << "\" fromLane=\"" << currLanes[j]->getID()
00086                                << "\" toLane=\"" << link->getLane()->getID()
00087                                << "\" begin=\"" << time2string(lastOn)
00088                                << "\" end=\"" << time2string(currentTime)
00089                                << "\" duration=\"" << time2string(currentTime - lastOn)
00090                                << "\"/>\n";
00091             }
00092             myPreviousLinkStates.erase(myPreviousLinkStates.find(i));
00093         }
00094     }
00095     return DELTA_T;
00096 }
00097 
00098 
00099 
00100 /****************************************************************************/
00101 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines