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-2020 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>
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  std::vector<std::string> ids;
48  MSRoute::insertIDs(ids);
49  return ids;
50 }
51 
52 std::vector<std::string>
53 Route::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 
63 int
64 Route::getIDCount() {
65  return (int)getIDList().size();
66 }
67 
68 
69 std::string
70 Route::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 
79 void
80 Route::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 
86 void
87 Route::add(const std::string& routeID, const std::vector<std::string>& edgeIDs) {
88  ConstMSEdgeVector edges;
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  if (!MSRoute::dictionary(routeID, new MSRoute(routeID, edges, true, nullptr, stops))) {
101  throw TraCIException("Could not add route.");
102  }
103 }
104 
105 
107 
108 
109 const MSRoute*
110 Route::getRoute(const std::string& id) {
111  const MSRoute* r = MSRoute::dictionary(id);
112  if (r == nullptr) {
113  throw TraCIException("Route '" + id + "' is not known");
114  }
115  return r;
116 }
117 
118 
119 std::shared_ptr<VariableWrapper>
120 Route::makeWrapper() {
121  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
122 }
123 
124 
125 bool
126 Route::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper) {
127  switch (variable) {
128  case TRACI_ID_LIST:
129  return wrapper->wrapStringList(objID, variable, getIDList());
130  case ID_COUNT:
131  return wrapper->wrapInt(objID, variable, getIDCount());
132  case VAR_EDGES:
133  return wrapper->wrapStringList(objID, variable, getEdges(objID));
134  default:
135  return false;
136  }
137 }
138 }
139 
140 
141 /****************************************************************************/
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:74
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: TraCIDefs.h:57
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition: TraCIDefs.h:104
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:814
static bool dictionary(const std::string &id, const MSRoute *route)
Adds a route to the dictionary.
Definition: MSRoute.cpp:113
static void insertIDs(std::vector< std::string > &into)
Definition: MSRoute.cpp:211
const ConstMSEdgeVector & getEdges() const
Definition: MSRoute.h:120
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.
TRACI_CONST int VAR_EDGES
TRACI_CONST int TRACI_ID_LIST
std::map< std::string, TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:250
TRACI_CONST int ID_COUNT
std::map< std::string, SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:251