From 362222c88297acaf2ffce44ff79a0445590f5085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Sat, 24 Jun 2017 17:37:05 +0200 Subject: [PATCH] Finished implementing reduction. --- .../detail/normalization/Reduction.cpp | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/lib/pddlparse/src/pddlparse/detail/normalization/Reduction.cpp b/lib/pddlparse/src/pddlparse/detail/normalization/Reduction.cpp index 6e004b8..88dae1f 100644 --- a/lib/pddlparse/src/pddlparse/detail/normalization/Reduction.cpp +++ b/lib/pddlparse/src/pddlparse/detail/normalization/Reduction.cpp @@ -24,6 +24,7 @@ namespace detail void eliminateImply(ast::Precondition &precondition); void negationNormalize(ast::Precondition &precondition); void eliminateForAll(ast::Precondition &precondition); +void eliminateDoubleNegations(ast::Precondition &precondition); //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -275,6 +276,67 @@ void eliminateForAll(ast::Precondition &precondition) //////////////////////////////////////////////////////////////////////////////////////////////////// +void eliminateDoubleNegations(ast::Precondition &precondition) +{ + const auto handleAtomicFormula = + [](ast::AtomicFormula &) + { + }; + + const auto handleAnd = + [](ast::AndPointer &and_) + { + for (auto &argument : and_->arguments) + eliminateDoubleNegations(argument); + }; + + const auto handleExists = + [](ast::ExistsPointer &exists) + { + eliminateDoubleNegations(exists->argument); + }; + + const auto handleForAll = + [&](ast::ForAllPointer &forAll) + { + eliminateDoubleNegations(forAll->argument); + }; + + const auto handleImply = + [&](ast::ImplyPointer &imply) + { + eliminateDoubleNegations(imply->argumentLeft); + eliminateDoubleNegations(imply->argumentRight); + }; + + const auto handleNot = + [&](ast::NotPointer ¬_) + { + if (not_->argument.is>()) + { + eliminateDoubleNegations(not_->argument); + + // As the parent contains the argument, the argument needs to be saved before overwriting the parent + // TODO: check whether this workaround can be avoided + auto argument = std::move(not_->argument.get>()); + precondition = std::move(argument); + } + + eliminateDoubleNegations(not_->argument); + }; + + const auto handleOr = + [](ast::OrPointer &or_) + { + for (auto &argument : or_->arguments) + eliminateDoubleNegations(argument); + }; + + precondition.match(handleAtomicFormula, handleAnd, handleExists, handleForAll, handleImply, handleNot, handleOr, handleUnsupported); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + void reduce(ast::Precondition &precondition) { // Replace “imply” statements with disjunctions @@ -285,6 +347,9 @@ void reduce(ast::Precondition &precondition) // Eliminate “forall” statements eliminateForAll(precondition); + + // Eliminate double negations introduced by eliminating “forall” statements + eliminateDoubleNegations(precondition); } ////////////////////////////////////////////////////////////////////////////////////////////////////