Eclipse SUMO - Simulation of Urban MObility
Command_SaveTLSState.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 /****************************************************************************/
19 // Writes the state of the tls to a file (in each second)
20 /****************************************************************************/
21 #include <config.h>
22 
23 #include "Command_SaveTLSState.h"
26 #include <microsim/MSNet.h>
30 
31 
32 // ===========================================================================
33 // method definitions
34 // ===========================================================================
36  OutputDevice& od, bool saveDetectors, bool saveConditions) :
37  myOutputDevice(od),
38  myLogics(logics),
39  mySaveDetectors(saveDetectors),
40  mySaveConditions(saveConditions) {
42 }
43 
44 
46 }
47 
48 
51  if (!myOutputDevice.wroteHeader()) {
52  // delay writing header to ensure tls is initialized
53  std::map<SumoXMLAttr, std::string> attrs;
54  if (mySaveDetectors) {
55  std::vector<std::string> IDs;
56  for (auto item : myLogics.getActive()->getDetectorStates()) {
57  IDs.push_back(item.first);
58  }
59  attrs[SUMO_ATTR_DETECTORS] = joinToString(IDs, " ");
60 
61  }
62  if (mySaveConditions) {
63  std::vector<std::string> IDs;
64  for (auto item : myLogics.getActive()->getConditions()) {
65  IDs.push_back(item.first);
66  }
67  attrs[SUMO_ATTR_CONDITIONS] = joinToString(IDs, " ");
68  }
69  myOutputDevice.writeXMLHeader("tlsStates", "tlsstates_file.xsd", attrs);
70  }
71  myOutputDevice.openTag("tlsState");
77  if (!myLogics.getActive()->getCurrentPhaseDef().getName().empty()) {
79  }
80  if (mySaveDetectors) {
81  std::vector<int> states;
82  for (auto item : myLogics.getActive()->getDetectorStates()) {
83  states.push_back((int)item.second);
84  }
86 
87  }
88  if (mySaveConditions) {
89  std::vector<double> states;
90  for (auto item : myLogics.getActive()->getConditions()) {
91  states.push_back(item.second);
92  }
94  }
96  return DELTA_T;
97 }
98 
99 
100 /****************************************************************************/
long long int SUMOTime
Definition: GUI.h:35
SUMOTime DELTA_T
Definition: SUMOTime.cpp:38
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition: SUMOTime.cpp:69
@ SUMO_ATTR_PHASE
@ SUMO_ATTR_NAME
@ SUMO_ATTR_DETECTORS
@ SUMO_ATTR_ID
@ SUMO_ATTR_PROGRAMID
@ SUMO_ATTR_STATE
The state of a link.
@ SUMO_ATTR_TIME
trigger: the time of the step
@ SUMO_ATTR_CONDITIONS
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:283
OutputDevice & myOutputDevice
The device to write to.
Command_SaveTLSState(const MSTLLogicControl::TLSLogicVariants &logics, OutputDevice &od, bool saveDetectors, bool saveConditions)
Constructor.
SUMOTime execute(SUMOTime currentTime)
Writes the current state of the tls.
const bool mySaveDetectors
additiona logging
const MSTLLogicControl::TLSLogicVariants & myLogics
The traffic light logic to use.
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
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.
virtual int getCurrentPhaseIndex() const =0
Returns the current index within the program.
virtual std::map< std::string, double > getDetectorStates() const
return activation state of all detectors that affect this traffic light
virtual std::map< std::string, double > getConditions() const
return all named conditions defined for this traffic light
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.
bool wroteHeader() const
Definition: OutputDevice.h:353
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.
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.