Implemented normalization of preconditions.
This commit is contained in:
@@ -20,48 +20,6 @@ namespace normalizedAST
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Compounds
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct DerivedPredicate
|
||||
{
|
||||
using Arguments = Terms;
|
||||
|
||||
explicit DerivedPredicate(Arguments &&arguments, DerivedPredicateDeclaration *declaration)
|
||||
: arguments{std::move(arguments)},
|
||||
declaration{declaration}
|
||||
{
|
||||
}
|
||||
|
||||
DerivedPredicate(const DerivedPredicate &other) = delete;
|
||||
DerivedPredicate &operator=(const DerivedPredicate &&other) = delete;
|
||||
DerivedPredicate(DerivedPredicate &&other) = default;
|
||||
DerivedPredicate &operator=(DerivedPredicate &&other) = default;
|
||||
|
||||
Arguments arguments;
|
||||
DerivedPredicateDeclaration *declaration;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct DerivedPredicateDeclaration
|
||||
{
|
||||
explicit DerivedPredicateDeclaration(std::string &&name, VariableDeclarations &¶meters)
|
||||
: name{std::move(name)},
|
||||
parameters{std::move(parameters)}
|
||||
{
|
||||
}
|
||||
|
||||
DerivedPredicateDeclaration(const DerivedPredicateDeclaration &other) = delete;
|
||||
DerivedPredicateDeclaration &operator=(const DerivedPredicateDeclaration &&other) = delete;
|
||||
DerivedPredicateDeclaration(DerivedPredicateDeclaration &&other) = default;
|
||||
DerivedPredicateDeclaration &operator=(DerivedPredicateDeclaration &&other) = default;
|
||||
|
||||
std::string name;
|
||||
VariableDeclarations parameters;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// PDDL Structure
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -103,6 +61,45 @@ struct Domain
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct DerivedPredicate
|
||||
{
|
||||
using Arguments = Terms;
|
||||
|
||||
explicit DerivedPredicate(Arguments &&arguments, DerivedPredicateDeclaration *declaration)
|
||||
: arguments{std::move(arguments)},
|
||||
declaration{declaration}
|
||||
{
|
||||
}
|
||||
|
||||
DerivedPredicate(const DerivedPredicate &other) = delete;
|
||||
DerivedPredicate &operator=(const DerivedPredicate &&other) = delete;
|
||||
DerivedPredicate(DerivedPredicate &&other) = default;
|
||||
DerivedPredicate &operator=(DerivedPredicate &&other) = default;
|
||||
|
||||
Arguments arguments;
|
||||
DerivedPredicateDeclaration *declaration;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct DerivedPredicateDeclaration
|
||||
{
|
||||
explicit DerivedPredicateDeclaration() = default;
|
||||
|
||||
DerivedPredicateDeclaration(const DerivedPredicateDeclaration &other) = delete;
|
||||
DerivedPredicateDeclaration &operator=(const DerivedPredicateDeclaration &&other) = delete;
|
||||
DerivedPredicateDeclaration(DerivedPredicateDeclaration &&other) = default;
|
||||
DerivedPredicateDeclaration &operator=(DerivedPredicateDeclaration &&other) = default;
|
||||
|
||||
std::string name;
|
||||
|
||||
std::vector<VariableDeclaration *> parameters;
|
||||
VariableDeclarations existentialParameters;
|
||||
std::experimental::optional<DerivedPredicatePrecondition> precondition;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct InitialState
|
||||
{
|
||||
InitialState() = default;
|
||||
|
@@ -71,6 +71,8 @@ using ast::ForAll;
|
||||
using ast::ForAllPointer;
|
||||
using ast::Not;
|
||||
using ast::NotPointer;
|
||||
using ast::Or;
|
||||
using ast::OrPointer;
|
||||
using ast::When;
|
||||
using ast::WhenPointer;
|
||||
|
||||
@@ -148,6 +150,25 @@ using Preconditions = std::vector<Precondition>;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class DerivedPredicatePrecondition;
|
||||
|
||||
namespace detail
|
||||
{
|
||||
using DerivedPredicatePreconditionT = Variant<
|
||||
Literal,
|
||||
AndPointer<Literal>,
|
||||
OrPointer<Literal>>;
|
||||
}
|
||||
|
||||
class DerivedPredicatePrecondition : public detail::DerivedPredicatePreconditionT
|
||||
{
|
||||
using detail::DerivedPredicatePreconditionT::DerivedPredicatePreconditionT;
|
||||
};
|
||||
|
||||
using DerivedPredicatePreconditions = std::vector<DerivedPredicatePrecondition>;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class ConditionalEffect;
|
||||
|
||||
namespace detail
|
||||
|
@@ -59,16 +59,71 @@ inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Derived
|
||||
|
||||
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const DerivedPredicateDeclaration &derivedPredicateDeclaration, pddl::detail::PrintContext &printContext)
|
||||
{
|
||||
// TODO: implement correctly
|
||||
stream << "(" << colorlog::Keyword(":derived-predicate") << " " << pddl::detail::Identifier(derivedPredicateDeclaration.name);
|
||||
|
||||
stream << "(" << pddl::detail::Identifier(derivedPredicateDeclaration.name);
|
||||
printContext.indentationLevel++;
|
||||
|
||||
for (const auto ¶meter : derivedPredicateDeclaration.parameters)
|
||||
if (!derivedPredicateDeclaration.parameters.empty())
|
||||
{
|
||||
stream << " ";
|
||||
print(stream, *parameter, printContext);
|
||||
pddl::detail::printIndentedNewline(stream, printContext);
|
||||
stream << colorlog::Keyword(":parameters");
|
||||
|
||||
printContext.indentationLevel++;
|
||||
|
||||
pddl::detail::printIndentedNewline(stream, printContext);
|
||||
stream << "(";
|
||||
|
||||
for (const auto ¶meter : derivedPredicateDeclaration.parameters)
|
||||
{
|
||||
if (¶meter != &derivedPredicateDeclaration.parameters.front())
|
||||
pddl::detail::printIndentedNewline(stream, printContext);
|
||||
|
||||
print(stream, *parameter, printContext);
|
||||
}
|
||||
|
||||
stream << ")";
|
||||
|
||||
printContext.indentationLevel--;
|
||||
}
|
||||
|
||||
if (!derivedPredicateDeclaration.existentialParameters.empty())
|
||||
{
|
||||
pddl::detail::printIndentedNewline(stream, printContext);
|
||||
stream << colorlog::Keyword(":exists");
|
||||
|
||||
printContext.indentationLevel++;
|
||||
|
||||
pddl::detail::printIndentedNewline(stream, printContext);
|
||||
stream << "(";
|
||||
|
||||
for (const auto ¶meter : derivedPredicateDeclaration.existentialParameters)
|
||||
{
|
||||
if (parameter.get() != derivedPredicateDeclaration.existentialParameters.front().get())
|
||||
pddl::detail::printIndentedNewline(stream, printContext);
|
||||
|
||||
print(stream, *parameter, printContext);
|
||||
}
|
||||
|
||||
stream << ")";
|
||||
|
||||
printContext.indentationLevel--;
|
||||
}
|
||||
|
||||
if (derivedPredicateDeclaration.precondition)
|
||||
{
|
||||
pddl::detail::printIndentedNewline(stream, printContext);
|
||||
stream << colorlog::Keyword(":precondition");
|
||||
|
||||
printContext.indentationLevel++;
|
||||
|
||||
pddl::detail::printIndentedNewline(stream, printContext);
|
||||
print(stream, derivedPredicateDeclaration.precondition.value(), printContext);
|
||||
|
||||
printContext.indentationLevel--;
|
||||
}
|
||||
|
||||
printContext.indentationLevel--;
|
||||
|
||||
return (stream << ")");
|
||||
}
|
||||
|
||||
@@ -180,16 +235,8 @@ inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Domain
|
||||
|
||||
if (!domain.derivedPredicates.empty())
|
||||
{
|
||||
printIndentedNewline(stream, printContext);
|
||||
stream << "(" << colorlog::Keyword(":derived-predicates");
|
||||
|
||||
printContext.indentationLevel++;
|
||||
|
||||
printIndentedNewline(stream, printContext);
|
||||
print(stream, domain.derivedPredicates, printContext);
|
||||
stream << ")";
|
||||
|
||||
printContext.indentationLevel--;
|
||||
}
|
||||
|
||||
if (!domain.actions.empty())
|
||||
@@ -234,16 +281,8 @@ inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Problem
|
||||
|
||||
if (!problem.derivedPredicates.empty())
|
||||
{
|
||||
printIndentedNewline(stream, printContext);
|
||||
stream << "(" << colorlog::Keyword(":derived-predicates");
|
||||
|
||||
printContext.indentationLevel++;
|
||||
|
||||
printIndentedNewline(stream, printContext);
|
||||
print(stream, problem.derivedPredicates, printContext);
|
||||
stream << ")";
|
||||
|
||||
printContext.indentationLevel--;
|
||||
}
|
||||
|
||||
if (!problem.objects.empty())
|
||||
|
@@ -24,6 +24,7 @@ class VariableStack
|
||||
void pop();
|
||||
|
||||
std::experimental::optional<ast::VariableDeclaration *> findVariableDeclaration(const std::string &variableName);
|
||||
bool contains(const ast::VariableDeclaration &variableDeclaration) const;
|
||||
|
||||
private:
|
||||
std::vector<Layer> m_layers;
|
||||
|
@@ -0,0 +1,128 @@
|
||||
#ifndef __PDDL_PARSE__DETAIL__NORMALIZATION__COLLECT_FREE_VARIABLES_H
|
||||
#define __PDDL_PARSE__DETAIL__NORMALIZATION__COLLECT_FREE_VARIABLES_H
|
||||
|
||||
#include <pddlparse/ASTForward.h>
|
||||
#include <pddlparse/Context.h>
|
||||
#include <pddlparse/Exception.h>
|
||||
|
||||
#include <pddlparse/NormalizedASTForward.h>
|
||||
#include <pddlparse/detail/VariableStack.h>
|
||||
|
||||
namespace pddl
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Collect Free Variables
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Variant>
|
||||
void collectFreeVariables(const Variant &variant, std::vector<normalizedAST::VariableDeclaration *> &freeVariables, VariableStack &variableStack)
|
||||
{
|
||||
variant.match([&](const auto &x){collectFreeVariables(x, freeVariables, variableStack);});
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void collectFreeVariables(const ast::ConstantPointer &, std::vector<normalizedAST::VariableDeclaration *> &, VariableStack &)
|
||||
{
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void collectFreeVariables(const ast::VariablePointer &variable, std::vector<normalizedAST::VariableDeclaration *> &freeVariables, VariableStack &variableStack)
|
||||
{
|
||||
if (variableStack.contains(*variable->declaration))
|
||||
return;
|
||||
|
||||
const auto matchingFreeVariable = std::find(freeVariables.cbegin(), freeVariables.cend(), variable->declaration);
|
||||
|
||||
if (matchingFreeVariable != freeVariables.cend())
|
||||
return;
|
||||
|
||||
freeVariables.emplace_back(variable->declaration);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void collectFreeVariables(const ast::PredicatePointer &predicate, std::vector<normalizedAST::VariableDeclaration *> &freeVariables, VariableStack &variableStack)
|
||||
{
|
||||
for (const auto &argument : predicate->arguments)
|
||||
collectFreeVariables(argument, freeVariables, variableStack);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Argument>
|
||||
void collectFreeVariables(const ast::AndPointer<Argument> &and_, std::vector<normalizedAST::VariableDeclaration *> &freeVariables, VariableStack &variableStack)
|
||||
{
|
||||
for (const auto &argument : and_->arguments)
|
||||
collectFreeVariables(argument, freeVariables, variableStack);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Argument>
|
||||
void collectFreeVariables(const ast::ExistsPointer<Argument> &exists, std::vector<normalizedAST::VariableDeclaration *> &freeVariables, VariableStack &variableStack)
|
||||
{
|
||||
variableStack.push(&exists->parameters);
|
||||
|
||||
collectFreeVariables(exists->argument, freeVariables, variableStack);
|
||||
|
||||
variableStack.pop();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Argument>
|
||||
void collectFreeVariables(const ast::ForAllPointer<Argument> &forAll, std::vector<normalizedAST::VariableDeclaration *> &freeVariables, VariableStack &variableStack)
|
||||
{
|
||||
variableStack.push(&forAll->parameters);
|
||||
|
||||
collectFreeVariables(forAll->argument, freeVariables, variableStack);
|
||||
|
||||
variableStack.pop();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Argument>
|
||||
void collectFreeVariables(const ast::ImplyPointer<Argument> &imply, std::vector<normalizedAST::VariableDeclaration *> &freeVariables, VariableStack &variableStack)
|
||||
{
|
||||
collectFreeVariables(imply->argumentLeft, freeVariables, variableStack);
|
||||
collectFreeVariables(imply->argumentRight, freeVariables, variableStack);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Argument>
|
||||
void collectFreeVariables(const ast::NotPointer<Argument> ¬_, std::vector<normalizedAST::VariableDeclaration *> &freeVariables, VariableStack &variableStack)
|
||||
{
|
||||
collectFreeVariables(not_->argument, freeVariables, variableStack);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Argument>
|
||||
void collectFreeVariables(const ast::OrPointer<Argument> &or_, std::vector<normalizedAST::VariableDeclaration *> &freeVariables, VariableStack &variableStack)
|
||||
{
|
||||
for (const auto &argument : or_->arguments)
|
||||
collectFreeVariables(argument, freeVariables, variableStack);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void collectFreeVariables(const ast::UnsupportedPointer &unsupported, std::vector<normalizedAST::VariableDeclaration *> &, VariableStack &)
|
||||
{
|
||||
throw NormalizationException("cannot collect free variables of unsupported “" + unsupported->type + "” expression");
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user