LCOV - code coverage report
Current view: top level - src/microsim/actions - Command_SaveTLSProgram.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 37 37
Test Date: 2024-11-22 15:46:21 Functions: 100.0 % 5 5

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2001-2024 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_SaveTLSProgram.cpp
      15              : /// @author  Jakob Erdmann
      16              : /// @date    18.09.2019
      17              : ///
      18              : // Writes the switch times of a tls into a file when the tls switches
      19              : /****************************************************************************/
      20              : #include <config.h>
      21              : 
      22              : #include "Command_SaveTLSProgram.h"
      23              : #include <microsim/traffic_lights/MSTrafficLightLogic.h>
      24              : #include <microsim/MSEventControl.h>
      25              : #include <microsim/MSNet.h>
      26              : #include <utils/common/UtilExceptions.h>
      27              : #include <utils/common/MsgHandler.h>
      28              : #include <utils/iodevices/OutputDevice.h>
      29              : 
      30              : 
      31              : // ===========================================================================
      32              : // method definitions
      33              : // ===========================================================================
      34           27 : Command_SaveTLSProgram::Command_SaveTLSProgram(const MSTLLogicControl::TLSLogicVariants& logics, OutputDevice& od):
      35           27 :     myOutputDevice(od),
      36           27 :     myLogics(logics) {
      37           27 :     MSNet::getInstance()->getEndOfTimestepEvents()->addEvent(this);
      38           54 :     myOutputDevice.writeXMLHeader("additional", "additional_file.xsd");
      39           27 : }
      40              : 
      41              : 
      42           52 : Command_SaveTLSProgram::~Command_SaveTLSProgram() {
      43           26 :     writeCurrent();
      44           52 : }
      45              : 
      46              : 
      47              : SUMOTime
      48        15042 : Command_SaveTLSProgram::execute(SUMOTime /*currentTime*/) {
      49        15042 :     const std::string& state = myLogics.getActive()->getCurrentPhaseDef().getState();
      50        15042 :     const std::string& name = myLogics.getActive()->getCurrentPhaseDef().getName();
      51        15042 :     if (myLogics.getActive()->getProgramID() != myPreviousProgramID) {
      52           31 :         writeCurrent();
      53           31 :         myPreviousProgramID = myLogics.getActive()->getProgramID();
      54           31 :         myTLSID = myLogics.getActive()->getID();
      55              :     }
      56        15042 :     if (myPreviousStates.size() == 0 || myPreviousStates.back().getState() != state) {
      57         3716 :         myPreviousStates.push_back(MSPhaseDefinition(0, state, name));
      58              :     }
      59        15042 :     myPreviousStates.back().duration += DELTA_T;
      60        15042 :     return DELTA_T;
      61              : }
      62              : 
      63              : void
      64           57 : Command_SaveTLSProgram::writeCurrent() {
      65           57 :     if (myPreviousStates.size() > 0) {
      66           31 :         myOutputDevice.openTag(SUMO_TAG_TLLOGIC);
      67           31 :         myOutputDevice.writeAttr(SUMO_ATTR_ID, myTLSID);
      68           31 :         myOutputDevice.writeAttr(SUMO_ATTR_TYPE, "static");
      69           31 :         myOutputDevice.writeAttr(SUMO_ATTR_PROGRAMID, myPreviousProgramID);
      70              :         // write the phases
      71         1889 :         for (const MSPhaseDefinition& p : myPreviousStates) {
      72         1858 :             myOutputDevice.openTag(SUMO_TAG_PHASE);
      73         1858 :             myOutputDevice.writeAttr(SUMO_ATTR_DURATION, STEPS2TIME(p.duration));
      74         1858 :             if (p.duration < TIME2STEPS(10)) {
      75         3210 :                 myOutputDevice.writePadding(" ");
      76              :             }
      77         1858 :             myOutputDevice.writeAttr(SUMO_ATTR_STATE, p.getState());
      78         1858 :             if (p.getName() != "") {
      79          622 :                 myOutputDevice.writeAttr(SUMO_ATTR_NAME, p.getName());
      80              :             }
      81         3716 :             myOutputDevice.closeTag();
      82              :         }
      83              :         // write params
      84           62 :         myOutputDevice.closeTag();
      85              :         myPreviousStates.clear();
      86              :     }
      87           57 : }
      88              : 
      89              : 
      90              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1