knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::ReasonerManager Class Reference

#include <ReasonerManager.h>

Inheritance diagram for knowrob::ReasonerManager:
Collaboration diagram for knowrob::ReasonerManager:

Public Member Functions

 ReasonerManager (KnowledgeBase *kb, const std::shared_ptr< StorageManager > &backendManager)
 
 ~ReasonerManager () override
 
auto kb () const
 
auto backendManager () const
 
auto & goalDriven () const
 
auto & dataDriven () const
 
std::vector< DefiningReasonerfindDefiningReasoner (const PredicateIndicator &indicator) const
 
std::shared_ptr< StoragegetReasonerStorage (const std::shared_ptr< NamedReasoner > &reasoner)
 
std::shared_ptr< NamedReasonerloadPlugin (const boost::property_tree::ptree &config) override
 
std::shared_ptr< NamedReasoneraddPlugin (std::string_view reasonerID, PluginLanguage language, const std::shared_ptr< Reasoner > &reasoner) override
 
 ReasonerManager (KnowledgeBase *kb, const std::shared_ptr< StorageManager > &backendManager)
 
 ~ReasonerManager () override
 
auto kb () const
 
auto backendManager () const
 
auto & goalDriven () const
 
auto & dataDriven () const
 
std::vector< DefiningReasonerfindDefiningReasoner (const PredicateIndicator &indicator) const
 
std::shared_ptr< StoragegetReasonerStorage (const std::shared_ptr< NamedReasoner > &reasoner)
 
std::shared_ptr< NamedReasonerloadPlugin (const boost::property_tree::ptree &config) override
 
std::shared_ptr< NamedReasoneraddPlugin (std::string_view reasonerID, PluginLanguage language, const std::shared_ptr< Reasoner > &reasoner) override
 
- Public Member Functions inherited from knowrob::PluginManager< Reasoner >
 PluginManager ()
 
 PluginManager ()
 
virtual ~PluginManager ()
 
virtual ~PluginManager ()
 
auto managerID () const
 
auto managerID () const
 
const auto & plugins () const
 
const auto & plugins () const
 
std::shared_ptr< NamedPlugin< Reasoner > > getPluginWithID (std::string_view pluginID)
 
std::shared_ptr< NamedPlugin< Reasoner > > getPluginWithID (std::string_view pluginID)
 
void removePlugin (const std::shared_ptr< NamedPlugin< Reasoner >> &namedPlugin)
 
void removePlugin (std::string_view pluginID)
 
void removePlugin (const std::shared_ptr< NamedPlugin< Reasoner >> &namedPlugin)
 
void removePlugin (std::string_view pluginID)
 

Static Public Member Functions

static TokenBufferPtr evaluateQuery (const GoalDrivenReasonerPtr &reasoner, const std::vector< FirstOrderLiteralPtr > &literals, const QueryContextPtr &ctx)
 
static TokenBufferPtr evaluateQuery (const GoalDrivenReasonerPtr &reasoner, const std::vector< FirstOrderLiteralPtr > &literals, const QueryContextPtr &ctx)
 
- Static Public Member Functions inherited from knowrob::PluginManager< Reasoner >
static PluginManager< Reasoner > * getManager (uint32_t managerID)
 
static PluginManager< Reasoner > * getManager (uint32_t managerID)
 
static bool addFactory (std::string_view typeName, const std::shared_ptr< PluginFactory< Reasoner >> &factory)
 
static bool addFactory (std::string_view typeName)
 
static bool addFactory (std::string_view typeName, const std::shared_ptr< PluginFactory< Reasoner >> &factory)
 
static bool addFactory (std::string_view typeName)
 

Additional Inherited Members

- Protected Member Functions inherited from knowrob::PluginManager< Reasoner >
std::shared_ptr< PluginLibrary< Reasoner > > loadSharedLibrary (std::string_view path)
 
std::shared_ptr< PluginLibrary< Reasoner > > loadSharedLibrary (std::string_view path)
 
std::shared_ptr< PluginModule< Reasoner > > loadPythonModule (std::string_view path, std::string_view type)
 
std::shared_ptr< PluginModule< Reasoner > > loadPythonModule (std::string_view path, std::string_view type)
 
std::shared_ptr< PluginFactory< Reasoner > > findFactory (const boost::property_tree::ptree &config)
 
