Line data Source code
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 : /****************************************************************************/
14 : /// @file MeanData.cpp
15 : /// @author Jakob Erdmann
16 : /// @date 16.03.2020
17 : ///
18 : // C++ TraCI client API implementation
19 : /****************************************************************************/
20 : #include <config.h>
21 :
22 : #include <microsim/MSNet.h>
23 : #include <microsim/MSEdge.h>
24 : #include <microsim/output/MSDetectorControl.h>
25 : #include <microsim/output/MSMeanData.h>
26 : #include <libsumo/TraCIConstants.h>
27 : #include "StorageHelper.h"
28 : #include "Helper.h"
29 : #include "MeanData.h"
30 :
31 :
32 : namespace libsumo {
33 : // ===========================================================================
34 : // static member initializations
35 : // ===========================================================================
36 : SubscriptionResults MeanData::mySubscriptionResults;
37 : ContextSubscriptionResults MeanData::myContextSubscriptionResults;
38 :
39 :
40 : // ===========================================================================
41 : // static member definitions
42 : // ===========================================================================
43 : std::vector<std::string>
44 245 : MeanData::getIDList() {
45 0 : std::vector<std::string> ids;
46 321 : for (const auto& item : MSNet::getInstance()->getDetectorControl().getMeanData()) {
47 76 : ids.push_back(item.first);
48 : }
49 243 : std::sort(ids.begin(), ids.end());
50 243 : return ids;
51 2 : }
52 :
53 : int
54 23 : MeanData::getIDCount() {
55 23 : return (int)getIDList().size();
56 : }
57 :
58 :
59 : double
60 6916 : MeanData::getAttributeValue(const std::string& meanDataID, const std::string& laneID, const std::string& attr) {
61 6916 : if (!SUMOXMLDefinitions::Attrs.hasString(attr)) {
62 0 : throw TraCIException("Unknown Meandata attribute '" + attr + "'.");
63 : }
64 6916 : MSLane* const lane = MSLane::dictionary(laneID);
65 6916 : if (lane == nullptr) {
66 0 : throw TraCIException("Lane '" + laneID + "' is not known");
67 : }
68 6916 : MSMeanData* md = getMeanData(meanDataID);
69 6916 : SumoXMLAttr a = (SumoXMLAttr)SUMOXMLDefinitions::Attrs.get(attr);
70 6916 : return md->getAttributeValue(lane, a, INVALID_DOUBLE_VALUE);
71 : }
72 :
73 :
74 : std::vector<std::string>
75 18 : MeanData::getIDs(const std::string&meanDataID) {
76 18 : MSMeanData* md = getMeanData(meanDataID);
77 0 : std::vector<std::string> result;
78 18 : if (md->isEdgeData()) {
79 254 : for (const MSEdge* e : md->getEdges()) {
80 240 : result.push_back(e->getID());
81 : }
82 : } else {
83 52 : for (const MSEdge* e : md->getEdges()) {
84 128 : for (const MSLane* lane : e->getLanes()) {
85 80 : result.push_back(lane->getID());
86 : }
87 : }
88 : }
89 18 : return result;
90 0 : }
91 :
92 :
93 : std::vector<double>
94 104 : MeanData::getAttributeValues(const std::string& meanDataID, const std::string& attr) {
95 104 : if (!SUMOXMLDefinitions::Attrs.hasString(attr)) {
96 0 : throw TraCIException("Unknown Meandata attribute '" + attr + "'.");
97 : }
98 104 : MSMeanData* md = getMeanData(meanDataID);
99 104 : SumoXMLAttr a = (SumoXMLAttr)SUMOXMLDefinitions::Attrs.get(attr);
100 0 : std::vector<double> result;
101 104 : if (md->isEdgeData()) {
102 956 : for (const MSEdge* e : md->getEdges()) {
103 888 : result.push_back(md->getAttributeValue(e->getLanes().front(), a, INVALID_DOUBLE_VALUE));
104 : }
105 : } else {
106 468 : for (const MSEdge* e : md->getEdges()) {
107 1152 : for (const MSLane* lane : e->getLanes()) {
108 720 : result.push_back(md->getAttributeValue(lane, a, INVALID_DOUBLE_VALUE));
109 : }
110 : }
111 : }
112 104 : return result;
113 0 : }
114 :
115 :
116 : std::string
117 16 : MeanData::getParameter(const std::string& /* dataID */, const std::string& /* param */) {
118 16 : return "";
119 : }
120 :
121 16 : LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(MeanData)
122 :
123 : void
124 0 : MeanData::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 0 : }
128 :
129 :
130 9 : LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(MeanData, MEANDATA)
131 :
132 :
133 : MSMeanData*
134 7038 : MeanData::getMeanData(const std::string& id) {
135 7038 : auto mdMap = MSNet::getInstance()->getDetectorControl().getMeanData();
136 : auto it = mdMap.find(id);
137 7038 : if (it == mdMap.end() || it->second.size() == 0) {
138 0 : throw TraCIException("MeanData '" + id + "' is not known");
139 : }
140 7038 : if (it->second.size() > 1) {
141 0 : WRITE_WARNINGF(TL("Found % meanData definitions with id '%'."), toString(it->second.size()), id);
142 : }
143 7038 : return it->second.front();
144 : }
145 :
146 :
147 : std::shared_ptr<VariableWrapper>
148 273 : MeanData::makeWrapper() {
149 273 : return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
150 : }
151 :
152 :
153 : bool
154 3684 : MeanData::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
155 3684 : switch (variable) {
156 116 : case TRACI_ID_LIST:
157 116 : return wrapper->wrapStringList(objID, variable, getIDList());
158 17 : case ID_COUNT:
159 17 : return wrapper->wrapInt(objID, variable, getIDCount());
160 8 : case libsumo::VAR_PARAMETER:
161 8 : paramData->readUnsignedByte();
162 16 : return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
163 8 : case libsumo::VAR_PARAMETER_WITH_KEY:
164 8 : paramData->readUnsignedByte();
165 16 : return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
166 : case libsumo::VAR_MEANDATA_LANE: {
167 3461 : StoHelp::readCompound(*paramData);
168 3460 : const std::string laneID = StoHelp::readTypedString(*paramData);
169 3460 : const std::string attr = StoHelp::readTypedString(*paramData);
170 3460 : return wrapper->wrapDouble(objID, variable, getAttributeValue(objID, laneID, attr));
171 : }
172 14 : case libsumo::VAR_MEANDATA_IDS:
173 14 : return wrapper->wrapStringList(objID, variable, getIDs(objID));
174 56 : case libsumo::VAR_MEANDATA_VALUES: {
175 112 : return wrapper->wrapDoubleList(objID, variable, getAttributeValues(objID, StoHelp::readTypedString(*paramData)));
176 : }
177 : default:
178 : return false;
179 : }
180 : }
181 :
182 : }
183 :
184 :
185 : /****************************************************************************/
|