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
26 #include <libsumo/StorageHelper.h>
27 #include <libsumo/TraCIConstants.h>
28 #include <libsumo/TrafficLight.h>
29 #include "Domain.h"
30 
31 // TODO remove the following line once the implementation is mature
32 #ifdef _MSC_VER
33 #pragma warning(disable: 4100)
34 #else
35 #pragma GCC diagnostic ignored "-Wunused-parameter"
36 #endif
37 
38 namespace libtraci {
39 
40 typedef Domain<libsumo::CMD_GET_TL_VARIABLE, libsumo::CMD_SET_TL_VARIABLE> Dom;
41 
42 // ===========================================================================
43 // static member definitions
44 // ===========================================================================
45 std::vector<std::string>
46 TrafficLight::getIDList() {
48 }
49 
50 
51 int
52 TrafficLight::getIDCount() {
53  return Dom::getInt(libsumo::ID_COUNT, "");
54 }
55 
56 
57 std::string
58 TrafficLight::getRedYellowGreenState(const std::string& tlsID) {
60 }
61 
62 
63 std::vector<libsumo::TraCILogic>
64 TrafficLight::getAllProgramLogics(const std::string& tlsID) {
66  std::vector<libsumo::TraCILogic> result;
67  int numLogics = ret.readInt();
68  while (numLogics-- > 0) {
69  StoHelp::readCompound(ret, 5);
70  libsumo::TraCILogic logic;
72  logic.type = StoHelp::readTypedInt(ret);
74  int numPhases = StoHelp::readCompound(ret);
75  while (numPhases-- > 0) {
76  StoHelp::readCompound(ret, 6);
78  phase->duration = StoHelp::readTypedDouble(ret);
79  phase->state = StoHelp::readTypedString(ret);
80  phase->minDur = StoHelp::readTypedDouble(ret);
81  phase->maxDur = StoHelp::readTypedDouble(ret);
82  int numNext = StoHelp::readCompound(ret);
83  while (numNext-- > 0) {
84  phase->next.push_back(StoHelp::readTypedInt(ret));
85  }
86  phase->name = StoHelp::readTypedString(ret);
87  logic.phases.emplace_back(phase);
88  }
89  int numParams = StoHelp::readCompound(ret);
90  while (numParams-- > 0) {
91  const std::vector<std::string> key_value = StoHelp::readTypedStringList(ret);
92  logic.subParameter[key_value[0]] = key_value[1];
93  }
94  result.emplace_back(logic);
95  }
96  return result;
97 }
98 
99 
100 std::vector<std::string>
101 TrafficLight::getControlledJunctions(const std::string& tlsID) {
103 }
104 
105 
106 std::vector<std::string>
107 TrafficLight::getControlledLanes(const std::string& tlsID) {
109 }
110 
111 
112 std::vector<std::vector<libsumo::TraCILink> >
113 TrafficLight::getControlledLinks(const std::string& tlsID) {
115  std::vector< std::vector<libsumo::TraCILink> > result;
116  ret.readInt();
117  int numSignals = StoHelp::readTypedInt(ret);
118  while (numSignals-- > 0) {
119  std::vector<libsumo::TraCILink> controlledLinks;
120  int numLinks = StoHelp::readTypedInt(ret);
121  while (numLinks-- > 0) {
122  std::vector<std::string> link = StoHelp::readTypedStringList(ret);
123  controlledLinks.emplace_back(link[0], link[2], link[1]);
124  }
125  result.emplace_back(controlledLinks);
126  }
127  return result;
128 }
129 
130 
131 std::string
132 TrafficLight::getProgram(const std::string& tlsID) {
134 }
135 
136 
137 int
138 TrafficLight::getPhase(const std::string& tlsID) {
139  return Dom::getInt(libsumo::TL_CURRENT_PHASE, tlsID);
140 }
141 
142 
143 std::string
144 TrafficLight::getPhaseName(const std::string& tlsID) {
145  return Dom::getString(libsumo::VAR_NAME, tlsID);
146 }
147 
148 
149 double
150 TrafficLight::getPhaseDuration(const std::string& tlsID) {
152 }
153 
154 
155 double
156 TrafficLight::getNextSwitch(const std::string& tlsID) {
158 }
159 
160 int
161 TrafficLight::getServedPersonCount(const std::string& tlsID, int index) {
162  tcpip::Storage content;
164  content.writeInt(index);
165  return Dom::getInt(libsumo::VAR_PERSON_NUMBER, tlsID, &content);
166 }
167 
168 std::vector<std::string>
169 TrafficLight::getBlockingVehicles(const std::string& tlsID, int linkIndex) {
170  tcpip::Storage content;
172  content.writeInt(linkIndex);
173  return Dom::getStringVector(libsumo::TL_BLOCKING_VEHICLES, tlsID, &content);
174 }
175 
176 std::vector<std::string>
177 TrafficLight::getRivalVehicles(const std::string& tlsID, int linkIndex) {
178  tcpip::Storage content;
180  content.writeInt(linkIndex);
181  return Dom::getStringVector(libsumo::TL_RIVAL_VEHICLES, tlsID, &content);
182 }
183 
184 std::vector<std::string>
185 TrafficLight::getPriorityVehicles(const std::string& tlsID, int linkIndex) {
186  tcpip::Storage content;
188  content.writeInt(linkIndex);
189  return Dom::getStringVector(libsumo::TL_PRIORITY_VEHICLES, tlsID, &content);
190 }
191 
192 std::vector<libsumo::TraCISignalConstraint>
193 TrafficLight::getConstraints(const std::string& tlsID, const std::string& tripId) {
194  std::vector<libsumo::TraCISignalConstraint> result;
195  tcpip::Storage content;
196  StoHelp::writeTypedString(content, tripId);
197  tcpip::Storage& ret = Dom::get(libsumo::TL_CONSTRAINT, tlsID, &content);
198  ret.readInt(); // components
199  // number of items
200  ret.readUnsignedByte();
201  const int n = ret.readInt();
202  for (int i = 0; i < n; ++i) {
208  c.limit = StoHelp::readTypedInt(ret);
209  c.type = StoHelp::readTypedInt(ret);
210  c.mustWait = StoHelp::readTypedByte(ret) != 0;
211  result.push_back(c);
212  }
213  return result;
214 }
215 
216 std::vector<libsumo::TraCISignalConstraint>
217 TrafficLight::getConstraintsByFoe(const std::string& foeSignal, const std::string& foeId) {
218  std::vector<libsumo::TraCISignalConstraint> result;
219  tcpip::Storage content;
220  StoHelp::writeTypedString(content, foeId);
221  tcpip::Storage& ret = Dom::get(libsumo::TL_CONSTRAINT_BYFOE, foeSignal, &content);
222  ret.readInt(); // components
223  // number of items
224  ret.readUnsignedByte();
225  const int n = ret.readInt();
226  for (int i = 0; i < n; ++i) {
232  c.limit = StoHelp::readTypedInt(ret);
233  c.type = StoHelp::readTypedInt(ret);
234  c.mustWait = StoHelp::readTypedByte(ret) != 0;
235  result.push_back(c);
236  }
237  return result;
238 }
239 
241 
242 void
243 TrafficLight::setRedYellowGreenState(const std::string& tlsID, const std::string& state) {
245 }
246 
247 
248 void
249 TrafficLight::setPhase(const std::string& tlsID, const int index) {
250  Dom::setInt(libsumo::TL_PHASE_INDEX, tlsID, index);
251 }
252 
253 
254 void
255 TrafficLight::setPhaseName(const std::string& tlsID, const std::string& name) {
256  Dom::setString(libsumo::VAR_NAME, tlsID, name);
257 }
258 
259 
260 void
261 TrafficLight::setProgram(const std::string& tlsID, const std::string& programID) {
262  Dom::setString(libsumo::TL_PROGRAM, tlsID, programID);
263 }
264 
265 
266 void
267 TrafficLight::setPhaseDuration(const std::string& tlsID, const double phaseDuration) {
268  Dom::setDouble(libsumo::TL_PHASE_DURATION, tlsID, phaseDuration);
269 }
270 
271 
272 void
273 TrafficLight::setProgramLogic(const std::string& tlsID, const libsumo::TraCILogic& logic) {
274  tcpip::Storage content;
275  StoHelp::writeCompound(content, 5);
276  StoHelp::writeTypedString(content, logic.programID);
277  StoHelp::writeTypedInt(content, logic.type);
279  StoHelp::writeCompound(content, (int)logic.phases.size());
280  for (const std::shared_ptr<libsumo::TraCIPhase>& phase : logic.phases) {
281  StoHelp::writeCompound(content, 6);
282  StoHelp::writeTypedDouble(content, phase->duration);
283  StoHelp::writeTypedString(content, phase->state);
284  StoHelp::writeTypedDouble(content, phase->minDur);
285  StoHelp::writeTypedDouble(content, phase->maxDur);
286  StoHelp::writeCompound(content, (int)phase->next.size());
287  for (int n : phase->next) {
288  StoHelp::writeTypedInt(content, n);
289  }
290  StoHelp::writeTypedString(content, phase->name);
291  }
292  StoHelp::writeCompound(content, (int)logic.subParameter.size());
293  for (const auto& key_value : logic.subParameter) {
294  StoHelp::writeTypedStringList(content, std::vector<std::string> {key_value.first, key_value.second});
295  }
296  Dom::set(libsumo::TL_COMPLETE_PROGRAM_RYG, tlsID, &content);
297 }
298 
299 
300 std::vector<libsumo::TraCISignalConstraint>
301 TrafficLight::swapConstraints(const std::string& tlsID, const std::string& tripId, const std::string& foeSignal, const std::string& foeId) {
302  std::vector<libsumo::TraCISignalConstraint> result;
303  tcpip::Storage content;
305  content.writeInt(3);
306  StoHelp::writeTypedString(content, tripId);
307  StoHelp::writeTypedString(content, foeSignal);
308  StoHelp::writeTypedString(content, foeId);
309  tcpip::Storage& ret = Dom::get(libsumo::TL_CONSTRAINT_SWAP, tlsID, &content);
310  ret.readInt(); // components
311  // number of items
312  ret.readUnsignedByte();
313  const int n = ret.readInt();
314  for (int i = 0; i < n; ++i) {
320  c.limit = StoHelp::readTypedInt(ret);
321  c.type = StoHelp::readTypedInt(ret);
322  c.mustWait = StoHelp::readTypedByte(ret) != 0;
323  result.push_back(c);
324  }
325  return result;
326 }
327 
328 
329 void
330 TrafficLight::removeConstraints(const std::string& tlsID, const std::string& tripId, const std::string& foeSignal, const std::string& foeId) {
331  tcpip::Storage content;
333  content.writeInt(3);
334  StoHelp::writeTypedString(content, tripId);
335  StoHelp::writeTypedString(content, foeSignal);
336  StoHelp::writeTypedString(content, foeId);
337  Dom::set(libsumo::TL_CONSTRAINT_REMOVE, tlsID, &content);
338 }
339 
340 std::string
341 to_string(const std::vector<double>& value) {
342  std::ostringstream tmp;
343  for (double d : value) {
344  tmp << d << " ";
345  }
346  std::string tmp2 = tmp.str();
347  tmp2.pop_back();
348  return tmp2;
349 }
350 
351 
352 void
353 TrafficLight::setNemaSplits(const std::string& tlsID, const std::vector<double>& splits) {
354  setParameter(tlsID, "NEMA.splits", to_string(splits));
355 }
356 
357 void
358 TrafficLight::setNemaMaxGreens(const std::string& tlsID, const std::vector<double>& maxGreens) {
359  setParameter(tlsID, "NEMA.maxGreens", to_string(maxGreens));
360 }
361 
362 void
363 TrafficLight::setNemaCycleLength(const std::string& tlsID, double cycleLength) {
364  setParameter(tlsID, "NEMA.cycleLength", std::to_string(cycleLength));
365 }
366 
367 void
368 TrafficLight::setNemaOffset(const std::string& tlsID, double offset) {
369  setParameter(tlsID, "NEMA.offset", std::to_string(offset));
370 }
371 
372 
374 
375 }
376 
377 
378 /****************************************************************************/
#define LIBTRACI_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: Domain.h:38
#define LIBTRACI_PARAMETER_IMPLEMENTATION(CLASS, DOMAIN)
Definition: Domain.h:76
C++ TraCI client API implementation.
Definition: TrafficLight.h:32
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 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 std::vector< std::string > readTypedStringList(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:78
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:319
std::string programID
Definition: TraCIDefs.h:315
std::vector< std::shared_ptr< libsumo::TraCIPhase > > phases
Definition: TraCIDefs.h:318
std::vector< int > next
Definition: TraCIDefs.h:295
std::string state
Definition: TraCIDefs.h:293
std::string name
Definition: TraCIDefs.h:296
static void setDouble(int var, const std::string &id, double value)
Definition: Domain.h:215
static std::vector< std::string > getStringVector(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:170
static std::string getString(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:166
static int getInt(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:124
static void set(int var, const std::string &id, tcpip::Storage *add)
Definition: Domain.h:204
static double getDouble(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:128
static tcpip::Storage & get(int var, const std::string &id, tcpip::Storage *add=nullptr, int expectedType=libsumo::TYPE_COMPOUND)
Definition: Domain.h:110
static void setInt(int var, const std::string &id, int value)
Definition: Domain.h:208
static void setString(int var, const std::string &id, const std::string &value)
Definition: Domain.h:222
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_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:596
std::string tripId
the tripId or vehicle id of the train that is constrained
Definition: TraCIDefs.h:594
std::string foeSignal
the tlsID of the rail signla that the foe must pass first
Definition: TraCIDefs.h:598
std::string signalId
the idea of the rail signal where this constraint is active
Definition: TraCIDefs.h:592
int type
the type of constraint (predecessor:0, insertionPredecessor:1)
Definition: TraCIDefs.h:602
bool mustWait
whether tripId must still wait for foeId to pass foeSignal
Definition: TraCIDefs.h:604
int limit
the number of trains that must be recorded at the foeSignal
Definition: TraCIDefs.h:600