Throw an error for invalid classes

This commit is contained in:
Patrick Niklaus 2017-08-14 22:18:57 +00:00 committed by Patrick Niklaus
parent 6339395cba
commit c6be2e768a
7 changed files with 63 additions and 6 deletions

View File

@ -102,7 +102,11 @@ template <template <typename A> class FacadeT, typename AlgorithmT> class DataFa
for (const auto &name : params.avoid)
{
auto class_mask_iter = name_to_class.find(name);
if (class_mask_iter != name_to_class.end())
if (class_mask_iter == name_to_class.end())
{
return {};
}
else
{
mask |= class_mask_iter->second;
}

View File

@ -53,6 +53,7 @@ class ScriptingEnvironment
virtual const ProfileProperties &GetProfileProperties() = 0;
virtual std::vector<std::vector<std::string>> GetAvoidableClasses() = 0;
virtual std::vector<std::string> GetClassNames() = 0;
virtual std::vector<std::string> GetNameSuffixList() = 0;
virtual std::vector<std::string> GetRestrictions() = 0;
virtual void ProcessTurn(ExtractionTurn &turn) = 0;

View File

@ -60,6 +60,7 @@ class Sol2ScriptingEnvironment final : public ScriptingEnvironment
std::vector<std::vector<std::string>> GetAvoidableClasses() override;
std::vector<std::string> GetNameSuffixList() override;
std::vector<std::string> GetClassNames() override;
std::vector<std::string> GetRestrictions() override;
void ProcessTurn(ExtractionTurn &turn) override;
void ProcessSegment(ExtractionSegment &segment) override;

View File

@ -100,11 +100,15 @@ function setup()
'vehicle'
},
classes = Sequence {
'toll', 'motorway', 'ferry', 'restricted'
},
-- classes to support for avoid flags
avoidable = Sequence {
Set {"toll"},
Set {"motorway"},
Set {"ferry"}
Set {'toll'},
Set {'motorway'},
Set {'ferry'}
},
avoid = Set {

View File

@ -68,9 +68,43 @@ namespace extractor
namespace
{
// Converts the class name map into a fixed mapping of index to name
void SetClassNames(const ExtractorCallbacks::ClassesMap &classes_map,
void SetClassNames(const std::vector<std::string> &class_names,
ExtractorCallbacks::ClassesMap &classes_map,
ProfileProperties &profile_properties)
{
// if we get a list of class names we can validate if we set invalid classes
// and add classes that were never reference
if (!class_names.empty())
{
// add class names that were never used explicitly on a way
// this makes sure we can correctly validate unkown class names later
for (const auto &name : class_names)
{
auto iter = classes_map.find(name);
if (iter == classes_map.end())
{
auto index = classes_map.size();
if (index > MAX_CLASS_INDEX)
{
throw util::exception("Maximum number of classes if " +
std::to_string(MAX_CLASS_INDEX + 1));
}
classes_map[name] = getClassData(index);
}
}
// check if class names are only from the list supplied by the user
for (const auto &pair : classes_map)
{
auto iter = std::find(class_names.begin(), class_names.end(), pair.first);
if (iter == class_names.end())
{
throw util::exception("Profile used unknown class name: " + pair.first);
}
}
}
for (const auto &pair : classes_map)
{
auto range = getClassIndexes(pair.second);
@ -379,7 +413,7 @@ Extractor::ParseOSMData(ScriptingEnvironment &scripting_environment,
config.GetPath(".osrm.names").string());
auto profile_properties = scripting_environment.GetProfileProperties();
SetClassNames(classes_map, profile_properties);
SetClassNames(scripting_environment.GetClassNames(), classes_map, profile_properties);
auto avoidable_classes = scripting_environment.GetAvoidableClasses();
SetAvoidableClasses(classes_map, avoidable_classes, profile_properties);
files::writeProfileProperties(config.GetPath(".osrm.properties").string(), profile_properties);

View File

@ -739,6 +739,18 @@ std::vector<std::vector<std::string>> Sol2ScriptingEnvironment::GetAvoidableClas
}
}
std::vector<std::string> Sol2ScriptingEnvironment::GetClassNames()
{
auto &context = GetSol2Context();
switch (context.api_version)
{
case 2:
return Sol2ScriptingEnvironment::GetStringListFromTable("classes");
default:
return {};
}
}
std::vector<std::string> Sol2ScriptingEnvironment::GetNameSuffixList()
{
auto &context = GetSol2Context();

View File

@ -28,6 +28,7 @@ class MockScriptingEnvironment : public extractor::ScriptingEnvironment
std::vector<std::string> GetNameSuffixList() override final { return {}; }
std::vector<std::vector<std::string>> GetAvoidableClasses() override final { return {}; };
std::vector<std::string> GetClassNames() override { return {}; };
std::vector<std::string> GetRestrictions() override final { return {}; }
void ProcessTurn(extractor::ExtractionTurn &) override final {}