Eclipse SUMO - Simulation of Urban MObility
libsumo/Route.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-2023 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/****************************************************************************/
22// C++ TraCI client API implementation
23/****************************************************************************/
24#include <config.h>
25
26#include <microsim/MSNet.h>
27#include <microsim/MSEdge.h>
28#include <microsim/MSRoute.h>
30#include "Helper.h"
31#include "Route.h"
32
33
34namespace libsumo {
35// ===========================================================================
36// static member initializations
37// ===========================================================================
38SubscriptionResults Route::mySubscriptionResults;
39ContextSubscriptionResults Route::myContextSubscriptionResults;
40
41
42// ===========================================================================
43// static member definitions
44// ===========================================================================
45std::vector<std::string>
46Route::getIDList() {
47 std::vector<std::string> ids;
49 return ids;
50}
51
52std::vector<std::string>
53Route::getEdges(const std::string& routeID) {
54 ConstMSRoutePtr r = getRoute(routeID);
55 std::vector<std::string> ids;
56 for (ConstMSEdgeVector::const_iterator i = r->getEdges().begin(); i != r->getEdges().end(); ++i) {
57 ids.push_back((*i)->getID());
58 }
59 return ids;
60}
61
62
63int
64Route::getIDCount() {
65 return (int)getIDList().size();
66}
67
68
69std::string
70Route::getParameter(const std::string& routeID, const std::string& param) {
71 ConstMSRoutePtr r = getRoute(routeID);
72 return r->getParameter(param, "");
73}
74
75
77
78
79void
80Route::setParameter(const std::string& routeID, const std::string& key, const std::string& value) {
81 MSRoute* r = const_cast<MSRoute*>(getRoute(routeID).get());
82 r->setParameter(key, value);
83}
84
85
86void
87Route::add(const std::string& routeID, const std::vector<std::string>& edgeIDs) {
89 if (edgeIDs.size() == 0) {
90 throw TraCIException("Cannot add route '" + routeID + "' without edges.");
91 }
92 for (std::vector<std::string>::const_iterator ei = edgeIDs.begin(); ei != edgeIDs.end(); ++ei) {
93 MSEdge* edge = MSEdge::dictionary(*ei);
94 if (edge == nullptr) {
95 throw TraCIException("Unknown edge '" + *ei + "' in route.");
96 }
97 edges.push_back(edge);
98 }
99 const std::vector<SUMOVehicleParameter::Stop> stops;
100 ConstMSRoutePtr route = std::make_shared<MSRoute>(routeID, edges, true, nullptr, stops);
101 if (!MSRoute::dictionary(routeID, route)) {
102 throw TraCIException("Could not add route '" + routeID + "'.");
103 }
104}
105
106
108
109
111Route::getRoute(const std::string& id) {
113 if (r == nullptr) {
114 throw TraCIException("Route '" + id + "' is not known");
115 }
116 return r;
117}
118
119
120std::shared_ptr<VariableWrapper>
121Route::makeWrapper() {
122 return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
123}
124
125
126bool
127Route::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
128 switch (variable) {
129 case TRACI_ID_LIST:
130 return wrapper->wrapStringList(objID, variable, getIDList());
131 case ID_COUNT:
132 return wrapper->wrapInt(objID, variable, getIDCount());
133 case VAR_EDGES:
134 return wrapper->wrapStringList(objID, variable, getEdges(objID));
136 paramData->readUnsignedByte();
137 return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
139 paramData->readUnsignedByte();
140 return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
141 default:
142 return false;
143 }
144}
145}
146
147
148/****************************************************************************/
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:74
std::shared_ptr< const MSRoute > ConstMSRoutePtr
Definition: Route.h:32
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOM)
Definition: TraCIDefs.h:76
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition: TraCIDefs.h:123
A road/street connecting two junctions.
Definition: MSEdge.h:77
static bool dictionary(const std::string &id, MSEdge *edge)
Inserts edge into the static dictionary Returns true if the key id isn't already in the dictionary....
Definition: MSEdge.cpp:945
static void insertIDs(std::vector< std::string > &into)
Definition: MSRoute.cpp:204
static bool dictionary(const std::string &id, ConstMSRoutePtr route)
Adds a route to the dictionary.
Definition: MSRoute.cpp:109
virtual void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
C++ TraCI client API implementation.
virtual std::string readString()
Definition: storage.cpp:180
virtual int readUnsignedByte()
Definition: storage.cpp:155
TRACI_CONST int VAR_EDGES
TRACI_CONST int TRACI_ID_LIST
std::map< std::string, libsumo::SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:338
std::map< std::string, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:337
TRACI_CONST int ID_COUNT
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int VAR_PARAMETER_WITH_KEY