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.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/****************************************************************************/
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 const MSRoute* 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 const MSRoute* 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));
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 MSRoute* route = new MSRoute(routeID, edges, true, nullptr, stops);
101 if (!MSRoute::dictionary(routeID, route)) {
102 delete route;
103 throw TraCIException("Could not add route '" + routeID + "'.");
104 }
105}
106
107
109
110
111const MSRoute*
112Route::getRoute(const std::string& id) {
113 const MSRoute* r = MSRoute::dictionary(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
#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
const ConstMSEdgeVector & getEdges() const
Definition: MSRoute.h:124
static bool dictionary(const std::string &id, const MSRoute *route)
Adds a route to the dictionary.
Definition: MSRoute.cpp:118
static void insertIDs(std::vector< std::string > &into)
Definition: MSRoute.cpp:216
virtual const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
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:301
std::map< std::string, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:300
TRACI_CONST int ID_COUNT
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int VAR_PARAMETER_WITH_KEY