Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/libsumo/Route.cpp
169665 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2017-2025 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
/****************************************************************************/
14
/// @file Route.cpp
15
/// @author Daniel Krajzewicz
16
/// @author Mario Krumnow
17
/// @author Jakob Erdmann
18
/// @author Michael Behrisch
19
/// @author Robert Hilbrich
20
/// @date 30.05.2012
21
///
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>
29
#include <libsumo/TraCIConstants.h>
30
#include "Helper.h"
31
#include "Route.h"
32
33
34
namespace libsumo {
35
// ===========================================================================
36
// static member initializations
37
// ===========================================================================
38
SubscriptionResults Route::mySubscriptionResults;
39
ContextSubscriptionResults Route::myContextSubscriptionResults;
40
41
42
// ===========================================================================
43
// static member definitions
44
// ===========================================================================
45
std::vector<std::string>
46
Route::getIDList() {
47
MSNet::getInstance(); // just to check that we actually have a network
48
std::vector<std::string> ids;
49
MSRoute::insertIDs(ids);
50
return ids;
51
}
52
53
std::vector<std::string>
54
Route::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
64
int
65
Route::getIDCount() {
66
return (int)getIDList().size();
67
}
68
69
70
std::string
71
Route::getParameter(const std::string& routeID, const std::string& param) {
72
ConstMSRoutePtr r = getRoute(routeID);
73
return r->getParameter(param, "");
74
}
75
76
77
LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(Route)
78
79
80
void
81
Route::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
87
void
88
Route::add(const std::string& routeID, const std::vector<std::string>& edgeIDs) {
89
ConstMSEdgeVector edges;
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 StopParVector 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
108
void
109
Route::remove(const std::string& routeID) {
110
MSRoute* r = const_cast<MSRoute*>(getRoute(routeID).get());
111
r->checkRemoval(true);
112
}
113
114
115
LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(Route, ROUTE)
116
117
118
ConstMSRoutePtr
119
Route::getRoute(const std::string& id) {
120
ConstMSRoutePtr r = MSRoute::dictionary(id);
121
if (r == nullptr) {
122
throw TraCIException("Route '" + id + "' is not known");
123
}
124
return r;
125
}
126
127
128
std::shared_ptr<VariableWrapper>
129
Route::makeWrapper() {
130
return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
131
}
132
133
134
bool
135
Route::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
136
switch (variable) {
137
case TRACI_ID_LIST:
138
return wrapper->wrapStringList(objID, variable, getIDList());
139
case ID_COUNT:
140
return wrapper->wrapInt(objID, variable, getIDCount());
141
case VAR_EDGES:
142
return wrapper->wrapStringList(objID, variable, getEdges(objID));
143
case libsumo::VAR_PARAMETER:
144
paramData->readUnsignedByte();
145
return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
146
case libsumo::VAR_PARAMETER_WITH_KEY:
147
paramData->readUnsignedByte();
148
return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
149
default:
150
return false;
151
}
152
}
153
}
154
155
156
/****************************************************************************/
157
158