2016-01-02 11:13:44 -05:00
|
|
|
#include "extractor/scripting_environment.hpp"
|
|
|
|
|
|
|
|
#include "extractor/extraction_helper_functions.hpp"
|
|
|
|
#include "extractor/extraction_node.hpp"
|
|
|
|
#include "extractor/extraction_way.hpp"
|
|
|
|
#include "extractor/internal_extractor_edge.hpp"
|
|
|
|
#include "extractor/external_memory_node.hpp"
|
|
|
|
#include "extractor/raster_source.hpp"
|
|
|
|
#include "util/lua_util.hpp"
|
2016-01-28 08:27:05 -05:00
|
|
|
#include "util/exception.hpp"
|
2016-01-02 11:13:44 -05:00
|
|
|
#include "util/simple_logger.hpp"
|
|
|
|
#include "util/typedefs.hpp"
|
2012-11-19 13:04:59 -05:00
|
|
|
|
2014-08-26 11:51:34 -04:00
|
|
|
#include <luabind/tag_function.hpp>
|
2015-05-29 20:28:29 -04:00
|
|
|
#include <luabind/operator.hpp>
|
2014-08-26 11:51:34 -04:00
|
|
|
|
|
|
|
#include <osmium/osm.hpp>
|
|
|
|
|
2014-07-21 09:46:19 -04:00
|
|
|
#include <sstream>
|
2016-01-05 10:51:13 -05:00
|
|
|
|
|
|
|
namespace osrm
|
|
|
|
{
|
|
|
|
namespace extractor
|
|
|
|
{
|
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
|
|
|
|
2016-01-04 05:44:23 -05:00
|
|
|
// Error handler
|
|
|
|
int luaErrorCallback(lua_State *state)
|
2014-11-19 03:49:39 -05:00
|
|
|
{
|
2016-01-04 05:44:23 -05:00
|
|
|
std::string error_msg = lua_tostring(state, -1);
|
2014-11-19 03:49:39 -05:00
|
|
|
std::ostringstream error_stream;
|
|
|
|
error_stream << error_msg;
|
2016-01-05 10:51:13 -05:00
|
|
|
throw util::exception("ERROR occurred 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
|
|
|
{
|
2016-01-05 10:51:13 -05:00
|
|
|
util::SimpleLogger().Write() << "Using script " << file_name;
|
2014-05-16 07:26:42 -04:00
|
|
|
}
|
2012-11-22 13:26:06 -05:00
|
|
|
|
2016-01-04 05:44:23 -05:00
|
|
|
void ScriptingEnvironment::InitLuaState(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);
|
|
|
|
|
2016-01-05 10:51:13 -05:00
|
|
|
util::luaAddScriptFolderToLoadPath(lua_state, file_name.c_str());
|
2014-05-16 07:26:42 -04:00
|
|
|
|
|
|
|
// Add our function to the state's global scope
|
2016-01-05 06:04:04 -05:00
|
|
|
luabind::module(lua_state)
|
2016-01-05 10:51:13 -05:00
|
|
|
[luabind::def("print", util::LUA_print<std::string>),
|
2016-01-05 06:04:04 -05:00
|
|
|
luabind::def("durationIsValid", durationIsValid),
|
|
|
|
luabind::def("parseDuration", parseDuration),
|
|
|
|
luabind::class_<SourceContainer>("sources")
|
|
|
|
.def(luabind::constructor<>())
|
|
|
|
.def("load", &SourceContainer::loadRasterSource)
|
|
|
|
.def("query", &SourceContainer::getRasterDataFromSource)
|
|
|
|
.def("interpolate", &SourceContainer::getRasterInterpolateFromSource),
|
|
|
|
luabind::class_<const float>("constants")
|
|
|
|
.enum_("enums")[luabind::value("precision", COORDINATE_PRECISION)],
|
|
|
|
|
|
|
|
luabind::class_<std::vector<std::string>>("vector")
|
|
|
|
.def("Add", static_cast<void (std::vector<std::string>::*)(const std::string &)>(
|
|
|
|
&std::vector<std::string>::push_back)),
|
|
|
|
|
|
|
|
luabind::class_<osmium::Location>("Location")
|
|
|
|
.def<location_member_ptr_type>("lat", &osmium::Location::lat)
|
|
|
|
.def<location_member_ptr_type>("lon", &osmium::Location::lon),
|
|
|
|
|
|
|
|
luabind::class_<osmium::Node>("Node")
|
|
|
|
// .def<node_member_ptr_type>("tags", &osmium::Node::tags)
|
|
|
|
.def("location", &osmium::Node::location)
|
|
|
|
.def("get_value_by_key", &osmium::Node::get_value_by_key)
|
|
|
|
.def("get_value_by_key", &get_value_by_key<osmium::Node>)
|
|
|
|
.def("id", &osmium::Node::id),
|
|
|
|
|
|
|
|
luabind::class_<ExtractionNode>("ResultNode")
|
|
|
|
.def_readwrite("traffic_lights", &ExtractionNode::traffic_lights)
|
|
|
|
.def_readwrite("barrier", &ExtractionNode::barrier),
|
|
|
|
|
|
|
|
luabind::class_<ExtractionWay>("ResultWay")
|
|
|
|
// .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("is_startpoint", &ExtractionWay::is_startpoint)
|
|
|
|
.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)],
|
|
|
|
luabind::class_<osmium::Way>("Way")
|
|
|
|
.def("get_value_by_key", &osmium::Way::get_value_by_key)
|
|
|
|
.def("get_value_by_key", &get_value_by_key<osmium::Way>)
|
|
|
|
.def("id", &osmium::Way::id),
|
|
|
|
luabind::class_<InternalExtractorEdge>("EdgeSource")
|
|
|
|
.property("source_coordinate", &InternalExtractorEdge::source_coordinate)
|
|
|
|
.property("weight_data", &InternalExtractorEdge::weight_data),
|
|
|
|
luabind::class_<InternalExtractorEdge::WeightData>("WeightData")
|
|
|
|
.def_readwrite("speed", &InternalExtractorEdge::WeightData::speed),
|
|
|
|
luabind::class_<ExternalMemoryNode>("EdgeTarget")
|
|
|
|
.property("lat", &ExternalMemoryNode::lat)
|
|
|
|
.property("lon", &ExternalMemoryNode::lon),
|
2016-01-05 10:51:13 -05:00
|
|
|
luabind::class_<util::FixedPointCoordinate>("Coordinate")
|
|
|
|
.property("lat", &util::FixedPointCoordinate::lat)
|
|
|
|
.property("lon", &util::FixedPointCoordinate::lon),
|
2016-01-05 06:04:04 -05:00
|
|
|
luabind::class_<RasterDatum>("RasterDatum")
|
|
|
|
.property("datum", &RasterDatum::datum)
|
|
|
|
.def("invalid_data", &RasterDatum::get_invalid)];
|
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;
|
2016-01-05 10:51:13 -05:00
|
|
|
throw util::exception("ERROR occurred in profile script:\n" + error_stream.str());
|
2012-11-19 13:04:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-04 05:44:23 -05:00
|
|
|
lua_State *ScriptingEnvironment::GetLuaState()
|
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;
|
2016-01-04 05:44:23 -05:00
|
|
|
InitLuaState(ref.get());
|
2012-11-19 13:04:59 -05:00
|
|
|
}
|
2016-01-04 05:44:23 -05:00
|
|
|
luabind::set_pcall_callback(&luaErrorCallback);
|
2014-05-16 07:26:42 -04:00
|
|
|
|
|
|
|
return ref.get();
|
2012-11-19 13:04:59 -05:00
|
|
|
}
|
2016-01-05 10:51:13 -05:00
|
|
|
}
|
|
|
|
}
|