patrick
/
plasp
Archived
1
0
Fork 0

Started implementing reduction, added support for “imply” expressions.

This commit is contained in:
Patrick Lühne 2017-06-24 16:03:32 +02:00
parent d5dd8e849f
commit 7ae2734e9f
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
3 changed files with 125 additions and 0 deletions

View File

@ -0,0 +1,26 @@
#ifndef __PDDL_PARSE__DETAIL__NORMALIZATION__REDUCTION_H
#define __PDDL_PARSE__DETAIL__NORMALIZATION__REDUCTION_H
#include <pddlparse/ASTForward.h>
#include <pddlparse/Context.h>
#include <pddlparse/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Reduction
//
////////////////////////////////////////////////////////////////////////////////////////////////////
void reduce(ast::Precondition &precondition);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@ -5,6 +5,7 @@
#include <pddlparse/NormalizedAST.h>
#include <pddlparse/detail/normalization/AtomicFormula.h>
#include <pddlparse/detail/normalization/CollectFreeVariables.h>
#include <pddlparse/detail/normalization/Reduction.h>
namespace pddl
{
@ -275,6 +276,9 @@ normalizedAST::AndPointer<normalizedAST::Literal> normalize(ast::AndPointer<ast:
normalizedAST::Precondition normalize(ast::Precondition &&precondition, normalizedAST::DerivedPredicateDeclarations &derivedPredicates)
{
// Bring precondition to normal form
reduce(precondition);
return precondition.match(
[&](auto &x) -> normalizedAST::Precondition
{

View File

@ -0,0 +1,95 @@
#include <pddlparse/detail/normalization/Reduction.h>
#include <pddlparse/AST.h>
#include <pddlparse/Exception.h>
#include <pddlparse/NormalizedAST.h>
#include <pddlparse/detail/normalization/AtomicFormula.h>
#include <pddlparse/detail/normalization/CollectFreeVariables.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Reduction
//
////////////////////////////////////////////////////////////////////////////////////////////////////
const auto handleUnsupported =
[](ast::UnsupportedPointer &unsupported)
{
throw NormalizationException("" + unsupported->type + "” expressions in preconditions cant be normalized currently");
};
////////////////////////////////////////////////////////////////////////////////////////////////////
void eliminateImply(ast::Precondition &precondition)
{
const auto handleAtomicFormula =
[](ast::AtomicFormula &)
{
};
const auto handleAnd =
[](ast::AndPointer<ast::Precondition> &and_)
{
for (auto &argument : and_->arguments)
eliminateImply(argument);
};
const auto handleExists =
[](ast::ExistsPointer<ast::Precondition> &exists)
{
eliminateImply(exists->argument);
};
const auto handleForAll =
[](ast::ForAllPointer<ast::Precondition> &forAll)
{
eliminateImply(forAll->argument);
};
const auto handleImply =
[&](ast::ImplyPointer<ast::Precondition> &imply)
{
eliminateImply(imply->argumentLeft);
eliminateImply(imply->argumentRight);
ast::Or<ast::Precondition>::Arguments arguments;
arguments.reserve(2);
arguments.emplace_back(std::make_unique<ast::Not<ast::Precondition>>(std::move(imply->argumentLeft)));
arguments.emplace_back(std::move(imply->argumentRight));
precondition = std::make_unique<ast::Or<ast::Precondition>>(std::move(arguments));
};
const auto handleNot =
[](ast::NotPointer<ast::Precondition> &not_)
{
eliminateImply(not_->argument);
};
const auto handleOr =
[](ast::OrPointer<ast::Precondition> &or_)
{
for (auto &argument : or_->arguments)
eliminateImply(argument);
};
precondition.match(handleAtomicFormula, handleAnd, handleExists, handleForAll, handleImply, handleNot, handleOr, handleUnsupported);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void reduce(ast::Precondition &precondition)
{
// Replace “imply” statements with disjunctions
eliminateImply(precondition);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}