From 2851f8d2860713ccaea835632f43f0513abb9e27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Fri, 23 Jun 2017 03:30:27 +0200 Subject: [PATCH] Corrected grammar of initial state facts. --- lib/pddlparse/include/pddlparse/ASTForward.h | 3 +- .../src/pddlparse/detail/parsing/Fact.cpp | 2 +- lib/pddlparse/tests/TestIssues.cpp | 4 +- .../tests/TestOfficialPDDLInstances.cpp | 6 +-- src/plasp/pddl/TranslatorASP.cpp | 44 ++++++++++++------- 5 files changed, 35 insertions(+), 24 deletions(-) diff --git a/lib/pddlparse/include/pddlparse/ASTForward.h b/lib/pddlparse/include/pddlparse/ASTForward.h index 25dd988..15180db 100644 --- a/lib/pddlparse/include/pddlparse/ASTForward.h +++ b/lib/pddlparse/include/pddlparse/ASTForward.h @@ -245,9 +245,8 @@ class Fact; namespace detail { using FactT = Variant< - AtomicFormula, AtPointer, - NotPointer>; + Literal>; } class Fact : public detail::FactT diff --git a/lib/pddlparse/src/pddlparse/detail/parsing/Fact.cpp b/lib/pddlparse/src/pddlparse/detail/parsing/Fact.cpp index 0553df5..e756cc8 100644 --- a/lib/pddlparse/src/pddlparse/detail/parsing/Fact.cpp +++ b/lib/pddlparse/src/pddlparse/detail/parsing/Fact.cpp @@ -37,7 +37,7 @@ std::experimental::optional parseFact(Context &context, ASTContext &a // Now, test supported expressions std::experimental::optional fact; - if ((fact = parseNot(context, astContext, variableStack, parseAtomicFormula)) + if ((fact = parseNot(context, astContext, variableStack, parseAtomicFormula)) || (fact = parseAtomicFormula(context, astContext, variableStack))) { return std::move(fact.value()); diff --git a/lib/pddlparse/tests/TestIssues.cpp b/lib/pddlparse/tests/TestIssues.cpp index 5361740..fd89c4b 100644 --- a/lib/pddlparse/tests/TestIssues.cpp +++ b/lib/pddlparse/tests/TestIssues.cpp @@ -79,7 +79,9 @@ TEST_CASE("[PDDL parser issues] Check past issues", "[PDDL parser issues]") { return fact.template is>() - || (fact.template is() && fact.template get().template is()); + || (fact.template is() + && fact.template get().template is() + && fact.template get().template get().template is()); }); const auto containsInvalidFact = (invalidFact != facts.cend()); diff --git a/lib/pddlparse/tests/TestOfficialPDDLInstances.cpp b/lib/pddlparse/tests/TestOfficialPDDLInstances.cpp index 4350798..0ba07db 100644 --- a/lib/pddlparse/tests/TestOfficialPDDLInstances.cpp +++ b/lib/pddlparse/tests/TestOfficialPDDLInstances.cpp @@ -143,17 +143,17 @@ TEST_CASE("[PDDL instances] The official PDDL instances are parsed correctly", " const auto &facts = problem->initialState.facts; REQUIRE(facts.size() == 9); - const auto &fact0 = facts[0].get().get(); + const auto &fact0 = facts[0].get().get().get(); // TODO: check declaration once implemented REQUIRE(fact0->arguments.size() == 1); CHECK(fact0->arguments[0].get()->declaration->name == "c"); CHECK(fact0->arguments[0].get()->declaration->type.value().get()->declaration == typeBlock.get()); - const auto &fact5 = facts[5].get().get(); + const auto &fact5 = facts[5].get().get().get(); // TODO: check declaration once implemented REQUIRE(fact5->arguments.size() == 1); CHECK(fact5->arguments[0].get()->declaration->name == "a"); CHECK(fact5->arguments[0].get()->declaration->type.value().get()->declaration == typeBlock.get()); - const auto &fact8 = facts[8].get().get(); + const auto &fact8 = facts[8].get().get().get(); // TODO: check declaration once implemented REQUIRE(fact8->arguments.empty()); diff --git a/src/plasp/pddl/TranslatorASP.cpp b/src/plasp/pddl/TranslatorASP.cpp index 425c7da..df3ba78 100644 --- a/src/plasp/pddl/TranslatorASP.cpp +++ b/src/plasp/pddl/TranslatorASP.cpp @@ -289,31 +289,41 @@ void TranslatorASP::translateInitialState() const const auto &facts = m_description.problem.value()->initialState.facts; + // TODO: move to separate header for (const auto &fact : facts) { m_outputStream << std::endl << colorlog::Function("initialState") << "("; - // Translate single predicate - if (fact.is<::pddl::ast::AtomicFormula>() && fact.get<::pddl::ast::AtomicFormula>().is<::pddl::ast::PredicatePointer>()) - { - const auto &predicate = fact.get<::pddl::ast::AtomicFormula>().get<::pddl::ast::PredicatePointer>(); + const auto handleUnsupported = + [&](const auto &) + { + throw TranslatorException("only predicates and their negations supported in initial state currently"); + }; - translatePredicateToVariable(m_outputStream, *predicate, true); - } - // Assuming that "not" expression may only contain a predicate - else if (fact.is<::pddl::ast::NotPointer<::pddl::ast::Fact>>()) - { - const auto ¬Expression = fact.get<::pddl::ast::NotPointer<::pddl::ast::Fact>>(); + const auto handleAtomicFormula = + [&](const ::pddl::ast::AtomicFormula &atomicFormula, bool isPositive = true) + { + if (!atomicFormula.is<::pddl::ast::PredicatePointer>()) + handleUnsupported(atomicFormula); - if (!notExpression->argument.is<::pddl::ast::AtomicFormula>() || !notExpression->argument.get<::pddl::ast::AtomicFormula>().is<::pddl::ast::PredicatePointer>()) - throw TranslatorException("only negations of simple predicates supported in initial state currently"); + const auto &predicate = atomicFormula.get<::pddl::ast::PredicatePointer>(); - const auto &predicate = notExpression->argument.get<::pddl::ast::AtomicFormula>().get<::pddl::ast::PredicatePointer>(); + translatePredicateToVariable(m_outputStream, *predicate, isPositive); + }; - translatePredicateToVariable(m_outputStream, *predicate, false); - } - else - throw TranslatorException("only predicates and their negations supported in initial state currently"); + const auto handleNot = + [&](const ::pddl::ast::NotPointer<::pddl::ast::AtomicFormula> ¬_) + { + handleAtomicFormula(not_->argument, false); + }; + + const auto handleLiteral = + [&](const ::pddl::ast::Literal &literal) + { + literal.match(handleAtomicFormula, handleNot); + }; + + fact.match(handleLiteral, handleUnsupported); m_outputStream << ")."; }