std::shared_ptr< PluginFactory< Reasoner > > findFactory (const boost::property_tree::ptree &config)
 
std::string getPluginID (const std::shared_ptr< PluginFactory< Reasoner >> &factory, const boost::property_tree::ptree &config)
 
std::string getPluginID (const std::shared_ptr< PluginFactory< Reasoner >> &factory, const boost::property_tree::ptree &config)
 
- Static Protected Member Functions inherited from knowrob::PluginManager< Reasoner >
static uint32_t & managerIDCounter ()
 
static uint32_t & managerIDCounter ()
 
static auto & pluginFactories ()
 
static auto & pluginFactories ()
 
static auto & pluginManagers ()
 
static auto & pluginManagers ()
 
- Protected Attributes inherited from knowrob::PluginManager< Reasoner >
std::shared_ptr< std::map< uint32_t, PluginManager * > > pluginManagers_
 
std::mutex staticMutex_
 
uint32_t managerID_
 
uint32_t pluginIndex_
 
std::map< std::string_view, std::shared_ptr< NamedPlugin< Reasoner > >, std::less<> > pluginPool_
 
std::map< std::string, std::shared_ptr< PluginLibrary< Reasoner > >, std::less<> > loadedPlugins_
 
std::map< std::string, std::shared_ptr< PluginModule< Reasoner > >, std::less<> > loadedModules_
 

Detailed Description

Manages a set of available reasoning subsystems.

Definition at line 19 of file ReasonerManager.h.

Constructor & Destructor Documentation

◆ ReasonerManager() [1/2]

ReasonerManager::ReasonerManager ( KnowledgeBase kb,
const std::shared_ptr< StorageManager > &  backendManager 
)

Create a new reasoner manager.

Parameters
kbthe knowledge base associated with this manager.
backendManagerthe backend manager associated with this manager.

Definition at line 12 of file ReasonerManager.cpp.

13  : PluginManager(),
14  kb_(kb),
15  backendManager_(backendManager) {
16 }

◆ ~ReasonerManager() [1/2]

ReasonerManager::~ReasonerManager ( )
override

Definition at line 18 of file ReasonerManager.cpp.

18  {
19  std::lock_guard<std::mutex> scoped_lock(staticMutex_);
20  for (auto &x: pluginPool_) {
21  x.second->value()->unload();
22  // make sure reasoner does not interact with the manager anymore
23  x.second->value()->setReasonerManager(nullptr);
24  }
25  pluginPool_.clear();
26 }
std::map< std::string_view, std::shared_ptr< NamedPlugin< Reasoner > >, std::less<> > pluginPool_

◆ ReasonerManager() [2/2]

knowrob::ReasonerManager::ReasonerManager ( KnowledgeBase kb,
const std::shared_ptr< StorageManager > &  backendManager 
)

Create a new reasoner manager.

Parameters
kbthe knowledge base associated with this manager.
backendManagerthe backend manager associated with this manager.

◆ ~ReasonerManager() [2/2]

knowrob::ReasonerManager::~ReasonerManager ( )
override

Member Function Documentation

◆ addPlugin() [1/2]

std::shared_ptr< NamedReasoner > ReasonerManager::addPlugin ( std::string_view  reasonerID,
PluginLanguage  language,
const std::shared_ptr< Reasoner > &  reasoner 
)
overridevirtual

Add a plugin to this manager.

Parameters
reasonerIDthe name of the reasoner
languagethe language of the plugin
reasonerthe reasoner instance

Implements knowrob::PluginManager< Reasoner >.

Definition at line 147 of file ReasonerManager.cpp.

147  {
148  if (pluginPool_.find(reasonerID) != pluginPool_.end()) {
149  KB_WARN("overwriting reasoner with name '{}'", reasonerID);
150  }
151  auto managedReasoner = std::make_shared<NamedReasoner>(reasonerID, language, reasoner);
152  pluginPool_.emplace(managedReasoner->name(), managedReasoner);
153  reasoner->setReasonerManager(this);
154  reasoner->setReasonerName(reasonerID);
155  initPlugin(managedReasoner);
156  // indicate that the origin `reasonerID` is a reasoner, and thus belongs to the session
157  backendManager_->vocabulary()->importHierarchy()->addDirectImport(
158  backendManager_->vocabulary()->importHierarchy()->ORIGIN_REASONER, reasonerID);
159 
160  // check if reasoner implements DataBackend interface
161  auto backend = std::dynamic_pointer_cast<Storage>(reasoner);
162  if (backend) {
163  setReasonerStorage(managedReasoner, backend);
164  backendManager_->addPlugin(reasonerID, language, backend);
165  }
166 
167  return managedReasoner;
168 }
#define KB_WARN
Definition: Logger.h:27

