FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
knowrob/include/knowrob/plugins/PluginModule.h at dev · knowrob/knowrob · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
knowrob
/
knowrob
Public
Notifications
You must be signed in to change notification settings
Fork
112
Star
159
Code
Issues
4
Pull requests
2
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
knowrob
/
include
/
knowrob
/
plugins
/
PluginModule.h
Copy path
More file actions
More file actions
Latest commit
History
History
History
123 lines (110 loc) · 3.41 KB
Breadcrumbs
knowrob
/
include
/
knowrob
/
plugins
/
PluginModule.h
Copy path
File metadata and controls
123 lines (110 loc) · 3.41 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* This file is part of KnowRob, please consult
* https://github.com/knowrob/knowrob for license details.
*/
#
ifndef
KNOWROB_PLUGIN_MODULE_H_
#
define
KNOWROB_PLUGIN_MODULE_H_
#
include
<
string
>
#
include
<
memory
>
#
include
<
set
>
#
include
<
filesystem
>
#
include
<
boost/python.hpp
>
#
include
"
knowrob/plugins/PluginFactory.h
"
#
include
"
knowrob/integration/python/utils.h
"
#
include
"
knowrob/Logger.h
"
namespace
knowrob
{
/*
*
* A plugin module is a plugin factory that creates plugin objects
* that are implemented in Python code.
*/
template
<
class
T
>
class
PluginModule
:
public
PluginFactory
<T> {
public:
/*
*
* @param modulePath the name or path of the Python module.
* @param pluginType the name of the plugin type.
*/
PluginModule
(std::string_view modulePath, std::string_view pluginType)
: modulePath_(knowrob::py::resolveModulePath(modulePath)),
pluginType_
(pluginType) {
}
/*
*
* Cannot be copy-assigned.
*/
PluginModule
(
const
PluginModule &) = delete;
~PluginModule
()
override
{
unloadModule
();
}
/*
*
* @return true if the module was loaded successfully.
*/
bool
isLoaded
() {
return
knowrob::py::call_with_gil<
bool
>([&] {
return
pyPluginType_ && !pyPluginType_.
is_none
();
});
}
/*
*
* Unload the module from the Python interpreter.
*/
void
unloadModule
() {
knowrob::py::gil_lock lock;
pyModule_ = {};
pyPluginType_ = {};
}
/*
*
* Try loading the module from filesystem.
* @return true on success.
*/
bool
loadModule
() {
//
try to make sure that the module can be imported.
//
the modules can be addressed either via an absolute path,
//
or via a relative path in the KnowRob project.
std::filesystem::path
modulePath
(modulePath_);
if
(!
std::filesystem::exists
(modulePath)) {
KB_ERROR
(
"
Module '{}' does not exist.
"
, modulePath_.
c_str
());
return
false
;
}
knowrob::py::gil_lock lock;
try
{
//
Make sure module can be imported by possibly extending the sys.path
//
of the Python interpreter.
auto
importString =
knowrob::py::addToSysPath
(modulePath);
pyModule_ =
boost::python::import
(importString.
c_str
());
if
(pyModule_.
is_none
()) {
KB_ERROR
(
"
Failed to import module '{}'.
"
, modulePath_.
c_str
());
return
false
;
}
pyPluginType_ = pyModule_.
attr
(pluginType_.
c_str
());
}
catch
(
const
boost::python::error_already_set &) {
throw
PythonError
();
}
return
isLoaded
();
}
//
Override PluginFactory
std::shared_ptr<NamedPlugin<T>>
create
(std::string_view pluginID)
override
{
knowrob::py::gil_lock lock;
try
{
boost::python::object pyReasoner =
pyPluginType_
();
//
extract the reasoner in appropriate type
boost::python::extract<std::shared_ptr<T>>
extracted
(pyReasoner);
if
(extracted.
check
()) {
return
std::make_shared<NamedPlugin<T>>(pluginID, PluginLanguage::
PYTHON
,
extracted
());
}
else
{
KB_ERROR
(
"
Failed to extract typed plugin from module '{}'
"
, modulePath_.
c_str
());
}
}
catch
(
const
boost::python::error_already_set &) {
throw
PythonError
();
}
KB_ERROR
(
"
Failed to create plugin from module '{}'
"
, modulePath_.
c_str
());
return
{};
}
//
Override PluginFactory
std::string_view
name
()
const
override
{
return
pluginType_; };
protected:
const
std::string modulePath_;
const
std::string pluginType_;
boost::python::object pyModule_;
boost::python::object pyPluginType_;
};
}
#
endif
//
KNOWROB_REASONER_MODULE_H_
Back
|
FazBrowse Home
|
New Git URL