Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
libsumo/Rerouter.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2017-2026 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/****************************************************************************/
18// C++ TraCI client API implementation
19/****************************************************************************/
20#include <config.h>
21
24#include <microsim/MSNet.h>
25#include <microsim/MSEdge.h>
28#include "Helper.h"
29#include "Rerouter.h"
30
31
32namespace libsumo {
33// ===========================================================================
34// static member initializations
35// ===========================================================================
36SubscriptionResults Rerouter::mySubscriptionResults;
37ContextSubscriptionResults Rerouter::myContextSubscriptionResults;
38
39
40// ===========================================================================
41// static member definitions
42// ===========================================================================
43std::vector<std::string>
44Rerouter::getIDList() {
45 MSNet::getInstance(); // just to check that we actually have a network
46 std::vector<std::string> ids;
47 for (const auto& item : MSTriggeredRerouter::getInstances()) {
48 ids.push_back(item.first);
49 }
50 return ids;
51}
52
53int
54Rerouter::getIDCount() {
55 return (int)MSTriggeredRerouter::getInstances().size();
56}
57
58
59std::string
60Rerouter::getParameter(const std::string& rerouterID, const std::string& param) {
61 if (param == "probability") {
62 return toString(getRerouter(rerouterID)->getProbability());
63 }
64 return "";
65}
66
67
69
70
71void
72Rerouter::setParameter(const std::string& rerouterID, const std::string& key, const std::string& value) {
73 MSTriggeredRerouter* r = getRerouter(rerouterID);
74 if (key == "probability") {
75 if (value.empty()) {
76 r->setUserMode(false);
77 } else {
78 double prob;
79 try {
80 prob = StringUtils::toDouble(value);
81 } catch (NumberFormatException&) {
82 throw TraCIException("Invalid value '" + value + "' for parameter '" + key + "' for rerouter '" + rerouterID + "'");
83 }
84 if (prob < 0. || prob > 1.) {
85 throw TraCIException("Invalid value '" + value + "' for parameter '" + key + "' for rerouter '" + rerouterID + "'");
86 }
88 r->setUserMode(true);
89 }
90 } else {
91 throw TraCIException("Rerouter '" + rerouterID + "' does not support parameter '" + key + "'");
92 }
93}
94
95
97
98
100Rerouter::getRerouter(const std::string& id) {
101 MSNet::getInstance(); // just to check that we actually have a network
102 const auto& instances = MSTriggeredRerouter::getInstances();
103 const auto it = instances.find(id);
104 if (it == instances.end()) {
105 throw TraCIException("Rerouter '" + id + "' is not known");
106 }
107 return it->second;
108}
109
110
111std::shared_ptr<VariableWrapper>
112Rerouter::makeWrapper() {
113 return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
114}
115
116
117bool
118Rerouter::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
119 switch (variable) {
120 case TRACI_ID_LIST:
121 return wrapper->wrapStringList(objID, variable, getIDList());
122 case ID_COUNT:
123 return wrapper->wrapInt(objID, variable, getIDCount());
125 paramData->readUnsignedByte();
126 return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
128 paramData->readUnsignedByte();
129 return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
130 default:
131 return false;
132 }
133}
134
135}
136
137
138/****************************************************************************/
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:57
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOM)
Definition TraCIDefs.h:77
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition TraCIDefs.h:124
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:201
Reroutes traffic objects passing an edge.
void setUserUsageProbability(double prob)
Sets the probability with which a vehicle is rerouted given by the user.
static const std::map< std::string, MSTriggeredRerouter * > & getInstances()
return all rerouter instances
void setUserMode(bool val)
Sets whether the process is currently steered by the user.
C++ TraCI client API implementation.
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
virtual std::string readString()
Definition storage.cpp:180
virtual int readUnsignedByte()
Definition storage.cpp:155
@ value
the parser finished reading a JSON value
TRACI_CONST int TRACI_ID_LIST
std::map< std::string, libsumo::SubscriptionResults > ContextSubscriptionResults
Definition TraCIDefs.h:384
std::map< std::string, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition TraCIDefs.h:383
TRACI_CONST int ID_COUNT
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int VAR_PARAMETER_WITH_KEY