Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
libsumo/MeanData.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2017-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/****************************************************************************/
18// C++ TraCI client API implementation
19/****************************************************************************/
20#include <config.h>
21
22#include <microsim/MSNet.h>
23#include <microsim/MSEdge.h>
27#include "StorageHelper.h"
28#include "Helper.h"
29#include "MeanData.h"
30
31
32namespace libsumo {
33// ===========================================================================
34// static member initializations
35// ===========================================================================
36SubscriptionResults MeanData::mySubscriptionResults;
37ContextSubscriptionResults MeanData::myContextSubscriptionResults;
38
39
40// ===========================================================================
41// static member definitions
42// ===========================================================================
43std::vector<std::string>
44MeanData::getIDList() {
45 std::vector<std::string> ids;
46 for (const auto& item : MSNet::getInstance()->getDetectorControl().getMeanData()) {
47 ids.push_back(item.first);
48 }
49 std::sort(ids.begin(), ids.end());
50 return ids;
51}
52
53int
54MeanData::getIDCount() {
55 return (int)getIDList().size();
56}
57
58
59double
60MeanData::getAttributeValue(const std::string& meanDataID, const std::string& laneID, const std::string& attr) {
61 if (!SUMOXMLDefinitions::Attrs.hasString(attr)) {
62 throw TraCIException("Unknown Meandata attribute '" + attr + "'.");
63 }
64 MSLane* const lane = MSLane::dictionary(laneID);
65 if (lane == nullptr) {
66 throw TraCIException("Lane '" + laneID + "' is not known");
67 }
68 MSMeanData* md = getMeanData(meanDataID);
70 return md->getAttributeValue(lane, a, INVALID_DOUBLE_VALUE);
71}
72
73
74std::vector<std::string>
75MeanData::getIDs(const std::string&meanDataID) {
76 MSMeanData* md = getMeanData(meanDataID);
77 std::vector<std::string> result;
78 if (md->isEdgeData()) {
79 for (const MSEdge* e : md->getEdges()) {
80 result.push_back(e->getID());
81 }
82 } else {
83 for (const MSEdge* e : md->getEdges()) {
84 for (const MSLane* lane : e->getLanes()) {
85 result.push_back(lane->getID());
86 }
87 }
88 }
89 return result;
90}
91
92
93std::vector<double>
94MeanData::getAttributeValues(const std::string& meanDataID, const std::string& attr) {
95 if (!SUMOXMLDefinitions::Attrs.hasString(attr)) {
96 throw TraCIException("Unknown Meandata attribute '" + attr + "'.");
97 }
98 MSMeanData* md = getMeanData(meanDataID);
100 std::vector<double> result;
101 if (md->isEdgeData()) {
102 for (const MSEdge* e : md->getEdges()) {
103 result.push_back(md->getAttributeValue(e->getLanes().front(), a, INVALID_DOUBLE_VALUE));
104 }
105 } else {
106 for (const MSEdge* e : md->getEdges()) {
107 for (const MSLane* lane : e->getLanes()) {
108 result.push_back(md->getAttributeValue(lane, a, INVALID_DOUBLE_VALUE));
109 }
110 }
111 }
112 return result;
113}
114
115
116std::string
117MeanData::getParameter(const std::string& /* dataID */, const std::string& /* param */) {
118 return "";
119}
120
122
123void
124MeanData::setParameter(const std::string& /* dataID */, const std::string& /* key */, const std::string& /* value */) {
125 //MSMeanData* r = const_cast<MSMeanData*>(getMeanData(dataID));
126 //r->setParameter(key, value);
127}
128
129
131
132
134MeanData::getMeanData(const std::string& id) {
136 auto it = mdMap.find(id);
137 if (it == mdMap.end() || it->second.size() == 0) {
138 throw TraCIException("MeanData '" + id + "' is not known");
139 }
140 if (it->second.size() > 1) {
141 WRITE_WARNINGF(TL("Found % meanData definitions with id '%'."), toString(it->second.size()), id);
142 }
143 return it->second.front();
144}
145
146
147std::shared_ptr<VariableWrapper>
148MeanData::makeWrapper() {
149 return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
150}
151
152
153bool
154MeanData::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
155 switch (variable) {
156 case TRACI_ID_LIST:
157 return wrapper->wrapStringList(objID, variable, getIDList());
158 case ID_COUNT:
159 return wrapper->wrapInt(objID, variable, getIDCount());
161 paramData->readUnsignedByte();
162 return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
164 paramData->readUnsignedByte();
165 return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
167 StoHelp::readCompound(*paramData);
168 const std::string laneID = StoHelp::readTypedString(*paramData);
169 const std::string attr = StoHelp::readTypedString(*paramData);
170 return wrapper->wrapDouble(objID, variable, getAttributeValue(objID, laneID, attr));
171 }
173 return wrapper->wrapStringList(objID, variable, getIDs(objID));
175 return wrapper->wrapDoubleList(objID, variable, getAttributeValues(objID, StoHelp::readTypedString(*paramData)));
176 }
177 default:
178 return false;
179 }
180}
181
182}
183
184
185/****************************************************************************/
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:287
#define TL(string)
Definition MsgHandler.h:304
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:49
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOM)
Definition TraCIDefs.h:77
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition TraCIDefs.h:124
const std::map< std::string, std::vector< MSMeanData * > > & getMeanData() const
A road/street connecting two junctions.
Definition MSEdge.h:77
Representation of a lane in the micro simulation.
Definition MSLane.h:84
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition MSLane.cpp:2528
Data collector for edges/lanes.
Definition MSMeanData.h:57
bool isEdgeData() const
Definition MSMeanData.h:391
const MSEdgeVector & getEdges() const
Definition MSMeanData.h:409
double getAttributeValue(const MSLane *lane, SumoXMLAttr a, double defaultValue) const
return attribute value for the given lane
The simulated network and simulation perfomer.
Definition MSNet.h:89
MSDetectorControl & getDetectorControl()
Returns the detector control.
Definition MSNet.h:465
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:201
C++ TraCI client API implementation.
static SequentialStringBijection Attrs
The names of SUMO-XML attributes for use in netbuild.
static int readCompound(tcpip::Storage &ret, int expectedSize=-1, const std::string &error="")
static std::string readTypedString(tcpip::Storage &ret, const std::string &error="")
virtual std::string readString()
Definition storage.cpp:180
virtual int readUnsignedByte()
Definition storage.cpp:155
TRACI_CONST double INVALID_DOUBLE_VALUE
TRACI_CONST int TRACI_ID_LIST
std::map< std::string, libsumo::SubscriptionResults > ContextSubscriptionResults
Definition TraCIDefs.h:384
TRACI_CONST int VAR_MEANDATA_VALUES
std::map< std::string, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition TraCIDefs.h:383
TRACI_CONST int ID_COUNT
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int VAR_MEANDATA_LANE
TRACI_CONST int VAR_PARAMETER_WITH_KEY
TRACI_CONST int VAR_MEANDATA_IDS