From 71c336d9ddccd3dc859b7ee7356a1885eb8fe358 Mon Sep 17 00:00:00 2001 From: Patrick Niklaus Date: Mon, 21 Mar 2016 22:42:47 +0100 Subject: [PATCH] Adds .properties file to osrm-extract ouput This file contains global properties set by the lua profile, such as enabling uturns at vias and penalties. This file will be consumed by the server. --- .../extractor/edge_based_graph_factory.hpp | 6 +- include/extractor/extractor.hpp | 8 +- include/extractor/extractor_config.hpp | 2 + include/extractor/graph_compressor.hpp | 5 +- include/extractor/profile_properties.hpp | 46 +++++ include/extractor/restriction_parser.hpp | 5 +- include/extractor/scripting_environment.hpp | 19 +- include/extractor/speed_profile.hpp | 23 --- profiles/bicycle.lua | 7 +- profiles/car.lua | 18 +- profiles/foot.lua | 8 +- profiles/testbot.lua | 11 +- src/extractor/edge_based_graph_factory.cpp | 37 ++-- src/extractor/extraction_containers.cpp | 4 +- src/extractor/extractor.cpp | 98 ++++------ src/extractor/graph_compressor.cpp | 3 +- src/extractor/restriction_parser.cpp | 25 +-- src/extractor/scripting_environment.cpp | 178 ++++++++++-------- 18 files changed, 253 insertions(+), 250 deletions(-) create mode 100644 include/extractor/profile_properties.hpp delete mode 100644 include/extractor/speed_profile.hpp diff --git a/include/extractor/edge_based_graph_factory.hpp b/include/extractor/edge_based_graph_factory.hpp index a6765dc67..1aff3908f 100644 --- a/include/extractor/edge_based_graph_factory.hpp +++ b/include/extractor/edge_based_graph_factory.hpp @@ -4,7 +4,7 @@ #define EDGE_BASED_GRAPH_FACTORY_HPP_ #include "extractor/edge_based_edge.hpp" -#include "extractor/speed_profile.hpp" +#include "extractor/profile_properties.hpp" #include "extractor/restriction_map.hpp" #include "extractor/compressed_edge_container.hpp" #include "extractor/edge_based_node.hpp" @@ -52,7 +52,7 @@ class EdgeBasedGraphFactory const std::unordered_set &traffic_lights, std::shared_ptr restriction_map, const std::vector &node_info_list, - SpeedProfileProperties speed_profile, + ProfileProperties profile_properties, const util::NameTable &name_table); void Run(const std::string &original_edge_data_filename, @@ -106,7 +106,7 @@ class EdgeBasedGraphFactory const std::unordered_set &m_traffic_lights; const CompressedEdgeContainer &m_compressed_edge_container; - SpeedProfileProperties speed_profile; + ProfileProperties profile_properties; const util::NameTable &name_table; diff --git a/include/extractor/extractor.hpp b/include/extractor/extractor.hpp index a512397bd..4ee27d06a 100644 --- a/include/extractor/extractor.hpp +++ b/include/extractor/extractor.hpp @@ -40,6 +40,8 @@ namespace osrm namespace extractor { +class ProfileProperties; + class Extractor { public: @@ -49,13 +51,15 @@ class Extractor private: ExtractorConfig config; - void SetupScriptingEnvironment(lua_State *myLuaState, SpeedProfileProperties &speed_profile); std::pair - BuildEdgeExpandedGraph(std::vector &internal_to_external_node_map, + BuildEdgeExpandedGraph(lua_State* lua_state, + const ProfileProperties& profile_properties, + std::vector &internal_to_external_node_map, std::vector &node_based_edge_list, std::vector &node_is_startpoint, std::vector &edge_based_node_weights, util::DeallocatingVector &edge_based_edge_list); + void WriteProfileProperties(const std::string& output_path, const ProfileProperties& properties) const; void WriteNodeMapping(const std::vector &internal_to_external_node_map); void FindComponents(unsigned max_edge_id, const util::DeallocatingVector &edges, diff --git a/include/extractor/extractor_config.hpp b/include/extractor/extractor_config.hpp index 2c037ab0c..80196db87 100644 --- a/include/extractor/extractor_config.hpp +++ b/include/extractor/extractor_config.hpp @@ -71,6 +71,7 @@ struct ExtractorConfig edge_segment_lookup_path = basepath + ".osrm.edge_segment_lookup"; edge_penalty_path = basepath + ".osrm.edge_penalties"; edge_based_node_weights_output_path = basepath + ".osrm.enw"; + profile_properties_output_path = basepath + ".osrm.properties"; } boost::filesystem::path config_file_path; @@ -88,6 +89,7 @@ struct ExtractorConfig std::string node_output_path; std::string rtree_nodes_output_path; std::string rtree_leafs_output_path; + std::string profile_properties_output_path; unsigned requested_num_threads; unsigned small_component_size; diff --git a/include/extractor/graph_compressor.hpp b/include/extractor/graph_compressor.hpp index 69d738e68..d1a5be524 100644 --- a/include/extractor/graph_compressor.hpp +++ b/include/extractor/graph_compressor.hpp @@ -3,7 +3,6 @@ #include "util/typedefs.hpp" -#include "extractor/speed_profile.hpp" #include "util/node_based_graph.hpp" #include @@ -22,7 +21,7 @@ class GraphCompressor using EdgeData = util::NodeBasedDynamicGraph::EdgeData; public: - GraphCompressor(SpeedProfileProperties speed_profile); + GraphCompressor(); void Compress(const std::unordered_set &barrier_nodes, const std::unordered_set &traffic_lights, @@ -34,8 +33,6 @@ class GraphCompressor void PrintStatistics(unsigned original_number_of_nodes, unsigned original_number_of_edges, const util::NodeBasedDynamicGraph &graph) const; - - SpeedProfileProperties speed_profile; }; } } diff --git a/include/extractor/profile_properties.hpp b/include/extractor/profile_properties.hpp new file mode 100644 index 000000000..8e2253a37 --- /dev/null +++ b/include/extractor/profile_properties.hpp @@ -0,0 +1,46 @@ +#ifndef PROFILE_PROPERTIES_HPP +#define PROFILE_PROPERTIES_HPP + +namespace osrm +{ +namespace extractor +{ + +struct ProfileProperties +{ + ProfileProperties() + : traffic_signal_penalty(0), u_turn_penalty(0), allow_u_turn_at_via(false), use_turn_restrictions(false) + { + } + + double GetUturnPenalty() const + { + return u_turn_penalty / 10.; + } + + void SetUturnPenalty(const double u_turn_penalty_) + { + u_turn_penalty = static_cast(u_turn_penalty_ * 10.); + } + + double GetTrafficSignalPenalty() const + { + return traffic_signal_penalty / 10.; + } + + void SetTrafficSignalPenalty(const double traffic_signal_penalty_) + { + traffic_signal_penalty = static_cast(traffic_signal_penalty_ * 10.); + } + + //! penalty to cross a traffic light in deci-seconds + int traffic_signal_penalty; + //! penalty to do a uturn in deci-seconds + int u_turn_penalty; + bool allow_u_turn_at_via; + bool use_turn_restrictions; +}; +} +} + +#endif diff --git a/include/extractor/restriction_parser.hpp b/include/extractor/restriction_parser.hpp index 075964ba0..d2be8c1f9 100644 --- a/include/extractor/restriction_parser.hpp +++ b/include/extractor/restriction_parser.hpp @@ -19,6 +19,8 @@ namespace osrm namespace extractor { +class ProfileProperties; + /** * Parses the relations that represents turn restrictions. * @@ -40,11 +42,10 @@ namespace extractor class RestrictionParser { public: - RestrictionParser(lua_State *lua_state); + RestrictionParser(lua_State *lua_state, const ProfileProperties& properties); boost::optional TryParse(const osmium::Relation &relation) const; private: - void ReadUseRestrictionsSetting(lua_State *lua_state); void ReadRestrictionExceptions(lua_State *lua_state); bool ShouldIgnoreRestriction(const std::string &except_tag_string) const; diff --git a/include/extractor/scripting_environment.hpp b/include/extractor/scripting_environment.hpp index cd9de04cc..e26383ac7 100644 --- a/include/extractor/scripting_environment.hpp +++ b/include/extractor/scripting_environment.hpp @@ -1,6 +1,9 @@ #ifndef SCRIPTING_ENVIRONMENT_HPP #define SCRIPTING_ENVIRONMENT_HPP +#include "extractor/profile_properties.hpp" +#include "extractor/raster_source.hpp" + #include #include #include @@ -23,18 +26,28 @@ namespace extractor class ScriptingEnvironment { public: + struct Context + { + Context(); + ~Context(); + + ProfileProperties properties; + SourceContainer sources; + lua_State *state; + }; + explicit ScriptingEnvironment(const std::string &file_name); ScriptingEnvironment(const ScriptingEnvironment &) = delete; ScriptingEnvironment &operator=(const ScriptingEnvironment &) = delete; - lua_State *GetLuaState(); + Context &GetContex(); private: - void InitLuaState(lua_State *lua_state); + void InitContext(Context &context); std::mutex init_mutex; std::string file_name; - tbb::enumerable_thread_specific> script_contexts; + tbb::enumerable_thread_specific> script_contexts; }; } } diff --git a/include/extractor/speed_profile.hpp b/include/extractor/speed_profile.hpp deleted file mode 100644 index 82aa34bd1..000000000 --- a/include/extractor/speed_profile.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef SPEED_PROFILE_PROPERTIES_HPP -#define SPEED_PROFILE_PROPERTIES_HPP - -namespace osrm -{ -namespace extractor -{ - -struct SpeedProfileProperties -{ - SpeedProfileProperties() - : traffic_signal_penalty(0), u_turn_penalty(0), has_turn_penalty_function(false) - { - } - - int traffic_signal_penalty; - int u_turn_penalty; - bool has_turn_penalty_function; -}; -} -} - -#endif diff --git a/profiles/bicycle.lua b/profiles/bicycle.lua index f42c90dc5..a2b090842 100644 --- a/profiles/bicycle.lua +++ b/profiles/bicycle.lua @@ -91,9 +91,10 @@ surface_speeds = { } -- these need to be global because they are accesed externaly -traffic_signal_penalty = 2 -use_turn_restrictions = false -u_turn_penalty = 20 +properties.traffic_signal_penalty = 2 +properties.use_turn_restrictions = false +properties.u_turn_penalty = 20 +properties.allow_u_turn_at_via = true local obey_oneway = true local ignore_areas = true diff --git a/profiles/car.lua b/profiles/car.lua index 3f377d540..77b2b658d 100644 --- a/profiles/car.lua +++ b/profiles/car.lua @@ -127,20 +127,20 @@ maxspeed_table = { ["uk:motorway"] = (70*1609)/1000 } --- these need to be global because they are accesed externaly -u_turn_penalty = 20 -traffic_signal_penalty = 2 -use_turn_restrictions = true +-- set profile properties +properties.u_turn_penalty = 20 +properties.traffic_signal_penalty = 2 +properties.use_turn_restrictions = true -side_road_speed_multiplier = 0.8 +local side_road_speed_multiplier = 0.8 -local turn_penalty = 10 +local turn_penalty = 10 -- Note: this biases right-side driving. Should be -- inverted for left-driving countries. -local turn_bias = 1.2 +local turn_bias = 1.2 -local obey_oneway = true -local ignore_areas = true +local obey_oneway = true +local ignore_areas = true local abs = math.abs local min = math.min diff --git a/profiles/foot.lua b/profiles/foot.lua index f4cad993e..120f35e77 100644 --- a/profiles/foot.lua +++ b/profiles/foot.lua @@ -64,9 +64,11 @@ leisure_speeds = { ["track"] = walking_speed } -traffic_signal_penalty = 2 -u_turn_penalty = 2 -use_turn_restrictions = false +properties.traffic_signal_penalty = 2 +properties.u_turn_penalty = 2 +properties.use_turn_restrictions = false +properties.allow_u_turn_at_via = true + local fallback_names = true function get_exceptions(vector) diff --git a/profiles/testbot.lua b/profiles/testbot.lua index 70ac236f3..5402c561f 100644 --- a/profiles/testbot.lua +++ b/profiles/testbot.lua @@ -16,13 +16,10 @@ speed_profile = { -- these settings are read directly by osrm -take_minimum_of_speeds = true -obey_oneway = true -obey_barriers = true -use_turn_restrictions = true -ignore_areas = true -- future feature -traffic_signal_penalty = 7 -- seconds -u_turn_penalty = 20 +properties.allow_u_turn_at_via = false +properties.use_turn_restrictions = true +properties.traffic_signal_penalty = 7 -- seconds +properties.u_turn_penalty = 20 function limit_speed(speed, limits) -- don't use ipairs(), since it stops at the first nil value diff --git a/src/extractor/edge_based_graph_factory.cpp b/src/extractor/edge_based_graph_factory.cpp index 494da0828..62a004e7e 100644 --- a/src/extractor/edge_based_graph_factory.cpp +++ b/src/extractor/edge_based_graph_factory.cpp @@ -35,13 +35,13 @@ EdgeBasedGraphFactory::EdgeBasedGraphFactory( const std::unordered_set &traffic_lights, std::shared_ptr restriction_map, const std::vector &node_info_list, - SpeedProfileProperties speed_profile, + ProfileProperties profile_properties, const util::NameTable &name_table) : m_max_edge_id(0), m_node_info_list(node_info_list), m_node_based_graph(std::move(node_based_graph)), m_restriction_map(std::move(restriction_map)), m_barrier_nodes(barrier_nodes), m_traffic_lights(traffic_lights), m_compressed_edge_container(compressed_edge_container), - speed_profile(std::move(speed_profile)), name_table(name_table) + profile_properties(std::move(profile_properties)), name_table(name_table) { } @@ -208,7 +208,7 @@ unsigned EdgeBasedGraphFactory::RenumberEdges() // oneway streets always require this self-loop. Other streets only if a u-turn plus // traversal // of the street takes longer than the loop - m_edge_based_node_weights.push_back(edge_data.distance + speed_profile.u_turn_penalty); + m_edge_based_node_weights.push_back(edge_data.distance + profile_properties.u_turn_penalty); BOOST_ASSERT(numbered_edges_count < m_node_based_graph->GetNumberOfEdges()); edge_data.edge_id = numbered_edges_count; @@ -277,6 +277,9 @@ void EdgeBasedGraphFactory::GenerateEdgeExpandedEdges( { util::SimpleLogger().Write() << "generating edge-expanded edges"; + BOOST_ASSERT(lua_state != nullptr); + const bool use_turn_function = util::lua_function_exists(lua_state, "turn_function"); + std::size_t node_based_edge_counter = 0; std::size_t original_edges_counter = 0; restricted_turns_counter = 0; @@ -338,15 +341,15 @@ void EdgeBasedGraphFactory::GenerateEdgeExpandedEdges( unsigned distance = edge_data1.distance; if (m_traffic_lights.find(node_v) != m_traffic_lights.end()) { - distance += speed_profile.traffic_signal_penalty; + distance += profile_properties.traffic_signal_penalty; } - const int turn_penalty = GetTurnPenalty(turn_angle, lua_state); + const int turn_penalty = use_turn_function ? GetTurnPenalty(turn_angle, lua_state) : 0; const auto turn_instruction = turn.instruction; if (guidance::isUturn(turn_instruction)) { - distance += speed_profile.u_turn_penalty; + distance += profile_properties.u_turn_penalty; } distance += turn_penalty; @@ -446,20 +449,16 @@ void EdgeBasedGraphFactory::GenerateEdgeExpandedEdges( int EdgeBasedGraphFactory::GetTurnPenalty(double angle, lua_State *lua_state) const { - - if (speed_profile.has_turn_penalty_function) + BOOST_ASSERT(lua_state != nullptr); + try { - try - { - // call lua profile to compute turn penalty - double penalty = - luabind::call_function(lua_state, "turn_function", 180. - angle); - return static_cast(penalty); - } - catch (const luabind::error &er) - { - util::SimpleLogger().Write(logWARNING) << er.what(); - } + // call lua profile to compute turn penalty + double penalty = luabind::call_function(lua_state, "turn_function", 180. - angle); + return static_cast(penalty); + } + catch (const luabind::error &er) + { + util::SimpleLogger().Write(logWARNING) << er.what(); } return 0; } diff --git a/src/extractor/extraction_containers.cpp b/src/extractor/extraction_containers.cpp index 0762d95b8..5c86eee6e 100644 --- a/src/extractor/extraction_containers.cpp +++ b/src/extractor/extraction_containers.cpp @@ -288,6 +288,8 @@ void ExtractionContainers::PrepareEdges(lua_State *segment_state) const auto all_edges_list_end_ = all_edges_list.end(); const auto all_nodes_list_end_ = all_nodes_list.end(); + auto has_segment_function = util::lua_function_exists(segment_state, "segment_function"); + while (edge_iterator != all_edges_list_end_ && node_iterator != all_nodes_list_end_) { // skip all invalid edges @@ -323,7 +325,7 @@ void ExtractionContainers::PrepareEdges(lua_State *segment_state) edge_iterator->source_coordinate, util::Coordinate(node_iterator->lon, node_iterator->lat)); - if (util::lua_function_exists(segment_state, "segment_function")) + if (has_segment_function) { luabind::call_function( segment_state, "segment_function", boost::cref(edge_iterator->source_coordinate), diff --git a/src/extractor/extractor.cpp b/src/extractor/extractor.cpp index 3e84fd30d..515d0f782 100644 --- a/src/extractor/extractor.cpp +++ b/src/extractor/extractor.cpp @@ -48,6 +48,7 @@ #include #include #include +#include namespace osrm { @@ -75,6 +76,9 @@ namespace extractor */ int Extractor::run() { + // setup scripting environment + ScriptingEnvironment scripting_environment(config.profile_path.string().c_str()); + try { util::LogPolicy::GetInstance().Unmute(); @@ -89,9 +93,6 @@ int Extractor::run() util::SimpleLogger().Write() << "Profile: " << config.profile_path.filename().string(); util::SimpleLogger().Write() << "Threads: " << number_of_threads; - // setup scripting environment - ScriptingEnvironment scripting_environment(config.profile_path.string().c_str()); - ExtractionContainers extraction_containers; auto extractor_callbacks = util::make_unique(extraction_containers); @@ -107,15 +108,12 @@ int Extractor::run() util::SimpleLogger().Write() << "Parsing in progress.."; TIMER_START(parsing); - lua_State *segment_state = scripting_environment.GetLuaState(); + auto& main_context = scripting_environment.GetContex(); - if (util::lua_function_exists(segment_state, "source_function")) + // setup raster sources + if (util::lua_function_exists(main_context.state, "source_function")) { - // bind a single instance of SourceContainer class to relevant lua state - SourceContainer sources; - luabind::globals(segment_state)["sources"] = sources; - - luabind::call_function(segment_state, "source_function"); + luabind::call_function(main_context.state, "source_function"); } std::string generator = header.get("generator"); @@ -142,7 +140,7 @@ int Extractor::run() tbb::concurrent_vector> resulting_restrictions; // setup restriction parser - const RestrictionParser restriction_parser(scripting_environment.GetLuaState()); + const RestrictionParser restriction_parser(main_context.state, main_context.properties); while (const osmium::memory::Buffer buffer = reader.read()) { @@ -165,7 +163,7 @@ int Extractor::run() { ExtractionNode result_node; ExtractionWay result_way; - lua_State *local_state = scripting_environment.GetLuaState(); + auto& local_context = scripting_environment.GetContex(); for (auto x = range.begin(), end = range.end(); x != end; ++x) { @@ -177,7 +175,7 @@ int Extractor::run() result_node.clear(); ++number_of_nodes; luabind::call_function( - local_state, "node_function", + local_context.state, "node_function", boost::cref(static_cast(*entity)), boost::ref(result_node)); resulting_nodes.push_back(std::make_pair(x, result_node)); @@ -186,7 +184,7 @@ int Extractor::run() result_way.clear(); ++number_of_ways; luabind::call_function( - local_state, "way_function", + local_context.state, "way_function", boost::cref(static_cast(*entity)), boost::ref(result_way)); resulting_ways.push_back(std::make_pair(x, result_way)); @@ -238,26 +236,29 @@ int Extractor::run() } extraction_containers.PrepareData(config.output_file_name, config.restriction_file_name, - config.names_file_name, segment_state); + config.names_file_name, main_context.state); + + WriteProfileProperties(config.profile_properties_output_path, main_context.properties); TIMER_STOP(extracting); util::SimpleLogger().Write() << "extraction finished after " << TIMER_SEC(extracting) << "s"; } + // we do this for scoping + // TODO move to own functions catch (const std::exception &e) { util::SimpleLogger().Write(logWARNING) << e.what(); return 1; } - try { // Transform the node-based graph that OSM is based on into an edge-based graph // that is better for routing. Every edge becomes a node, and every valid // movement (e.g. turn from A->B, and B->A) becomes an edge // - // - // // Create a new lua state + + auto& main_context = scripting_environment.GetContex(); util::SimpleLogger().Write() << "Generating edge-expanded graph representation"; @@ -268,7 +269,9 @@ int Extractor::run() std::vector node_is_startpoint; std::vector edge_based_node_weights; std::vector internal_to_external_node_map; - auto graph_size = BuildEdgeExpandedGraph(internal_to_external_node_map, + auto graph_size = BuildEdgeExpandedGraph(main_context.state, + main_context.properties, + internal_to_external_node_map, edge_based_node_list, node_is_startpoint, edge_based_node_weights, edge_based_edge_list); @@ -314,46 +317,15 @@ int Extractor::run() return 0; } -/** - \brief Setups scripting environment (lua-scripting) - Also initializes speed profile. -*/ -void Extractor::SetupScriptingEnvironment(lua_State *lua_state, - SpeedProfileProperties &speed_profile) +void Extractor::WriteProfileProperties(const std::string& output_path, const ProfileProperties& properties) const { - // open utility libraries string library; - luaL_openlibs(lua_state); - - // adjust lua load path - util::luaAddScriptFolderToLoadPath(lua_state, config.profile_path.string().c_str()); - - // Now call our function in a lua script - if (0 != luaL_dofile(lua_state, config.profile_path.string().c_str())) + boost::filesystem::ofstream out_stream(output_path); + if (!out_stream) { - std::stringstream msg; - msg << lua_tostring(lua_state, -1) << " occurred in scripting block"; - throw util::exception(msg.str()); + throw util::exception("Could not open " + output_path + " for writing."); } - if (0 != luaL_dostring(lua_state, "return traffic_signal_penalty\n")) - { - std::stringstream msg; - msg << lua_tostring(lua_state, -1) << " occurred in scripting block"; - throw util::exception(msg.str()); - } - speed_profile.traffic_signal_penalty = 10 * lua_tointeger(lua_state, -1); - util::SimpleLogger().Write(logDEBUG) << "traffic_signal_penalty: " - << speed_profile.traffic_signal_penalty; - - if (0 != luaL_dostring(lua_state, "return u_turn_penalty\n")) - { - std::stringstream msg; - msg << lua_tostring(lua_state, -1) << " occurred in scripting block"; - throw util::exception(msg.str()); - } - - speed_profile.u_turn_penalty = 10 * lua_tointeger(lua_state, -1); - speed_profile.has_turn_penalty_function = util::lua_function_exists(lua_state, "turn_function"); + out_stream.write(reinterpret_cast(&properties), sizeof(properties)); } void Extractor::FindComponents(unsigned max_edge_id, @@ -496,18 +468,14 @@ Extractor::LoadNodeBasedGraph(std::unordered_set &barrier_nodes, \brief Building an edge-expanded graph from node-based input and turn restrictions */ std::pair -Extractor::BuildEdgeExpandedGraph(std::vector &internal_to_external_node_map, +Extractor::BuildEdgeExpandedGraph(lua_State* lua_state, + const ProfileProperties& profile_properties, + std::vector &internal_to_external_node_map, std::vector &node_based_edge_list, std::vector &node_is_startpoint, std::vector &edge_based_node_weights, util::DeallocatingVector &edge_based_edge_list) { - lua_State *lua_state = luaL_newstate(); - luabind::open(lua_state); - - SpeedProfileProperties speed_profile; - SetupScriptingEnvironment(lua_state, speed_profile); - std::unordered_set barrier_nodes; std::unordered_set traffic_lights; @@ -516,7 +484,7 @@ Extractor::BuildEdgeExpandedGraph(std::vector &internal_to_external_n LoadNodeBasedGraph(barrier_nodes, traffic_lights, internal_to_external_node_map); CompressedEdgeContainer compressed_edge_container; - GraphCompressor graph_compressor(speed_profile); + GraphCompressor graph_compressor; graph_compressor.Compress(barrier_nodes, traffic_lights, *restriction_map, *node_based_graph, compressed_edge_container); @@ -527,14 +495,12 @@ Extractor::BuildEdgeExpandedGraph(std::vector &internal_to_external_n EdgeBasedGraphFactory edge_based_graph_factory( node_based_graph, compressed_edge_container, barrier_nodes, traffic_lights, std::const_pointer_cast(restriction_map), - internal_to_external_node_map, speed_profile, name_table); + internal_to_external_node_map, profile_properties, name_table); edge_based_graph_factory.Run(config.edge_output_path, lua_state, config.edge_segment_lookup_path, config.edge_penalty_path, config.generate_edge_lookup); - lua_close(lua_state); - edge_based_graph_factory.GetEdgeBasedEdges(edge_based_edge_list); edge_based_graph_factory.GetEdgeBasedNodes(node_based_edge_list); edge_based_graph_factory.GetStartPointMarkers(node_is_startpoint); diff --git a/src/extractor/graph_compressor.cpp b/src/extractor/graph_compressor.cpp index 374ff4866..6df9d485e 100644 --- a/src/extractor/graph_compressor.cpp +++ b/src/extractor/graph_compressor.cpp @@ -13,8 +13,7 @@ namespace osrm namespace extractor { -GraphCompressor::GraphCompressor(SpeedProfileProperties speed_profile) - : speed_profile(std::move(speed_profile)) +GraphCompressor::GraphCompressor() { } diff --git a/src/extractor/restriction_parser.cpp b/src/extractor/restriction_parser.cpp index ce54576b8..68aa8d622 100644 --- a/src/extractor/restriction_parser.cpp +++ b/src/extractor/restriction_parser.cpp @@ -1,5 +1,5 @@ #include "extractor/restriction_parser.hpp" -#include "extractor/extraction_way.hpp" +#include "extractor/profile_properties.hpp" #include "extractor/external_memory_node.hpp" #include "util/lua_util.hpp" @@ -33,34 +33,15 @@ int luaErrorCallback(lua_State *lua_state) } } -RestrictionParser::RestrictionParser(lua_State *lua_state) : use_turn_restrictions(true) +RestrictionParser::RestrictionParser(lua_State *lua_state, const ProfileProperties &properties) + : use_turn_restrictions(properties.use_turn_restrictions) { - ReadUseRestrictionsSetting(lua_state); - if (use_turn_restrictions) { ReadRestrictionExceptions(lua_state); } } -void RestrictionParser::ReadUseRestrictionsSetting(lua_State *lua_state) -{ - if (0 == luaL_dostring(lua_state, "return use_turn_restrictions\n") && - lua_isboolean(lua_state, -1)) - { - use_turn_restrictions = lua_toboolean(lua_state, -1); - } - - if (use_turn_restrictions) - { - util::SimpleLogger().Write() << "Using turn restrictions"; - } - else - { - util::SimpleLogger().Write() << "Ignoring turn restrictions"; - } -} - void RestrictionParser::ReadRestrictionExceptions(lua_State *lua_state) { if (util::lua_function_exists(lua_state, "get_exceptions")) diff --git a/src/extractor/scripting_environment.cpp b/src/extractor/scripting_environment.cpp index 4fe47f2d2..b2a54be16 100644 --- a/src/extractor/scripting_environment.cpp +++ b/src/extractor/scripting_environment.cpp @@ -6,6 +6,7 @@ #include "extractor/internal_extractor_edge.hpp" #include "extractor/external_memory_node.hpp" #include "extractor/raster_source.hpp" +#include "extractor/profile_properties.hpp" #include "util/lua_util.hpp" #include "util/exception.hpp" #include "util/simple_logger.hpp" @@ -51,122 +52,137 @@ int luaErrorCallback(lua_State *state) } } +ScriptingEnvironment::Context::Context() : state(luaL_newstate()) {} + +ScriptingEnvironment::Context::~Context() { lua_close(state); } + ScriptingEnvironment::ScriptingEnvironment(const std::string &file_name) : file_name(file_name) { util::SimpleLogger().Write() << "Using script " << file_name; } -void ScriptingEnvironment::InitLuaState(lua_State *lua_state) +void ScriptingEnvironment::InitContext(ScriptingEnvironment::Context &context) { typedef double (osmium::Location::*location_member_ptr_type)() const; - luabind::open(lua_state); + luabind::open(context.state); // open utility libraries string library; - luaL_openlibs(lua_state); + luaL_openlibs(context.state); - util::luaAddScriptFolderToLoadPath(lua_state, file_name.c_str()); + util::luaAddScriptFolderToLoadPath(context.state, file_name.c_str()); // Add our function to the state's global scope - luabind::module( - lua_state)[luabind::def("print", util::LUA_print), - luabind::def("durationIsValid", durationIsValid), - luabind::def("parseDuration", parseDuration), - luabind::class_("mode") - .enum_("enums")[luabind::value("inaccessible", TRAVEL_MODE_INACCESSIBLE), - luabind::value("driving", TRAVEL_MODE_DRIVING), - luabind::value("cycling", TRAVEL_MODE_CYCLING), - luabind::value("walking", TRAVEL_MODE_WALKING), - luabind::value("ferry", TRAVEL_MODE_FERRY), - luabind::value("train", TRAVEL_MODE_TRAIN), - luabind::value("pushing_bike", TRAVEL_MODE_PUSHING_BIKE), - luabind::value("movable_bridge", TRAVEL_MODE_MOVABLE_BRIDGE), - luabind::value("steps_up", TRAVEL_MODE_STEPS_UP), - luabind::value("steps_down", TRAVEL_MODE_STEPS_DOWN), - luabind::value("river_up", TRAVEL_MODE_RIVER_UP), - luabind::value("river_down", TRAVEL_MODE_RIVER_DOWN), - luabind::value("route", TRAVEL_MODE_ROUTE)], - luabind::class_("sources") - .def(luabind::constructor<>()) - .def("load", &SourceContainer::loadRasterSource) - .def("query", &SourceContainer::getRasterDataFromSource) - .def("interpolate", &SourceContainer::getRasterInterpolateFromSource), - luabind::class_("constants") - .enum_("enums")[luabind::value("precision", COORDINATE_PRECISION)], + luabind::module(context.state) + [luabind::def("print", util::LUA_print), + luabind::def("durationIsValid", durationIsValid), + luabind::def("parseDuration", parseDuration), + luabind::class_("mode") + .enum_("enums")[luabind::value("inaccessible", TRAVEL_MODE_INACCESSIBLE), + luabind::value("driving", TRAVEL_MODE_DRIVING), + luabind::value("cycling", TRAVEL_MODE_CYCLING), + luabind::value("walking", TRAVEL_MODE_WALKING), + luabind::value("ferry", TRAVEL_MODE_FERRY), + luabind::value("train", TRAVEL_MODE_TRAIN), + luabind::value("pushing_bike", TRAVEL_MODE_PUSHING_BIKE), + luabind::value("movable_bridge", TRAVEL_MODE_MOVABLE_BRIDGE), + luabind::value("steps_up", TRAVEL_MODE_STEPS_UP), + luabind::value("steps_down", TRAVEL_MODE_STEPS_DOWN), + luabind::value("river_up", TRAVEL_MODE_RIVER_UP), + luabind::value("river_down", TRAVEL_MODE_RIVER_DOWN), + luabind::value("route", TRAVEL_MODE_ROUTE)], + luabind::class_("sources") + .def(luabind::constructor<>()) + .def("load", &SourceContainer::loadRasterSource) + .def("query", &SourceContainer::getRasterDataFromSource) + .def("interpolate", &SourceContainer::getRasterInterpolateFromSource), + luabind::class_("constants") + .enum_("enums")[luabind::value("precision", COORDINATE_PRECISION)], - luabind::class_>("vector").def( - "Add", static_cast::*)(const std::string &)>( - &std::vector::push_back)), + luabind::class_("ProfileProperties") + .def(luabind::constructor<>()) + .property("traffic_signal_penalty", &ProfileProperties::GetTrafficSignalPenalty, + &ProfileProperties::SetTrafficSignalPenalty) + .property("u_turn_penalty", &ProfileProperties::GetUturnPenalty, + &ProfileProperties::SetUturnPenalty) + .def_readwrite("allow_u_turn_at_via", &ProfileProperties::allow_u_turn_at_via), - luabind::class_("Location") - .def("lat", &osmium::Location::lat) - .def("lon", &osmium::Location::lon), + luabind::class_>("vector") + .def("Add", static_cast::*)(const std::string &)>( + &std::vector::push_back)), - luabind::class_("Node") - // .def("tags", &osmium::Node::tags) - .def("location", &osmium::Node::location) - .def("get_value_by_key", &osmium::Node::get_value_by_key) - .def("get_value_by_key", &get_value_by_key) - .def("id", &osmium::Node::id), + luabind::class_("Location") + .def("lat", &osmium::Location::lat) + .def("lon", &osmium::Location::lon), - luabind::class_("ResultNode") - .def_readwrite("traffic_lights", &ExtractionNode::traffic_lights) - .def_readwrite("barrier", &ExtractionNode::barrier), + luabind::class_("Node") + // .def("tags", &osmium::Node::tags) + .def("location", &osmium::Node::location) + .def("get_value_by_key", &osmium::Node::get_value_by_key) + .def("get_value_by_key", &get_value_by_key) + .def("id", &osmium::Node::id), - luabind::class_("ResultWay") - // .def(luabind::constructor<>()) - .def_readwrite("forward_speed", &ExtractionWay::forward_speed) - .def_readwrite("backward_speed", &ExtractionWay::backward_speed) - .def_readwrite("name", &ExtractionWay::name) - .def_readwrite("roundabout", &ExtractionWay::roundabout) - .def_readwrite("is_access_restricted", &ExtractionWay::is_access_restricted) - .def_readwrite("is_startpoint", &ExtractionWay::is_startpoint) - .def_readwrite("duration", &ExtractionWay::duration) - .property("forward_mode", &ExtractionWay::get_forward_mode, - &ExtractionWay::set_forward_mode) - .property("backward_mode", &ExtractionWay::get_backward_mode, - &ExtractionWay::set_backward_mode), - luabind::class_("Way") - .def("get_value_by_key", &osmium::Way::get_value_by_key) - .def("get_value_by_key", &get_value_by_key) - .def("id", &osmium::Way::id), - luabind::class_("EdgeSource") - .def_readonly("source_coordinate", &InternalExtractorEdge::source_coordinate) - .def_readwrite("weight_data", &InternalExtractorEdge::weight_data), - luabind::class_("WeightData") - .def_readwrite("speed", &InternalExtractorEdge::WeightData::speed), - luabind::class_("EdgeTarget") - .property("lon", &lonToDouble) - .property("lat", &latToDouble), - luabind::class_("Coordinate") - .property("lon", &lonToDouble) - .property("lat", &latToDouble), - luabind::class_("RasterDatum") - .def_readonly("datum", &RasterDatum::datum) - .def("invalid_data", &RasterDatum::get_invalid)]; + luabind::class_("ResultNode") + .def_readwrite("traffic_lights", &ExtractionNode::traffic_lights) + .def_readwrite("barrier", &ExtractionNode::barrier), - if (0 != luaL_dofile(lua_state, file_name.c_str())) + luabind::class_("ResultWay") + // .def(luabind::constructor<>()) + .def_readwrite("forward_speed", &ExtractionWay::forward_speed) + .def_readwrite("backward_speed", &ExtractionWay::backward_speed) + .def_readwrite("name", &ExtractionWay::name) + .def_readwrite("roundabout", &ExtractionWay::roundabout) + .def_readwrite("is_access_restricted", &ExtractionWay::is_access_restricted) + .def_readwrite("is_startpoint", &ExtractionWay::is_startpoint) + .def_readwrite("duration", &ExtractionWay::duration) + .property("forward_mode", &ExtractionWay::get_forward_mode, + &ExtractionWay::set_forward_mode) + .property("backward_mode", &ExtractionWay::get_backward_mode, + &ExtractionWay::set_backward_mode), + luabind::class_("Way") + .def("get_value_by_key", &osmium::Way::get_value_by_key) + .def("get_value_by_key", &get_value_by_key) + .def("id", &osmium::Way::id), + luabind::class_("EdgeSource") + .def_readonly("source_coordinate", &InternalExtractorEdge::source_coordinate) + .def_readwrite("weight_data", &InternalExtractorEdge::weight_data), + luabind::class_("WeightData") + .def_readwrite("speed", &InternalExtractorEdge::WeightData::speed), + luabind::class_("EdgeTarget") + .property("lon", &lonToDouble) + .property("lat", &latToDouble), + luabind::class_("Coordinate") + .property("lon", &lonToDouble) + .property("lat", &latToDouble), + luabind::class_("RasterDatum") + .def_readonly("datum", &RasterDatum::datum) + .def("invalid_data", &RasterDatum::get_invalid)]; + + luabind::globals(context.state)["properties"] = context.properties; + luabind::globals(context.state)["sources"] = context.sources; + + if (0 != luaL_dofile(context.state, file_name.c_str())) { - luabind::object error_msg(luabind::from_stack(lua_state, -1)); + luabind::object error_msg(luabind::from_stack(context.state, -1)); std::ostringstream error_stream; error_stream << error_msg; throw util::exception("ERROR occurred in profile script:\n" + error_stream.str()); } } -lua_State *ScriptingEnvironment::GetLuaState() +ScriptingEnvironment::Context &ScriptingEnvironment::GetContex() { std::lock_guard lock(init_mutex); bool initialized = false; auto &ref = script_contexts.local(initialized); if (!initialized) { - std::shared_ptr state(luaL_newstate(), lua_close); + std::shared_ptr state = std::make_shared(); ref = state; - InitLuaState(ref.get()); + InitContext(*ref); } luabind::set_pcall_callback(&luaErrorCallback); - return ref.get(); + return *ref; } } }