1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2012-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
10 /// @file    TraCIDefs.h
11 /// @author  Daniel Krajzewicz
12 /// @author  Mario Krumnow
13 /// @author  Michael Behrisch
14 /// @author  Robert Hilbrich
15 /// @date    30.05.2012
16 /// @version $Id$
17 ///
18 // C++ TraCI client API implementation
19 /****************************************************************************/
20 #ifndef TraCIDefs_h
21 #define TraCIDefs_h
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 // we do not include config.h here, since we should be independent of a special sumo build
28 #include <libsumo/TraCIConstants.h>
29 #include <vector>
30 #include <limits>
31 #include <map>
32 #include <string>
33 #include <stdexcept>
34 #include <sstream>
35 #include <memory>
36 
37 
38 // ===========================================================================
39 // global definitions
40 // ===========================================================================
41 
42 #define LIBSUMO_SUBSCRIPTION_API \
43 static void subscribe(const std::string& objID, const std::vector<int>& vars = std::vector<int>(), double beginTime = libsumo::INVALID_DOUBLE_VALUE, double endTime = libsumo::INVALID_DOUBLE_VALUE); \
44 static void subscribeContext(const std::string& objID, int domain, double range, const std::vector<int>& vars = std::vector<int>(), double beginTime = libsumo::INVALID_DOUBLE_VALUE, double endTime = libsumo::INVALID_DOUBLE_VALUE); \
45 static const SubscriptionResults getAllSubscriptionResults(); \
46 static const TraCIResults getSubscriptionResults(const std::string& objID); \
47 static const ContextSubscriptionResults getAllContextSubscriptionResults(); \
48 static const SubscriptionResults getContextSubscriptionResults(const std::string& objID);
49 
50 #define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN) \
51 void \
52 CLASS::subscribe(const std::string& objID, const std::vector<int>& vars, double beginTime, double endTime) { \
53     libsumo::Helper::subscribe(CMD_SUBSCRIBE_##DOMAIN##_VARIABLE, objID, vars, beginTime, endTime); \
54 } \
55 void \
56 CLASS::subscribeContext(const std::string& objID, int domain, double range, const std::vector<int>& vars, double beginTime, double endTime) { \
57     libsumo::Helper::subscribe(CMD_SUBSCRIBE_##DOMAIN##_CONTEXT, objID, vars, beginTime, endTime, domain, range); \
58 } \
59 const SubscriptionResults \
60 CLASS::getAllSubscriptionResults() { \
61     return mySubscriptionResults; \
62 } \
63 const TraCIResults \
64 CLASS::getSubscriptionResults(const std::string& objID) { \
65     return mySubscriptionResults[objID]; \
66 } \
67 const ContextSubscriptionResults \
68 CLASS::getAllContextSubscriptionResults() { \
69     return myContextSubscriptionResults; \
70 } \
71 const SubscriptionResults \
72 CLASS::getContextSubscriptionResults(const std::string& objID) { \
73     return myContextSubscriptionResults[objID]; \
74 }
75 
76 
77 
78 // ===========================================================================
79 // class and type definitions
80 // ===========================================================================
81 namespace libsumo {
82 /**
83 * @class TraCIException
84 */
85 class TraCIException : public std::runtime_error {
86 public:
87     /** constructor */
TraCIException(std::string what)88     TraCIException(std::string what)
89         : std::runtime_error(what) {}
90 };
91 
92 /// @name Structures definitions
93 /// @{
94 
95 struct TraCIResult {
~TraCIResultTraCIResult96     virtual ~TraCIResult() {}
getStringTraCIResult97     virtual std::string getString() {
98         return "";
99     }
100 };
101 
102 /** @struct TraCIPosition
103     * @brief A 3D-position
104     */
105 struct TraCIPosition : TraCIResult {
getStringTraCIPosition106     std::string getString() {
107         std::ostringstream os;
108         os << "TraCIPosition(" << x << "," << y << "," << z << ")";
109         return os.str();
110     }
111     double x = INVALID_DOUBLE_VALUE, y = INVALID_DOUBLE_VALUE, z = INVALID_DOUBLE_VALUE;
112 };
113 
114 /** @struct TraCIRoadPosition
115     * @brief An edgeId, position and laneIndex
116     */
117 struct TraCIRoadPosition : TraCIResult {
getStringTraCIRoadPosition118     std::string getString() {
119         std::ostringstream os;
120         os << "TraCIRoadPosition(" << edgeID << "_" << laneIndex << "," << pos << ")";
121         return os.str();
122     }
123     std::string edgeID;
124     double pos;
125     int laneIndex = INVALID_INT_VALUE;
126 };
127 
128 /** @struct TraCIColor
129     * @brief A color
130     */
131 struct TraCIColor : TraCIResult {
TraCIColorTraCIColor132     TraCIColor() : r(0), g(0), b(0), a(255) {}
rTraCIColor133     TraCIColor(int r, int g, int b, int a = 255) : r(r), g(g), b(b), a(a) {}
getStringTraCIColor134     std::string getString() {
135         std::ostringstream os;
136         os << "TraCIColor(" << r << "," << g << "," << b << "," << a << ")";
137         return os.str();
138     }
139     int r, g, b, a;
140 };
141 
142 /** @struct TraCIPositionVector
143     * @brief A list of positions
144     */
145 typedef std::vector<TraCIPosition> TraCIPositionVector;
146 
147 
148 struct TraCIInt : TraCIResult {
TraCIIntTraCIInt149     TraCIInt() : value(0) {}
TraCIIntTraCIInt150     TraCIInt(int v) : value(v) {}
getStringTraCIInt151     std::string getString() {
152         std::ostringstream os;
153         os << value;
154         return os.str();
155     }
156     int value;
157 };
158 
159 
160 struct TraCIDouble : TraCIResult {
TraCIDoubleTraCIDouble161     TraCIDouble() : value(0.) {}
TraCIDoubleTraCIDouble162     TraCIDouble(double v) : value(v) {}
getStringTraCIDouble163     std::string getString() {
164         std::ostringstream os;
165         os << value;
166         return os.str();
167     }
168     double value;
169 };
170 
171 
172 struct TraCIString : TraCIResult {
TraCIStringTraCIString173     TraCIString() : value("") {}
TraCIStringTraCIString174     TraCIString(std::string v) : value(v) {}
getStringTraCIString175     std::string getString() {
176         return value;
177     }
178     std::string value;
179 };
180 
181 
182 struct TraCIStringList : TraCIResult {
getStringTraCIStringList183     std::string getString() {
184         std::ostringstream os;
185         os << "[";
186         for (std::string v : value) {
187             os << v << ",";
188         }
189         os << "]";
190         return os.str();
191     }
192     std::vector<std::string> value;
193 };
194 
195 
196 /// @brief {variable->value}
197 typedef std::map<int, std::shared_ptr<TraCIResult> > TraCIResults;
198 /// @brief {object->{variable->value}}
199 typedef std::map<std::string, TraCIResults> SubscriptionResults;
200 typedef std::map<std::string, SubscriptionResults> ContextSubscriptionResults;
201 
202 
203 class TraCIPhase {
204 public:
TraCIPhase()205     TraCIPhase() {}
206     TraCIPhase(const double _duration, const std::string& _state, const double _minDur = libsumo::INVALID_DOUBLE_VALUE,
207                const double _maxDur = libsumo::INVALID_DOUBLE_VALUE,
208                const std::vector<int>& _next = std::vector<int>(),
209                const std::string& _name = "") :
duration(_duration)210         duration(_duration), state(_state), minDur(_minDur), maxDur(_maxDur), next(_next), name(_name) {}
~TraCIPhase()211     ~TraCIPhase() {}
212 
213     double duration;
214     std::string state;
215     double minDur, maxDur;
216     std::vector<int> next;
217     std::string name;
218 };
219 }
220 
221 
222 #ifdef SWIG
223 %template(TraCIPhaseVector) std::vector<libsumo::TraCIPhase>;
224 #endif
225 
226 
227 namespace libsumo {
228 class TraCILogic {
229 public:
TraCILogic()230     TraCILogic() {}
TraCILogic(const std::string & _programID,const int _type,const int _currentPhaseIndex)231     TraCILogic(const std::string& _programID, const int _type, const int _currentPhaseIndex)
232         : programID(_programID), type(_type), currentPhaseIndex(_currentPhaseIndex) {}
~TraCILogic()233     ~TraCILogic() {}
234 
235     std::string programID;
236     int type;
237     int currentPhaseIndex;
238     std::vector<TraCIPhase> phases;
239     std::map<std::string, std::string> subParameter;
240 };
241 
242 
243 class TraCILink {
244 public:
TraCILink(const std::string & _from,const std::string & _via,const std::string & _to)245     TraCILink(const std::string& _from, const std::string& _via, const std::string& _to)
246         : fromLane(_from), viaLane(_via), toLane(_to) {}
~TraCILink()247     ~TraCILink() {}
248 
249     std::string fromLane;
250     std::string viaLane;
251     std::string toLane;
252 };
253 
254 
255 class TraCIConnection {
256 public:
TraCIConnection()257     TraCIConnection() {} // this is needed by SWIG when building a vector of this type, please don't use it
TraCIConnection(const std::string & _approachedLane,const bool _hasPrio,const bool _isOpen,const bool _hasFoe,const std::string _approachedInternal,const std::string _state,const std::string _direction,const double _length)258     TraCIConnection(const std::string& _approachedLane, const bool _hasPrio, const bool _isOpen, const bool _hasFoe,
259                     const std::string _approachedInternal, const std::string _state, const std::string _direction, const double _length)
260         : approachedLane(_approachedLane), hasPrio(_hasPrio), isOpen(_isOpen), hasFoe(_hasFoe),
261           approachedInternal(_approachedInternal), state(_state), direction(_direction), length(_length) {}
~TraCIConnection()262     ~TraCIConnection() {}
263 
264     std::string approachedLane;
265     bool hasPrio;
266     bool isOpen;
267     bool hasFoe;
268     std::string approachedInternal;
269     std::string state;
270     std::string direction;
271     double length;
272 };
273 
274 
275 /// @brief mirrors MSInductLoop::VehicleData
276 struct TraCIVehicleData {
277     /// @brief The id of the vehicle
278     std::string id;
279     /// @brief Length of the vehicle
280     double length;
281     /// @brief Entry-time of the vehicle in [s]
282     double entryTime;
283     /// @brief Leave-time of the vehicle in [s]
284     double leaveTime;
285     /// @brief Type of the vehicle in
286     std::string typeID;
287 };
288 
289 
290 struct TraCINextTLSData {
291     /// @brief The id of the next tls
292     std::string id;
293     /// @brief The tls index of the controlled link
294     int tlIndex;
295     /// @brief The distance to the tls
296     double dist;
297     /// @brief The current state of the tls
298     char state;
299 };
300 
301 
302 struct TraCINextStopData {
303     /// @brief The lane to stop at
304     std::string lane;
305     /// @brief The stopping position end
306     double endPos;
307     /// @brief Id assigned to the stop
308     std::string stoppingPlaceID;
309     /// @brief Stop flags
310     int stopFlags;
311     /// @brief The stopping duration
312     double duration;
313     /// @brief The time at which the vehicle may continue its journey
314     double until;
315 };
316 
317 
318 struct TraCIBestLanesData {
319     /// @brief The id of the lane
320     std::string laneID;
321     /// @brief The length than can be driven from that lane without lane change
322     double length;
323     /// @brief The traffic density along length
324     double occupation;
325     /// @brief The offset of this lane from the best lane
326     int bestLaneOffset;
327     /// @brief Whether this lane allows continuing the route
328     bool allowsContinuation;
329     /// @brief The sequence of lanes that best allows continuing the route without lane change
330     std::vector<std::string> continuationLanes;
331 };
332 
333 
334 class TraCIStage {
335 public:
TraCIStage()336     TraCIStage() {} // only to make swig happy
TraCIStage(int _type)337     TraCIStage(int _type) : type(_type) {}
338     /// @brief The type of stage (walking, driving, ...)
339     int type;
340     /// @brief The vehicle type when using a private car or bike
341     std::string vType;
342     /// @brief The line or the id of the vehicle type
343     std::string line;
344     /// @brief The id of the destination stop
345     std::string destStop;
346     /// @brief The sequence of edges to travel
347     std::vector<std::string> edges;
348     /// @brief duration of the stage in seconds
349     double travelTime;
350     /// @brief effort needed
351     double cost;
352     /// @brief length in m
353     double length = INVALID_DOUBLE_VALUE;
354     /// @brief id of the intended vehicle for public transport ride
355     std::string intended = "";
356     /// @brief intended depart time for public transport ride or INVALID_DOUBLE_VALUE
357     double depart = INVALID_DOUBLE_VALUE;
358     /// @brief position on the lane when starting the stage
359     double departPos = INVALID_DOUBLE_VALUE;
360     /// @brief position on the lane when ending the stage
361     double arrivalPos = INVALID_DOUBLE_VALUE;
362     /// @brief arbitrary description string
363     std::string description = "";
364 };
365 }
366 
367 
368 #endif
369 
370 /****************************************************************************/
371