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 }
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 146 of file ReasonerManager.cpp.

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

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

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

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

◆ 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 94 of file ReasonerManager.cpp.

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