From ea50cffac970c726669b1b372e930d914aa6c1f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Tue, 27 Jun 2017 12:29:43 +0200 Subject: [PATCH] Separating top-level and nested preconditions correctly. --- .../detail/normalization/Precondition.cpp | 82 +++++++++++++++++-- 1 file changed, 75 insertions(+), 7 deletions(-) diff --git a/lib/pddlparse/src/pddlparse/detail/normalization/Precondition.cpp b/lib/pddlparse/src/pddlparse/detail/normalization/Precondition.cpp index fd839ea..87a3be2 100644 --- a/lib/pddlparse/src/pddlparse/detail/normalization/Precondition.cpp +++ b/lib/pddlparse/src/pddlparse/detail/normalization/Precondition.cpp @@ -35,7 +35,7 @@ normalizedAST::Literal normalizeTopLevel(ast::ExistsPointer & normalizedAST::Literal normalizeTopLevel(ast::ForAllPointer &forAll, normalizedAST::DerivedPredicateDeclarations &); normalizedAST::Literal normalizeTopLevel(ast::ImplyPointer &, normalizedAST::DerivedPredicateDeclarations &); normalizedAST::NotPointer normalizeTopLevel(ast::NotPointer ¬_, normalizedAST::DerivedPredicateDeclarations &derivedPredicates); -normalizedAST::Literal normalizeTopLevel(ast::OrPointer &or_, normalizedAST::DerivedPredicateDeclarations &derivedPredicates); +normalizedAST::OrPointer normalizeTopLevel(ast::OrPointer &or_, normalizedAST::DerivedPredicateDeclarations &derivedPredicates); //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -277,9 +277,39 @@ normalizedAST::NotPointer normalizeTopLevel(ast::N //////////////////////////////////////////////////////////////////////////////////////////////////// -normalizedAST::Literal normalizeTopLevel(ast::OrPointer &or_, normalizedAST::DerivedPredicateDeclarations &derivedPredicates) +// TODO: refactor to avoid code duplication +normalizedAST::OrPointer normalizeTopLevel(ast::OrPointer &or_, normalizedAST::DerivedPredicateDeclarations &derivedPredicates) { - return normalizeNested(or_, derivedPredicates); + normalizedAST::Or::Arguments arguments; + + arguments.reserve(or_->arguments.size()); + + const auto handleAtomicFormula = + [&](ast::AtomicFormula &atomicFormula) -> normalizedAST::Literal + { + return normalize(std::move(atomicFormula)); + }; + + const auto handleNot = + [&](ast::NotPointer ¬_) -> normalizedAST::Literal + { + return normalizeTopLevel(not_, derivedPredicates); + }; + + const auto handleNested = + [&](auto &nested) -> normalizedAST::Literal + { + return normalizeNested(nested, derivedPredicates); + }; + + for (auto &argument : or_->arguments) + { + auto normalizedArgument = argument.match(handleAtomicFormula, handleNot, handleNested); + + arguments.emplace_back(std::move(normalizedArgument)); + } + + return std::make_unique>(std::move(arguments)); } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -289,11 +319,49 @@ normalizedAST::Precondition normalize(ast::Precondition &&precondition, normaliz // Bring precondition to normal form reduce(precondition); - return precondition.match( - [&](auto &x) -> normalizedAST::Precondition + const auto handleAtomicFormula = + [&](ast::AtomicFormula &atomicFormula) -> normalizedAST::Precondition { - return normalizeTopLevel(x, derivedPredicates); - }); + return normalizeTopLevel(atomicFormula, derivedPredicates); + }; + + const auto handleAnd = + [&](ast::AndPointer &and_) -> normalizedAST::Precondition + { + return normalizeTopLevel(and_, derivedPredicates); + }; + + const auto handleExists = + [&](ast::ExistsPointer &exists) -> normalizedAST::Precondition + { + return normalizeTopLevel(exists, derivedPredicates); + }; + + const auto handleForAll = + [&](ast::ForAllPointer &forAll) -> normalizedAST::Precondition + { + return normalizeTopLevel(forAll, derivedPredicates); + }; + + const auto handleImply = + [&](ast::ImplyPointer &imply) -> normalizedAST::Precondition + { + return normalizeTopLevel(imply, derivedPredicates); + }; + + const auto handleNot = + [&](ast::NotPointer ¬_) -> normalizedAST::Precondition + { + return normalizeTopLevel(not_, derivedPredicates); + }; + + const auto handleOr = + [&](ast::OrPointer &or_) -> normalizedAST::Precondition + { + return normalizeNested(or_, derivedPredicates); + }; + + return precondition.match(handleAtomicFormula, handleAnd, handleExists, handleForAll, handleImply, handleNot, handleOr); } ////////////////////////////////////////////////////////////////////////////////////////////////////