◆ addPlugin() [2/2]

std::shared_ptr<NamedReasoner> knowrob::ReasonerManager::addPlugin ( std::string_view  reasonerID,
PluginLanguage  language,
const std::shared_ptr< Reasoner > &  reasoner 
)
overridevirtual

Add a plugin to this manager.

Parameters
reasonerIDthe name of the reasoner
languagethe language of the plugin
reasonerthe reasoner instance

Implements knowrob::PluginManager< Reasoner >.

◆ backendManager() [1/2]

auto knowrob::ReasonerManager::backendManager ( ) const
inline
Returns
the backend manager associated with this manager.

Definition at line 38 of file ReasonerManager.h.

38 { return backendManager_; }

◆ backendManager() [2/2]

auto knowrob::ReasonerManager::backendManager ( ) const
inline
Returns
the backend manager associated with this manager.

Definition at line 38 of file ReasonerManager.h.

38 { return backendManager_; }

◆ dataDriven() [1/2]

auto& knowrob::ReasonerManager::dataDriven ( ) const
inline
Returns
the data-driven reasoners defined by this manager.

Definition at line 48 of file ReasonerManager.h.

48 { return dataDriven_; }

◆ dataDriven() [2/2]

auto& knowrob::ReasonerManager::dataDriven ( ) const
inline
Returns
the data-driven reasoners defined by this manager.

Definition at line 48 of file ReasonerManager.h.

48 { return dataDriven_; }

◆ evaluateQuery() [1/2]

TokenBufferPtr ReasonerManager::evaluateQuery ( const GoalDrivenReasonerPtr reasoner,
const std::vector< FirstOrderLiteralPtr > &  literals,
const QueryContextPtr ctx 
)
static

Evaluate a query using a goal-driven reasoner.

Parameters
reasonerthe reasoner to use.
literalsthe query to evaluate.
ctxthe query context.
Returns
a buffer that can be used to retrieve the results of the query.

Definition at line 196 of file ReasonerManager.cpp.

199  {
200  auto reasonerRunner = std::make_shared<ReasonerRunner>();
201  reasonerRunner->reasoner = reasoner;
202  if(literals.size()>1) {
203  auto conjunction = std::make_shared<SimpleConjunction>(literals);
204  reasonerRunner->query = std::make_shared<Goal>(conjunction, ctx);
205  } else if (literals.size() == 1) {
206  reasonerRunner->query = std::make_shared<Goal>(literals[0], ctx);
207  } else {
208  throw ReasonerError("Reasoner {} received an empty query.", *reasoner->reasonerName());
209  }
210  KB_DEBUG("Evaluating query `{}` with reasoner \"{}\"",
211  *reasonerRunner->query->formula(), *reasoner->reasonerName());
212  // avoid that the exception handler keeps a reference to the reasonerRunner as
213  // this would prevent the runner from being destroyed.
214  auto reasonerRunnerPtr = reasonerRunner.get();
215  // run reasoner in a thread
216  DefaultThreadPool()->pushWork(
217  reasonerRunner,
218  [reasonerRunnerPtr](const std::exception &exc) {
219  KB_ERROR("Reasoner {} produced an error in query evaluation. {} [{}]",
220  *reasonerRunnerPtr->reasoner->reasonerName(), exc.what(), *reasonerRunnerPtr->query->formula());
221  reasonerRunnerPtr->query->finish();
222  });
223  // return the (incomplete) answer buffer
224  return reasonerRunner->query->answerBuffer();
225 }
#define KB_DEBUG
Definition: Logger.h:25
#define KB_ERROR
Definition: Logger.h:28
std::shared_ptr< ThreadPool > DefaultThreadPool()
Definition: ThreadPool.cpp:19

◆ evaluateQuery() [2/2]

static TokenBufferPtr knowrob::ReasonerManager::evaluateQuery ( const GoalDrivenReasonerPtr reasoner,
const std::vector< FirstOrderLiteralPtr > &  literals,
const QueryContextPtr ctx 
)
static

