LCOV - code coverage report
Current view: top level - src/traci-server/lib - TraCI_Edge.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 152 169 89.9 %
Date: 2017-11-11 03:29:50 Functions: 30 33 90.9 %

          Line data    Source code
       1             : /****************************************************************************/
       2             : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
       3             : // Copyright (C) 2017-2017 German Aerospace Center (DLR) and others.
       4             : /****************************************************************************/
       5             : //
       6             : //   This program and the accompanying materials
       7             : //   are made available under the terms of the Eclipse Public License v2.0
       8             : //   which accompanies this distribution, and is available at
       9             : //   http://www.eclipse.org/legal/epl-v20.html
      10             : //
      11             : /****************************************************************************/
      12             : /// @file    TraCI_Edge.cpp
      13             : /// @author  Gregor Laemmel
      14             : /// @date    15.09.2017
      15             : /// @version $Id$
      16             : ///
      17             : // C++ TraCI client API implementation
      18             : /****************************************************************************/
      19             : 
      20             : 
      21             : #include <microsim/MSEdge.h>
      22             : #include <microsim/MSLane.h>
      23             : #include <microsim/MSEdgeWeightsStorage.h>
      24             : #include <microsim/MSTransportable.h>
      25             : #include <microsim/MSVehicle.h>
      26             : #include <traci-server/TraCIDefs.h>
      27             : #include <utils/emissions/HelpersHarmonoise.h>
      28             : #include "TraCI_Edge.h"
      29             : 
      30             : 
      31             : std::vector<std::string>
      32          16 : TraCI_Edge::getIDList() {
      33          16 :     std::vector<std::string> ids;
      34          16 :     MSEdge::insertIDs(ids);
      35          16 :     return ids;
      36             : }
      37             : 
      38             : int
      39           6 : TraCI_Edge::getIDCount() {
      40           6 :     return (int) getIDList().size();
      41             : }
      42             : 
      43          36 : double TraCI_Edge::retrieveExistingTravelTime(const std::string& id, double time) {
      44          36 :     const MSEdge* e = getEdge(id);
      45             :     double value;
      46          36 :     if (!MSNet::getInstance()->getWeightsStorage().retrieveExistingTravelTime(e, time, value)) {
      47          19 :         return -1.;
      48             :     }
      49          17 :     return value;
      50             : }
      51             : 
      52             : double
      53          24 : TraCI_Edge::retrieveExistingEffort(const std::string& id, double time) {
      54          24 :     const MSEdge* e = getEdge(id);
      55             :     double value;
      56          24 :     if (!MSNet::getInstance()->getWeightsStorage().retrieveExistingEffort(e, time, value)) {
      57           9 :         return -1.;
      58             :     }
      59          15 :     return value;
      60             : }
      61             : 
      62             : double
      63         137 : TraCI_Edge::getCurrentTravelTime(const std::string& id) {
      64         137 :     return getEdge(id)->getCurrentTravelTime();
      65             : }
      66             : 
      67             : MSEdge*
      68       29724 : TraCI_Edge::getEdge(const std::string& id) {
      69       29724 :     MSEdge* e = MSEdge::dictionary(id);
      70       29724 :     if (e == nullptr) {
      71           4 :         throw TraCIException("Edge '" + id + "' is not known");
      72             :     }
      73       29720 :     return e;
      74             : }
      75             : 
      76             : double
      77           4 : TraCI_Edge::getWaitingSeconds(const std::string& id) {
      78           4 :     double wtime = 0;
      79           4 :     const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
      80           8 :     for (auto lane : lanes) {
      81           4 :         wtime += lane->getWaitingSeconds();
      82             :     }
      83           4 :     return wtime;
      84             : }
      85             : 
      86             : const std::vector<std::string>
      87       17171 : TraCI_Edge::getPersonIDs(const std::string& id) {
      88       17171 :     std::vector<std::string> personIDs;
      89       34342 :     std::vector<MSTransportable*> persons = getEdge(id)->getSortedPersons(MSNet::getInstance()->getCurrentTimeStep(), true);
      90       17171 :     personIDs.reserve(persons.size());
      91       18950 :     for (MSTransportable* p : persons) {
      92        1779 :         personIDs.push_back(p->getID());
      93             :     }
      94       34342 :     return personIDs;
      95             : }
      96             : 
      97             : const std::vector<std::string>
      98          13 : TraCI_Edge::getVehicleIDs(const std::string& id) {
      99          13 :     std::vector<std::string> vehIDs;
     100          13 :     const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
     101          22 :     for (auto lane : lanes) {
     102          11 :         const MSLane::VehCont& vehs = lane->getVehiclesSecure();
     103          18 :         for (auto veh : vehs) {
     104           7 :             vehIDs.push_back(veh->getID());
     105             :         }
     106          11 :         lane->releaseVehicles();
     107             :     }
     108          11 :     return vehIDs;
     109             : }
     110             : 
     111             : 
     112             : double
     113           7 : TraCI_Edge::getCO2Emissions(const std::string& id) {
     114           7 :     double sum = 0;
     115           7 :     const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
     116          14 :     for (auto lane : lanes) {
     117           7 :         sum += lane->getCO2Emissions();
     118             :     }
     119           7 :     return sum;
     120             : }
     121             : 
     122             : double
     123           7 : TraCI_Edge::getCOEmissions(const std::string& id) {
     124           7 :     double sum = 0;
     125           7 :     const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
     126          14 :     for (auto lane : lanes) {
     127           7 :         sum += lane->getCOEmissions();
     128             :     }
     129           7 :     return sum;
     130             : }
     131             : 
     132             : double
     133           7 : TraCI_Edge::getHCEmissions(const std::string& id) {
     134           7 :     double sum = 0;
     135           7 :     const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
     136          14 :     for (auto lane : lanes) {
     137           7 :         sum += lane->getHCEmissions();
     138             :     }
     139           7 :     return sum;
     140             : }
     141             : 
     142             : double
     143           7 : TraCI_Edge::getPMxEmissions(const std::string& id) {
     144           7 :     double sum = 0;
     145           7 :     const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
     146          14 :     for (auto lane : lanes) {
     147           7 :         sum += lane->getPMxEmissions();
     148             :     }
     149           7 :     return sum;
     150             : }
     151             : 
     152             : double
     153           7 : TraCI_Edge::getNOxEmissions(const std::string& id) {
     154           7 :     double sum = 0;
     155           7 :     const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
     156          14 :     for (auto lane : lanes) {
     157           7 :         sum += lane->getNOxEmissions();
     158             :     }
     159           7 :     return sum;
     160             : }
     161             : 
     162             : double
     163           7 : TraCI_Edge::getFuelConsumption(const std::string& id) {
     164           7 :     double sum = 0;
     165           7 :     const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
     166          14 :     for (auto lane : lanes) {
     167           7 :         sum += lane->getFuelConsumption();
     168             :     }
     169           7 :     return sum;
     170             : }
     171             : 
     172             : double
     173           7 : TraCI_Edge::getNoiseEmissions(const std::string& id) {
     174           7 :     double sum = 0;
     175           7 :     const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
     176          14 :     for (auto lane : lanes) {
     177           7 :         sum += pow(10., (lane->getHarmonoise_NoiseEmissions() / 10.));
     178             :     }
     179           7 :     if (sum != 0) {
     180           7 :         return HelpersHarmonoise::sum(sum);
     181             :     }
     182           0 :     return sum;
     183             : }
     184             : 
     185             : double
     186           3 : TraCI_Edge::getElectricityConsumption(const std::string& id) {
     187           3 :     double sum = 0;
     188           3 :     const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
     189           6 :     for (auto lane : lanes) {
     190           3 :         sum += lane->getElectricityConsumption();
     191             :     }
     192           3 :     return sum;
     193             : }
     194             : 
     195             : int
     196          79 : TraCI_Edge::getVehicleNumber(const std::string& id) {
     197          79 :     int sum = 0;
     198          79 :     const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
     199         218 :     for (auto lane : lanes) {
     200         139 :         sum += lane->getVehicleNumber();
     201             :     }
     202          79 :     return sum;
     203             : }
     204             : 
     205             : double
     206          37 : TraCI_Edge::getMeanSpeed(const std::string& id) {
     207          37 :     return getEdge(id)->getMeanSpeed();
     208             : }
     209             : 
     210             : double
     211          10 : TraCI_Edge::getOccupancy(const std::string& id) {
     212          10 :     double sum = 0;
     213          10 :     const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
     214          20 :     for (auto lane : lanes) {
     215          10 :         sum += lane->getNettoOccupancy();
     216             :     }
     217          10 :     return sum / (double) lanes.size();
     218             : }
     219             : 
     220             : int
     221           7 : TraCI_Edge::getVehicleHaltingNumber(const std::string& id) {
     222           7 :     int halting = 0;
     223           7 :     const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
     224          14 :     for (auto lane : lanes) {
     225           7 :         const MSLane::VehCont& vehs = lane->getVehiclesSecure();
     226          12 :         for (auto veh : vehs) {
     227           5 :             if (veh->getSpeed() < SUMO_const_haltingSpeed) {
     228           0 :                 ++halting;
     229             :             }
     230             :         }
     231           7 :         lane->releaseVehicles();
     232             :     }
     233           7 :     return halting;
     234             : }
     235             : 
     236             : double
     237           7 : TraCI_Edge::getVehicleAverageLength(const std::string& id) {
     238           7 :     double lengthSum = 0;
     239           7 :     int noVehicles = 0;
     240           7 :     const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
     241          14 :     for (auto lane : lanes) {
     242           7 :         const MSLane::VehCont& vehs = lane->getVehiclesSecure();
     243          12 :         for (auto veh : vehs) {
     244           5 :             lengthSum += veh->getVehicleType().getLength();
     245             :         }
     246           7 :         noVehicles += (int) vehs.size();
     247           7 :         lane->releaseVehicles();
     248             :     }
     249           7 :     if (noVehicles == 0) {
     250           2 :         return 0;
     251             :     }
     252           5 :     return lengthSum / (double) noVehicles;
     253             : }
     254             : 
     255             : std::string
     256           8 : TraCI_Edge::getParameter(const std::string& id, const std::string& paramName) {
     257           8 :     return getEdge(id)->getParameter(paramName, "");
     258             : }
     259             : 
     260             : void
     261           0 : TraCI_Edge::setAllowedVehicleClasses(const std::string& id, std::vector<std::string> classes) {
     262           0 :     SVCPermissions permissions = parseVehicleClasses(classes);
     263           0 :     setAllowedSVCPermissions(id, permissions);
     264           0 : }
     265             : 
     266             : void
     267           0 : TraCI_Edge::setDisallowedVehicleClasses(const std::string& id, std::vector<std::string> classes) {
     268           0 :     SVCPermissions permissions = invertPermissions(parseVehicleClasses(classes));
     269           0 :     setAllowedSVCPermissions(id, permissions);
     270           0 : }
     271             : 
     272             : void
     273           0 : TraCI_Edge::setAllowedSVCPermissions(const std::string& id, SVCPermissions permissions) {
     274           0 :     MSEdge* e = getEdge(id);
     275           0 :     const std::vector<MSLane*>& lanes = e->getLanes();
     276           0 :     for (auto lane : lanes) {
     277           0 :         lane->setPermissions(permissions, MSLane::CHANGE_PERMISSIONS_PERMANENT);
     278             :     }
     279           0 :     e->rebuildAllowedLanes();
     280           0 : }
     281             : 
     282             : void
     283         100 : TraCI_Edge::addTravelTime(const std::string& id, double begTime, double endTime, double value) {
     284         100 :     MSNet::getInstance()->getWeightsStorage().addTravelTime(getEdge(id), begTime, endTime, value);
     285          99 : }
     286             : 
     287             : void
     288          10 : TraCI_Edge::addEffort(const std::string& id, double begTime, double endTime, double value) {
     289          10 :     MSNet::getInstance()->getWeightsStorage().addEffort(getEdge(id), begTime, endTime, value);
     290           9 : }
     291             : 
     292             : void
     293           4 : TraCI_Edge::setMaxSpeed(const std::string& id, double value) {
     294           4 :     const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
     295           8 :     for (auto lane : lanes) {
     296           4 :         lane->setMaxSpeed(value);
     297             :     }
     298           4 : }
     299             : 
     300             : void
     301           4 : TraCI_Edge::setParameter(const std::string& id, const std::string& name, const std::string& value) {
     302           4 :     getEdge(id)->setParameter(name, value);
     303           4 : }
     304             : 
     305             : void
     306       12021 : TraCI_Edge::getShape(const std::string& id, PositionVector& shape) {
     307       12021 :     const std::vector<MSLane*>& lanes = getEdge(id)->getLanes();
     308       12021 :     shape = lanes.front()->getShape();
     309       12021 :     if (lanes.size() > 1) {
     310           9 :         copy(lanes.back()->getShape().begin(), lanes.back()->getShape().end(), back_inserter(shape));
     311             :     }
     312       55575 : }
     313             : 
     314             : 
     315             : 

Generated by: LCOV version 1.12