LCOV - code coverage report
Current view: top level - src/microsim/actions - Command_SaveTLSSwitches.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 30 30
Test Date: 2026-03-02 16:00:03 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.
       4              : // This program and the accompanying materials are made available under the
       5              : // terms of the Eclipse Public License 2.0 which is available at
       6              : // https://www.eclipse.org/legal/epl-2.0/
       7              : // This Source Code may also be made available under the following Secondary
       8              : // Licenses when the conditions for such availability set forth in the Eclipse
       9              : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10              : // or later which is available at
      11              : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12              : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13              : /****************************************************************************/
      14              : /// @file    Command_SaveTLSSwitches.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Jakob Erdmann
      17              : /// @author  Michael Behrisch
      18              : /// @date    06 Jul 2006
      19              : ///
      20              : // Writes information about the green durations of a tls
      21              : /****************************************************************************/
      22              : #include <config.h>
      23              : 
      24              : #include "Command_SaveTLSSwitches.h"
      25              : #include <microsim/traffic_lights/MSTrafficLightLogic.h>
      26              : #include <microsim/MSEventControl.h>
      27              : #include <microsim/MSNet.h>
      28              : #include <microsim/MSLink.h>
      29              : #include <microsim/MSLane.h>
      30              : #include <utils/common/UtilExceptions.h>
      31              : #include <utils/common/MsgHandler.h>
      32              : #include <utils/iodevices/OutputDevice.h>
      33              : 
      34              : 
      35              : // ===========================================================================
      36              : // method definitions
      37              : // ===========================================================================
      38          140 : Command_SaveTLSSwitches::Command_SaveTLSSwitches(const MSTLLogicControl::TLSLogicVariants& logics,
      39          140 :         OutputDevice& od)
      40          140 :     : myOutputDevice(od), myLogics(logics) {
      41          140 :     MSNet::getInstance()->getEndOfTimestepEvents()->addEvent(this);
      42          280 :     myOutputDevice.writeXMLHeader("tlsSwitches", "tlsswitches_file.xsd");
      43          140 : }
      44              : 
      45              : 
      46          278 : Command_SaveTLSSwitches::~Command_SaveTLSSwitches() {
      47          278 : }
      48              : 
      49              : 
      50              : SUMOTime
      51       168873 : Command_SaveTLSSwitches::execute(SUMOTime currentTime) {
      52       168873 :     MSTrafficLightLogic* light = myLogics.getActive();
      53              :     const MSTrafficLightLogic::LinkVectorVector& links = light->getLinks();
      54       168873 :     const std::string& state = light->getCurrentPhaseDef().getState();
      55       926973 :     for (int i = 0; i < (int) links.size(); i++) {
      56       758100 :         if (state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR) {
      57       292156 :             if (myPreviousLinkStates.find(i) == myPreviousLinkStates.end()) {
      58              :                 // was not saved before
      59        10290 :                 myPreviousLinkStates[i] = currentTime;
      60        10290 :                 continue;
      61              :             }
      62              :         } else {
      63       465944 :             if (myPreviousLinkStates.find(i) == myPreviousLinkStates.end()) {
      64              :                 // was not yet green
      65       455964 :                 continue;
      66              :             }
      67              :             const MSTrafficLightLogic::LinkVector& currLinks = links[i];
      68              :             const MSTrafficLightLogic::LaneVector& currLanes = light->getLanesAt(i);
      69         9980 :             SUMOTime lastOn = myPreviousLinkStates[i];
      70        19960 :             for (int j = 0; j < (int) currLinks.size(); j++) {
      71         9980 :                 MSLink* link = currLinks[j];
      72         9980 :                 myOutputDevice << "   <tlsSwitch id=\"" << light->getID()
      73         9980 :                                << "\" programID=\"" << light->getProgramID()
      74         9980 :                                << "\" fromLane=\"" << currLanes[j]->getID()
      75         9980 :                                << "\" toLane=\"" << link->getLane()->getID()
      76        19960 :                                << "\" begin=\"" << time2string(lastOn)
      77        19960 :                                << "\" end=\"" << time2string(currentTime)
      78        19960 :                                << "\" duration=\"" << time2string(currentTime - lastOn)
      79         9980 :                                << "\"/>\n";
      80              :             }
      81              :             myPreviousLinkStates.erase(myPreviousLinkStates.find(i));
      82              :         }
      83              :     }
      84       168873 :     return DELTA_T;
      85              : }
      86              : 
      87              : 
      88              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1