2017-04-08 18:47:06 +02:00
|
|
|
#include <catch.hpp>
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
2017-05-30 03:53:51 +02:00
|
|
|
#include <anthem/AST.h>
|
2017-04-08 18:47:06 +02:00
|
|
|
#include <anthem/Context.h>
|
|
|
|
#include <anthem/Translation.h>
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-06-01 02:37:45 +02:00
|
|
|
TEST_CASE("[completion] Rules are completed", "[completion]")
|
2017-04-08 18:47:06 +02:00
|
|
|
{
|
|
|
|
std::stringstream input;
|
|
|
|
std::stringstream output;
|
|
|
|
std::stringstream errors;
|
|
|
|
|
|
|
|
anthem::output::Logger logger(output, errors);
|
2017-05-31 18:03:19 +02:00
|
|
|
anthem::Context context(std::move(logger));
|
2017-04-08 18:47:06 +02:00
|
|
|
context.simplify = true;
|
|
|
|
context.complete = true;
|
|
|
|
|
2017-04-10 14:30:35 +02:00
|
|
|
SECTION("predicate in single rule head")
|
2017-04-08 18:47:06 +02:00
|
|
|
{
|
|
|
|
input << "p :- q.";
|
2017-04-10 16:32:12 +02:00
|
|
|
anthem::translate("input", input, context);
|
2017-04-08 18:47:06 +02:00
|
|
|
|
2017-04-10 16:32:12 +02:00
|
|
|
CHECK(output.str() ==
|
|
|
|
"(p <-> q)\n"
|
|
|
|
"not q\n");
|
2017-04-08 18:47:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("predicate in multiple rule heads")
|
|
|
|
{
|
2017-04-10 16:32:12 +02:00
|
|
|
input <<
|
|
|
|
"p :- q.\n"
|
2017-04-08 18:47:06 +02:00
|
|
|
"p :- r.\n"
|
|
|
|
"p :- s.";
|
2017-04-10 16:32:12 +02:00
|
|
|
anthem::translate("input", input, context);
|
2017-04-08 18:47:06 +02:00
|
|
|
|
2017-04-10 16:32:12 +02:00
|
|
|
CHECK(output.str() ==
|
|
|
|
"(p <-> (q or r or s))\n"
|
|
|
|
"not q\n"
|
|
|
|
"not r\n"
|
|
|
|
"not s\n");
|
2017-04-08 18:47:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("multiple predicates are correctly separated")
|
|
|
|
{
|
2017-04-10 16:32:12 +02:00
|
|
|
input <<
|
|
|
|
"p :- s.\n"
|
2017-04-08 18:47:06 +02:00
|
|
|
"q :- t.\n"
|
|
|
|
"p :- q.\n"
|
|
|
|
"r :- t.\n"
|
|
|
|
"q :- r.";
|
2017-04-10 16:32:12 +02:00
|
|
|
anthem::translate("input", input, context);
|
|
|
|
|
|
|
|
CHECK(output.str() ==
|
|
|
|
"(p <-> (s or q))\n"
|
|
|
|
"(q <-> (t or r))\n"
|
|
|
|
"(r <-> t)\n"
|
|
|
|
"not s\n"
|
|
|
|
"not t\n");
|
2017-04-08 18:47:06 +02:00
|
|
|
}
|
2017-04-08 18:50:42 +02:00
|
|
|
|
|
|
|
SECTION("integrity constraints")
|
|
|
|
{
|
2017-04-10 16:32:12 +02:00
|
|
|
input <<
|
|
|
|
":- q.\n"
|
|
|
|
":- r(5).\n"
|
|
|
|
":- s(N).\n"
|
2017-04-08 20:02:20 +02:00
|
|
|
"#false :- t.\n"
|
2017-04-10 16:32:12 +02:00
|
|
|
"#false :- u(5).";
|
|
|
|
anthem::translate("input", input, context);
|
|
|
|
|
|
|
|
CHECK(output.str() ==
|
|
|
|
"not q\n"
|
|
|
|
"forall V1 not r(V1)\n"
|
2017-06-01 02:37:45 +02:00
|
|
|
"forall V2 not s(V2)\n"
|
2017-04-10 16:32:12 +02:00
|
|
|
"not t\n"
|
2017-06-01 02:37:45 +02:00
|
|
|
"forall V3 not u(V3)\n"
|
2017-04-10 16:32:12 +02:00
|
|
|
"not q\n"
|
|
|
|
"not r(5)\n"
|
2017-06-01 02:37:45 +02:00
|
|
|
"forall U1 not s(U1)\n"
|
2017-04-10 16:32:12 +02:00
|
|
|
"not t\n"
|
|
|
|
"not u(5)\n");
|
|
|
|
}
|
2017-04-08 18:50:42 +02:00
|
|
|
|
2017-04-10 16:32:12 +02:00
|
|
|
SECTION("Booleans")
|
|
|
|
{
|
|
|
|
input <<
|
|
|
|
"#true :- #true.\n"
|
|
|
|
"#true :- #false.\n"
|
|
|
|
"#false :- #true.\n"
|
|
|
|
"#false :- #false.\n";
|
|
|
|
anthem::translate("input", input, context);
|
|
|
|
|
|
|
|
CHECK(output.str() ==
|
|
|
|
"not #true\n"
|
|
|
|
"not #false\n");
|
2017-04-08 18:50:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("facts")
|
|
|
|
{
|
2017-04-10 16:32:12 +02:00
|
|
|
input <<
|
|
|
|
"q.\n"
|
2017-04-08 19:59:59 +02:00
|
|
|
"r.\n"
|
2017-04-10 16:32:12 +02:00
|
|
|
"s :- #true.\n"
|
|
|
|
"t :- #true.";
|
|
|
|
anthem::translate("input", input, context);
|
|
|
|
|
|
|
|
CHECK(output.str() ==
|
|
|
|
"q\n"
|
|
|
|
"r\n"
|
|
|
|
"s\n"
|
|
|
|
"t\n");
|
2017-04-08 18:50:42 +02:00
|
|
|
}
|
2017-04-08 20:17:01 +02:00
|
|
|
|
|
|
|
SECTION("useless implications")
|
|
|
|
{
|
2017-04-10 16:32:12 +02:00
|
|
|
input <<
|
|
|
|
"#true :- p, q(N), t(1, 2).\n"
|
2017-04-08 20:17:01 +02:00
|
|
|
"#true.\n"
|
2017-04-10 16:32:12 +02:00
|
|
|
"v :- #false.";
|
|
|
|
anthem::translate("input", input, context);
|
2017-04-08 20:17:01 +02:00
|
|
|
|
2017-04-10 16:32:12 +02:00
|
|
|
CHECK(output.str() ==
|
|
|
|
"not p\n"
|
|
|
|
"forall V1 not q(V1)\n"
|
2017-06-01 02:37:45 +02:00
|
|
|
"forall V2, V3 not t(V2, V3)\n"
|
2017-04-10 16:32:12 +02:00
|
|
|
"not v\n");
|
2017-04-08 20:17:01 +02:00
|
|
|
}
|
|
|
|
|
2017-04-08 20:22:50 +02:00
|
|
|
SECTION("example")
|
|
|
|
{
|
2017-04-10 16:32:12 +02:00
|
|
|
input <<
|
|
|
|
"{in(1..n, 1..r)}.\n"
|
2017-04-08 20:22:50 +02:00
|
|
|
"covered(I) :- in(I, S).\n"
|
|
|
|
":- I = 1..n, not covered(I).\n"
|
|
|
|
":- in(I, S), in(J, S), in(I + J, S).";
|
2017-04-10 16:32:12 +02:00
|
|
|
anthem::translate("input", input, context);
|
2017-04-08 20:22:50 +02:00
|
|
|
|
2017-04-10 16:32:12 +02:00
|
|
|
CHECK(output.str() ==
|
2017-06-01 02:37:45 +02:00
|
|
|
"forall V1 (covered(V1) <-> exists U1, U2 (V1 = U1 and in(U1, U2)))\n"
|
|
|
|
"forall V2, V3 (in(V2, V3) <-> (V2 in 1..n and V3 in 1..r and in(V2, V3)))\n"
|
|
|
|
"forall U3 not (U3 in 1..n and not covered(U3))\n"
|
|
|
|
"forall U4, U5, U6 not (in(U4, U5) and in(U6, U5) and exists X1 (X1 in (U4 + U6) and in(X1, U5)))\n");
|
2017-04-08 20:22:50 +02:00
|
|
|
}
|
2017-04-08 18:47:06 +02:00
|
|
|
}
|