knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::PluginModule< T > Class Template Reference

#include <PluginModule.h>

Inheritance diagram for knowrob::PluginModule< T >:
Collaboration diagram for knowrob::PluginModule< T >:

Public Member Functions

 PluginModule (std::string_view modulePath, std::string_view pluginType)
 
 PluginModule (const PluginModule &)=delete
 
 ~PluginModule () override
 
bool isLoaded ()
 
void unloadModule ()
 
bool loadModule ()
 
std::shared_ptr< NamedPlugin< T > > create (std::string_view pluginID) override
 
std::string_view name () const override
 
 PluginModule (std::string_view modulePath, std::string_view pluginType)
 
 PluginModule (const PluginModule &)=delete
 
 ~PluginModule () override
 
bool isLoaded ()
 
void unloadModule ()
 
bool loadModule ()
 
std::shared_ptr< NamedPlugin< T > > create (std::string_view pluginID) override
 
std::string_view name () const override
 
- Public Member Functions inherited from knowrob::PluginFactory< T >
virtual ~PluginFactory ()=default
 
virtual ~PluginFactory ()=default
 

Protected Attributes

const std::string modulePath_
 
const std::string pluginType_
 
boost::python::object pyModule_
 
boost::python::object pyPluginType_
 

Detailed Description

template<class T>
class knowrob::PluginModule< T >

A plugin module is a plugin factory that creates plugin objects that are implemented in Python code.

Definition at line 24 of file PluginModule.h.

Constructor & Destructor Documentation

◆ PluginModule() [1/4]

template<class T >
knowrob::PluginModule< T >::PluginModule ( std::string_view  modulePath,
std::string_view  pluginType 
)
inline
Parameters
modulePaththe name or path of the Python module.
pluginTypethe name of the plugin type.

Definition at line 30 of file PluginModule.h.

32  pluginType_(pluginType) {
33  }
const std::string pluginType_
Definition: PluginModule.h:117
const std::string modulePath_
Definition: PluginModule.h:113
std::string resolveModulePath(std::string_view modulePath)
Definition: utils.cpp:13

◆ PluginModule() [2/4]

template<class T >
knowrob::PluginModule< T >::PluginModule ( const PluginModule< T > &  )
delete

Cannot be copy-assigned.

◆ ~PluginModule() [1/2]

template<class T >
knowrob::PluginModule< T >::~PluginModule ( )
inlineoverride

Definition at line 40 of file PluginModule.h.

40  {
41  unloadModule();
42  }

◆ PluginModule() [3/4]

template<class T >
knowrob::PluginModule< T >::PluginModule ( std::string_view  modulePath,
std::string_view  pluginType 
)
inline
Parameters
modulePaththe name or path of the Python module.
pluginTypethe name of the plugin type.

Definition at line 30 of file PluginModule.h.

32  pluginType_(pluginType) {
33  }

◆ PluginModule() [4/4]

template<class T >
knowrob::PluginModule< T >::PluginModule ( const PluginModule< T > &  )
delete

Cannot be copy-assigned.

◆ ~PluginModule() [2/2]

template<class T >
knowrob::PluginModule< T >::~PluginModule ( )
inlineoverride

Definition at line 40 of file PluginModule.h.

40  {
41  unloadModule();
42  }

Member Function Documentation

◆ create() [1/2]

template<class T >
std::shared_ptr<NamedPlugin<T> > knowrob::PluginModule< T >::create ( std::string_view  pluginID)
inlineoverridevirtual

Create a new plugin instance.

Parameters
pluginIDthe ID of the plugin.
Returns
the plugin created.

Implements knowrob::PluginFactory< T >.

Definition at line 94 of file PluginModule.h.

94  {
96  try {
97  boost::python::object pyReasoner = pyPluginType_();
98  // extract the reasoner in appropriate type
99  boost::python::extract<std::shared_ptr<T>> extracted(pyReasoner);
100  if (extracted.check()) {
101  return std::make_shared<NamedPlugin<T>>(pluginID, PluginLanguage::PYTHON, extracted());
102  } else {
103  KB_ERROR("Failed to extract typed plugin from module '{}'", modulePath_.c_str());
104  }
105  } catch (const boost::python::error_already_set &) {
106  throw PythonError();
107  }
108  KB_ERROR("Failed to create plugin from module '{}'", modulePath_.c_str());
109  return {};
110  }
#define KB_ERROR
Definition: Logger.h:28
boost::python::object pyPluginType_
Definition: PluginModule.h:119

◆ create() [2/2]

template<class T >
std::shared_ptr<NamedPlugin<T> > knowrob::PluginModule< T >::create ( std::string_view  pluginID)
inlineoverridevirtual

Create a new plugin instance.

Parameters
pluginIDthe ID of the plugin.
Returns
the plugin created.

Implements knowrob::PluginFactory< T >.

Definition at line 94 of file PluginModule.h.

94  {
96  try {
97  boost::python::object pyReasoner = pyPluginType_();
98  // extract the reasoner in appropriate type
99  boost::python::extract<std::shared_ptr<T>> extracted(pyReasoner);
100  if (extracted.check()) {
101  return std::make_shared<NamedPlugin<T>>(pluginID, PluginLanguage::PYTHON, extracted());
102  } else {
103  KB_ERROR("Failed to extract typed plugin from module '{}'", modulePath_.c_str());
104  }
105  } catch (const boost::python::error_already_set &) {
106  throw PythonError();
107  }
108  KB_ERROR("Failed to create plugin from module '{}'", modulePath_.c_str());
109  return {};
110  }

