Skip to content

Commit

Permalink
refactoring parameter interface to make it similar to attributes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
behrisch committed Jan 12, 2024
1 parent 591b8ef commit 7135a35
Show file tree
Hide file tree
Showing 34 changed files with 99 additions and 112 deletions.
2 changes: 1 addition & 1 deletion src/guisim/GUIInductLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ GUIInductLoop::~GUIInductLoop() {}

GUIDetectorWrapper*
GUIInductLoop::buildDetectorGUIRepresentation() {
if (knowsParameter("hotkey")) {
if (hasParameter("hotkey")) {
Command_Hotkey_InductionLoop::registerHotkey(getParameter("hotkey"), this);
}
// caller (GUINet) takes responsibility for pointer
Expand Down
8 changes: 4 additions & 4 deletions src/guisim/GUILane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1179,12 +1179,12 @@ GUILane::setFunctionalColor(const GUIColorer& c, RGBColor& col, int activeScheme
col = c.getScheme().getColor(0);
std::vector<RGBColor> tazColors;
for (MSEdge* e : myEdge->getPredecessors()) {
if (e->isTazConnector() && e->knowsParameter("tazColor")) {
if (e->isTazConnector() && e->hasParameter("tazColor")) {
tazColors.push_back(RGBColor::parseColor(e->getParameter("tazColor")));
}
}
for (MSEdge* e : myEdge->getSuccessors()) {
if (e->isTazConnector() && e->knowsParameter("tazColor")) {
if (e->isTazConnector() && e->hasParameter("tazColor")) {
tazColors.push_back(RGBColor::parseColor(e->getParameter("tazColor")));
}
}
Expand Down Expand Up @@ -1352,7 +1352,7 @@ GUILane::getColorValue(const GUIVisualizationSettings& s, int activeScheme) cons
return getPendingEmits();
case 31: {
// by numerical edge param value
if (myEdge->knowsParameter(s.edgeParam)) {
if (myEdge->hasParameter(s.edgeParam)) {
try {
return StringUtils::toDouble(myEdge->getParameter(s.edgeParam, "0"));
} catch (NumberFormatException&) {
Expand All @@ -1368,7 +1368,7 @@ GUILane::getColorValue(const GUIVisualizationSettings& s, int activeScheme) cons
}
case 32: {
// by numerical lane param value
if (knowsParameter(s.laneParam)) {
if (hasParameter(s.laneParam)) {
try {
return StringUtils::toDouble(getParameter(s.laneParam, "0"));
} catch (NumberFormatException&) {
Expand Down
2 changes: 1 addition & 1 deletion src/guisim/GUINet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ GUINet::createTLWrapper(MSTrafficLightLogic* tll) {
}
// build the wrapper
GUITrafficLightLogicWrapper* tllw = new GUITrafficLightLogicWrapper(*myLogics, *tll);
if (tll->knowsParameter("hotkeyAbort")) {
if (tll->hasParameter("hotkeyAbort")) {
Command_Hotkey_TrafficLight::registerHotkey(tll->getParameter("hotkeyAbort"), *tll);
}
// build the association link->wrapper
Expand Down
6 changes: 3 additions & 3 deletions src/libsumo/Simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ Simulation::getParameter(const std::string& objectID, const std::string& key) {
return toString(cs->getMyName());
} else if (attrName == "lane") {
return cs->getLane().getID();
} else if (cs->knowsParameter(attrName)) {
} else if (cs->hasParameter(attrName)) {
return cs->getParameter(attrName);
} else {
throw TraCIException("Invalid chargingStation parameter '" + attrName + "'");
Expand Down Expand Up @@ -792,7 +792,7 @@ Simulation::getParameter(const std::string& objectID, const std::string& key) {
return toString(pa->getMyName());
} else if (attrName == "lane") {
return pa->getLane().getID();
} else if (pa->knowsParameter(attrName)) {
} else if (pa->hasParameter(attrName)) {
return pa->getParameter(attrName);
} else {
throw TraCIException("Invalid parkingArea parameter '" + attrName + "'");
Expand All @@ -807,7 +807,7 @@ Simulation::getParameter(const std::string& objectID, const std::string& key) {
return toString(bs->getMyName());
} else if (attrName == "lane") {
return bs->getLane().getID();
} else if (bs->knowsParameter(attrName)) {
} else if (bs->hasParameter(attrName)) {
return bs->getParameter(attrName);
} else {
throw TraCIException("Invalid busStop parameter '" + attrName + "'");
Expand Down
2 changes: 1 addition & 1 deletion src/microsim/MSEdge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ MSEdge::insertVehicle(SUMOVehicle& v, SUMOTime time, const bool checkOnly, const

if (!success) {
// constraints may enforce explicit re-ordering so we need to try other vehicles after failure
if (!insertionLane->knowsParameter("insertionOrder" + v.getID())) {
if (!insertionLane->hasParameter("insertionOrder" + v.getID())) {
myFailedInsertionMemory.insert(insertionLane->getIndex());
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/microsim/devices/MSDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ MSDevice::loadState(const SUMOSAXAttributes& /* attrs */) {
std::string
MSDevice::getStringParam(const SUMOVehicle& v, const OptionsCont& oc, const std::string& paramName, const std::string& deflt, bool required) {
const std::string key = "device." + paramName;
if (v.getParameter().knowsParameter(key)) {
if (v.getParameter().hasParameter(key)) {
return v.getParameter().getParameter(key, "");
} else if (v.getVehicleType().getParameter().knowsParameter(key)) {
} else if (v.getVehicleType().getParameter().hasParameter(key)) {
return v.getVehicleType().getParameter().getParameter(key, "");
} else {
if (oc.exists(key) && oc.isSet(key)) {
Expand Down
6 changes: 3 additions & 3 deletions src/microsim/devices/MSDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,13 @@ MSDevice::equippedByDefaultAssignmentOptions(const OptionsCont& oc, const std::s
bool haveByParameter = false;
bool parameterGiven = false;
const std::string key = "has." + deviceName + ".device";
if (v.getParameter().knowsParameter(key)) {
if (v.getParameter().hasParameter(key)) {
parameterGiven = true;
haveByParameter = StringUtils::toBool(v.getParameter().getParameter(key, "false"));
} else if (v.getVehicleType().getParameter().knowsParameter(key)) {
} else if (v.getVehicleType().getParameter().hasParameter(key)) {
parameterGiven = true;
haveByParameter = StringUtils::toBool(v.getVehicleType().getParameter().getParameter(key, "false"));
} else if (v.getVehicleType().getParameter().knowsParameter(prefix + ".probability")) {
} else if (v.getVehicleType().getParameter().hasParameter(prefix + ".probability")) {
// override global options
numberGiven = true;
haveByNumber = RandHelper::rand(&myEquipmentRNG) < StringUtils::toDouble(v.getVehicleType().getParameter().getParameter(prefix + ".probability", "0"));
Expand Down
2 changes: 1 addition & 1 deletion src/microsim/devices/MSDevice_Battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ double
MSDevice_Battery::readParameterValue(SUMOVehicle& v, const SumoXMLAttr& attr, const std::string& paramName, double defaultVal) {
const std::string& oldParam = toString(attr);
const SUMOVTypeParameter& typeParams = v.getVehicleType().getParameter();
if (v.getParameter().knowsParameter(oldParam) || typeParams.knowsParameter(oldParam)) {
if (v.getParameter().hasParameter(oldParam) || typeParams.hasParameter(oldParam)) {
WRITE_WARNINGF(TL("Battery device in vehicle '%s' still uses old parameter '%'. Please update to 'device.%'."), v.getID(), oldParam, paramName);
if (v.getParameter().getParameter(oldParam, "-") == "-") {
return typeParams.getDouble(oldParam, defaultVal);
Expand Down
8 changes: 4 additions & 4 deletions src/microsim/devices/MSDevice_ElecHybrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ MSDevice_ElecHybrid::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDe
/* The actual battery capacity can be a parameter of the vehicle or its vehicle type.
The vehicle parameter takes precedence over the type parameter. */
std::string attrName = toString(SUMO_ATTR_ACTUALBATTERYCAPACITY);
if (vehicleParams.knowsParameter(attrName)) {
if (vehicleParams.hasParameter(attrName)) {
const std::string abc = vehicleParams.getParameter(attrName, "-1");
try {
actualBatteryCapacity = StringUtils::toDouble(abc);
} catch (...) {
WRITE_WARNING("Invalid value '" + abc + "'for vehicle parameter '" + attrName + "'. Using the default of " + std::to_string(actualBatteryCapacity));
}
} else {
if (typeParams.knowsParameter(attrName)) {
if (typeParams.hasParameter(attrName)) {
const std::string abc = typeParams.getParameter(attrName, "-1");
try {
actualBatteryCapacity = StringUtils::toDouble(abc);
Expand All @@ -94,7 +94,7 @@ MSDevice_ElecHybrid::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDe
// obtain maximumBatteryCapacity
double maximumBatteryCapacity = 0;
attrName = toString(SUMO_ATTR_MAXIMUMBATTERYCAPACITY);
if (typeParams.knowsParameter(attrName)) {
if (typeParams.hasParameter(attrName)) {
const std::string mbc = typeParams.getParameter(attrName, "-1");
try {
maximumBatteryCapacity = StringUtils::toDouble(mbc);
Expand All @@ -108,7 +108,7 @@ MSDevice_ElecHybrid::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDe
// obtain overheadWireChargingPower
double overheadWireChargingPower = 0;
attrName = toString(SUMO_ATTR_OVERHEADWIRECHARGINGPOWER);
if (typeParams.knowsParameter(attrName)) {
if (typeParams.hasParameter(attrName)) {
const std::string ocp = typeParams.getParameter(attrName, "-1");
try {
overheadWireChargingPower = StringUtils::toDouble(ocp);
Expand Down
4 changes: 2 additions & 2 deletions src/microsim/devices/MSDevice_Example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ MSDevice_Example::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevic
// build the device
// get custom vehicle parameter
double customParameter2 = -1;
if (v.getParameter().knowsParameter("example")) {
if (v.getParameter().hasParameter("example")) {
try {
customParameter2 = StringUtils::toDouble(v.getParameter().getParameter("example", "-1"));
} catch (...) {
Expand All @@ -68,7 +68,7 @@ MSDevice_Example::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevic
}
// get custom vType parameter
double customParameter3 = -1;
if (v.getVehicleType().getParameter().knowsParameter("example")) {
if (v.getVehicleType().getParameter().hasParameter("example")) {
try {
customParameter3 = StringUtils::toDouble(v.getVehicleType().getParameter().getParameter("example", "-1"));
} catch (...) {
Expand Down

0 comments on commit 7135a35

Please sign in to comment.