Evaluate a query using a goal-driven reasoner.

Parameters
reasonerthe reasoner to use.
literalsthe query to evaluate.
ctxthe query context.
Returns
a buffer that can be used to retrieve the results of the query.

◆ findDefiningReasoner() [1/2]

std::vector< DefiningReasoner > ReasonerManager::findDefiningReasoner ( const PredicateIndicator indicator) const
Parameters
indicatora predicate indicator.
Returns
the reasoners that define the given relation.

Definition at line 48 of file ReasonerManager.cpp.

48  {
49  std::vector<DefiningReasoner> reasoners;
50 
51  if (indicator.arity() == 1) {
52  // unary predicates can be RDF class expressions.
53  // in this case we need to check the class hierarchy.
54  auto rdfClass = kb_->vocabulary()->getDefinedClass(indicator.functor()->stringForm());
55  if (rdfClass) {
56  for (auto &x: goalDriven_) {
57  for (auto &definedClassIndicator : x.second->definedClasses()) {
58  auto definedClass = kb_->vocabulary()->getDefinedClass(definedClassIndicator.functor()->stringForm());
59  if(definedClass && definedClass->isSubClassOf(rdfClass)) {
60  reasoners.push_back({x.second, definedClass->iriAtom()});
61  break;
62  }
63  }
64  }
65  return reasoners;
66  }
67  }
68 
69  if (indicator.arity() == 2) {
70  // binary predicates can be RDF properties.
71  // in this case we need to check the property hierarchy.
72  auto rdfProperty = kb_->vocabulary()->getDefinedProperty(indicator.functor()->stringForm());
73  if (rdfProperty) {
74  for (auto &x: goalDriven_) {
75  for (auto &definedPropertyIndicator : x.second->definedRelations()) {
76  auto definedProperty = kb_->vocabulary()->getDefinedProperty(definedPropertyIndicator.functor()->stringForm());
77  if ( definedProperty && definedProperty->isSubPropertyOf(rdfProperty) ) {
78  reasoners.push_back({x.second, definedProperty->iriAtom()});
79  break;
80  }
81  }
82  }
83  return reasoners;
84  }
85  }
86 
87  for (auto &x: goalDriven_) {
88  if (x.second->isRelationDefined(indicator) || (indicator.arity()==1 && x.second->isClassDefined(indicator.functor()->stringForm()))) {
89  reasoners.push_back({x.second, indicator.functor()});
90  }
91  }
92  return reasoners;
93 }
auto & vocabulary() const
Definition: KnowledgeBase.h:67

◆ findDefiningReasoner() [2/2]

std::vector<DefiningReasoner> knowrob::ReasonerManager::findDefiningReasoner ( const PredicateIndicator indicator) const
Parameters
indicatora predicate indicator.
Returns
the reasoners that define the given relation.

◆ getReasonerStorage() [1/2]

std::shared_ptr< Storage > ReasonerManager::getReasonerStorage ( const std::shared_ptr< NamedReasoner > &  reasoner)

Return the backend associated with a reasoner if any.

Parameters
reasonera defined reasoner.
Returns
a backend or a null reference.

Definition at line 39 of file ReasonerManager.cpp.

39  {
40  auto it = reasonerBackends_.find(reasoner->name());
41  if (it != reasonerBackends_.end()) {
42  return it->second;
43  } else {
44  return nullptr;
45  }
46 }

◆ getReasonerStorage() [2/2]

std::shared_ptr<Storage> knowrob::ReasonerManager::getReasonerStorage ( const std::shared_ptr< NamedReasoner > &  reasoner)

Return the backend associated with a reasoner if any.

Parameters
reasonera defined reasoner.
Returns
a backend or a null reference.

◆ goalDriven() [1/2]

auto& knowrob::ReasonerManager::goalDriven ( ) const
inline
Returns
the goal-driven reasoners defined by this manager.

Definition at line 43 of file ReasonerManager.h.

43 { return goalDriven_; }

◆ goalDriven() [2/2]

auto& knowrob::ReasonerManager::goalDriven ( ) const
inline
Returns
the goal-driven reasoners defined by this manager.

Definition at line 43 of file ReasonerManager.h.

43 { return goalDriven_; }

◆ kb() [1/2]

auto knowrob::ReasonerManager::kb ( ) const
inline
Returns
the knowledge base associated with this manager.