◆ isLoaded() [1/2]

template<class T >
bool knowrob::PluginModule< T >::isLoaded ( )
inline
Returns
true if the module was loaded successfully.

Definition at line 47 of file PluginModule.h.

47  {
48  return knowrob::py::call_with_gil<bool>([&] {
49  return pyPluginType_ && !pyPluginType_.is_none();
50  });
51  }

◆ isLoaded() [2/2]

template<class T >
bool knowrob::PluginModule< T >::isLoaded ( )
inline
Returns
true if the module was loaded successfully.

Definition at line 47 of file PluginModule.h.

47  {
48  return knowrob::py::call_with_gil<bool>([&] {
49  return pyPluginType_ && !pyPluginType_.is_none();
50  });
51  }

◆ loadModule() [1/2]

template<class T >
bool knowrob::PluginModule< T >::loadModule ( )
inline

Try loading the module from filesystem.

Returns
true on success.

Definition at line 66 of file PluginModule.h.

66  {
67  // try to make sure that the module can be imported.
68  // the modules can be addressed either via an absolute path,
69  // or via a relative path in the KnowRob project.
70  std::filesystem::path modulePath(modulePath_);
71  if (!std::filesystem::exists(modulePath)) {
72  KB_ERROR("Module '{}' does not exist.", modulePath_.c_str());
73  return false;
74  }
75 
77  try {
78  // Make sure module can be imported by possibly extending the sys.path
79  // of the Python interpreter.
80  auto importString = knowrob::py::addToSysPath(modulePath);
81  pyModule_ = boost::python::import(importString.c_str());
82  if (pyModule_.is_none()) {
83  KB_ERROR("Failed to import module '{}'.", modulePath_.c_str());
84  return false;
85  }
86  pyPluginType_ = pyModule_.attr(pluginType_.c_str());
87  } catch (const boost::python::error_already_set &) {
88  throw PythonError();
89  }
90  return isLoaded();
91  }
boost::python::object pyModule_
Definition: PluginModule.h:118
std::string addToSysPath(const std::filesystem::path &modulePath)
Definition: utils.cpp:25

◆ loadModule() [2/2]

template<class T >
bool knowrob::PluginModule< T >::loadModule ( )
inline

Try loading the module from filesystem.

Returns
true on success.

Definition at line 66 of file PluginModule.h.

66  {
67  // try to make sure that the module can be imported.
68  // the modules can be addressed either via an absolute path,
69  // or via a relative path in the KnowRob project.
70  std::filesystem::path modulePath(modulePath_);
71  if (!std::filesystem::exists(modulePath)) {
72  KB_ERROR("Module '{}' does not exist.", modulePath_.c_str());
73  return false;
74  }
75 
77  try {
78  // Make sure module can be imported by possibly extending the sys.path
79  // of the Python interpreter.
80  auto importString = knowrob::py::addToSysPath(modulePath);
81  pyModule_ = boost::python::import(importString.c_str());
82  if (pyModule_.is_none()) {
83  KB_ERROR("Failed to import module '{}'.", modulePath_.c_str());
84  return false;
85  }
86  pyPluginType_ = pyModule_.attr(pluginType_.c_str());
87  } catch (const boost::python::error_already_set &) {
88  throw PythonError();
89  }
90  return isLoaded();
91  }

◆ name() [1/2]

template<class T >
std::string_view knowrob::PluginModule< T >::name ( ) const
inlineoverridevirtual
Returns
name of the plugin type for which the factory can create instances.

Implements knowrob::PluginFactory< T >.

Definition at line 113 of file PluginModule.h.

113 { return pluginType_; };

◆ name() [2/2]

template<class T >
std::string_view knowrob::PluginModule< T >::name ( ) const
inlineoverridevirtual
Returns
name of the plugin type for which the factory can create instances.

Implements knowrob::PluginFactory< T >.

Definition at line 113 of file PluginModule.h.

113 { return pluginType_; };

◆ unloadModule() [1/2]

template<class T >
void knowrob::PluginModule< T >::unloadModule ( )
inline

Unload the module from the Python interpreter.

Definition at line 56 of file PluginModule.h.

56  {
58  pyModule_ = {};
59  pyPluginType_ = {};
60  }

◆ unloadModule() [2/2]

template<class T >
void knowrob::PluginModule< T >::unloadModule ( )
inline

Unload the module from the Python interpreter.

Definition at line 56 of file PluginModule.h.

56  {
58  pyModule_ = {};
59  pyPluginType_ = {};
60  }

Member Data Documentation

◆ modulePath_

template<class T >
const std::string knowrob::PluginModule< T >::modulePath_
protected

Definition at line 116 of file PluginModule.h.

◆ pluginType_

template<class T >
const std::string knowrob::PluginModule< T >::pluginType_
protected

Definition at line 117 of file PluginModule.h.

◆ pyModule_

template<class T >
boost::python::object knowrob::PluginModule< T >::pyModule_
protected

Definition at line 118 of file PluginModule.h.

◆ pyPluginType_

template<class T >
boost::python::object knowrob::PluginModule< T >::pyPluginType_
protected

Definition at line 119 of file PluginModule.h.


The documentation for this class was generated from the following file: