Eclipse SUMO - Simulation of Urban MObility
libtraci/TrafficLight.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3// Copyright (C) 2017-2022 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/****************************************************************************/
21// C++ TraCI client API implementation
22/****************************************************************************/
23#include <config.h>
24
25#define LIBTRACI 1
29#include "Domain.h"
30
31
32namespace libtraci {
33
34typedef Domain<libsumo::CMD_GET_TL_VARIABLE, libsumo::CMD_SET_TL_VARIABLE> Dom;
35
36// ===========================================================================
37// static member definitions
38// ===========================================================================
39std::vector<std::string>
40TrafficLight::getIDList() {
42}
43
44
45int
46TrafficLight::getIDCount() {
48}
49
50
51std::string
52TrafficLight::getRedYellowGreenState(const std::string& tlsID) {
54}
55
56
57std::vector<libsumo::TraCILogic>
58TrafficLight::getAllProgramLogics(const std::string& tlsID) {
60 std::vector<libsumo::TraCILogic> result;
61 int numLogics = ret.readInt();
62 while (numLogics-- > 0) {
66 logic.type = StoHelp::readTypedInt(ret);
68 int numPhases = StoHelp::readCompound(ret);
69 while (numPhases-- > 0) {
73 phase->state = StoHelp::readTypedString(ret);
74 phase->minDur = StoHelp::readTypedDouble(ret);
75 phase->maxDur = StoHelp::readTypedDouble(ret);
76 int numNext = StoHelp::readCompound(ret);
77 while (numNext-- > 0) {
78 phase->next.push_back(StoHelp::readTypedInt(ret));
79 }
80 phase->name = StoHelp::readTypedString(ret);
81 logic.phases.emplace_back(phase);
82 }
83 int numParams = StoHelp::readCompound(ret);
84 while (numParams-- > 0) {
85 const std::vector<std::string> key_value = StoHelp::readTypedStringList(ret);
86 logic.subParameter[key_value[0]] = key_value[1];
87 }
88 result.emplace_back(logic);
89 }
90 return result;
91}
92
93
94std::vector<std::string>
95TrafficLight::getControlledJunctions(const std::string& tlsID) {
97}
98
99
100std::vector<std::string>
101TrafficLight::getControlledLanes(const std::string& tlsID) {
103}
104
105
106std::vector<std::vector<libsumo::TraCILink> >
107TrafficLight::getControlledLinks(const std::string& tlsID) {
109 std::vector< std::vector<libsumo::TraCILink> > result;
110 ret.readInt();
111 int numSignals = StoHelp::readTypedInt(ret);
112 while (numSignals-- > 0) {
113 std::vector<libsumo::TraCILink> controlledLinks;
114 int numLinks = StoHelp::readTypedInt(ret);
115 while (numLinks-- > 0) {
116 std::vector<std::string> link = StoHelp::readTypedStringList(ret);
117 controlledLinks.emplace_back(link[0], link[2], link[1]);
118 }
119 result.emplace_back(controlledLinks);
120 }
121 return result;
122}
123
124
125std::string
126TrafficLight::getProgram(const std::string& tlsID) {
128}
129
130
131int
132TrafficLight::getPhase(const std::string& tlsID) {
134}
135
136
137std::string
138TrafficLight::getPhaseName(const std::string& tlsID) {
139 return Dom::getString(libsumo::VAR_NAME, tlsID);
140}
141
142
143double
144TrafficLight::getPhaseDuration(const std::string& tlsID) {
146}
147
148
149double
150TrafficLight::getNextSwitch(const std::string& tlsID) {
152}
153
154int
155TrafficLight::getServedPersonCount(const std::string& tlsID, int index) {
156 tcpip::Storage content;
158 content.writeInt(index);
159 return Dom::getInt(libsumo::VAR_PERSON_NUMBER, tlsID, &content);
160}
161
162std::vector<std::string>
163TrafficLight::getBlockingVehicles(const std::string& tlsID, int linkIndex) {
164 tcpip::Storage content;
166 content.writeInt(linkIndex);
168}
169
170std::vector<std::string>
171TrafficLight::getRivalVehicles(const std::string& tlsID, int linkIndex) {
172 tcpip::Storage content;
174 content.writeInt(linkIndex);
175 return Dom::getStringVector(libsumo::TL_RIVAL_VEHICLES, tlsID, &content);
176}
177
178std::vector<std::string>
179TrafficLight::getPriorityVehicles(const std::string& tlsID, int linkIndex) {
180 tcpip::Storage content;
182 content.writeInt(linkIndex);
184}
185
186std::vector<libsumo::TraCISignalConstraint>
187TrafficLight::getConstraints(const std::string& tlsID, const std::string& tripId) {
188 std::vector<libsumo::TraCISignalConstraint> result;
189 tcpip::Storage content;
190 StoHelp::writeTypedString(content, tripId);
191 tcpip::Storage& ret = Dom::get(libsumo::TL_CONSTRAINT, tlsID, &content);
192 ret.readInt(); // components
193 // number of items
194 ret.readUnsignedByte();
195 const int n = ret.readInt();
196 for (int i = 0; i < n; ++i) {
204 c.mustWait = StoHelp::readTypedByte(ret) != 0;
205 c.active = StoHelp::readTypedByte(ret) != 0;
206 const std::vector<std::string> paramItems = StoHelp::readTypedStringList(ret);
207 for (int j = 0; j < (int)paramItems.size(); j += 2) {
208 c.param[paramItems[j]] = paramItems[j + 1];
209 }
210 result.push_back(c);
211 }
212 return result;
213}
214
215std::vector<libsumo::TraCISignalConstraint>
216TrafficLight::getConstraintsByFoe(const std::string& foeSignal, const std::string& foeId) {
217 std::vector<libsumo::TraCISignalConstraint> result;
218 tcpip::Storage content;
219 StoHelp::writeTypedString(content, foeId);
220 tcpip::Storage& ret = Dom::get(libsumo::TL_CONSTRAINT_BYFOE, foeSignal, &content);
221 ret.readInt(); // components
222 // number of items
223 ret.readUnsignedByte();
224 const int n = ret.readInt();
225 for (int i = 0; i < n; ++i) {
233 c.mustWait = StoHelp::readTypedByte(ret) != 0;
234 c.active = StoHelp::readTypedByte(ret) != 0;
235 const std::vector<std::string> paramItems = StoHelp::readTypedStringList(ret);
236 for (int j = 0; j < (int)paramItems.size(); j += 2) {
237 c.param[paramItems[j]] = paramItems[j + 1];
238 }
239 result.push_back(c);
240 }
241 return result;
242}
243
245
246void
247TrafficLight::setRedYellowGreenState(const std::string& tlsID, const std::string& state) {
249}
250
251
252void
253TrafficLight::setPhase(const std::string& tlsID, const int index) {
255}
256
257
258void
259TrafficLight::setPhaseName(const std::string& tlsID, const std::string& name) {
260 Dom::setString(libsumo::VAR_NAME, tlsID, name);
261}
262
263
264void
265TrafficLight::setProgram(const std::string& tlsID, const std::string& programID) {
266 Dom::setString(libsumo::TL_PROGRAM, tlsID, programID);
267}
268
269
270void
271TrafficLight::setPhaseDuration(const std::string& tlsID, const double phaseDuration) {
272 Dom::setDouble(libsumo::TL_PHASE_DURATION, tlsID, phaseDuration);
273}
274
275
276void
277TrafficLight::setProgramLogic(const std::string& tlsID, const libsumo::TraCILogic& logic) {
278 tcpip::Storage content;
279 StoHelp::writeCompound(content, 5);
280 StoHelp::writeTypedString(content, logic.programID);
281 StoHelp::writeTypedInt(content, logic.type);
283 StoHelp::writeCompound(content, (int)logic.phases.size());
284 for (const std::shared_ptr<libsumo::TraCIPhase>& phase : logic.phases) {
285 StoHelp::writeCompound(content, 6);
286 StoHelp::writeTypedDouble(content, phase->duration);
287 StoHelp::writeTypedString(content, phase->state);
288 StoHelp::writeTypedDouble(content, phase->minDur);
289 StoHelp::writeTypedDouble(content, phase->maxDur);
290 StoHelp::writeCompound(content, (int)phase->next.size());
291 for (int n : phase->next) {
292 StoHelp::writeTypedInt(content, n);
293 }
294 StoHelp::writeTypedString(content, phase->name);
295 }
296 StoHelp::writeCompound(content, (int)logic.subParameter.size());
297 for (const auto& key_value : logic.subParameter) {
298 StoHelp::writeTypedStringList(content, std::vector<std::string> {key_value.first, key_value.second});
299 }
301}
302
303
304std::vector<libsumo::TraCISignalConstraint>
305TrafficLight::swapConstraints(const std::string& tlsID, const std::string& tripId, const std::string& foeSignal, const std::string& foeId) {
306 std::vector<libsumo::TraCISignalConstraint> result;
307 tcpip::Storage content;
309 content.writeInt(3);
310 StoHelp::writeTypedString(content, tripId);
311 StoHelp::writeTypedString(content, foeSignal);
312 StoHelp::writeTypedString(content, foeId);
313 tcpip::Storage& ret = Dom::get(libsumo::TL_CONSTRAINT_SWAP, tlsID, &content);
314 ret.readInt(); // components
315 // number of items
316 ret.readUnsignedByte();
317 const int n = ret.readInt();
318 for (int i = 0; i < n; ++i) {
326 c.mustWait = StoHelp::readTypedByte(ret) != 0;
327 c.active = StoHelp::readTypedByte(ret) != 0;
328 const std::vector<std::string> paramItems = StoHelp::readTypedStringList(ret);
329 for (int j = 0; j < (int)paramItems.size(); j += 2) {
330 c.param[paramItems[j]] = paramItems[j + 1];
331 }
332 result.push_back(c);
333 }
334 return result;
335}
336
337
338void
339TrafficLight::removeConstraints(const std::string& tlsID, const std::string& tripId, const std::string& foeSignal, const std::string& foeId) {
340 tcpip::Storage content;
342 content.writeInt(3);
343 StoHelp::writeTypedString(content, tripId);
344 StoHelp::writeTypedString(content, foeSignal);
345 StoHelp::writeTypedString(content, foeId);
346 Dom::set(libsumo::TL_CONSTRAINT_REMOVE, tlsID, &content);
347}
348
349void
350TrafficLight::updateConstraints(const std::string& vehID, std::string tripId) {
352}
353
354std::string
355to_string(const std::vector<double>& value) {
356 std::ostringstream tmp;
357 for (double d : value) {
358 tmp << d << " ";
359 }
360 std::string tmp2 = tmp.str();
361 tmp2.pop_back();
362 return tmp2;
363}
364
365
366void
367TrafficLight::setNemaSplits(const std::string& tlsID, const std::vector<double>& splits) {
368 setParameter(tlsID, "NEMA.splits", to_string(splits));
369}
370
371void
372TrafficLight::setNemaMaxGreens(const std::string& tlsID, const std::vector<double>& maxGreens) {
373 setParameter(tlsID, "NEMA.maxGreens", to_string(maxGreens));
374}
375
376void
377TrafficLight::setNemaCycleLength(const std::string& tlsID, double cycleLength) {
378 setParameter(tlsID, "NEMA.cycleLength", std::to_string(cycleLength));
379}
380
381void
382TrafficLight::setNemaOffset(const std::string& tlsID, double offset) {
383 setParameter(tlsID, "NEMA.offset", std::to_string(offset));
384}
385
386
388
389}
390
391
392/****************************************************************************/
#define LIBTRACI_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: Domain.h:38
#define LIBTRACI_PARAMETER_IMPLEMENTATION(CLASS, DOMAIN)
Definition: Domain.h:77
#define TL(string)
Definition: MsgHandler.h:282
C++ TraCI client API implementation.
static int readTypedByte(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:57
static void writeTypedDouble(tcpip::Storage &content, double value)
static int readCompound(tcpip::Storage &ret, int expectedSize=-1, const std::string &error="")
Definition: StorageHelper.h:85
static std::vector< std::string > readTypedStringList(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:78
static int readTypedInt(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:50
static void writeCompound(tcpip::Storage &content, int size)
static std::string readTypedString(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:71
static void writeTypedInt(tcpip::Storage &content, int value)
static void writeTypedStringList(tcpip::Storage &content, const std::vector< std::string > &value)
static void writeTypedString(tcpip::Storage &content, const std::string &value)
static double readTypedDouble(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:64
std::map< std::string, std::string > subParameter
Definition: TraCIDefs.h:341
std::string programID
Definition: TraCIDefs.h:337
std::vector< std::shared_ptr< libsumo::TraCIPhase > > phases
Definition: TraCIDefs.h:340
std::vector< int > next
Definition: TraCIDefs.h:317
std::string state
Definition: TraCIDefs.h:315
std::string name
Definition: TraCIDefs.h:318
static void setDouble(int var, const std::string &id, double value)
Definition: Domain.h:220
static std::vector< std::string > getStringVector(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:171
static std::string getString(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:167
static int getInt(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:125
static void set(int var, const std::string &id, tcpip::Storage *add)
Definition: Domain.h:209
static double getDouble(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:129
static void setInt(int var, const std::string &id, int value)
Definition: Domain.h:213
static void setString(int var, const std::string &id, const std::string &value)
Definition: Domain.h:227
static tcpip::Storage & get(int var, const std::string &id, tcpip::Storage *add=nullptr, int expectedType=libsumo::TYPE_COMPOUND)
Definition: Domain.h:111
virtual void writeInt(int)
Definition: storage.cpp:321
virtual int readUnsignedByte()
Definition: storage.cpp:155
virtual void writeUnsignedByte(int)
Definition: storage.cpp:165
StorageType::size_type size() const
Definition: storage.h:119
virtual void writeByte(int)
Definition: storage.cpp:140
virtual int readInt()
Definition: storage.cpp:311
TRACI_CONST int VAR_NAME
TRACI_CONST int TRACI_ID_LIST
TRACI_CONST int TL_CONSTRAINT_REMOVE
TRACI_CONST int TL_BLOCKING_VEHICLES
TRACI_CONST int TL_CONSTRAINT_SWAP
TRACI_CONST int TL_PRIORITY_VEHICLES
TRACI_CONST int TL_CONTROLLED_LANES
TRACI_CONST int TYPE_COMPOUND
TRACI_CONST int TL_COMPLETE_DEFINITION_RYG
TRACI_CONST int VAR_PERSON_NUMBER
TRACI_CONST int TL_CONSTRAINT_UPDATE
TRACI_CONST int TL_CONTROLLED_JUNCTIONS
TRACI_CONST int TL_CONTROLLED_LINKS
TRACI_CONST int TYPE_INTEGER
TRACI_CONST int ID_COUNT
TRACI_CONST int TL_CONSTRAINT_BYFOE
TRACI_CONST int TL_CONSTRAINT
TRACI_CONST int TL_NEXT_SWITCH
TRACI_CONST int TL_PROGRAM
TRACI_CONST int TL_PHASE_DURATION
TRACI_CONST int TL_PHASE_INDEX
TRACI_CONST int TL_CURRENT_PHASE
TRACI_CONST int TL_COMPLETE_PROGRAM_RYG
TRACI_CONST int TL_RED_YELLOW_GREEN_STATE
TRACI_CONST int TL_CURRENT_PROGRAM
TRACI_CONST int TL_RIVAL_VEHICLES
Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE > Dom
std::string to_string(const std::vector< double > &value)
std::string foeId
the tripId or vehicle id of the train that must pass first
Definition: TraCIDefs.h:618
std::string tripId
the tripId or vehicle id of the train that is constrained
Definition: TraCIDefs.h:616
std::string foeSignal
the tlsID of the rail signla that the foe must pass first
Definition: TraCIDefs.h:620
std::string signalId
the idea of the rail signal where this constraint is active
Definition: TraCIDefs.h:614
std::map< std::string, std::string > param
additional parameters
Definition: TraCIDefs.h:630
bool active
whether this constraint is active
Definition: TraCIDefs.h:628
int type
the type of constraint (predecessor:0, insertionPredecessor:1)
Definition: TraCIDefs.h:624
bool mustWait
whether tripId must still wait for foeId to pass foeSignal
Definition: TraCIDefs.h:626
int limit
the number of trains that must be recorded at the foeSignal
Definition: TraCIDefs.h:622