Eclipse SUMO - Simulation of Urban MObility
Command_SaveTLSProgram.cpp
Go to the documentation of this file.
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 /****************************************************************************/
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"
25 #include <microsim/MSNet.h>
29 
30 
31 // ===========================================================================
32 // method definitions
33 // ===========================================================================
35  myOutputDevice(od),
36  myLogics(logics) {
38  myOutputDevice.writeXMLHeader("additional", "additional_file.xsd");
39 }
40 
41 
43  writeCurrent();
44 }
45 
46 
49  const std::string& state = myLogics.getActive()->getCurrentPhaseDef().getState();
50  const std::string& name = myLogics.getActive()->getCurrentPhaseDef().getName();
52  writeCurrent();
55  }
56  if (myPreviousStates.size() == 0 || myPreviousStates.back().getState() != state) {
57  myPreviousStates.push_back(MSPhaseDefinition(0, state, name));
58  }
59  myPreviousStates.back().duration += DELTA_T;
60  return DELTA_T;
61 }
62 
63 void
65  if (myPreviousStates.size() > 0) {
70  // write the phases
71  for (const MSPhaseDefinition& p : myPreviousStates) {
74  if (p.duration < TIME2STEPS(10)) {
76  }
78  if (p.getName() != "") {
80  }
82  }
83  // write params
85  myPreviousStates.clear();
86  }
87 }
88 
89 
90 /****************************************************************************/
long long int SUMOTime
Definition: GUI.h:35
SUMOTime DELTA_T
Definition: SUMOTime.cpp:38
#define STEPS2TIME(x)
Definition: SUMOTime.h:55
#define TIME2STEPS(x)
Definition: SUMOTime.h:57
@ SUMO_TAG_PHASE
a single phase description
@ SUMO_TAG_TLLOGIC
a traffic light logic
@ SUMO_ATTR_NAME
@ SUMO_ATTR_TYPE
@ SUMO_ATTR_ID
@ SUMO_ATTR_PROGRAMID
@ SUMO_ATTR_DURATION
@ SUMO_ATTR_STATE
The state of a link.
SUMOTime execute(SUMOTime currentTime)
Writes the state of the tls if a change occurred.
std::vector< MSPhaseDefinition > myPreviousStates
Storage for prior state.
Command_SaveTLSProgram(const MSTLLogicControl::TLSLogicVariants &logics, OutputDevice &od)
Constructor.
const MSTLLogicControl::TLSLogicVariants & myLogics
The traffic light logic to use.
void writeCurrent()
Write the current program.
std::string myTLSID
Storage for the tls ID (needed in destructor)
std::string myPreviousProgramID
Storage for prior sub-id.
OutputDevice & myOutputDevice
The device to write to.
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:182
MSEventControl * getEndOfTimestepEvents()
Returns the event control for events executed at the end of a time step.
Definition: MSNet.h:481
The definition of a single phase of a tls logic.
const std::string & getState() const
Returns the state within this phase.
const std::string & getName() const
Storage for all programs of a single tls.
MSTrafficLightLogic * getActive() const
virtual const MSPhaseDefinition & getCurrentPhaseDef() const =0
Returns the definition of the current phase.
const std::string & getProgramID() const
Returns this tl-logic's id.
const std::string & getID() const
Returns the id.
Definition: Named.h:74
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:254
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
OutputDevice & writePadding(const std::string &val)
writes padding (ignored for binary output)
Definition: OutputDevice.h:325
bool writeXMLHeader(const std::string &rootElement, const std::string &schemaFile, std::map< SumoXMLAttr, std::string > attrs=std::map< SumoXMLAttr, std::string >(), bool includeConfig=true)
Writes an XML header with optional configuration.