knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
StorageManager.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of KnowRob, please consult
3  * https://github.com/knowrob/knowrob for license details.
4  */
5 
6 #include <filesystem>
7 
8 #include "knowrob/Logger.h"
9 #include "knowrob/storage/StorageManager.h"
10 #include "knowrob/storage/StorageError.h"
11 #include "knowrob/storage/QueryableStorage.h"
12 
13 using namespace knowrob;
14 
15 StorageManager::StorageManager(const std::shared_ptr<Vocabulary> &vocabulary)
16  : PluginManager(),
17  vocabulary_(vocabulary) {
18 }
19 
20 std::shared_ptr<NamedBackend> StorageManager::loadPlugin(const boost::property_tree::ptree &config) {
21  // get a backend factory
22  std::shared_ptr<BackendFactory> factory = findFactory(config);
23  // make sure factory was found above
24  if (!factory) throw StorageError("failed to load a backend.");
25  // create a reasoner id, or use name property
26  std::string backendID = getPluginID(factory, config);
27  KB_INFO("Using backend `{}` with type `{}`.", backendID, factory->name());
28 
29  // create a new DataBackend instance
30  auto definedBackend = factory->create(backendID);
31  definedBackend->value()->setVocabulary(vocabulary());
32 
33  PropertyTree pluginConfig(std::make_shared<boost::property_tree::ptree>(config));
34  bool success;
35  {
36  if (definedBackend->language() == PluginLanguage::PYTHON) {
37  py::gil_lock lock;
38  success = definedBackend->value()->initializeBackend(pluginConfig);
39  } else {
40  success = definedBackend->value()->initializeBackend(pluginConfig);
41  }
42  }
43  if (!success) {
44  KB_WARN("Backend `{}` failed to loadConfig.", backendID);
45  } else {
46  addPlugin(definedBackend);
47  }
48 
49  return definedBackend;
50 }
51 
52 std::shared_ptr<NamedBackend> StorageManager::addPlugin(std::string_view backendID, PluginLanguage language, const StoragePtr &backend) {
53  if (pluginPool_.find(backendID) != pluginPool_.end()) {
54  KB_WARN("Overwriting backend with name '{}'", backendID);
55  }
56  auto managedBackend = std::make_shared<NamedBackend>(backendID, language, backend);
57  pluginPool_.emplace(managedBackend->name(), managedBackend);
58  initBackend(managedBackend);
59  return managedBackend;
60 }
61 
62 void StorageManager::addPlugin(const std::shared_ptr<NamedBackend> &definedKG) {
63  if (pluginPool_.find(definedKG->name()) != pluginPool_.end()) {
64  KB_WARN("Overwriting backend with name '{}'", definedKG->name());
65  }
66  pluginPool_[definedKG->name()] = definedKG;
67  initBackend(definedKG);
68 }
69 
70 void StorageManager::initBackend(const std::shared_ptr<NamedBackend> &definedKG) {
71  definedKG->value()->setStorageLanguage(definedKG->language());
72  definedKG->value()->setVocabulary(vocabulary());
73  // check if the backend is a QueryableBackend, if so store it in the queryable_ map
74  auto queryable = std::dynamic_pointer_cast<QueryableStorage>(definedKG->value());
75  if (queryable) {
76  KB_INFO("Using queryable backend with id '{}'.", definedKG->name());
77  queryable_[definedKG->name()] = queryable;
78  }
79  // check if the backend is a PersistentBackend, if so store it in the persistent_ map
80  if (queryable && queryable->isPersistent()) {
81  KB_INFO("Using persistent backend with id '{}'.", definedKG->name());
82  persistent_[definedKG->name()] = queryable;
83  }
84 }
#define KB_INFO
Definition: Logger.h:26
#define KB_WARN
Definition: Logger.h:27
std::shared_ptr< PluginFactory< Storage > > findFactory(const boost::property_tree::ptree &config)
std::string getPluginID(const std::shared_ptr< PluginFactory< Storage >> &factory, const boost::property_tree::ptree &config)
std::map< std::string_view, std::shared_ptr< NamedPlugin< Storage > >, std::less<> > pluginPool_
StorageManager(const std::shared_ptr< Vocabulary > &vocabulary)
std::shared_ptr< NamedBackend > loadPlugin(const boost::property_tree::ptree &config) override
const auto & queryable() const
void addPlugin(const std::shared_ptr< NamedBackend > &backend)
auto & vocabulary() const
TermRule & string()
Definition: terms.cpp:63
PluginLanguage
Definition: NamedPlugin.h:16
std::shared_ptr< Storage > StoragePtr
Definition: Storage.h:154