Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
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-2024 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 MSNet::getInstance(); // just to check that we actually have a network
48 std::vector<std::string> ids;
50 return ids;
51}
52
53std::vector<std::string>
54Route::getEdges(const std::string& routeID) {
55 ConstMSRoutePtr r = getRoute(routeID);
56 std::vector<std::string> ids;
57 for (ConstMSEdgeVector::const_iterator i = r->getEdges().begin(); i != r->getEdges().end(); ++i) {
58 ids.push_back((*i)->getID());
59 }
60 return ids;
61}
62
63
64int
65Route::getIDCount() {
66 return (int)getIDList().size();
67}
68
69
70std::string
71Route::getParameter(const std::string& routeID, const std::string& param) {
72 ConstMSRoutePtr r = getRoute(routeID);
73 return r->getParameter(param, "");
74}
75
76
78
79
80void
81Route::setParameter(const std::string& routeID, const std::string& key, const std::string& value) {
82 MSRoute* r = const_cast<MSRoute*>(getRoute(routeID).get());
83 r->setParameter(key, value);
84}
85
86
87void
88Route::add(const std::string& routeID, const std::vector<std::string>& edgeIDs) {
90 if (edgeIDs.size() == 0) {
91 throw TraCIException("Cannot add route '" + routeID + "' without edges.");
92 }
93 for (std::vector<std::string>::const_iterator ei = edgeIDs.begin(); ei != edgeIDs.end(); ++ei) {
94 MSEdge* edge = MSEdge::dictionary(*ei);
95 if (edge == nullptr) {
96 throw TraCIException("Unknown edge '" + *ei + "' in route.");
97 }
98 edges.push_back(edge);
99 }
100 const std::vector<SUMOVehicleParameter::Stop> stops;
101 ConstMSRoutePtr route = std::make_shared<MSRoute>(routeID, edges, true, nullptr, stops);
102 if (!MSRoute::dictionary(routeID, route)) {
103 throw TraCIException("Could not add route '" + routeID + "'.");
104 }
105}
106
107
109
110
112Route::getRoute(const std::string& id) {
114 if (r == nullptr) {
115 throw TraCIException("Route '" + id + "' is not known");
116 }
117 return r;
118}
119
120
121std::shared_ptr<VariableWrapper>
122Route::makeWrapper() {
123 return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
124}
125
126
127bool
128Route::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
129 switch (variable) {
130 case TRACI_ID_LIST:
131 return wrapper->wrapStringList(objID, variable, getIDList());
132 case ID_COUNT:
133 return wrapper->wrapInt(objID, variable, getIDCount());
134 case VAR_EDGES:
135 return wrapper->wrapStringList(objID, variable, getEdges(objID));
137 paramData->readUnsignedByte();
138 return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
140 paramData->readUnsignedByte();
141 return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
142 default:
143 return false;
144 }
145}
146}
147
148
149/****************************************************************************/
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:1047
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:185
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