LCOV - code coverage report
Current view: top level - src/libsumo - Calibrator.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 96 106 90.6 %
Date: 2024-04-28 15:39:05 Functions: 24 31 77.4 %

          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    Calibrator.cpp
      15             : /// @author  Jakob Erdmann
      16             : /// @date    16.03.2020
      17             : ///
      18             : // C++ TraCI client API implementation
      19             : /****************************************************************************/
      20             : #include <config.h>
      21             : 
      22             : #include <microsim/MSNet.h>
      23             : #include <microsim/MSEdge.h>
      24             : #include <microsim/MSLane.h>
      25             : #include <microsim/output/MSRouteProbe.h>
      26             : #include <microsim/trigger/MSCalibrator.h>
      27             : #include <microsim/MSVehicleControl.h>
      28             : #include <libsumo/TraCIConstants.h>
      29             : #include "Helper.h"
      30             : #include "Calibrator.h"
      31             : 
      32             : 
      33             : namespace libsumo {
      34             : // ===========================================================================
      35             : // static member initializations
      36             : // ===========================================================================
      37             : SubscriptionResults Calibrator::mySubscriptionResults;
      38             : ContextSubscriptionResults Calibrator::myContextSubscriptionResults;
      39             : 
      40             : 
      41             : // ===========================================================================
      42             : // static member definitions
      43             : // ===========================================================================
      44             : std::vector<std::string>
      45         146 : Calibrator::getIDList() {
      46         146 :     MSNet::getInstance(); // just to check that we actually have a network
      47             :     std::vector<std::string> ids;
      48         420 :     for (const auto& item : MSCalibrator::getInstances()) {
      49         276 :         ids.push_back(item.first);
      50             :     }
      51         144 :     return ids;
      52           0 : }
      53             : 
      54             : int
      55           6 : Calibrator::getIDCount() {
      56           6 :     return (int)getIDList().size();
      57             : }
      58             : 
      59             : std::string
      60          12 : Calibrator::getEdgeID(const std::string& calibratorID) {
      61          12 :     return getCalibrator(calibratorID)->getEdge()->getID();
      62             : }
      63             : 
      64             : std::string
      65          12 : Calibrator::getLaneID(const std::string& calibratorID) {
      66          12 :     const MSLane* lane = getCalibrator(calibratorID)->getLane();
      67          12 :     if (lane == nullptr) {
      68           7 :         return "";
      69             :     } else {
      70             :         return lane->getID();
      71             :     }
      72             : }
      73             : 
      74             : double
      75          18 : Calibrator::getVehsPerHour(const std::string& calibratorID) {
      76          18 :     return Helper::getCalibratorState(getCalibrator(calibratorID)).q;
      77             : }
      78             : 
      79             : double
      80          12 : Calibrator::getSpeed(const std::string& calibratorID) {
      81          12 :     return Helper::getCalibratorState(getCalibrator(calibratorID)).v;
      82             : }
      83             : 
      84             : std::string
      85          12 : Calibrator::getTypeID(const std::string& calibratorID) {
      86          12 :     return Helper::getCalibratorState(getCalibrator(calibratorID)).vehicleParameter->vtypeid;
      87             : }
      88             : 
      89             : double
      90          18 : Calibrator::getBegin(const std::string& calibratorID) {
      91          18 :     return STEPS2TIME(Helper::getCalibratorState(getCalibrator(calibratorID)).begin);
      92             : }
      93             : 
      94             : double
      95          18 : Calibrator::getEnd(const std::string& calibratorID) {
      96          18 :     return STEPS2TIME(Helper::getCalibratorState(getCalibrator(calibratorID)).end);
      97             : }
      98             : 
      99             : std::string
     100          12 : Calibrator::getRouteID(const std::string& calibratorID) {
     101          12 :     return Helper::getCalibratorState(getCalibrator(calibratorID)).vehicleParameter->routeid;
     102             : }
     103             : 
     104             : std::string
     105          12 : Calibrator::getRouteProbeID(const std::string& calibratorID) {
     106          12 :     const MSRouteProbe* rp = getCalibrator(calibratorID)->getRouteProbe();
     107          12 :     if (rp == nullptr) {
     108          12 :         return "";
     109             :     } else {
     110             :         return rp->getID();
     111             :     }
     112             : }
     113             : 
     114             : std::vector<std::string>
     115          12 : Calibrator::getVTypes(const std::string& calibratorID) {
     116             :     std::vector<std::string> result;
     117          12 :     const std::set<std::string>& vTypes = getCalibrator(calibratorID)->getVehicleTypes();
     118          12 :     result.insert(result.begin(), vTypes.begin(), vTypes.end());
     119          12 :     std::sort(result.begin(), result.end());
     120          12 :     return result;
     121           0 : }
     122             : 
     123             : 
     124             : int
     125          12 : Calibrator::getPassed(const std::string& calibratorID) {
     126          12 :     return getCalibrator(calibratorID)->passed();
     127             : }
     128             : 
     129             : int
     130          12 : Calibrator::getInserted(const std::string& calibratorID) {
     131          12 :     return getCalibrator(calibratorID)->getInserted();
     132             : }
     133             : 
     134             : int
     135          12 : Calibrator::getRemoved(const std::string& calibratorID) {
     136          12 :     return getCalibrator(calibratorID)->getRemoved();
     137             : }
     138             : 
     139             : std::string
     140          12 : Calibrator::getParameter(const std::string& calibratorID, const std::string& param) {
     141          12 :     const MSCalibrator* c = getCalibrator(calibratorID);
     142          24 :     return c->getParameter(param, "");
     143             : }
     144             : 
     145           0 : LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(Calibrator)
     146             : 
     147             : void
     148          12 : Calibrator::setParameter(const std::string& calibratorID, const std::string& key, const std::string& value) {
     149          12 :     MSCalibrator* c = getCalibrator(calibratorID);
     150          12 :     c->setParameter(key, value);
     151          12 : }
     152             : 
     153             : void
     154          12 : Calibrator::setFlow(const std::string& calibratorID, double begin, double end, double vehsPerHour, double speed, const std::string& typeID,
     155             :                     const std::string& routeID,
     156             :                     const std::string& departLane,
     157             :                     const std::string& departSpeed) {
     158             :     std::string error;
     159          12 :     SUMOVehicleParameter vehicleParams;
     160             :     vehicleParams.vtypeid = typeID;
     161             :     vehicleParams.routeid = routeID;
     162          12 :     MSVehicleType* t = MSNet::getInstance()->getVehicleControl().getVType(typeID);
     163          12 :     if (t == nullptr) {
     164           0 :         throw TraCIException("Vehicle type '" + typeID + "' is not known");
     165             :     }
     166          24 :     if (!SUMOVehicleParameter::parseDepartLane(departLane, "calibrator", calibratorID, vehicleParams.departLane, vehicleParams.departLaneProcedure, error)) {
     167           0 :         throw TraCIException(error);
     168             :     }
     169          24 :     if (!SUMOVehicleParameter::parseDepartSpeed(departSpeed, "calibrator", calibratorID, vehicleParams.departSpeed, vehicleParams.departSpeedProcedure, error)) {
     170           0 :         throw TraCIException(error);
     171             :     }
     172          12 :     getCalibrator(calibratorID)->setFlow(TIME2STEPS(begin), TIME2STEPS(end), vehsPerHour, speed, vehicleParams);
     173          24 : }
     174             : 
     175             : 
     176         152 : LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(Calibrator, CALIBRATOR)
     177             : 
     178             : 
     179             : MSCalibrator*
     180         395 : Calibrator::getCalibrator(const std::string& id) {
     181             :     const auto& dict = MSCalibrator::getInstances();
     182             :     auto it = dict.find(id);
     183         395 :     if (it == dict.end()) {
     184           0 :         throw TraCIException("Calibrator '" + id + "' is not known");
     185             :     }
     186         395 :     return it->second;
     187             : }
     188             : 
     189             : 
     190             : std::shared_ptr<VariableWrapper>
     191         263 : Calibrator::makeWrapper() {
     192         263 :     return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
     193             : }
     194             : 
     195             : 
     196             : bool
     197         212 : Calibrator::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
     198         212 :     switch (variable) {
     199          84 :         case TRACI_ID_LIST:
     200          84 :             return wrapper->wrapStringList(objID, variable, getIDList());
     201           4 :         case ID_COUNT:
     202           4 :             return wrapper->wrapInt(objID, variable, getIDCount());
     203           8 :         case VAR_ROAD_ID:
     204          16 :             return wrapper->wrapString(objID, variable, getEdgeID(objID));
     205           8 :         case VAR_LANE_ID:
     206          16 :             return wrapper->wrapString(objID, variable, getLaneID(objID));
     207          12 :         case VAR_VEHSPERHOUR:
     208          12 :             return wrapper->wrapDouble(objID, variable, getVehsPerHour(objID));
     209           8 :         case VAR_SPEED:
     210           8 :             return wrapper->wrapDouble(objID, variable, getSpeed(objID));
     211           8 :         case VAR_TYPE:
     212          16 :             return wrapper->wrapString(objID, variable, getTypeID(objID));
     213          12 :         case VAR_BEGIN:
     214          12 :             return wrapper->wrapDouble(objID, variable, getBegin(objID));
     215          12 :         case VAR_END:
     216          12 :             return wrapper->wrapDouble(objID, variable, getEnd(objID));
     217           8 :         case VAR_ROUTE_ID:
     218          16 :             return wrapper->wrapString(objID, variable, getRouteID(objID));
     219           8 :         case VAR_ROUTE_PROBE:
     220          16 :             return wrapper->wrapString(objID, variable, getRouteProbeID(objID));
     221           8 :         case VAR_VTYPES:
     222           8 :             return wrapper->wrapStringList(objID, variable, getVTypes(objID));
     223           8 :         case VAR_PASSED:
     224           8 :             return wrapper->wrapInt(objID, variable, getPassed(objID));
     225           8 :         case VAR_INSERTED:
     226           8 :             return wrapper->wrapInt(objID, variable, getInserted(objID));
     227           8 :         case VAR_REMOVED:
     228           8 :             return wrapper->wrapInt(objID, variable, getRemoved(objID));
     229           8 :         case libsumo::VAR_PARAMETER:
     230           8 :             paramData->readUnsignedByte();
     231          16 :             return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
     232           0 :         case libsumo::VAR_PARAMETER_WITH_KEY:
     233           0 :             paramData->readUnsignedByte();
     234           0 :             return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
     235             :         default:
     236             :             return false;
     237             :     }
     238             : }
     239             : 
     240             : }
     241             : 
     242             : 
     243             : /****************************************************************************/

Generated by: LCOV version 1.14