knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
ImportHierarchy.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 <memory>
7 #include "knowrob/semweb/ImportHierarchy.h"
8 #include "knowrob/Logger.h"
9 #include "knowrob/integration/python/utils.h"
10 
11 using namespace knowrob;
12 
14  : defaultGraph_(ORIGIN_USER) {
20 }
21 
22 bool ImportHierarchy::isReservedOrigin(std::string_view origin) {
23  static std::set<std::string_view> reservedOrigins = {ORIGIN_SYSTEM, ORIGIN_SESSION, ORIGIN_USER, ORIGIN_REASONER, ORIGIN_TEST};
24  return reservedOrigins.count(origin) > 0;
25 }
26 
27 bool ImportHierarchy::isCurrentGraph(std::string_view graphName) const {
28  return graphs_.count(graphName) > 0;
29 }
30 
32  auto originSystem = getCurrentGraph(ORIGIN_SYSTEM);
33  return graph == originSystem || originSystem.imports_.count(&graph) > 0;
34 }
35 
37  auto originSession = getCurrentGraph(ORIGIN_SESSION);
38  return graph == originSession || originSession.imports_.count(&graph) > 0;
39 }
40 
41 void ImportHierarchy::addCurrentGraph(std::string_view graphName) {
42  auto it = graphs_.find(graphName);
43  if (it == graphs_.end()) {
44  auto newGraph = new CurrentGraph(graphName);
45  graphs_.emplace(newGraph->name(), newGraph);
46  }
47 }
48 
49 const std::set<CurrentGraph *> &ImportHierarchy::getImports(std::string_view graphName) {
50  auto it = graphs_.find(graphName);
51  if (it == graphs_.end()) {
52  static std::set<CurrentGraph *> empty;
53  return empty;
54  } else {
55  return it->second->imports_;
56  }
57 }
58 
60  auto it1 = graphs_.find(name);
61  if (it1 == graphs_.end()) {
62  auto newGraph = new CurrentGraph(name);
63  auto inserted = graphs_.emplace(newGraph->name(), newGraph);
64  it1 = inserted.first;
65  }
66  return *it1->second;
67 }
68 
69 void ImportHierarchy::addDirectImport(std::string_view importerGraphName, std::string_view importedGraphName) {
70  auto &g_importer = getCurrentGraph(importerGraphName);
71  auto g_imported = &(getCurrentGraph(importedGraphName));
72 
73  // if a system graph imports a session graph, remove the session graph from the session
74  // before adding to the system. And if a session graph imports a system graph, ignore the import.
75  if (isSystemOrigin(g_importer) && isSessionOrigin(*g_imported)) {
76  removeCurrentGraph(importedGraphName);
77  g_imported = &(getCurrentGraph(importedGraphName));
78  } else if (isSessionOrigin(g_importer) && isSystemOrigin(*g_imported)) {
79  KB_WARN("Ignoring session graph \"{}\" import of system graph \"{}\".", importerGraphName, importedGraphName);
80  return;
81  }
82 
83  // g_importer.directlyImports += [g_imported]
84  auto pair = g_importer.directImports_.insert(g_imported);
85  if (!pair.second) return;
86  KB_DEBUG("Graph \"{}\" imports \"{}\".", importerGraphName, importedGraphName);
87  // g_importer.imports += g_imported.imports + [gb]
88  g_importer.imports_.insert(g_imported->imports_.begin(), g_imported->imports_.end());
89  g_importer.imports_.insert(g_imported);
90  // for every graph gx that imports g_importer:
91  // gx.imports += (g_imported.imports + [g_imported])
92  for (auto &it: graphs_) {
93  if (it.second->imports_.count(&g_importer) > 0) {
94  it.second->imports_.insert(g_imported->imports_.begin(), g_imported->imports_.end());
95  it.second->imports_.insert(g_imported);
96  }
97  }
98 }
99 
100 void ImportHierarchy::removeCurrentGraph(std::string_view graphName) {
101  auto it = graphs_.find(graphName);
102  if (it == graphs_.end()) return;
103  auto &ga = *it->second;
104 
105  // for every graph gx that directly imports ga
106  for (auto &jt: graphs_) {
107  auto &gx = *jt.second;
108  if (gx.directImports_.count(&ga) > 0) {
109  // gx.directlyImports -= [ga]
110  gx.directImports_.erase(&ga);
111  gx.imports_.erase(&ga);
112  // gx.directlyImports += [ga.directImports]
113  gx.directImports_.insert(ga.directImports_.begin(), ga.directImports_.end());
114  }
115  }
116  // for every graph gy that imports ga:
117  for (auto &kt: graphs_) {
118  auto &gy = *kt.second;
119  if (gy.imports_.count(&ga) > 0) {
120  // gy.imports -= [ga]
121  gy.imports_.erase(&ga);
122  }
123  }
124 
125  // finally delete the node
126  graphs_.erase(it);
127 }
128 
129 namespace knowrob::py {
130  template<>
132  using namespace boost::python;
133 
134  class_<ImportHierarchy, std::shared_ptr<ImportHierarchy>, boost::noncopyable>
135  ("ImportHierarchy", init<>())
136  .def("isCurrentGraph", &ImportHierarchy::isCurrentGraph)
137  .def("isReservedOrigin", &ImportHierarchy::isReservedOrigin)
138  .def("clear", &ImportHierarchy::clear)
139  .def("setDefaultGraph", &ImportHierarchy::setDefaultGraph)
140  .def("defaultGraph", &ImportHierarchy::defaultGraph, return_value_policy<copy_const_reference>())
141  .def("addCurrentGraph", &ImportHierarchy::addCurrentGraph)
142  .def("removeCurrentGraph", &ImportHierarchy::removeCurrentGraph)
143  .def("addDirectImport", &ImportHierarchy::addDirectImport)
144  .def("getImports", &ImportHierarchy::getImports, return_value_policy<copy_const_reference>());
145  }
146 }
#define KB_DEBUG
Definition: Logger.h:25
#define KB_WARN
Definition: Logger.h:27
std::set< CurrentGraph * > imports_
Definition: CurrentGraph.h:48
CurrentGraph & getCurrentGraph(std::string_view name)
std::map< std::string_view, std::unique_ptr< CurrentGraph > > graphs_
void addDirectImport(std::string_view importerGraphName, std::string_view importedGraphName)
static bool isReservedOrigin(std::string_view origin)
bool isCurrentGraph(std::string_view graphName) const
static constexpr std::string_view ORIGIN_TEST
bool isSessionOrigin(CurrentGraph &graph)
const auto & defaultGraph() const
void setDefaultGraph(std::string_view defaultGraph)
static constexpr std::string_view ORIGIN_SESSION
void removeCurrentGraph(std::string_view graphName)
static constexpr std::string_view ORIGIN_USER
static constexpr std::string_view ORIGIN_REASONER
static constexpr std::string_view ORIGIN_SYSTEM
const std::set< CurrentGraph * > & getImports(std::string_view graphName)
static constexpr std::string_view ORIGIN_ANY
void addCurrentGraph(std::string_view graphName)
bool isSystemOrigin(CurrentGraph &graph)
void createType< ImportHierarchy >()