6 #define PL_SAFE_ARG_MACROS
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"
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 =
23 if (!definedReasoner)
return null;
25 std::dynamic_pointer_cast<PrologReasoner>(definedReasoner->value());
26 return prologReasoner ? prologReasoner->reasonerManager().backendManager()->vocabulary()->importHierarchy() :
null;
29 static inline KnowledgeBase *getKnowledgeBase(term_t t_manager, term_t t_reasoner) {
30 auto definedReasoner =
32 if (!definedReasoner)
return nullptr;
33 return definedReasoner->value()->reasonerManager().kb();
37 auto hierarchy = getImportHierarchy(t_manager, t_reasoner);
40 if (hierarchy && PL_get_atom_chars(t_graph, &graph)) {
41 return hierarchy->isCurrentGraph(graph);
48 auto hierarchy = getImportHierarchy(t_manager, t_reasoner);
51 if (hierarchy && PL_get_atom_chars(t_graph, &graph)) {
52 hierarchy->addCurrentGraph(graph);
60 auto kb = getKnowledgeBase(t_manager, t_reasoner);
64 if (PL_get_atom_chars(t_origin, &origin)) {
65 return kb->removeAllWithOrigin(origin);
72 auto hierarchy = getImportHierarchy(t_manager, t_reasoner);
74 char *importer, *imported;
76 PL_get_atom_chars(t_importer, &importer) &&
77 PL_get_atom_chars(t_imported, &imported)) {
78 hierarchy->addDirectImport(importer, imported);
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);
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());
99 auto hierarchy = getImportHierarchy(t_manager, t_reasoner);
101 if (hierarchy && PL_get_atom_chars(t_graph, &graph)) {
102 hierarchy->setDefaultGraph(graph);
110 auto hierarchy = getImportHierarchy(t_manager, t_reasoner);
111 return hierarchy && PL_unify_atom_chars(t_graph, hierarchy->defaultGraph().c_str());
140 if (PL_get_atom_chars(t_url, &url)) {
142 return PL_unify_atom_chars(t_graph, name.c_str());
149 if (PL_get_atom_chars(t_url, &url)) {
151 return PL_unify_atom_chars(t_version, version.c_str());
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)) {
188 {
nullptr, 0,
nullptr, 0}
static std::string getNameFromURI(const std::string &uriString)
static std::string getVersionFromURI(const std::string &uriString)
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[]
foreign_t sw_load_rdf_xml4(term_t t_manager, term_t t_reasoner, term_t t_url, term_t t_parentGraph)
foreign_t sw_origin_any1(term_t t_origin)
foreign_t sw_url_version2(term_t t_url, term_t t_version)
foreign_t sw_url_graph2(term_t t_url, term_t t_graph)
foreign_t sw_origin_reasoner1(term_t t_origin)
foreign_t sw_current_graph3(term_t t_manager, term_t t_reasoner, term_t t_graph)
foreign_t sw_origin_system1(term_t t_origin)
foreign_t sw_graph_add_direct_import4(term_t t_manager, term_t t_reasoner, term_t t_importer, term_t t_imported)
foreign_t sw_set_default_graph3(term_t t_manager, term_t t_reasoner, term_t t_graph)
foreign_t sw_graph_get_imports4(term_t t_manager, term_t t_reasoner, term_t t_importer, term_t t_importedList)
foreign_t sw_unset_current_graph3(term_t t_manager, term_t t_reasoner, term_t t_origin)
foreign_t sw_origin_user1(term_t t_origin)
foreign_t sw_origin_session1(term_t t_origin)
foreign_t sw_default_graph3(term_t t_manager, term_t t_reasoner, term_t t_graph)
foreign_t sw_set_current_graph3(term_t t_manager, term_t t_reasoner, term_t t_graph)
foreign_t sw_origin_test1(term_t t_origin)