Switch profiles from Lua to library interface

There's now an abstracted interface and no direct calls to Lua anymore.

fixes #1974
This commit is contained in:
Konstantin Käfer
2016-07-11 17:44:58 +02:00
committed by Patrick Niklaus
parent 9b737230d6
commit 1309dd2a0f
15 changed files with 382 additions and 245 deletions
@@ -36,13 +36,13 @@
#include <boost/filesystem/fstream.hpp>
struct lua_State;
namespace osrm
{
namespace extractor
{
class ScriptingEnvironment;
namespace lookup
{
// Set to 1 byte alignment
@@ -95,9 +95,9 @@ class EdgeBasedGraphFactory
const std::vector<std::uint32_t> &turn_lane_offsets,
const std::vector<guidance::TurnLaneType::Mask> &turn_lane_masks);
void Run(const std::string &original_edge_data_filename,
void Run(ScriptingEnvironment &scripting_environment,
const std::string &original_edge_data_filename,
const std::string &turn_lane_data_filename,
lua_State *lua_state,
const std::string &edge_segment_lookup_filename,
const std::string &edge_penalty_filename,
const bool generate_edge_lookup);
@@ -127,8 +127,6 @@ class EdgeBasedGraphFactory
const NodeID w,
const double angle) const;
std::int32_t GetTurnPenalty(double angle, lua_State *lua_state) const;
private:
using EdgeData = util::NodeBasedDynamicGraph::EdgeData;
@@ -162,9 +160,9 @@ class EdgeBasedGraphFactory
void CompressGeometry();
unsigned RenumberEdges();
void GenerateEdgeExpandedNodes();
void GenerateEdgeExpandedEdges(const std::string &original_edge_data_filename,
void GenerateEdgeExpandedEdges(ScriptingEnvironment &scripting_environment,
const std::string &original_edge_data_filename,
const std::string &turn_lane_data_filename,
lua_State *lua_state,
const std::string &edge_segment_lookup_filename,
const std::string &edge_fixed_penalties_filename,
const bool generate_edge_lookup);
+4 -4
View File
@@ -34,7 +34,7 @@ class ExtractionContainers
#endif
void PrepareNodes();
void PrepareRestrictions();
void PrepareEdges(lua_State *segment_state);
void PrepareEdges(ScriptingEnvironment &scripting_environment);
void WriteNodes(std::ofstream &file_out_stream) const;
void WriteRestrictions(const std::string &restrictions_file_name) const;
@@ -69,11 +69,11 @@ class ExtractionContainers
ExtractionContainers();
void PrepareData(const std::string &output_file_name,
void PrepareData(ScriptingEnvironment &scripting_environment,
const std::string &output_file_name,
const std::string &restrictions_file_name,
const std::string &names_file_name,
const std::string &turn_lane_file_name,
lua_State *segment_state);
const std::string &turn_lane_file_name);
};
}
}
+3 -3
View File
@@ -43,20 +43,20 @@ namespace osrm
namespace extractor
{
class ScriptingEnvironment;
struct ProfileProperties;
class Extractor
{
public:
Extractor(ExtractorConfig extractor_config) : config(std::move(extractor_config)) {}
int run();
int run(ScriptingEnvironment &scripting_environment);
private:
ExtractorConfig config;
std::pair<std::size_t, EdgeID>
BuildEdgeExpandedGraph(lua_State *lua_state,
const ProfileProperties &profile_properties,
BuildEdgeExpandedGraph(ScriptingEnvironment &scripting_environment,
std::vector<QueryNode> &internal_to_external_node_map,
std::vector<EdgeBasedNode> &node_based_edge_list,
std::vector<bool> &node_is_startpoint,
-1
View File
@@ -77,7 +77,6 @@ struct ExtractorConfig
intersection_class_data_output_path = basepath + ".osrm.icd";
}
boost::filesystem::path config_file_path;
boost::filesystem::path input_path;
boost::filesystem::path profile_path;
+2 -4
View File
@@ -8,7 +8,6 @@
#include <string>
#include <vector>
struct lua_State;
namespace osmium
{
class Relation;
@@ -19,7 +18,7 @@ namespace osrm
namespace extractor
{
struct ProfileProperties;
class ScriptingEnvironment;
/**
* Parses the relations that represents turn restrictions.
@@ -42,11 +41,10 @@ struct ProfileProperties;
class RestrictionParser
{
public:
RestrictionParser(lua_State *lua_state, const ProfileProperties &properties);
RestrictionParser(ScriptingEnvironment &scripting_environment);
boost::optional<InputRestrictionContainer> TryParse(const osmium::Relation &relation) const;
private:
void ReadRestrictionExceptions(lua_State *lua_state);
bool ShouldIgnoreRestriction(const std::string &except_tag_string) const;
std::vector<std::string> restriction_exceptions;
+44 -26
View File
@@ -1,52 +1,70 @@
#ifndef SCRIPTING_ENVIRONMENT_HPP
#define SCRIPTING_ENVIRONMENT_HPP
#include "extractor/guidance/turn_lane_types.hpp"
#include "extractor/internal_extractor_edge.hpp"
#include "extractor/profile_properties.hpp"
#include "extractor/raster_source.hpp"
#include "extractor/restriction.hpp"
#include "util/lua_util.hpp"
#include <osmium/memory/buffer.hpp>
#include <boost/optional/optional.hpp>
#include <tbb/concurrent_vector.h>
#include <memory>
#include <mutex>
#include <string>
#include <tbb/enumerable_thread_specific.h>
#include <vector>
struct lua_State;
namespace osmium
{
class Node;
class Way;
}
namespace osrm
{
namespace util
{
struct Coordinate;
}
namespace extractor
{
class RestrictionParser;
struct ExtractionNode;
struct ExtractionWay;
/**
* Creates a lua context and binds osmium way, node and relation objects and
* ExtractionWay and ExtractionNode to lua objects.
*
* Each thread has its own lua state which is implemented with thread specific
* storage from TBB.
* Abstract class that handles processing osmium ways, nodes and relation objects by applying
* user supplied profiles.
*/
class ScriptingEnvironment
{
public:
struct Context
{
ProfileProperties properties;
SourceContainer sources;
util::LuaState state;
};
explicit ScriptingEnvironment(const std::string &file_name);
ScriptingEnvironment() = default;
ScriptingEnvironment(const ScriptingEnvironment &) = delete;
ScriptingEnvironment &operator=(const ScriptingEnvironment &) = delete;
virtual ~ScriptingEnvironment() = default;
Context &GetContex();
virtual const ProfileProperties &GetProfileProperties() = 0;
private:
void InitContext(Context &context);
std::mutex init_mutex;
std::string file_name;
tbb::enumerable_thread_specific<std::unique_ptr<Context>> script_contexts;
virtual std::vector<std::string> GetNameSuffixList() = 0;
virtual std::vector<std::string> GetExceptions() = 0;
virtual void SetupSources() = 0;
virtual int32_t GetTurnPenalty(double angle) = 0;
virtual void ProcessSegment(const osrm::util::Coordinate &source,
const osrm::util::Coordinate &target,
double distance,
InternalExtractorEdge::WeightData &weight) = 0;
virtual void
ProcessElements(const std::vector<osmium::memory::Buffer::const_iterator> &osm_elements,
const RestrictionParser &restriction_parser,
tbb::concurrent_vector<std::pair<std::size_t, ExtractionNode>> &resulting_nodes,
tbb::concurrent_vector<std::pair<std::size_t, ExtractionWay>> &resulting_ways,
tbb::concurrent_vector<boost::optional<InputRestrictionContainer>>
&resulting_restrictions) = 0;
};
}
}
@@ -0,0 +1,80 @@
#ifndef SCRIPTING_ENVIRONMENT_LUA_HPP
#define SCRIPTING_ENVIRONMENT_LUA_HPP
#include "extractor/scripting_environment.hpp"
#include "extractor/raster_source.hpp"
#include "util/lua_util.hpp"
#include <tbb/enumerable_thread_specific.h>
#include <memory>
#include <mutex>
#include <string>
struct lua_State;
namespace osrm
{
namespace extractor
{
struct LuaScriptingContext final
{
void processNode(const osmium::Node &, ExtractionNode &result);
void processWay(const osmium::Way &, ExtractionWay &result);
ProfileProperties properties;
SourceContainer sources;
util::LuaState state;
bool has_turn_penalty_function;
bool has_node_function;
bool has_way_function;
bool has_segment_function;
};
/**
* Creates a lua context and binds osmium way, node and relation objects and
* ExtractionWay and ExtractionNode to lua objects.
*
* Each thread has its own lua state which is implemented with thread specific
* storage from TBB.
*/
class LuaScriptingEnvironment final : public ScriptingEnvironment
{
public:
explicit LuaScriptingEnvironment(const std::string &file_name);
~LuaScriptingEnvironment() override = default;
const ProfileProperties& GetProfileProperties() override;
LuaScriptingContext &GetLuaContext();
std::vector<std::string> GetNameSuffixList() override;
std::vector<std::string> GetExceptions() override;
void SetupSources() override;
int32_t GetTurnPenalty(double angle) override;
void ProcessSegment(const osrm::util::Coordinate &source,
const osrm::util::Coordinate &target,
double distance,
InternalExtractorEdge::WeightData &weight) override;
void
ProcessElements(const std::vector<osmium::memory::Buffer::const_iterator> &osm_elements,
const RestrictionParser &restriction_parser,
tbb::concurrent_vector<std::pair<std::size_t, ExtractionNode>> &resulting_nodes,
tbb::concurrent_vector<std::pair<std::size_t, ExtractionWay>> &resulting_ways,
tbb::concurrent_vector<boost::optional<InputRestrictionContainer>>
&resulting_restrictions) override;
private:
void InitContext(LuaScriptingContext &context);
std::mutex init_mutex;
std::string file_name;
tbb::enumerable_thread_specific<std::unique_ptr<LuaScriptingContext>> script_contexts;
};
}
}
#endif /* SCRIPTING_ENVIRONMENT_LUA_HPP */
+4 -3
View File
@@ -4,19 +4,20 @@
#include <string>
#include <unordered_set>
struct lua_State;
namespace osrm
{
namespace extractor
{
class ScriptingEnvironment;
// A table containing suffixes.
// At the moment, it is only a front for an unordered set. At some point we might want to make it
// country dependent and have it behave accordingly
class SuffixTable final
{
public:
SuffixTable(lua_State *lua_state);
SuffixTable(ScriptingEnvironment &scripting_environment);
// check whether a string is part of the know suffix list
bool isSuffix(const std::string &possible_suffix) const;