From 0cf84dd5ca90cc9e2820882d44b2c6b2c50d5896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Thu, 9 Nov 2017 19:34:47 +0100 Subject: [PATCH] Added normalization tests for implications. These tests ensure that implications are correctly reduced to disjunctions in preconditions (nested and not) and goal descriptions. --- lib/pddl/tests/TestNormalization.cpp | 61 +++++++++++++++++++ tests/data/normalization/normalization-2.pddl | 49 +++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 tests/data/normalization/normalization-2.pddl diff --git a/lib/pddl/tests/TestNormalization.cpp b/lib/pddl/tests/TestNormalization.cpp index 6b95127..f893d30 100644 --- a/lib/pddl/tests/TestNormalization.cpp +++ b/lib/pddl/tests/TestNormalization.cpp @@ -97,3 +97,64 @@ TEST_CASE("[normalization] A simple PDDL description is preserved by normalizati CHECK(goalAnd2->arguments[0].get()->declaration == objects[1].get()); CHECK(goalAnd2->arguments[1].get()->declaration == objects[5].get()); } + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +TEST_CASE("[normalization] Implications are correctly reduced", "[normalization]") +{ + pddl::Tokenizer tokenizer; + pddl::Context context(std::move(tokenizer), ignoreWarnings); + + const auto domainFile = fs::path("data") / "normalization" / "normalization-2.pddl"; + context.tokenizer.read(domainFile); + auto description = pddl::parseDescription(context); + const auto normalizedDescription = pddl::normalize(std::move(description)); + + const auto &actions = normalizedDescription.domain->actions; + + SECTION("top-level “imply” statement") + { + const auto &a1Pre = actions[0]->precondition.value().get().get().get(); + REQUIRE(a1Pre->arguments.size() == 1); + CHECK(a1Pre->arguments[0].get()->declaration->name == "x"); + CHECK(a1Pre->declaration->existentialParameters.empty()); + CHECK(a1Pre->declaration->parameters.size() == 1); + const auto &a1PreOr = a1Pre->declaration->precondition.value().get>(); + REQUIRE(a1PreOr->arguments.size() == 2); + const auto &a1PreOr1Not = a1PreOr->arguments[0].get>()->argument.get(); + CHECK(a1PreOr1Not->declaration->name == "test-predicate-0"); + const auto &a1PreOr2 = a1PreOr->arguments[1].get().get(); + CHECK(a1PreOr2->declaration->name == "test-predicate-1"); + } + + SECTION("nested “imply” statement") + { + const auto &and1 = actions[1]->precondition.value().get>(); + REQUIRE(and1->arguments.size() == 2); + const auto &d1 = and1->arguments[0].get().get(); + const auto &and2 = d1->declaration->precondition.value().get>(); + REQUIRE(and2->arguments.size() == 2); + const auto &d2 = and2->arguments[0].get().get(); + const auto &or_ = d2->declaration->precondition.value().get>(); + REQUIRE(or_->arguments.size() == 2); + const auto &or1Not = or_->arguments[0].get>()->argument.get(); + CHECK(or1Not->declaration->name == "test-predicate-0"); + const auto &or2 = or_->arguments[1].get().get(); + CHECK(or2->declaration->name == "test-predicate-1"); + } + + SECTION("“imply” statement in goal description") + { + const auto &goal = normalizedDescription.problem.value()->goal.value(); + + const auto &and_ = goal.get>(); + REQUIRE(and_->arguments.size() == 2); + const auto &d = and_->arguments[0].get().get(); + const auto &or_ = d->declaration->precondition.value().get>(); + REQUIRE(or_->arguments.size() == 2); + const auto &or1Not = or_->arguments[0].get>()->argument.get(); + CHECK(or1Not->declaration->name == "test-predicate-0"); + const auto &or2 = or_->arguments[1].get().get(); + CHECK(or2->declaration->name == "test-predicate-1"); + } +} diff --git a/tests/data/normalization/normalization-2.pddl b/tests/data/normalization/normalization-2.pddl new file mode 100644 index 0000000..a60fdd5 --- /dev/null +++ b/tests/data/normalization/normalization-2.pddl @@ -0,0 +1,49 @@ +; tests that “imply” statements in preconditions are correctly reduced +(define (domain test-normalization) + (:predicates + (test-predicate-0) + (test-predicate-1 ?x) + (test-predicate-2 ?x ?y)) + + ; top-level “imply” statement + (:action test-action-1 + :parameters + (?x) + :precondition + (imply + (test-predicate-0) + (test-predicate-1 ?x)) + :effect + (test-predicate-1 ?x)) + + ; nested “imply” statement + (:action test-action-2 + :parameters + (?x) + :precondition + (and + (and + (imply + (test-predicate-0) + (test-predicate-1 ?x)) + (test-predicate-0)) + (test-predicate-0)) + :effect + (test-predicate-1 ?x)) +) + +(define (problem test-normalization) + (:domain test-normalization) + + (:objects a b c) + + (:init + (test-predicate-0)) + + ; “imply” statement in goal + (:goal + (and + (imply + (test-predicate-0) + (test-predicate-1 a)) + (test-predicate-0))))