knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
semweb.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 #define PL_SAFE_ARG_MACROS
7 
8 #include <SWI-cpp.h>
9 
10 #include "knowrob/reasoner/prolog/semweb.h"
11 #include "knowrob/reasoner/prolog/PrologReasoner.h"
12 #include "knowrob/reasoner/ReasonerManager.h"
13 #include "knowrob/semweb/ImportHierarchy.h"
14 #include "knowrob/semweb/OntologyFile.h"
15 #include "knowrob/URI.h"
16 
17 using namespace knowrob;
18 
19 static inline std::shared_ptr<ImportHierarchy> getImportHierarchy(term_t t_manager, term_t t_reasoner) {
20  static std::shared_ptr<ImportHierarchy> null;
21  auto definedReasoner =
22  PrologReasoner::getDefinedReasoner(t_manager, t_reasoner);
23  if (!definedReasoner) return null;
24  auto prologReasoner =
25  std::dynamic_pointer_cast<PrologReasoner>(definedReasoner->value());
26  return prologReasoner ? prologReasoner->reasonerManager().backendManager()->vocabulary()->importHierarchy() : null;
27 }
28 
29 static inline KnowledgeBase *getKnowledgeBase(term_t t_manager, term_t t_reasoner) {
30  auto definedReasoner =
31  PrologReasoner::getDefinedReasoner(t_manager, t_reasoner);
32  if (!definedReasoner) return nullptr;
33  return definedReasoner->value()->reasonerManager().kb();
34 }
35 
36 foreign_t sw_current_graph3(term_t t_manager, term_t t_reasoner, term_t t_graph) {
37  auto hierarchy = getImportHierarchy(t_manager, t_reasoner);
38 
39  char *graph;
40  if (hierarchy && PL_get_atom_chars(t_graph, &graph)) {
41  return hierarchy->isCurrentGraph(graph);
42  } else {
43  return false;
44  }
45 }
46 
47 foreign_t sw_set_current_graph3(term_t t_manager, term_t t_reasoner, term_t t_graph) {
48  auto hierarchy = getImportHierarchy(t_manager, t_reasoner);
49 
50  char *graph;
51  if (hierarchy && PL_get_atom_chars(t_graph, &graph)) {
52  hierarchy->addCurrentGraph(graph);
53  return true;
54  } else {
55  return false;
56  }
57 }
58 
59 foreign_t sw_unset_current_graph3(term_t t_manager, term_t t_reasoner, term_t t_origin) {
60  auto kb = getKnowledgeBase(t_manager, t_reasoner);
61  if(!kb) return false;
62 
63  char *origin;
64  if (PL_get_atom_chars(t_origin, &origin)) {
65  return kb->removeAllWithOrigin(origin);
66  } else {
67  return false;
68  }
69 }
70 
71 foreign_t sw_graph_add_direct_import4(term_t t_manager, term_t t_reasoner, term_t t_importer, term_t t_imported) {
72  auto hierarchy = getImportHierarchy(t_manager, t_reasoner);
73 
74  char *importer, *imported;
75  if (hierarchy &&
76  PL_get_atom_chars(t_importer, &importer) &&
77  PL_get_atom_chars(t_imported, &imported)) {
78  hierarchy->addDirectImport(importer, imported);
79  return true;
80  } else {
81  return false;
82  }
83 }
84 
85 foreign_t sw_graph_get_imports4(term_t t_manager, term_t t_reasoner, term_t t_importer, term_t t_importedList) {
86  auto hierarchy = getImportHierarchy(t_manager, t_reasoner);
87  char *importer;
88  if (hierarchy && PL_get_atom_chars(t_importer, &importer)) {
89  PlTail l(t_importedList);
90  for (auto &x: hierarchy->getImports(importer))
91  l.append(x->name().c_str());
92  l.close();
93  return true;
94  }
95  return false;
96 }
97 
98 foreign_t sw_set_default_graph3(term_t t_manager, term_t t_reasoner, term_t t_graph) {
99  auto hierarchy = getImportHierarchy(t_manager, t_reasoner);
100  char *graph;
101  if (hierarchy && PL_get_atom_chars(t_graph, &graph)) {
102  hierarchy->setDefaultGraph(graph);
103  return true;
104  }
105  return false;
106 
107 }
108 
109 foreign_t sw_default_graph3(term_t t_manager, term_t t_reasoner, term_t t_graph) {
110  auto hierarchy = getImportHierarchy(t_manager, t_reasoner);
111  return hierarchy && PL_unify_atom_chars(t_graph, hierarchy->defaultGraph().c_str());
112 }
113 
114 foreign_t sw_origin_any1(term_t t_origin) {
115  return PL_unify_atom_chars(t_origin, ImportHierarchy::ORIGIN_ANY.data());
116 }
117 
118 foreign_t sw_origin_system1(term_t t_origin) {
119  return PL_unify_atom_chars(t_origin, ImportHierarchy::ORIGIN_SYSTEM.data());
120 }
121 
122 foreign_t sw_origin_session1(term_t t_origin) {
123  return PL_unify_atom_chars(t_origin, ImportHierarchy::ORIGIN_SESSION.data());
124 }
125 
126 foreign_t sw_origin_user1(term_t t_origin) {
127  return PL_unify_atom_chars(t_origin, ImportHierarchy::ORIGIN_USER.data());
128 }
129 
130 foreign_t sw_origin_reasoner1(term_t t_origin) {
131  return PL_unify_atom_chars(t_origin, ImportHierarchy::ORIGIN_REASONER.data());
132 }
133 
134 foreign_t sw_origin_test1(term_t t_origin) {
135  return PL_unify_atom_chars(t_origin, ImportHierarchy::ORIGIN_TEST.data());
136 }
137 
138 foreign_t sw_url_graph2(term_t t_url, term_t t_graph) {
139  char *url;
140  if (PL_get_atom_chars(t_url, &url)) {
141  auto name = DataSource::getNameFromURI(url);
142  return PL_unify_atom_chars(t_graph, name.c_str());
143  }
144  return false;
145 }
146 
147 foreign_t sw_url_version2(term_t t_url, term_t t_version) {
148  char *url;
149  if (PL_get_atom_chars(t_url, &url)) {
150  auto version = DataSource::getVersionFromURI(url);
151  return PL_unify_atom_chars(t_version, version.c_str());
152  }
153  return false;
154 }
155 
156 foreign_t sw_load_rdf_xml4(term_t t_manager, term_t t_reasoner, term_t t_url, term_t t_parentGraph) {
157  auto kb = getKnowledgeBase(t_manager, t_reasoner);
158  char *url, *parentGraph;
159  if (kb && PL_get_atom_chars(t_url, &url) && PL_get_atom_chars(t_parentGraph, &parentGraph)) {
160  URI ontologyURI(url);
161  auto ontologyFile = std::make_shared<OntologyFile>(kb->vocabulary(), ontologyURI, "rdf-xml");
162  ontologyFile->setParentOrigin(parentGraph);
163  if (kb->loadDataSource(ontologyFile)) {
164  return true;
165  }
166  }
167  return false;
168 }
169 
170 namespace knowrob::prolog {
171  PL_extension PL_extension_semweb[] = {
172  {"sw_url_graph", 2, (pl_function_t) sw_url_graph2, 0},
173  {"sw_url_version", 2, (pl_function_t) sw_url_version2, 0},
174  {"sw_default_graph_cpp", 3, (pl_function_t) sw_default_graph3, 0},
175  {"sw_origin_any", 1, (pl_function_t) sw_origin_any1, 0},
176  {"sw_origin_system", 1, (pl_function_t) sw_origin_system1, 0},
177  {"sw_origin_session", 1, (pl_function_t) sw_origin_session1, 0},
178  {"sw_origin_user", 1, (pl_function_t) sw_origin_user1, 0},
179  {"sw_origin_reasoner", 1, (pl_function_t) sw_origin_reasoner1, 0},
180  {"sw_origin_test", 1, (pl_function_t) sw_origin_test1, 0},
181  {"sw_set_default_graph_cpp", 3, (pl_function_t) sw_set_default_graph3, 0},
182  {"sw_graph_get_imports_cpp", 4, (pl_function_t) sw_graph_get_imports4, 0},
183  {"sw_graph_add_direct_import_cpp", 4, (pl_function_t) sw_graph_add_direct_import4, 0},
184  {"sw_current_graph_cpp", 3, (pl_function_t) sw_current_graph3, 0},
185  {"sw_set_current_graph_cpp", 3, (pl_function_t) sw_set_current_graph3, 0},
186  {"sw_unset_current_graph_cpp", 3, (pl_function_t) sw_unset_current_graph3, 0},
187  {"sw_load_rdf_xml_cpp", 4, (pl_function_t) sw_load_rdf_xml4, 0},
188  {nullptr, 0, nullptr, 0}
189  };
190 }
static std::string getNameFromURI(const std::string &uriString)
Definition: DataSource.cpp:36
static std::string getVersionFromURI(const std::string &uriString)
Definition: DataSource.cpp:48
static constexpr std::string_view ORIGIN_TEST
static constexpr std::string_view ORIGIN_SESSION
static constexpr std::string_view ORIGIN_USER
static constexpr std::string_view ORIGIN_REASONER
static constexpr std::string_view ORIGIN_SYSTEM
static constexpr std::string_view ORIGIN_ANY
static std::shared_ptr< NamedReasoner > getDefinedReasoner(const term_t &t_reasonerManager, const term_t &t_reasonerModule)
PL_extension PL_extension_semweb[]
Definition: semweb.h:12
foreign_t sw_load_rdf_xml4(term_t t_manager, term_t t_reasoner, term_t t_url, term_t t_parentGraph)
Definition: semweb.cpp:156
foreign_t sw_origin_any1(term_t t_origin)
Definition: semweb.cpp:114
foreign_t sw_url_version2(term_t t_url, term_t t_version)
Definition: semweb.cpp:147
foreign_t sw_url_graph2(term_t t_url, term_t t_graph)
Definition: semweb.cpp:138
foreign_t sw_origin_reasoner1(term_t t_origin)
Definition: semweb.cpp:130
foreign_t sw_current_graph3(term_t t_manager, term_t t_reasoner, term_t t_graph)
Definition: semweb.cpp:36
foreign_t sw_origin_system1(term_t t_origin)
Definition: semweb.cpp:118
foreign_t sw_graph_add_direct_import4(term_t t_manager, term_t t_reasoner, term_t t_importer, term_t t_imported)
Definition: semweb.cpp:71
foreign_t sw_set_default_graph3(term_t t_manager, term_t t_reasoner, term_t t_graph)
Definition: semweb.cpp:98
foreign_t sw_graph_get_imports4(term_t t_manager, term_t t_reasoner, term_t t_importer, term_t t_importedList)
Definition: semweb.cpp:85
foreign_t sw_unset_current_graph3(term_t t_manager, term_t t_reasoner, term_t t_origin)
Definition: semweb.cpp:59
foreign_t sw_origin_user1(term_t t_origin)
Definition: semweb.cpp:126
foreign_t sw_origin_session1(term_t t_origin)
Definition: semweb.cpp:122
foreign_t sw_default_graph3(term_t t_manager, term_t t_reasoner, term_t t_graph)
Definition: semweb.cpp:109
foreign_t sw_set_current_graph3(term_t t_manager, term_t t_reasoner, term_t t_graph)
Definition: semweb.cpp:47
foreign_t sw_origin_test1(term_t t_origin)
Definition: semweb.cpp:134