From 1876d1fe0b041f741b4eb0ca2ae995913bb9e212 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Fri, 16 Jun 2017 04:21:19 +0200 Subject: [PATCH] Fixed parsing of types. --- .../include/pddlparse/detail/ASTCopy.h | 23 +---------- .../src/pddlparse/detail/parsing/Type.cpp | 5 +-- .../detail/parsing/VariableDeclaration.cpp | 3 +- .../TestAcceptanceOfOfficialPDDLInstances.cpp | 15 ++++--- .../tests/TestOfficialPDDLInstances.cpp | 40 +++++++++++++++++++ 5 files changed, 51 insertions(+), 35 deletions(-) create mode 100644 lib/pddlparse/tests/TestOfficialPDDLInstances.cpp diff --git a/lib/pddlparse/include/pddlparse/detail/ASTCopy.h b/lib/pddlparse/include/pddlparse/detail/ASTCopy.h index 2079a80..2b19b34 100644 --- a/lib/pddlparse/include/pddlparse/detail/ASTCopy.h +++ b/lib/pddlparse/include/pddlparse/detail/ASTCopy.h @@ -128,16 +128,6 @@ At deepCopy(At &other) return At(other.timePoint, std::move(argument)); } -//////////////////////////////////////////////////////////////////////////////////////////////////// -/* -template -Either deepCopy(Either &other) -{ - auto argument{deepCopy(other.argument)}; - - return Not(std::move(argument)); -}*/ - //////////////////////////////////////////////////////////////////////////////////////////////////// template @@ -152,20 +142,9 @@ Not deepCopy(Not &other) // Variants //////////////////////////////////////////////////////////////////////////////////////////////////// -struct DeepCopyVisitor -{ - template - Argument visit(Argument &other) - { - return deepCopy(other); - } -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - ast::Type deepCopy(ast::Type &other) { - return other.match([](auto &x){deepCopy(x); return std::make_unique(nullptr);}); + return other.match([](auto &x) -> ast::Type {return deepCopy(x);}); } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/lib/pddlparse/src/pddlparse/detail/parsing/Type.cpp b/lib/pddlparse/src/pddlparse/detail/parsing/Type.cpp index 803f8b3..5b74b3d 100644 --- a/lib/pddlparse/src/pddlparse/detail/parsing/Type.cpp +++ b/lib/pddlparse/src/pddlparse/detail/parsing/Type.cpp @@ -23,9 +23,8 @@ ast::Type parseType(Context &context, ast::Domain &domain) if (tokenizer.testAndReturn('(')) { - // TODO: put into Type parsing unit // TODO: refactor - auto p = + auto parsePrimitiveTypeWrapper = [](auto &context, auto &astContext, auto &) -> std::experimental::optional { return parsePrimitiveType(context, *astContext.domain); @@ -35,7 +34,7 @@ ast::Type parseType(Context &context, ast::Domain &domain) ASTContext astContext(domain); VariableStack variableStack; - auto eitherType = parseEither(context, astContext, variableStack, p); + auto eitherType = parseEither(context, astContext, variableStack, parsePrimitiveTypeWrapper); if (!eitherType) throw ParserException(tokenizer.location(), "expected primitive type or “either” expression"); diff --git a/lib/pddlparse/src/pddlparse/detail/parsing/VariableDeclaration.cpp b/lib/pddlparse/src/pddlparse/detail/parsing/VariableDeclaration.cpp index fdd3735..dfa25c1 100644 --- a/lib/pddlparse/src/pddlparse/detail/parsing/VariableDeclaration.cpp +++ b/lib/pddlparse/src/pddlparse/detail/parsing/VariableDeclaration.cpp @@ -49,13 +49,12 @@ ast::VariableDeclarations parseVariableDeclarations(Context &context, ast::Domai continue; auto parentType = parseType(context, domain); - parentType = ast::deepCopy(parentType); for (size_t i = inheritanceIndex; i < variableDeclarations.size(); i++) variableDeclarations[i]->type = ast::deepCopy(parentType); // All types up to now are labeled with their parent types - inheritanceIndex = variableDeclarations.size() + 1; + inheritanceIndex = variableDeclarations.size(); tokenizer.skipWhiteSpace(); } diff --git a/lib/pddlparse/tests/TestAcceptanceOfOfficialPDDLInstances.cpp b/lib/pddlparse/tests/TestAcceptanceOfOfficialPDDLInstances.cpp index 4920cb5..f53e24a 100644 --- a/lib/pddlparse/tests/TestAcceptanceOfOfficialPDDLInstances.cpp +++ b/lib/pddlparse/tests/TestAcceptanceOfOfficialPDDLInstances.cpp @@ -7,13 +7,14 @@ namespace fs = std::experimental::filesystem; +const pddl::Context::WarningCallback ignoreWarnings = [](const auto &, const auto &){}; +const auto pddlInstanceBasePath = fs::path("data") / "pddl-instances"; + //////////////////////////////////////////////////////////////////////////////////////////////////// -TEST_CASE("[PDDL parser acceptance] All the IPC domains are parsed without errors", "[PDDL parser acceptance]") +TEST_CASE("[PDDL parser acceptance] All official PDDL domains are parsed without errors", "[PDDL parser acceptance]") { - const pddl::Context::WarningCallback ignoreWarnings = [](const auto &, const auto &){}; - - for (const auto &competitionDirectory : fs::directory_iterator("data/pddl-instances")) + for (const auto &competitionDirectory : fs::directory_iterator(pddlInstanceBasePath)) { if (!fs::is_directory(competitionDirectory)) continue; @@ -36,11 +37,9 @@ TEST_CASE("[PDDL parser acceptance] All the IPC domains are parsed without error //////////////////////////////////////////////////////////////////////////////////////////////////// -TEST_CASE("[PDDL parser acceptance] The first instance of every IPC domain is parsed without errors", "[PDDL parser acceptance]") +TEST_CASE("[PDDL parser acceptance] The first instance for all official PDDL domains is parsed without errors", "[PDDL parser acceptance]") { - const pddl::Context::WarningCallback ignoreWarnings = [](const auto &, const auto &){}; - - for (const auto &competitionDirectory : fs::directory_iterator("data/pddl-instances")) + for (const auto &competitionDirectory : fs::directory_iterator(pddlInstanceBasePath)) { if (!fs::is_directory(competitionDirectory)) continue; diff --git a/lib/pddlparse/tests/TestOfficialPDDLInstances.cpp b/lib/pddlparse/tests/TestOfficialPDDLInstances.cpp new file mode 100644 index 0000000..4d68c1e --- /dev/null +++ b/lib/pddlparse/tests/TestOfficialPDDLInstances.cpp @@ -0,0 +1,40 @@ +#include + +#include + +#include +#include + +namespace fs = std::experimental::filesystem; + +const pddl::Context::WarningCallback ignoreWarnings = [](const auto &, const auto &){}; +const auto pddlInstanceBasePath = fs::path("data") / "pddl-instances"; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +TEST_CASE("[PDDL parser] The official PDDL instances are parsed correctly", "[PDDL parser]") +{ + std::cout << std::endl; + + pddl::Tokenizer tokenizer; + pddl::Context context(std::move(tokenizer), ignoreWarnings); + + SECTION("“either” type in zenotravel domain") + { + const auto domainFile = pddlInstanceBasePath / "ipc-2002" / "domains" / "zenotravel-numeric-hand-coded" / "domain.pddl"; + context.tokenizer.read(domainFile); + auto description = pddl::parseDescription(context); + + const auto &predicates = description.domain->predicates; + + REQUIRE(predicates.size() == 2); + REQUIRE(predicates[0]->name == "at"); + REQUIRE(predicates[0]->parameters.size() == 2); + REQUIRE(predicates[0]->parameters[0]->name == "x"); + REQUIRE(predicates[0]->parameters[0]->type); + CHECK(predicates[0]->parameters[0]->type.value().is>()); + REQUIRE(predicates[0]->parameters[1]->name == "c"); + REQUIRE(predicates[0]->parameters[1]->type); + CHECK(predicates[0]->parameters[1]->type.value().is()); + } +}