6 #include <gtest/gtest.h>
8 #include "knowrob/Logger.h"
9 #include "knowrob/reasoner/prolog/PrologTests.h"
10 #include "knowrob/reasoner/ReasonerManager.h"
11 #include "knowrob/reasoner/mongolog/MongologReasoner.h"
12 #include "knowrob/queries/QueryError.h"
13 #include "knowrob/reasoner/ReasonerError.h"
14 #include "knowrob/terms/Numeric.h"
15 #include "knowrob/knowrob.h"
27 foreign_t
pl_uri3(term_t, term_t, term_t);
29 foreign_t
pl_assert_triple_cpp9(term_t, term_t, term_t, term_t, term_t, term_t, term_t, term_t, term_t);
38 static auto unload_f =
"mongolog_reasoner_unload";
45 static bool initialized =
false;
50 consult(std::filesystem::path(
"reasoner") /
"mongolog" /
"__init__.pl",
53 PL_register_foreign(
"mng_is_readonly_cpp",
55 PL_register_foreign(
"mng_db_name_cpp",
57 PL_register_foreign(
"mng_uri_cpp",
60 PL_register_foreign(
"mng_assert_triple_cpp",
79 KB_WARN(
"Falling back to default configuration for MongoDB!");
86 static const auto call_f =
"mongolog_call";
90 static inline std::shared_ptr<MongologReasoner> getMongologReasoner(term_t t_reasonerManager,
91 term_t t_reasonerModule) {
93 if (!definedReasoner) {
94 KB_ERROR(
"unable to find reasoner with id '{}' (manager id: {}).",
99 auto reasoner = definedReasoner->value();
100 auto mongolog = std::dynamic_pointer_cast<MongologReasoner>(reasoner);
102 KB_ERROR(
"reasoner with id '{}' (manager id: {}) is not a mongolog reasoner.",
111 auto mongolog = getMongologReasoner(t_reasonerManager, t_reasonerModule);
113 return mongolog->knowledgeGraph()->isReadOnly();
118 foreign_t
pl_db_name3(term_t t_reasonerManager, term_t t_reasonerModule, term_t t_dbName) {
119 auto mongolog = getMongologReasoner(t_reasonerManager, t_reasonerModule);
121 auto &dbName = mongolog->knowledgeGraph()->dbName();
122 return PL_unify_atom_chars(t_dbName, dbName.c_str());
127 foreign_t
pl_uri3(term_t t_reasonerManager, term_t t_reasonerModule, term_t t_uri) {
128 auto mongolog = getMongologReasoner(t_reasonerManager, t_reasonerModule);
130 auto &uri = mongolog->knowledgeGraph()->dbURI();
131 return PL_unify_atom_chars(t_uri, uri.c_str());
137 term_t t_reasonerModule,
138 term_t t_subjectTerm,
139 term_t t_propertyTerm,
144 term_t t_confidenceTerm) {
145 auto mongolog = getMongologReasoner(t_reasonerManager, t_reasonerModule);
162 auto atomic = std::static_pointer_cast<Atomic>(objectTerm);
163 if (
atomic->isNumeric()) {
165 std::static_pointer_cast<Numeric>(
atomic)->xsdType());
166 }
else if (
atomic->isIRI()) {
168 }
else if (
atomic->isBlank()) {
174 throw QueryError(
"object term {} of triple has an invalid type", *objectTerm);
179 if (!PL_is_variable(t_graphTerm)) {
184 tripleData.
setGraph(mongolog->reasonerManager().backendManager()->vocabulary()->importHierarchy()->defaultGraph());
189 if (!PL_is_variable(t_confidenceTerm)) {
191 if (confidenceTerm->isNumeric()) {
192 tripleData.
setConfidence(std::static_pointer_cast<Numeric>(confidenceTerm)->asDouble());
194 throw QueryError(
"invalid confidence term {}", *confidenceTerm);
200 if (!PL_is_variable(t_beginTerm)) {
202 if (beginTerm->isNumeric()) {
203 tripleData.
setBegin(std::static_pointer_cast<Numeric>(beginTerm)->asDouble());
205 throw QueryError(
"invalid begin term {}", *beginTerm);
211 if (!PL_is_variable(t_endTerm)) {
213 if (endTerm->isNumeric()) {
214 tripleData.
setEnd(std::static_pointer_cast<Numeric>(endTerm)->asDouble());
216 throw QueryError(
"invalid end term {}", *endTerm);
220 mongolog->reasonerManager().kb()->edb()->mergeInsert(mongolog->knowledgeGraph(), tripleData);
224 KB_WARN(
"[mongolog] unable to assert triple: reasoner not found");
232 static std::shared_ptr<MongologReasoner> reasoner_;
233 static std::shared_ptr<KnowledgeBase> kb_;
234 static std::shared_ptr<MongoKnowledgeGraph> db_;
236 static std::shared_ptr<knowrob::MongoKnowledgeGraph>
237 createBackend2(
const std::string &name,
const std::shared_ptr<KnowledgeBase> &kb) {
238 auto kg = std::make_shared<MongoKnowledgeGraph>();
240 kg->initializeBackend(
245 kg->tripleCollection()->createTripleIndex();
249 static std::shared_ptr<MongologReasoner>
250 createReasoner2(
const std::string &name,
const std::shared_ptr<KnowledgeBase> &kb,
251 const std::shared_ptr<MongoKnowledgeGraph> &db) {
252 auto r = std::make_shared<MongologReasoner>();
256 r->load_rdf_xml(
"http://www.ease-crc.org/ont/SOMA.owl");
261 static void SetUpTestSuite() {
265 }
catch (std::exception &e) {
266 FAIL() <<
"SetUpTestSuite failed: " << e.what();
271 static void TearDownTestSuite() {
280 }
catch (std::exception &e) {
281 FAIL() <<
"runTests failed: " << e.what();
285 static std::shared_ptr<MongologReasoner> reasoner() {
287 std::stringstream ss;
292 db_ = createBackend2(ss.str(), kb_);
293 reasoner_ = createReasoner2(ss.str(), kb_, db_);
302 return std::filesystem::path(
"reasoner") /
"mongolog" / filename;
306 std::shared_ptr<MongologReasoner> MongologTests::reasoner_;
307 std::shared_ptr<KnowledgeBase> MongologTests::kb_;
308 std::shared_ptr<MongoKnowledgeGraph> MongologTests::db_;
312 TEST_F(MongologTests, arithmetic) { runTests(getPath(
"iso/arithmetic.pl")); }
314 TEST_F(MongologTests, atoms) { runTests(getPath(
"iso/atoms.pl")); }
316 TEST_F(MongologTests, comparison) { runTests(getPath(
"iso/comparison.pl")); }
318 TEST_F(MongologTests, control) { runTests(getPath(
"iso/control.pl")); }
320 TEST_F(MongologTests, findall) { runTests(getPath(
"iso/findall.pl")); }
322 TEST_F(MongologTests, lists) { runTests(getPath(
"iso/lists.pl")); }
324 TEST_F(MongologTests, meta) { runTests(getPath(
"iso/meta.pl")); }
326 TEST_F(MongologTests, terms) { runTests(getPath(
"iso/terms.pl")); }
328 TEST_F(MongologTests, typecheck) { runTests(getPath(
"iso/typecheck.pl")); }
330 TEST_F(MongologTests, unification) { runTests(getPath(
"iso/unification.pl")); }
332 TEST_F(MongologTests, database) { runTests(getPath(
"database.pl")); }
334 TEST_F(MongologTests, fluents) { runTests(getPath(
"fluents.pl")); }
336 TEST_F(MongologTests, sgml) { runTests(getPath(
"sgml.pl")); }
338 TEST_F(MongologTests, annotation) { runTests(getPath(
"annotation.pl")); }
340 TEST_F(MongologTests, triple) { runTests(getPath(
"triple.plt")); }
TEST_F(MongologTests, arithmetic)
foreign_t pl_is_readonly2(term_t, term_t)
foreign_t pl_assert_triple_cpp9(term_t, term_t, term_t, term_t, term_t, term_t, term_t, term_t, term_t)
foreign_t pl_uri3(term_t, term_t, term_t)
foreign_t pl_db_name3(term_t, term_t, term_t)
#define PROLOG_ENGINE_EVAL(term)
#define KNOWROB_BUILTIN_REASONER(Name, Type)
static std::shared_ptr< KnowledgeBase > create()
static const std::string COLL_NAME_TRIPLES
static const std::string DB_NAME_KNOWROB
static const std::string DB_NAME_TESTS
static const std::string DB_URI_DEFAULT
bool initializeReasoner(const PropertyTree &cfg) override
bool initializeDefaultPackages() override
std::shared_ptr< MongoKnowledgeGraph > knowledgeGraph_
~MongologReasoner() override
std::string_view callFunctor() override
bool consult(const std::filesystem::path &uri, const char *module={}, bool doTransformQuery=true)
PrologTerm getReasonerQuery(const PrologTerm &goal)
static std::shared_ptr< NamedReasoner > getDefinedReasoner(const term_t &t_reasonerManager, const term_t &t_reasonerModule)
bool initializeReasoner(const PropertyTree &cfg) override
TermPtr toKnowRobTerm() const
static void runPrologTests(const std::shared_ptr< knowrob::PrologReasoner > &reasoner, const std::string &target)
ReasonerManager & reasonerManager() const
auto backendManager() const
void setConfidence(double confidence)
void setBegin(double begin)
void setXSDValue(std::string_view v, XSDType type)
void setStringValue(std::string_view v) override
void setObjectIRI(std::string_view object) override
void setPredicate(std::string_view predicate) override
void setObjectBlank(std::string_view identifier) override
void setGraph(std::string_view graph) override
void setSubject(std::string_view subject) override
GraphTermRule & graphTerm()
std::shared_ptr< Term > TermPtr
void insertUnique(std::ostream &os)