support lua require()

This commit is contained in:
Emil Tin 2012-12-19 15:53:05 +01:00 committed by Emil Tin
parent b869184c10
commit ce43b09991
7 changed files with 83 additions and 28 deletions

View File

@ -27,6 +27,7 @@ extern "C" {
#include "ScriptingEnvironment.h" #include "ScriptingEnvironment.h"
#include "../typedefs.h" #include "../typedefs.h"
#include "../Util/OpenMPWrapper.h" #include "../Util/OpenMPWrapper.h"
#include "../Util/Lua.h"
ScriptingEnvironment::ScriptingEnvironment() {} ScriptingEnvironment::ScriptingEnvironment() {}
ScriptingEnvironment::ScriptingEnvironment(const char * fileName) { ScriptingEnvironment::ScriptingEnvironment(const char * fileName) {
@ -51,6 +52,9 @@ ScriptingEnvironment::ScriptingEnvironment(const char * fileName) {
luabind::def("durationIsValid", durationIsValid), luabind::def("durationIsValid", durationIsValid),
luabind::def("parseDuration", parseDuration) luabind::def("parseDuration", parseDuration)
]; ];
luaAddScriptFolderToLoadPath( myLuaState, fileName );
//#pragma omp critical //#pragma omp critical
// { // {
// if(0 != luaL_dostring( // if(0 != luaL_dostring(

View File

@ -7,6 +7,7 @@ require 'sys/proctable'
DATA_FOLDER = 'sandbox' DATA_FOLDER = 'sandbox'
PROFILE = 'bicycle' PROFILE = 'bicycle'
OSRM_PORT = 5000 OSRM_PORT = 5000
PROFILES_FOLDER = '../profiles'
Cucumber::Rake::Task.new do |t| Cucumber::Rake::Task.new do |t|
t.cucumber_opts = %w{--format pretty} t.cucumber_opts = %w{--format pretty}
@ -116,9 +117,9 @@ end
desc "Reprocess OSM data." desc "Reprocess OSM data."
task :process => :setup do task :process => :setup do
Dir.chdir DATA_FOLDER 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 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 puts
end end
end end

44
Util/Lua.h Normal file
View File

@ -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 <string>
#include <boost/filesystem/convenience.hpp>
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_ */

View File

@ -47,6 +47,7 @@ extern "C" {
#include "Util/InputFileUtil.h" #include "Util/InputFileUtil.h"
#include "Util/GraphLoader.h" #include "Util/GraphLoader.h"
#include "Util/StringUtil.h" #include "Util/StringUtil.h"
#include "Util/Lua.h"
using namespace std; using namespace std;
@ -111,6 +112,11 @@ int main (int argc, char *argv[]) {
// Connect LuaBind to this lua state // Connect LuaBind to this lua state
luabind::open(myLuaState); 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 // Now call our function in a lua script
INFO("Parsing speedprofile from " << (argc > 3 ? argv[3] : "profile.lua") ); INFO("Parsing speedprofile from " << (argc > 3 ? argv[3] : "profile.lua") );

View File

@ -13,7 +13,7 @@ OSM_TIMESTAMP = '2000-00-00T00:00:00Z'
DEFAULT_SPEEDPROFILE = 'bicycle' DEFAULT_SPEEDPROFILE = 'bicycle'
WAY_SPACING = 100 WAY_SPACING = 100
DEFAULT_GRID_SIZE = 100 #meters DEFAULT_GRID_SIZE = 100 #meters
PROFILES_PATH = '../profiles'
ORIGIN = [1,1] ORIGIN = [1,1]
@ -205,7 +205,7 @@ def reprocess
unless extracted? unless extracted?
log_preprocess_info log_preprocess_info
log "== Extracting #{@osm_file}.osm...", :preprocess 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 log "*** Exited with code #{$?.exitstatus}.", :preprocess
raise ExtractError.new $?.exitstatus, "osrm-extract exited with code #{$?.exitstatus}." raise ExtractError.new $?.exitstatus, "osrm-extract exited with code #{$?.exitstatus}."
end end
@ -214,7 +214,7 @@ def reprocess
unless prepared? unless prepared?
log_preprocess_info log_preprocess_info
log "== Preparing #{@osm_file}.osm...", :preprocess 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 log "*** Exited with code #{$?.exitstatus}.", :preprocess
raise PrepareError.new $?.exitstatus, "osrm-prepare exited with code #{$?.exitstatus}." raise PrepareError.new $?.exitstatus, "osrm-prepare exited with code #{$?.exitstatus}."
end end

View File

@ -1,39 +1,47 @@
require 'digest/sha1' require 'digest/sha1'
def hash_of_file path def hash_of_files paths
paths = [paths] unless paths.is_a? Array
hash = Digest::SHA1.new hash = Digest::SHA1.new
open(path,'r') do |io| for path in paths do
while !io.eof open(path,'r') do |io|
buf = io.readpartial 1024 while !io.eof
hash.update buf buf = io.readpartial 1024
hash.update buf
end
end end
end end
return hash.hexdigest return hash.hexdigest
end end
def profile_hash def profile_hash
@@profile_hashes ||= {} @@profile_hashes ||= {}
@@profile_hashes[@profile] ||= hash_of_file "../profiles/#{@profile}.lua" @@profile_hashes[@profile] ||= hash_of_files "#{PROFILES_PATH}/#{@profile}.lua"
end end
def osm_hash def osm_hash
@osm_hash ||= Digest::SHA1.hexdigest osm_str @osm_hash ||= Digest::SHA1.hexdigest osm_str
end end
def lua_lib_hash
@lua_lib_hash ||= hash_of_files Dir.glob("../profiles/lib/*.lua")
end
def bin_extract_hash def bin_extract_hash
@@bin_extract_hash ||= hash_of_file '../osrm-extract' @@bin_extract_hash ||= hash_of_files '../osrm-extract'
end end
def bin_prepare_hash def bin_prepare_hash
@@bin_prepare_hash ||= hash_of_file '../osrm-prepare' @@bin_prepare_hash ||= hash_of_files '../osrm-prepare'
end end
def bin_routed_hash def bin_routed_hash
@@bin_routed_hash ||= hash_of_file '../osrm-routed' @@bin_routed_hash ||= hash_of_files '../osrm-routed'
end end
#combine state of data, profile and binaries into a hash that identifies the exact test scenario #combine state of data, profile and binaries into a hash that identifies the exact test scenario
def fingerprint 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 end

View File

@ -1,3 +1,5 @@
require("lib/access")
-- Begin of globals -- 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} 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 } access_tag_whitelist = { ["yes"] = true, ["permissive"] = true, ["designated"] = true }
@ -58,7 +60,6 @@ amenity_speeds = {
route_speeds = { route_speeds = {
["ferry"] = 5 ["ferry"] = 5
} }
take_minimum_of_speeds = true take_minimum_of_speeds = true
obey_oneway = true obey_oneway = true
obey_bollards = false obey_bollards = false
@ -70,19 +71,10 @@ u_turn_penalty = 20
-- End of globals -- End of globals
--find first tag in access hierachy which is set --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) function node_function (node)
local barrier = node.tags:Find ("barrier") 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") local traffic_signal = node.tags:Find("highway")
-- flag node if it carries a traffic light -- flag node if it carries a traffic light
@ -91,7 +83,7 @@ function node_function (node)
end end
-- parse access and barrier tags -- parse access and barrier tags
if access and access ~= "" then if access and access ~= "" then
if access_tag_blacklist[access] then if access_tag_blacklist[access] then
node.bollard = true node.bollard = true
else else
@ -134,7 +126,7 @@ function way_function (way, numberOfNodesInWay)
local service = way.tags:Find("service") local service = way.tags:Find("service")
local area = way.tags:Find("area") local area = way.tags:Find("area")
local amenity = way.tags:Find("amenity") 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 -- initial routability check, filters out buildings, boundaries, etc
if (not highway or highway == '') and if (not highway or highway == '') and