From ce43b09991d8a3882bead391866a86c7526b820a Mon Sep 17 00:00:00 2001 From: Emil Tin Date: Wed, 19 Dec 2012 15:53:05 +0100 Subject: [PATCH 1/4] support lua require() --- Extractor/ScriptingEnvironment.cpp | 4 +++ Rakefile | 5 ++-- Util/Lua.h | 44 ++++++++++++++++++++++++++++++ createHierarchy.cpp | 6 ++++ features/support/data.rb | 6 ++-- features/support/hash.rb | 28 ++++++++++++------- profiles/bicycle.lua | 18 ++++-------- 7 files changed, 83 insertions(+), 28 deletions(-) create mode 100644 Util/Lua.h diff --git a/Extractor/ScriptingEnvironment.cpp b/Extractor/ScriptingEnvironment.cpp index 01f183166..c91ba1c65 100644 --- a/Extractor/ScriptingEnvironment.cpp +++ b/Extractor/ScriptingEnvironment.cpp @@ -27,6 +27,7 @@ extern "C" { #include "ScriptingEnvironment.h" #include "../typedefs.h" #include "../Util/OpenMPWrapper.h" +#include "../Util/Lua.h" ScriptingEnvironment::ScriptingEnvironment() {} ScriptingEnvironment::ScriptingEnvironment(const char * fileName) { @@ -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/Rakefile b/Rakefile index 727412797..2588c4f59 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/Util/Lua.h b/Util/Lua.h new file mode 100644 index 000000000..f6cfe4720 --- /dev/null +++ b/Util/Lua.h @@ -0,0 +1,44 @@ +/* + open source routing machine + Copyright (C) Dennis Luxen, others 2010 + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU AFFERO General Public License as published by +the Free Software Foundation; either version 3 of the License, or +any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +or see http://www.gnu.org/licenses/agpl.txt. + */ + +#ifndef LUA_UTIL_ +#define LUA_UTIL_ + +#include +#include + +bool lua_function_exists(lua_State* lua_state, const char* name) +{ + using namespace luabind; + object g = globals(lua_state); + object func = g[name]; + return func && type(func) == LUA_TFUNCTION; +} + +void luaAddScriptFolderToLoadPath(lua_State* myLuaState, const char* fileName) { + //add the folder contain the script to the lua load path, so script can easily require() other lua scripts inside that folder + //see http://lua-users.org/wiki/PackagePath for details on the package.path syntax + const boost::filesystem::path profilePath( fileName ); + const std::string folder = profilePath.parent_path().c_str(); + const std::string luaCode = "package.path = \"" + folder + "/?.lua;\" .. package.path"; + luaL_dostring(myLuaState,luaCode.c_str()); +} + +#endif /* LUA_UTIL_ */ diff --git a/createHierarchy.cpp b/createHierarchy.cpp index 5d7f13d32..6dfae5d5d 100644 --- a/createHierarchy.cpp +++ b/createHierarchy.cpp @@ -47,6 +47,7 @@ extern "C" { #include "Util/InputFileUtil.h" #include "Util/GraphLoader.h" #include "Util/StringUtil.h" +#include "Util/Lua.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 From 09e331a76b685d19bb3c6e3cf3abc6e8ddeda3e5 Mon Sep 17 00:00:00 2001 From: Emil Tin Date: Thu, 20 Dec 2012 22:12:07 +0100 Subject: [PATCH 2/4] fix gitignore to avoid ignoring subfolders. add missing lua file --- .gitignore | 26 +++++++++++++------------- profiles/lib/access.lua | 13 +++++++++++++ 2 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 profiles/lib/access.lua 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/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 From 28f0782b17bd861add8629186a14b0f58e1b9369 Mon Sep 17 00:00:00 2001 From: Emil Tin Date: Fri, 28 Dec 2012 22:05:28 +0100 Subject: [PATCH 3/4] code cleanup, merge Lua.h and LuaUtil.h --- Extractor/LuaUtil.h | 35 ------------------------------ Extractor/ScriptingEnvironment.cpp | 2 +- Extractor/ScriptingEnvironment.h | 1 - Util/{Lua.h => LuaUtil.h} | 34 +++++++++++++++++++---------- createHierarchy.cpp | 2 +- 5 files changed, 24 insertions(+), 50 deletions(-) delete mode 100644 Extractor/LuaUtil.h rename Util/{Lua.h => LuaUtil.h} (55%) diff --git a/Extractor/LuaUtil.h b/Extractor/LuaUtil.h deleted file mode 100644 index 7134f96e0..000000000 --- a/Extractor/LuaUtil.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - open source routing machine - Copyright (C) Dennis Luxen, others 2010 - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU AFFERO General Public License as published by -the Free Software Foundation; either version 3 of the License, or -any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU Affero General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -or see http://www.gnu.org/licenses/agpl.txt. - */ - - - -#ifndef LUAUTIL_H_ -#define LUAUTIL_H_ - -#include - -template -void LUA_print(T number) { - std::cout << "[LUA] " << number << std::endl; -} - - - -#endif /* LUAUTIL_H_ */ diff --git a/Extractor/ScriptingEnvironment.cpp b/Extractor/ScriptingEnvironment.cpp index c91ba1c65..dbeb2f5f0 100644 --- a/Extractor/ScriptingEnvironment.cpp +++ b/Extractor/ScriptingEnvironment.cpp @@ -27,7 +27,7 @@ extern "C" { #include "ScriptingEnvironment.h" #include "../typedefs.h" #include "../Util/OpenMPWrapper.h" -#include "../Util/Lua.h" +#include "../Util/LuaUtil.h" ScriptingEnvironment::ScriptingEnvironment() {} ScriptingEnvironment::ScriptingEnvironment(const char * fileName) { 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/Util/Lua.h b/Util/LuaUtil.h similarity index 55% rename from Util/Lua.h rename to Util/LuaUtil.h index f6cfe4720..f47869a8a 100644 --- a/Util/Lua.h +++ b/Util/LuaUtil.h @@ -18,27 +18,37 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or see http://www.gnu.org/licenses/agpl.txt. */ -#ifndef LUA_UTIL_ -#define LUA_UTIL_ + +#ifndef LUAUTIL_H_ +#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) { - using namespace luabind; - object g = globals(lua_state); - object func = g[name]; - return func && type(func) == LUA_TFUNCTION; + 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) { - //add the folder contain the script to the lua load path, so script can easily require() other lua scripts inside that folder - //see http://lua-users.org/wiki/PackagePath for details on the package.path syntax const boost::filesystem::path profilePath( fileName ); - const std::string folder = profilePath.parent_path().c_str(); - const std::string luaCode = "package.path = \"" + folder + "/?.lua;\" .. package.path"; - luaL_dostring(myLuaState,luaCode.c_str()); + 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 /* LUA_UTIL_ */ +#endif /* LUAUTIL_H_ */ diff --git a/createHierarchy.cpp b/createHierarchy.cpp index 6dfae5d5d..eb06a8842 100644 --- a/createHierarchy.cpp +++ b/createHierarchy.cpp @@ -47,7 +47,7 @@ extern "C" { #include "Util/InputFileUtil.h" #include "Util/GraphLoader.h" #include "Util/StringUtil.h" -#include "Util/Lua.h" +#include "Util/LuaUtil.h" using namespace std; From 4aa7ade32cf3c8758f86524ed44f2e37e123e148 Mon Sep 17 00:00:00 2001 From: Emil Tin Date: Tue, 1 Jan 2013 23:20:16 +0100 Subject: [PATCH 4/4] ordering includes alphabetically --- Extractor/ScriptingEnvironment.cpp | 2 +- createHierarchy.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Extractor/ScriptingEnvironment.cpp b/Extractor/ScriptingEnvironment.cpp index dbeb2f5f0..2ef215cd1 100644 --- a/Extractor/ScriptingEnvironment.cpp +++ b/Extractor/ScriptingEnvironment.cpp @@ -26,8 +26,8 @@ extern "C" { #include "ScriptingEnvironment.h" #include "../typedefs.h" -#include "../Util/OpenMPWrapper.h" #include "../Util/LuaUtil.h" +#include "../Util/OpenMPWrapper.h" ScriptingEnvironment::ScriptingEnvironment() {} ScriptingEnvironment::ScriptingEnvironment(const char * fileName) { diff --git a/createHierarchy.cpp b/createHierarchy.cpp index eb06a8842..2596d312b 100644 --- a/createHierarchy.cpp +++ b/createHierarchy.cpp @@ -44,10 +44,10 @@ extern "C" { #include "DataStructures/NNGrid.h" #include "DataStructures/QueryEdge.h" #include "Util/BaseConfiguration.h" -#include "Util/InputFileUtil.h" #include "Util/GraphLoader.h" -#include "Util/StringUtil.h" +#include "Util/InputFileUtil.h" #include "Util/LuaUtil.h" +#include "Util/StringUtil.h" using namespace std;