LCOV - code coverage report
Current view: top level - src/microsim/devices - MSDevice.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 92 109 84.4 %
Date: 2024-04-29 15:38:36 Functions: 10 12 83.3 %

          Line data    Source code
       1             : /****************************************************************************/
       2             : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3             : // Copyright (C) 2013-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    MSDevice.cpp
      15             : /// @author  Daniel Krajzewicz
      16             : /// @author  Michael Behrisch
      17             : /// @date    14.08.2013
      18             : ///
      19             : // Abstract in-vehicle device
      20             : /****************************************************************************/
      21             : #include <config.h>
      22             : 
      23             : #include <utils/options/OptionsCont.h>
      24             : #include <utils/common/StringUtils.h>
      25             : #include <microsim/MSVehicle.h>
      26             : #include <microsim/transportables/MSTransportable.h>
      27             : #include <microsim/MSVehicleControl.h>
      28             : #include <microsim/MSEdge.h>
      29             : 
      30             : #include "MSDevice_Vehroutes.h"
      31             : #include "MSDevice_Tripinfo.h"
      32             : #include "MSDevice_Routing.h"
      33             : #include "MSDevice_Emissions.h"
      34             : #include "MSDevice_BTreceiver.h"
      35             : #include "MSDevice_BTsender.h"
      36             : #include "MSDevice_Example.h"
      37             : #include "MSDevice_StationFinder.h"
      38             : #include "MSDevice_Battery.h"
      39             : #include "MSDevice_SSM.h"
      40             : #include "MSDevice_ToC.h"
      41             : #include "MSDevice_DriverState.h"
      42             : #include "MSDevice_Bluelight.h"
      43             : #include "MSDevice_FCD.h"
      44             : #include "MSDevice_Taxi.h"
      45             : #include "MSDevice_GLOSA.h"
      46             : #include "MSDevice_ElecHybrid.h"
      47             : #include "MSTransportableDevice_Routing.h"
      48             : #include "MSTransportableDevice_FCD.h"
      49             : #include "MSTransportableDevice_FCDReplay.h"
      50             : #include "MSRoutingEngine.h"
      51             : #include "MSDevice_Friction.h"
      52             : #include "MSDevice_FCDReplay.h"
      53             : #include "MSDevice.h"
      54             : 
      55             : 
      56             : // ===========================================================================
      57             : // static member variables
      58             : // ===========================================================================
      59             : std::map<std::string, std::set<std::string> > MSDevice::myExplicitIDs;
      60             : SumoRNG MSDevice::myEquipmentRNG("deviceEquipment");
      61             : 
      62             : // ===========================================================================
      63             : // debug flags
      64             : // ===========================================================================
      65             : //#define DEBUG_DEVICE_PARAMS
      66             : 
      67             : 
      68             : // ===========================================================================
      69             : // method definitions
      70             : // ===========================================================================
      71             : // ---------------------------------------------------------------------------
      72             : // static initialisation methods
      73             : // ---------------------------------------------------------------------------
      74             : void
      75       36320 : MSDevice::insertOptions(OptionsCont& oc) {
      76       36320 :     MSDevice_Routing::insertOptions(oc);
      77       36320 :     MSDevice_Emissions::insertOptions(oc);
      78       36320 :     MSVehicleDevice_BTreceiver::insertOptions(oc);
      79       36320 :     MSVehicleDevice_BTsender::insertOptions(oc);
      80       36320 :     MSDevice_Example::insertOptions(oc);
      81       36320 :     MSDevice_StationFinder::insertOptions(oc);
      82       36320 :     MSDevice_Battery::insertOptions(oc);
      83       36320 :     MSDevice_SSM::insertOptions(oc);
      84       36320 :     MSDevice_ToC::insertOptions(oc);
      85       36320 :     MSDevice_DriverState::insertOptions(oc);
      86       36320 :     MSDevice_Bluelight::insertOptions(oc);
      87       36320 :     MSDevice_FCD::insertOptions(oc);
      88       36320 :     MSDevice_ElecHybrid::insertOptions(oc);
      89       36320 :     MSDevice_Taxi::insertOptions(oc);
      90       36320 :     MSDevice_GLOSA::insertOptions(oc);
      91       36320 :     MSDevice_Tripinfo::insertOptions(oc);
      92       36320 :     MSDevice_Vehroutes::insertOptions(oc);
      93       36320 :     MSDevice_Friction::insertOptions(oc);
      94       36320 :     MSDevice_FCDReplay::insertOptions(oc);
      95             : 
      96       36320 :     MSTransportableDevice_Routing::insertOptions(oc);
      97       36320 :     MSTransportableDevice_FCD::insertOptions(oc);
      98       36320 :     MSTransportableDevice_BTsender::insertOptions(oc);
      99       36320 :     MSTransportableDevice_BTreceiver::insertOptions(oc);
     100       36320 : }
     101             : 
     102             : 
     103             : bool
     104       35750 : MSDevice::checkOptions(OptionsCont& oc) {
     105             :     bool ok = true;
     106       35750 :     ok &= MSDevice_Routing::checkOptions(oc);
     107       35750 :     return ok;
     108             : }
     109             : 
     110             : 
     111             : void
     112     4658952 : MSDevice::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into) {
     113     4658952 :     MSDevice_Vehroutes::buildVehicleDevices(v, into);
     114     4658952 :     MSDevice_Tripinfo::buildVehicleDevices(v, into);
     115     4658952 :     MSDevice_Routing::buildVehicleDevices(v, into);
     116     4658946 :     MSDevice_Emissions::buildVehicleDevices(v, into);
     117     4658946 :     MSVehicleDevice_BTreceiver::buildVehicleDevices(v, into);
     118     4658946 :     MSVehicleDevice_BTsender::buildVehicleDevices(v, into);
     119     4658946 :     MSDevice_Example::buildVehicleDevices(v, into);
     120             :     const size_t numBefore = into.size();
     121     4658946 :     MSDevice_StationFinder::buildVehicleDevices(v, into);
     122     4658946 :     MSDevice_Battery::buildVehicleDevices(v, into, into.size() == numBefore ? nullptr : static_cast<MSDevice_StationFinder*>(into.back()));
     123     4658946 :     MSDevice_SSM::buildVehicleDevices(v, into);
     124     4658946 :     MSDevice_ToC::buildVehicleDevices(v, into);
     125     4658938 :     MSDevice_DriverState::buildVehicleDevices(v, into);
     126     4658938 :     MSDevice_Bluelight::buildVehicleDevices(v, into);
     127     4658938 :     MSDevice_FCD::buildVehicleDevices(v, into);
     128     4658938 :     MSDevice_ElecHybrid::buildVehicleDevices(v, into);
     129     4658938 :     MSDevice_Taxi::buildVehicleDevices(v, into);
     130     4658932 :     MSDevice_GLOSA::buildVehicleDevices(v, into);
     131     4658932 :     MSDevice_Friction::buildVehicleDevices(v, into);
     132     4658932 :     MSDevice_FCDReplay::buildVehicleDevices(v, into);
     133     4658932 : }
     134             : 
     135             : 
     136             : void
     137      456422 : MSDevice::buildTransportableDevices(MSTransportable& p, std::vector<MSTransportableDevice*>& into) {
     138      456422 :     MSTransportableDevice_Routing::buildDevices(p, into);
     139      456422 :     MSTransportableDevice_FCD::buildDevices(p, into);
     140      456422 :     MSTransportableDevice_BTsender::buildDevices(p, into);
     141      456422 :     MSTransportableDevice_BTreceiver::buildDevices(p, into);
     142      456422 :     MSTransportableDevice_FCDReplay::buildDevices(p, into);
     143      456422 : }
     144             : 
     145             : 
     146             : void
     147       35155 : MSDevice::cleanupAll() {
     148       35155 :     MSRoutingEngine::cleanup();
     149       35155 :     MSDevice_Tripinfo::cleanup();
     150       35155 :     MSDevice_FCD::cleanup();
     151       35155 :     MSDevice_Taxi::cleanup();
     152       35155 : }
     153             : 
     154             : void
     155      835360 : MSDevice::insertDefaultAssignmentOptions(const std::string& deviceName, const std::string& optionsTopic, OptionsCont& oc, const bool isPerson) {
     156     1525440 :     const std::string prefix = (isPerson ? "person-device." : "device.") + deviceName;
     157     1525440 :     const std::string object = isPerson ? "person" : "vehicle";
     158      835360 :     oc.doRegister(prefix + ".probability", new Option_Float(-1.0));// (default: no need to call RNG)
     159     2506080 :     oc.addDescription(prefix + ".probability", optionsTopic, "The probability for a " + object + " to have a '" + deviceName + "' device");
     160             : 
     161     1670720 :     oc.doRegister(prefix + ".explicit", new Option_StringVector());
     162     1670720 :     oc.addSynonyme(prefix + ".explicit", prefix + ".knownveh", true);
     163     2506080 :     oc.addDescription(prefix + ".explicit", optionsTopic, "Assign a '" + deviceName + "' device to named " + object + "s");
     164             : 
     165      835360 :     oc.doRegister(prefix + ".deterministic", new Option_Bool(false));
     166     2506080 :     oc.addDescription(prefix + ".deterministic", optionsTopic, "The '" + deviceName + "' devices are set deterministic using a fraction of 1000");
     167      835360 : }
     168             : 
     169             : 
     170             : void
     171           0 : MSDevice::saveState(OutputDevice& /* out */) const {
     172           0 :     WRITE_WARNINGF(TL("Device '%' cannot save state"), getID());
     173           0 : }
     174             : 
     175             : 
     176             : void
     177           0 : MSDevice::loadState(const SUMOSAXAttributes& /* attrs */) {
     178           0 : }
     179             : 
     180             : 
     181             : std::string
     182     8905736 : MSDevice::getStringParam(const SUMOVehicle& v, const OptionsCont& oc, const std::string& paramName, const std::string& deflt, bool required) {
     183     8905736 :     const std::string key = "device." + paramName;
     184     8905736 :     if (v.getParameter().hasParameter(key)) {
     185        1828 :         return v.getParameter().getParameter(key, "");
     186     8904822 :     } else if (v.getVehicleType().getParameter().hasParameter(key)) {
     187       32262 :         return v.getVehicleType().getParameter().getParameter(key, "");
     188             :     } else {
     189     8888693 :         if (oc.exists(key) && oc.isSet(key)) {
     190     8832713 :             return oc.getValueString(key);
     191             :         } else {
     192       55980 :             if (required) {
     193           8 :                 throw ProcessError("Missing parameter '" + key + "' for vehicle '" + v.getID());
     194             :             } else {
     195             : #ifdef DEBUG_DEVICE_PARAMS
     196             :                 std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter '" + key + "'. Using default of '" << result << "'\n";
     197             : #endif
     198             :                 return deflt;
     199             :             }
     200             :         }
     201             :     }
     202             : }
     203             : 
     204             : 
     205             : double
     206       45240 : MSDevice::getFloatParam(const SUMOVehicle& v, const OptionsCont& oc, const std::string& paramName, const double deflt, bool required) {
     207       45240 :     const std::string val = getStringParam(v, oc, paramName, toString(deflt), required);
     208             :     try {
     209       45240 :         return Distribution_Parameterized(val).sample();
     210           0 :     } catch (const ProcessError&) {
     211           0 :         WRITE_ERRORF(TL("Invalid distribution / float value '%' for parameter '%' in vehicle '%'."), val, "device." + paramName, v.getID());
     212           0 :         return deflt;
     213           0 :     }
     214             : }
     215             : 
     216             : 
     217             : bool
     218     1390956 : MSDevice::getBoolParam(const SUMOVehicle& v, const OptionsCont& oc, const std::string& paramName, const bool deflt, bool required) {
     219     1390956 :     const std::string val = getStringParam(v, oc, paramName, toString(deflt), required);
     220             :     try {
     221     1390956 :         return StringUtils::toBool(val);
     222           0 :     } catch (const ProcessError&) {
     223           0 :         WRITE_ERRORF(TL("Invalid boolean value '%' for parameter '%' in vehicle '%'."), val, "device." + paramName, v.getID());
     224           0 :         return deflt;
     225           0 :     }
     226             : }
     227             : 
     228             : 
     229             : SUMOTime
     230     2781498 : MSDevice::getTimeParam(const SUMOVehicle& v, const OptionsCont& oc, const std::string& paramName, const SUMOTime deflt, bool required) {
     231     2781498 :     const std::string val = getStringParam(v, oc, paramName, toString(deflt), required);
     232             :     try {
     233     2781498 :         return string2time(val);
     234           0 :     } catch (const ProcessError&) {
     235           0 :         WRITE_ERRORF(TL("Invalid time value '%' for parameter '%' in vehicle '%'."), val, "device." + paramName, v.getID());
     236           0 :         return deflt;
     237           0 :     }
     238             : }
     239             : 
     240             : 
     241             : /****************************************************************************/

Generated by: LCOV version 1.14