#include <config.h>
#include <utils/common/MsgHandler.h>
#include <utils/common/ToString.h>
#include <utils/options/OptionsCont.h>
#include "SUMOVehicleParameter.h"
#include "SUMOTrafficObject.h"
std::string
SUMOTrafficObject::getStringParam(const std::string& paramName, const bool required, const std::string& deflt) const {
if (getParameter().hasParameter(paramName)) {
return getParameter().getParameter(paramName, "");
} else if (getVTypeParameter().hasParameter(paramName)) {
return getVTypeParameter().getParameter(paramName, "");
} else {
const OptionsCont& oc = OptionsCont::getOptions();
if (oc.exists(paramName) && oc.isSet(paramName)) {
return oc.getValueString(paramName);
} else {
if (required) {
const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
throw ProcessError(TLF("Missing parameter '%' for % '%'.", paramName, type, getID()));
} else {
if (oc.exists(paramName)) {
return oc.getValueString(paramName);
}
return deflt;
}
}
}
}
double
SUMOTrafficObject::getFloatParam(const std::string& paramName, const bool required, const double deflt, bool checkDist) const {
const std::string val = getStringParam(paramName, required, toString(deflt));
if (!checkDist) {
try {
return StringUtils::toDouble(val);
} catch (NumberFormatException& e) {
const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
WRITE_ERRORF(TL("Invalid float value '%' for parameter '%' in % '%' (%)."), val, paramName, type, getID(), e.what());
return deflt;
}
}
try {
Distribution_Parameterized dist(val);
const std::string& error = dist.isValid();
if (error != "") {
throw ProcessError(error);
}
return dist.sample();
} catch (const ProcessError& e) {
const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
WRITE_ERRORF(TL("Invalid distribution / float value '%' for parameter '%' in % '%' (%)."), val, paramName, type, getID(), e.what());
return deflt;
}
}
bool
SUMOTrafficObject::getBoolParam(const std::string& paramName, const bool required, const bool deflt) const {
const std::string val = getStringParam(paramName, required, toString(deflt));
try {
return StringUtils::toBool(val);
} catch (const ProcessError&) {
const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
WRITE_ERRORF(TL("Invalid boolean value '%' for parameter '%' in % '%'."), val, paramName, type, getID());
return deflt;
}
}
SUMOTime
SUMOTrafficObject::getTimeParam(const std::string& paramName, const bool required, const SUMOTime deflt) const {
const std::string val = getStringParam(paramName, required, time2string(deflt));
try {
return string2time(val);
} catch (const ProcessError&) {
const std::string type = isVehicle() ? "vehicle" : (isPerson() ? "person" : "container");
WRITE_ERRORF(TL("Invalid time value '%' for parameter '%' in % '%'."), val, paramName, type, getID());
return deflt;
}
}