6 #include <boost/algorithm/string/replace.hpp>
7 #include "knowrob/Logger.h"
8 #include "knowrob/terms/ListTerm.h"
9 #include "knowrob/terms/OptionList.h"
10 #include "knowrob/reasoner/prolog/PrologTests.h"
11 #include "knowrob/integration/prolog/PrologBackend.h"
12 #include "knowrob/queries/QueryParser.h"
18 boost::replace_all(result,
"\\n",
"\n");
19 boost::replace_all(result,
"\\t",
"\t");
24 const std::shared_ptr<knowrob::PrologReasoner> &reasoner,
26 bool hasResult =
false;
28 int numFailedTests = 0;
38 ASSERT_EQ(pElem->arity(), 3);
39 ASSERT_EQ(*(pElem->arguments()[0]), *
Atom::Tabled(
"testcase"));
47 const auto &name = trace.
getString(
"name",
"");
48 const char *file = trace.
getString(
"file",
"").data();
49 const long line = trace.
getLong(
"line", 0);
52 auto *failureList = (
ListTerm *) pElem->arguments()[2].get();
53 if (!failureList->isNIL()) {
56 for (
const auto &failureTerm: (*failureList)) {
59 auto *errElem = (
Function *) failureTerm.get();
60 ASSERT_EQ(*errElem->functor(), *
Atom::Tabled(
"element"));
61 ASSERT_EQ(errElem->arity(), 3);
62 ASSERT_EQ(*(errElem->arguments()[0]), *
Atom::Tabled(
"failure"));
65 ASSERT_TRUE(errOpts.
contains(
"type"));
66 ASSERT_TRUE(errOpts.
contains(
"message"));
68 std::ostringstream message_os;
69 message_os << errOpts.
getString(
"type",
"");
71 message_os << errOpts.
getString(
"message",
"");
72 auto message = message_os.str();
74 std::ostringstream summary_os;
75 summary_os <<
"test: ";
77 auto summary = summary_os.str();
82 GTEST_MESSAGE_AT_(file, line, summary.c_str(), \
83 testing::TestPartResult::kNonFatalFailure) << unescapeString(message);
86 EXPECT_TRUE(hasResult);
87 KB_INFO1(target.c_str(), 1,
"[plunit] {}/{} tests succeeded for target '{}'.", (numTests - numFailedTests),
92 class PrologReasonerTests :
public PrologTests<knowrob::PrologReasoner, knowrob::PrologBackend> {
95 return std::filesystem::path(
"reasoner") /
"prolog" / filename;
100 auto query = std::make_shared<Goal>(
formula, ctx);
101 reasoner()->evaluate(query);
103 auto answerQueue = query->answerBuffer()->createQueue();
104 std::vector<BindingsPtr> out;
105 while (!answerQueue->empty()) {
106 auto solution = answerQueue->pop_front();
107 if (solution->indicatesEndOfEvaluation())
break;
109 auto answer = std::static_pointer_cast<const Answer>(solution);
110 if (answer->isPositive()) {
111 auto answerYes = std::static_pointer_cast<const AnswerYes>(answer);
112 out.push_back(answerYes->substitution());
122 #define EXPECT_ONLY_SOLUTION(phi, sol) { \
123 auto sols = lookup(phi); \
124 EXPECT_EQ(sols.size(),1); \
125 if(sols.size()==1) { EXPECT_EQ(*sols[0], sol); } }
127 #define EXPECT_NO_SOLUTION(phi) EXPECT_EQ(lookupAll(phi).size(),0)
129 TEST_F(PrologReasonerTests, simple_conjunction) {
132 auto queryFormula = std::make_shared<SimpleConjunction>(std::vector<FirstOrderLiteralPtr>{
133 std::make_shared<FirstOrderLiteral>(p1,
false),
134 std::make_shared<FirstOrderLiteral>(p2,
false)
142 {std::make_shared<Variable>(
"ABC"),
Atom::Tabled(
"abc")}
147 TEST_F(PrologReasonerTests, semweb) { runTests(getPath(
"semweb.pl")); }
TEST_F(PrologReasonerTests, simple_conjunction)
#define EXPECT_ONLY_SOLUTION(phi, sol)
#define KB_INFO1(file, line,...)
static std::shared_ptr< knowrob::Atom > Tabled(std::string_view stringForm)
std::string_view getString(const std::string &key, const std::string &defaultValue) const
bool contains(const std::string &key) const
long getLong(const std::string &key, long defaultValue) const
static std::filesystem::path getPrologPath(const std::filesystem::path &filename)
static void runPrologTests(const std::shared_ptr< knowrob::PrologReasoner > &reasoner, const std::string &target)
static PredicatePtr parsePredicate(const std::string &queryString)
std::shared_ptr< SimpleConjunction > SimpleConjunctionPtr
@ QUERY_FLAG_ALL_SOLUTIONS