LCOV - code coverage report
Current view: top level - src/libsumo - Calibrator.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 94.3 % 106 100
Test Date: 2025-11-13 15:38:19 Functions: 87.1 % 31 27

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