Line data Source code
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 : /****************************************************************************/ 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 209 : Route::getIDList() { 47 209 : MSNet::getInstance(); // just to check that we actually have a network 48 : std::vector<std::string> ids; 49 207 : MSRoute::insertIDs(ids); 50 207 : return ids; 51 0 : } 52 : 53 : std::vector<std::string> 54 34 : Route::getEdges(const std::string& routeID) { 55 34 : ConstMSRoutePtr r = getRoute(routeID); 56 : std::vector<std::string> ids; 57 196 : for (ConstMSEdgeVector::const_iterator i = r->getEdges().begin(); i != r->getEdges().end(); ++i) { 58 164 : ids.push_back((*i)->getID()); 59 : } 60 32 : return ids; 61 0 : } 62 : 63 : 64 : int 65 14 : Route::getIDCount() { 66 14 : return (int)getIDList().size(); 67 : } 68 : 69 : 70 : std::string 71 110 : Route::getParameter(const std::string& routeID, const std::string& param) { 72 110 : ConstMSRoutePtr r = getRoute(routeID); 73 330 : return r->getParameter(param, ""); 74 : } 75 : 76 : 77 56 : LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(Route) 78 : 79 : 80 : void 81 67 : Route::setParameter(const std::string& routeID, const std::string& key, const std::string& value) { 82 67 : MSRoute* r = const_cast<MSRoute*>(getRoute(routeID).get()); 83 67 : r->setParameter(key, value); 84 67 : } 85 : 86 : 87 : void 88 251 : Route::add(const std::string& routeID, const std::vector<std::string>& edgeIDs) { 89 : ConstMSEdgeVector edges; 90 251 : if (edgeIDs.size() == 0) { 91 12 : throw TraCIException("Cannot add route '" + routeID + "' without edges."); 92 : } 93 602 : for (std::vector<std::string>::const_iterator ei = edgeIDs.begin(); ei != edgeIDs.end(); ++ei) { 94 357 : MSEdge* edge = MSEdge::dictionary(*ei); 95 357 : if (edge == nullptr) { 96 0 : throw TraCIException("Unknown edge '" + *ei + "' in route."); 97 : } 98 357 : edges.push_back(edge); 99 : } 100 : const std::vector<SUMOVehicleParameter::Stop> stops; 101 245 : ConstMSRoutePtr route = std::make_shared<MSRoute>(routeID, edges, true, nullptr, stops); 102 496 : if (!MSRoute::dictionary(routeID, route)) { 103 12 : throw TraCIException("Could not add route '" + routeID + "'."); 104 : } 105 484 : } 106 : 107 : 108 172 : LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(Route, ROUTE) 109 : 110 : 111 : ConstMSRoutePtr 112 211 : Route::getRoute(const std::string& id) { 113 211 : ConstMSRoutePtr r = MSRoute::dictionary(id); 114 211 : if (r == nullptr) { 115 4 : throw TraCIException("Route '" + id + "' is not known"); 116 : } 117 209 : return r; 118 : } 119 : 120 : 121 : std::shared_ptr<VariableWrapper> 122 267 : Route::makeWrapper() { 123 267 : return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults); 124 : } 125 : 126 : 127 : bool 128 275 : Route::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) { 129 275 : switch (variable) { 130 139 : case TRACI_ID_LIST: 131 139 : return wrapper->wrapStringList(objID, variable, getIDList()); 132 12 : case ID_COUNT: 133 12 : return wrapper->wrapInt(objID, variable, getIDCount()); 134 28 : case VAR_EDGES: 135 28 : return wrapper->wrapStringList(objID, variable, getEdges(objID)); 136 62 : case libsumo::VAR_PARAMETER: 137 62 : paramData->readUnsignedByte(); 138 124 : return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString())); 139 24 : case libsumo::VAR_PARAMETER_WITH_KEY: 140 24 : paramData->readUnsignedByte(); 141 24 : return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString())); 142 : default: 143 : return false; 144 : } 145 : } 146 : } 147 : 148 : 149 : /****************************************************************************/