diff --git a/.gitignore b/.gitignore index d76967bc1..ccedb3d78 100644 --- a/.gitignore +++ b/.gitignore @@ -54,23 +54,23 @@ stxxl.errlog # compiled protobuffers # ######################### -DataStructures/pbf-proto/*.pb.h -DataStructures/pbf-proto/*.pb.cc +/DataStructures/pbf-proto/*.pb.h +/DataStructures/pbf-proto/*.pb.cc # External Libs # ################# -lib/ -win/lib +/lib/ +/win/lib # Visual Studio Temp + build Files # #################################### -win/*.user -win/*.ncb -win/*.suo -win/Debug/ -win/Release/ -win/bin/ -win/bin-debug/ +/win/*.user +/win/*.ncb +/win/*.suo +/win/Debug/ +/win/Release/ +/win/bin/ +/win/bin-debug/ /osrm-extract /osrm-routed /osrm-prepare @@ -78,6 +78,6 @@ win/bin-debug/ # Sandbox folder # ################### -sandbox/ +/sandbox/ -test/profile.lua \ No newline at end of file +/test/profile.lua \ No newline at end of file diff --git a/Extractor/ScriptingEnvironment.cpp b/Extractor/ScriptingEnvironment.cpp index 01f183166..2ef215cd1 100644 --- a/Extractor/ScriptingEnvironment.cpp +++ b/Extractor/ScriptingEnvironment.cpp @@ -26,6 +26,7 @@ extern "C" { #include "ScriptingEnvironment.h" #include "../typedefs.h" +#include "../Util/LuaUtil.h" #include "../Util/OpenMPWrapper.h" ScriptingEnvironment::ScriptingEnvironment() {} @@ -51,6 +52,9 @@ ScriptingEnvironment::ScriptingEnvironment(const char * fileName) { luabind::def("durationIsValid", durationIsValid), luabind::def("parseDuration", parseDuration) ]; + + luaAddScriptFolderToLoadPath( myLuaState, fileName ); + //#pragma omp critical // { // if(0 != luaL_dostring( diff --git a/Extractor/ScriptingEnvironment.h b/Extractor/ScriptingEnvironment.h index 05528d408..40f389878 100644 --- a/Extractor/ScriptingEnvironment.h +++ b/Extractor/ScriptingEnvironment.h @@ -30,7 +30,6 @@ extern "C" { #include "ExtractionHelperFunctions.h" #include "ExtractorStructs.h" -#include "LuaUtil.h" #include "../DataStructures/ImportNode.h" diff --git a/Rakefile b/Rakefile index 3f4ecbcba..a7c6ae809 100644 --- a/Rakefile +++ b/Rakefile @@ -7,6 +7,7 @@ require 'sys/proctable' DATA_FOLDER = 'sandbox' PROFILE = 'bicycle' OSRM_PORT = 5000 +PROFILES_FOLDER = '../profiles' Cucumber::Rake::Task.new do |t| t.cucumber_opts = %w{--format pretty} @@ -116,9 +117,9 @@ end desc "Reprocess OSM data." task :process => :setup do Dir.chdir DATA_FOLDER do - raise "Error while extracting data." unless system "../osrm-extract #{osm_data_area_name}.osm.pbf ../profiles/#{PROFILE}.lua" + raise "Error while extracting data." unless system "../osrm-extract #{osm_data_area_name}.osm.pbf #{PROFILES_FOLDER}/#{PROFILE}.lua" puts - raise "Error while preparing data." unless system "../osrm-prepare #{osm_data_area_name}.osrm #{osm_data_area_name}.osrm.restrictions ../profiles/#{PROFILE}.lua" + raise "Error while preparing data." unless system "../osrm-prepare #{osm_data_area_name}.osrm #{osm_data_area_name}.osrm.restrictions #{PROFILES_FOLDER}/#{PROFILE}.lua" puts end end diff --git a/Extractor/LuaUtil.h b/Util/LuaUtil.h similarity index 50% rename from Extractor/LuaUtil.h rename to Util/LuaUtil.h index 7134f96e0..f47869a8a 100644 --- a/Extractor/LuaUtil.h +++ b/Util/LuaUtil.h @@ -24,12 +24,31 @@ or see http://www.gnu.org/licenses/agpl.txt. #define LUAUTIL_H_ #include +#include +#include template void LUA_print(T number) { std::cout << "[LUA] " << number << std::endl; } +// Check if the lua function is defined +bool lua_function_exists(lua_State* lua_state, const char* name) +{ + luabind::object g = luabind::globals(lua_state); + luabind::object func = g[name]; + return func && (luabind::type(func) == LUA_TFUNCTION); +} +// Add the folder contain the script to the lua load path, so script can easily require() other lua scripts inside that folder, or subfolders. +// See http://lua-users.org/wiki/PackagePath for details on the package.path syntax. +void luaAddScriptFolderToLoadPath(lua_State* myLuaState, const char* fileName) { + const boost::filesystem::path profilePath( fileName ); + if( !profilePath.parent_path().empty() ) { + const std::string folder = profilePath.parent_path().string(); + const std::string luaCode = "package.path = \"" + folder + "/?.lua;\" .. package.path"; + luaL_dostring( myLuaState, luaCode.c_str() ); + } +} #endif /* LUAUTIL_H_ */ diff --git a/createHierarchy.cpp b/createHierarchy.cpp index 5d7f13d32..2596d312b 100644 --- a/createHierarchy.cpp +++ b/createHierarchy.cpp @@ -44,8 +44,9 @@ extern "C" { #include "DataStructures/NNGrid.h" #include "DataStructures/QueryEdge.h" #include "Util/BaseConfiguration.h" -#include "Util/InputFileUtil.h" #include "Util/GraphLoader.h" +#include "Util/InputFileUtil.h" +#include "Util/LuaUtil.h" #include "Util/StringUtil.h" using namespace std; @@ -111,6 +112,11 @@ int main (int argc, char *argv[]) { // Connect LuaBind to this lua state luabind::open(myLuaState); + //open utility libraries string library; + luaL_openlibs(myLuaState); + + //adjust lua load path + luaAddScriptFolderToLoadPath( myLuaState, (argc > 3 ? argv[3] : "profile.lua") ); // Now call our function in a lua script INFO("Parsing speedprofile from " << (argc > 3 ? argv[3] : "profile.lua") ); diff --git a/features/support/data.rb b/features/support/data.rb index 392265da4..f83bbd223 100644 --- a/features/support/data.rb +++ b/features/support/data.rb @@ -13,7 +13,7 @@ OSM_TIMESTAMP = '2000-00-00T00:00:00Z' DEFAULT_SPEEDPROFILE = 'bicycle' WAY_SPACING = 100 DEFAULT_GRID_SIZE = 100 #meters - +PROFILES_PATH = '../profiles' ORIGIN = [1,1] @@ -205,7 +205,7 @@ def reprocess unless extracted? log_preprocess_info log "== Extracting #{@osm_file}.osm...", :preprocess - unless system "../osrm-extract #{@osm_file}.osm.pbf 1>>#{PREPROCESS_LOG_FILE} 2>>#{PREPROCESS_LOG_FILE} ../profiles/#{@profile}.lua" + unless system "../osrm-extract #{@osm_file}.osm.pbf 1>>#{PREPROCESS_LOG_FILE} 2>>#{PREPROCESS_LOG_FILE} #{PROFILES_PATH}/#{@profile}.lua" log "*** Exited with code #{$?.exitstatus}.", :preprocess raise ExtractError.new $?.exitstatus, "osrm-extract exited with code #{$?.exitstatus}." end @@ -214,7 +214,7 @@ def reprocess unless prepared? log_preprocess_info log "== Preparing #{@osm_file}.osm...", :preprocess - unless system "../osrm-prepare #{@osm_file}.osrm #{@osm_file}.osrm.restrictions 1>>#{PREPROCESS_LOG_FILE} 2>>#{PREPROCESS_LOG_FILE} ../profiles/#{@profile}.lua" + unless system "../osrm-prepare #{@osm_file}.osrm #{@osm_file}.osrm.restrictions 1>>#{PREPROCESS_LOG_FILE} 2>>#{PREPROCESS_LOG_FILE} #{PROFILES_PATH}/#{@profile}.lua" log "*** Exited with code #{$?.exitstatus}.", :preprocess raise PrepareError.new $?.exitstatus, "osrm-prepare exited with code #{$?.exitstatus}." end diff --git a/features/support/hash.rb b/features/support/hash.rb index b4b2f762b..83e5f916d 100644 --- a/features/support/hash.rb +++ b/features/support/hash.rb @@ -1,39 +1,47 @@ require 'digest/sha1' -def hash_of_file path +def hash_of_files paths + paths = [paths] unless paths.is_a? Array hash = Digest::SHA1.new - open(path,'r') do |io| - while !io.eof - buf = io.readpartial 1024 - hash.update buf + for path in paths do + open(path,'r') do |io| + while !io.eof + buf = io.readpartial 1024 + hash.update buf + end end end return hash.hexdigest end + def profile_hash @@profile_hashes ||= {} - @@profile_hashes[@profile] ||= hash_of_file "../profiles/#{@profile}.lua" + @@profile_hashes[@profile] ||= hash_of_files "#{PROFILES_PATH}/#{@profile}.lua" end def osm_hash @osm_hash ||= Digest::SHA1.hexdigest osm_str end +def lua_lib_hash + @lua_lib_hash ||= hash_of_files Dir.glob("../profiles/lib/*.lua") +end + def bin_extract_hash - @@bin_extract_hash ||= hash_of_file '../osrm-extract' + @@bin_extract_hash ||= hash_of_files '../osrm-extract' end def bin_prepare_hash - @@bin_prepare_hash ||= hash_of_file '../osrm-prepare' + @@bin_prepare_hash ||= hash_of_files '../osrm-prepare' end def bin_routed_hash - @@bin_routed_hash ||= hash_of_file '../osrm-routed' + @@bin_routed_hash ||= hash_of_files '../osrm-routed' end #combine state of data, profile and binaries into a hash that identifies the exact test scenario def fingerprint - @fingerprint ||= Digest::SHA1.hexdigest "#{bin_extract_hash}-#{bin_prepare_hash}-#{bin_routed_hash}-#{profile_hash}-#{osm_hash}" + @fingerprint ||= Digest::SHA1.hexdigest "#{bin_extract_hash}-#{bin_prepare_hash}-#{bin_routed_hash}-#{profile_hash}-#{lua_lib_hash}-#{osm_hash}" end diff --git a/profiles/bicycle.lua b/profiles/bicycle.lua index c27771b14..f077aa1d6 100644 --- a/profiles/bicycle.lua +++ b/profiles/bicycle.lua @@ -1,3 +1,5 @@ +require("lib/access") + -- Begin of globals barrier_whitelist = { [""] = true, ["cycle_barrier"] = true, ["bollard"] = true, ["entrance"] = true, ["cattle_grid"] = true, ["border_control"] = true, ["toll_booth"] = true, ["sally_port"] = true, ["gate"] = true} access_tag_whitelist = { ["yes"] = true, ["permissive"] = true, ["designated"] = true } @@ -58,7 +60,6 @@ amenity_speeds = { route_speeds = { ["ferry"] = 5 } - take_minimum_of_speeds = true obey_oneway = true obey_bollards = false @@ -70,19 +71,10 @@ u_turn_penalty = 20 -- End of globals --find first tag in access hierachy which is set -function find_access_tag(source) - for i,v in ipairs(access_tags_hierachy) do - local tag = source.tags:Find(v) - if tag ~= '' then --and tag ~= "" then - return tag - end - end - return nil -end function node_function (node) local barrier = node.tags:Find ("barrier") - local access = find_access_tag(node) + local access = Access.find_access_tag(node, access_tags_hierachy) local traffic_signal = node.tags:Find("highway") -- flag node if it carries a traffic light @@ -91,7 +83,7 @@ function node_function (node) end -- parse access and barrier tags - if access and access ~= "" then + if access and access ~= "" then if access_tag_blacklist[access] then node.bollard = true else @@ -134,7 +126,7 @@ function way_function (way, numberOfNodesInWay) local service = way.tags:Find("service") local area = way.tags:Find("area") local amenity = way.tags:Find("amenity") - local access = find_access_tag(way) + local access = Access.find_access_tag(way, access_tags_hierachy) -- initial routability check, filters out buildings, boundaries, etc if (not highway or highway == '') and diff --git a/profiles/lib/access.lua b/profiles/lib/access.lua new file mode 100644 index 000000000..a1e2fcf4d --- /dev/null +++ b/profiles/lib/access.lua @@ -0,0 +1,13 @@ +local ipairs = ipairs + +module "Access" + +function find_access_tag(source,access_tags_hierachy) + for i,v in ipairs(access_tags_hierachy) do + local tag = source.tags:Find(v) + if tag ~= '' then + return tag + end + end + return nil +end \ No newline at end of file