LCOV - code coverage report
Current view: top level - src/libsumo - Route.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 94.9 % 59 56
Test Date: 2025-05-18 15:30:03 Functions: 90.0 % 20 18

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

Generated by: LCOV version 2.0-1