2012-11-19 13:04:59 -05:00
|
|
|
/*
|
2013-10-14 07:42:28 -04:00
|
|
|
|
2015-02-19 03:19:51 -05:00
|
|
|
Copyright (c) 2015, Project OSRM contributors
|
2013-10-14 07:42:28 -04:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
Redistributions of source code must retain the above copyright notice, this list
|
|
|
|
of conditions and the following disclaimer.
|
|
|
|
Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
2012-11-19 13:04:59 -05:00
|
|
|
|
2014-11-28 09:00:48 -05:00
|
|
|
#include "scripting_environment.hpp"
|
2014-01-09 10:13:35 -05:00
|
|
|
|
2014-11-28 09:00:48 -05:00
|
|
|
#include "extraction_helper_functions.hpp"
|
|
|
|
#include "extraction_node.hpp"
|
|
|
|
#include "extraction_way.hpp"
|
2014-11-28 06:13:18 -05:00
|
|
|
#include "../data_structures/external_memory_node.hpp"
|
2015-01-27 11:44:46 -05:00
|
|
|
#include "../util/lua_util.hpp"
|
|
|
|
#include "../util/osrm_exception.hpp"
|
|
|
|
#include "../util/simple_logger.hpp"
|
2013-12-16 05:29:38 -05:00
|
|
|
#include "../typedefs.h"
|
2012-11-19 13:04:59 -05:00
|
|
|
|
2014-08-26 11:51:34 -04:00
|
|
|
#include <luabind/tag_function.hpp>
|
|
|
|
|
|
|
|
#include <osmium/osm.hpp>
|
|
|
|
|
2014-07-21 09:46:19 -04:00
|
|
|
#include <sstream>
|
2015-01-22 06:19:11 -05:00
|
|
|
namespace
|
|
|
|
{
|
2014-08-27 10:28:03 -04:00
|
|
|
// wrapper method as luabind doesn't automatically overload funcs w/ default parameters
|
2015-01-22 06:19:11 -05:00
|
|
|
template <class T>
|
|
|
|
auto get_value_by_key(T const &object, const char *key) -> decltype(object.get_value_by_key(key))
|
2014-08-27 10:28:03 -04:00
|
|
|
{
|
2014-08-27 12:09:10 -04:00
|
|
|
return object.get_value_by_key(key, "");
|
2014-08-27 10:28:03 -04:00
|
|
|
}
|
2014-11-19 03:49:39 -05:00
|
|
|
|
|
|
|
int lua_error_callback(lua_State *L) // This is so I can use my own function as an
|
|
|
|
// exception handler, pcall_log()
|
|
|
|
{
|
2015-05-21 18:39:23 -04:00
|
|
|
std::string error_msg = lua_tostring(L, -1);
|
2014-11-19 03:49:39 -05:00
|
|
|
std::ostringstream error_stream;
|
|
|
|
error_stream << error_msg;
|
2015-01-05 09:40:05 -05:00
|
|
|
throw osrm::exception("ERROR occured in profile script:\n" + error_stream.str());
|
2014-08-29 03:34:05 -04:00
|
|
|
}
|
2014-11-19 03:49:39 -05:00
|
|
|
}
|
|
|
|
|
2015-01-22 06:19:11 -05:00
|
|
|
ScriptingEnvironment::ScriptingEnvironment(const std::string &file_name) : file_name(file_name)
|
2014-05-09 10:17:31 -04:00
|
|
|
{
|
|
|
|
SimpleLogger().Write() << "Using script " << file_name;
|
2014-05-16 07:26:42 -04:00
|
|
|
}
|
2012-11-22 13:26:06 -05:00
|
|
|
|
2015-01-22 06:19:11 -05:00
|
|
|
void ScriptingEnvironment::init_lua_state(lua_State *lua_state)
|
2014-05-16 07:26:42 -04:00
|
|
|
{
|
2015-01-22 06:19:11 -05:00
|
|
|
typedef double (osmium::Location::*location_member_ptr_type)() const;
|
2014-08-26 11:51:34 -04:00
|
|
|
|
2014-05-16 07:26:42 -04:00
|
|
|
luabind::open(lua_state);
|
|
|
|
// open utility libraries string library;
|
|
|
|
luaL_openlibs(lua_state);
|
|
|
|
|
|
|
|
luaAddScriptFolderToLoadPath(lua_state, file_name.c_str());
|
|
|
|
|
|
|
|
// Add our function to the state's global scope
|
|
|
|
luabind::module(lua_state)[
|
|
|
|
luabind::def("print", LUA_print<std::string>),
|
|
|
|
luabind::def("durationIsValid", durationIsValid),
|
2014-07-24 12:26:40 -04:00
|
|
|
luabind::def("parseDuration", parseDuration),
|
|
|
|
|
2014-08-26 11:51:34 -04:00
|
|
|
luabind::class_<std::vector<std::string>>("vector")
|
2015-01-22 06:19:11 -05:00
|
|
|
.def("Add", static_cast<void (std::vector<std::string>::*)(const std::string &)>(
|
|
|
|
&std::vector<std::string>::push_back)),
|
2014-07-24 12:26:40 -04:00
|
|
|
|
2014-08-26 11:51:34 -04:00
|
|
|
luabind::class_<osmium::Location>("Location")
|
2015-01-22 06:19:11 -05:00
|
|
|
.def<location_member_ptr_type>("lat", &osmium::Location::lat)
|
|
|
|
.def<location_member_ptr_type>("lon", &osmium::Location::lon),
|
2014-08-26 11:51:34 -04:00
|
|
|
|
|
|
|
luabind::class_<osmium::Node>("Node")
|
2015-01-22 06:19:11 -05:00
|
|
|
// .def<node_member_ptr_type>("tags", &osmium::Node::tags)
|
2015-04-10 05:45:24 -04:00
|
|
|
.def("location", &osmium::Node::location)
|
2015-01-22 06:19:11 -05:00
|
|
|
.def("get_value_by_key", &osmium::Node::get_value_by_key)
|
2015-04-13 03:58:59 -04:00
|
|
|
.def("get_value_by_key", &get_value_by_key<osmium::Node>)
|
|
|
|
.def("id", &osmium::Node::id),
|
2014-08-26 11:51:34 -04:00
|
|
|
|
|
|
|
luabind::class_<ExtractionNode>("ResultNode")
|
2015-01-22 06:19:11 -05:00
|
|
|
.def_readwrite("traffic_lights", &ExtractionNode::traffic_lights)
|
|
|
|
.def_readwrite("barrier", &ExtractionNode::barrier),
|
2014-08-26 11:51:34 -04:00
|
|
|
|
|
|
|
luabind::class_<ExtractionWay>("ResultWay")
|
2015-01-22 06:19:11 -05:00
|
|
|
// .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("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)
|
|
|
|
.enum_("constants")[
|
|
|
|
luabind::value("notSure", 0),
|
|
|
|
luabind::value("oneway", 1),
|
|
|
|
luabind::value("bidirectional", 2),
|
|
|
|
luabind::value("opposite", 3)
|
|
|
|
],
|
2014-08-26 11:51:34 -04:00
|
|
|
luabind::class_<osmium::Way>("Way")
|
2015-01-22 06:19:11 -05:00
|
|
|
.def("get_value_by_key", &osmium::Way::get_value_by_key)
|
|
|
|
.def("get_value_by_key", &get_value_by_key<osmium::Way>)
|
2015-02-05 05:54:30 -05:00
|
|
|
.def("id", &osmium::Way::id)
|
2014-05-16 07:26:42 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
if (0 != luaL_dofile(lua_state, file_name.c_str()))
|
2012-11-19 13:04:59 -05:00
|
|
|
{
|
2014-07-21 09:46:19 -04:00
|
|
|
luabind::object error_msg(luabind::from_stack(lua_state, -1));
|
|
|
|
std::ostringstream error_stream;
|
|
|
|
error_stream << error_msg;
|
2015-01-05 09:40:05 -05:00
|
|
|
throw osrm::exception("ERROR occured in profile script:\n" + error_stream.str());
|
2012-11-19 13:04:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-17 08:34:23 -05:00
|
|
|
lua_State *ScriptingEnvironment::get_lua_state()
|
2014-05-09 10:17:31 -04:00
|
|
|
{
|
2014-12-17 08:34:23 -05:00
|
|
|
std::lock_guard<std::mutex> lock(init_mutex);
|
2014-05-16 07:26:42 -04:00
|
|
|
bool initialized = false;
|
2015-01-22 06:19:11 -05:00
|
|
|
auto &ref = script_contexts.local(initialized);
|
2014-05-16 07:26:42 -04:00
|
|
|
if (!initialized)
|
2014-05-09 10:17:31 -04:00
|
|
|
{
|
2014-05-16 07:26:42 -04:00
|
|
|
std::shared_ptr<lua_State> state(luaL_newstate(), lua_close);
|
|
|
|
ref = state;
|
2014-12-17 08:34:23 -05:00
|
|
|
init_lua_state(ref.get());
|
2012-11-19 13:04:59 -05:00
|
|
|
}
|
2014-11-19 03:49:39 -05:00
|
|
|
luabind::set_pcall_callback(&lua_error_callback);
|
2014-05-16 07:26:42 -04:00
|
|
|
|
|
|
|
return ref.get();
|
2012-11-19 13:04:59 -05:00
|
|
|
}
|