Definition at line 33 of file ReasonerManager.h.

33 { return kb_; }

◆ kb() [2/2]

auto knowrob::ReasonerManager::kb ( ) const
inline
Returns
the knowledge base associated with this manager.

Definition at line 33 of file ReasonerManager.h.

33 { return kb_; }

◆ loadPlugin() [1/2]

std::shared_ptr< NamedReasoner > ReasonerManager::loadPlugin ( const boost::property_tree::ptree &  config)
overridevirtual

Load a new plugin instance into the plugin manager. The type of the plugin is determined based on either the value of "type", "lib" or "module" in the property tree root. The tree is further used to generate a plugin configuration. Plugin factories for libraries are created on the fly, the ones for built-in plugin types need to be added to the plugin manager before.

Parameters
configa property tree holding a plugin configuration

Implements knowrob::PluginManager< Reasoner >.

Definition at line 95 of file ReasonerManager.cpp.

95  {
96  // get a reasoner factory
97  std::shared_ptr<ReasonerFactory> factory = findFactory(config);
98  // make sure factory was found above
99  if (!factory) throw ReasonerError("failed to load a reasoner.");
100  // create a reasoner id, or use name property
101  std::string reasonerID = getPluginID(factory, config);
102  KB_INFO("Using reasoner `{}` with type `{}`.", reasonerID, factory->name());
103 
104  // create a new reasoner instance
105  auto reasoner = factory->create(reasonerID);
106  // reasoner need to have a reference to the reasoner manager such that
107  // predicates can be defined that interact with the KB
108  reasoner->value()->setReasonerManager(this);
109  reasoner->value()->setReasonerName(reasonerID);
110 
111  auto backendName = config.get_optional<std::string>("data-backend");
112  if (backendName.has_value()) {
113  auto definedBackend = backendManager_->getPluginWithID(backendName.value());
114  if (definedBackend) {
115  setReasonerStorage(reasoner, definedBackend->value());
116  } else {
117  throw ReasonerError("Reasoner `{}` refers to unknown data-backend `{}`.", reasonerID, backendName.value());
118  }
119  } else {
120  // check if reasoner implements DataBackend interface
121  auto backend = std::dynamic_pointer_cast<Storage>(reasoner->value());
122  if (backend) {
123  setReasonerStorage(reasoner, backend);
124  backendManager_->addPlugin(reasonerID, reasoner->language(), backend);
125  } else {
126  throw ReasonerError("Reasoner `{}` has no 'data-backend' configured.", reasonerID);
127  }
128  }
129  auto definedReasoner = addPlugin(reasonerID, reasoner->language(), reasoner->value());
130 
131  PropertyTree pluginConfig(std::make_shared<boost::property_tree::ptree>(config));
132  if (!initializeReasoner(reasoner, pluginConfig)) {
133  KB_WARN("Reasoner `{}` failed to loadConfig.", reasonerID);
134  } else {
135  // load the reasoner-specific data sources.
136  for (auto &dataSource: pluginConfig.dataSources()) {
137  if (!reasoner->value()->loadDataSource(dataSource)) {
138  KB_WARN("Reasoner `{}` failed to load data source {}.", reasonerID, dataSource->uri());
139  }
140  }
141  }
142 
143  return definedReasoner;
144 }
#define KB_INFO
Definition: Logger.h:26
std::shared_ptr< PluginFactory< Reasoner > > findFactory(const boost::property_tree::ptree &config)
std::string getPluginID(const std::shared_ptr< PluginFactory< Reasoner >> &factory, const boost::property_tree::ptree &config)
std::shared_ptr< NamedReasoner > addPlugin(std::string_view reasonerID, PluginLanguage language, const std::shared_ptr< Reasoner > &reasoner) override
TermRule & string()
Definition: terms.cpp:63

◆ loadPlugin() [2/2]

std::shared_ptr<NamedReasoner> knowrob::ReasonerManager::loadPlugin ( const boost::property_tree::ptree &  config)
overridevirtual

Load a new plugin instance into the plugin manager. The type of the plugin is determined based on either the value of "type", "lib" or "module" in the property tree root. The tree is further used to generate a plugin configuration. Plugin factories for libraries are created on the fly, the ones for built-in plugin types need to be added to the plugin manager before.

Parameters
configa property tree holding a plugin configuration

Implements knowrob::PluginManager< Reasoner >.


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