support lua require()
This commit is contained in:
		
							parent
							
								
									b869184c10
								
							
						
					
					
						commit
						ce43b09991
					
				| @ -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(
 | ||||
|  | ||||
							
								
								
									
										5
									
								
								Rakefile
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								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 | ||||
|  | ||||
							
								
								
									
										44
									
								
								Util/Lua.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								Util/Lua.h
									
									
									
									
									
										Normal 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_ */ | ||||
| @ -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") ); | ||||
|  | ||||
| @ -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  | ||||
|  | ||||
| @ -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 | ||||
| 
 | ||||
|  | ||||
| @ -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  | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user