Started implementing PDDL normalization.

This commit is contained in:
Patrick Lühne 2017-06-23 04:18:07 +02:00
parent e4f4b5e066
commit 9d1a8f4c0f
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
35 changed files with 1028 additions and 117 deletions

View File

@ -11,6 +11,8 @@
#include <pddlparse/AST.h>
#include <pddlparse/Exception.h>
#include <pddlparse/Mode.h>
#include <pddlparse/Normalize.h>
#include <pddlparse/NormalizedASTOutput.h>
#include <pddlparse/Parse.h>
#include <plasp/LanguageDetection.h>
@ -187,7 +189,8 @@ int main(int argc, char **argv)
auto context = pddl::Context(std::move(tokenizer), logWarning);
context.mode = parsingMode;
auto description = pddl::parseDescription(context);
const auto translator = plasp::pddl::TranslatorASP(std::move(description), logger.outputStream());
auto normalizedDescription = pddl::normalize(std::move(description));
const auto translator = plasp::pddl::TranslatorASP(std::move(normalizedDescription), logger.outputStream());
translator.translate();
}
else if (language == plasp::Language::Type::SAS)

View File

@ -3,7 +3,7 @@
#include <colorlog/ColorStream.h>
#include <pddlparse/ASTForward.h>
#include <pddlparse/NormalizedASTForward.h>
#include <pddlparse/Parse.h>
namespace plasp
@ -20,7 +20,7 @@ namespace pddl
class TranslatorASP
{
public:
explicit TranslatorASP(const ::pddl::ast::Description &description, colorlog::ColorStream &outputStream);
explicit TranslatorASP(const ::pddl::normalizedAST::Description &description, colorlog::ColorStream &outputStream);
void translate() const;
@ -35,7 +35,7 @@ class TranslatorASP
void translateGoal() const;
void translateConstants(const std::string &heading, const ::pddl::ast::ConstantDeclarations &constants) const;
const ::pddl::ast::Description &m_description;
const ::pddl::normalizedAST::Description &m_description;
colorlog::ColorStream &m_outputStream;
};

View File

@ -3,7 +3,7 @@
#include <colorlog/Formatting.h>
#include <pddlparse/AST.h>
#include <pddlparse/NormalizedAST.h>
#include <plasp/TranslatorException.h>
@ -22,7 +22,7 @@ namespace pddl
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename PrintObjectName>
inline void translateEffect(colorlog::ColorStream &outputStream, const ::pddl::ast::Effect &effect, const std::string &objectType, PrintObjectName printObjectName)
inline void translateEffect(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::Effect &effect, const std::string &objectType, PrintObjectName printObjectName)
{
const auto handleUnsupported =
[](const auto &)
@ -31,7 +31,7 @@ inline void translateEffect(colorlog::ColorStream &outputStream, const ::pddl::a
};
const auto handlePredicate =
[&](const ::pddl::ast::PredicatePointer &predicate, bool isPositive = true)
[&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true)
{
outputStream << std::endl << colorlog::Function("postcondition") << "(";
printObjectName();
@ -46,24 +46,24 @@ inline void translateEffect(colorlog::ColorStream &outputStream, const ::pddl::a
};
const auto handleAtomicFormula =
[&](const ::pddl::ast::AtomicFormula &atomicFormula)
[&](const ::pddl::normalizedAST::AtomicFormula &atomicFormula)
{
atomicFormula.match(handlePredicate, handleUnsupported);
};
const auto handleNot =
[&](const ::pddl::ast::NotPointer<::pddl::ast::Effect> &not_)
[&](const ::pddl::normalizedAST::NotPointer<::pddl::normalizedAST::Effect> &not_)
{
if (!not_->argument.is<::pddl::ast::AtomicFormula>() || !not_->argument.get<::pddl::ast::AtomicFormula>().is<::pddl::ast::PredicatePointer>())
if (!not_->argument.is<::pddl::normalizedAST::AtomicFormula>() || !not_->argument.get<::pddl::normalizedAST::AtomicFormula>().is<::pddl::normalizedAST::PredicatePointer>())
handleUnsupported(not_);
const auto &predicate = not_->argument.get<::pddl::ast::AtomicFormula>().get<::pddl::ast::PredicatePointer>();
const auto &predicate = not_->argument.get<::pddl::normalizedAST::AtomicFormula>().get<::pddl::normalizedAST::PredicatePointer>();
handlePredicate(predicate, false);
};
const auto handleAnd =
[&](const ::pddl::ast::AndPointer<::pddl::ast::Effect> &and_)
[&](const ::pddl::normalizedAST::AndPointer<::pddl::normalizedAST::Effect> &and_)
{
for (const auto &argument : and_->arguments)
translateEffect(outputStream, argument, objectType, printObjectName);

View File

@ -3,7 +3,7 @@
#include <colorlog/Formatting.h>
#include <pddlparse/AST.h>
#include <pddlparse/NormalizedAST.h>
#include <plasp/TranslatorException.h>
@ -21,16 +21,10 @@ namespace pddl
//
////////////////////////////////////////////////////////////////////////////////////////////////////
inline void translateGoal(colorlog::ColorStream &outputStream, const ::pddl::ast::Goal &goal)
inline void translateGoal(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::Goal &goal)
{
const auto handleUnsupported =
[](const auto &)
{
throw TranslatorException("only “and” expressions and (negated) predicates supported as goals currently");
};
const auto handlePredicate =
[&](const ::pddl::ast::PredicatePointer &predicate, bool isPositive = true)
[&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true)
{
outputStream << std::endl << colorlog::Function("goal") << "(";
// TODO: assert that goal is variable-free
@ -38,31 +32,54 @@ inline void translateGoal(colorlog::ColorStream &outputStream, const ::pddl::ast
outputStream << ").";
};
const auto handleAtomicFormula =
[&](const ::pddl::ast::AtomicFormula &atomicFormula)
const auto handleNegatedPredicate =
[&](const ::pddl::normalizedAST::PredicatePointer &predicate)
{
atomicFormula.match(handlePredicate, handleUnsupported);
};
const auto handleNot =
[&](const ::pddl::ast::NotPointer<::pddl::ast::Goal> &not_)
{
if (!not_->argument.is<::pddl::ast::AtomicFormula>() || !not_->argument.get<::pddl::ast::AtomicFormula>().is<::pddl::ast::PredicatePointer>())
handleUnsupported(not_);
const auto &predicate = not_->argument.get<::pddl::ast::AtomicFormula>().get<::pddl::ast::PredicatePointer>();
handlePredicate(predicate, false);
};
const auto handleAnd =
[&](const ::pddl::ast::AndPointer<::pddl::ast::Goal> &and_)
const auto handleDerivedPredicate =
[&](const ::pddl::normalizedAST::DerivedPredicatePointer &, bool = true)
{
for (const auto &argument : and_->arguments)
translateGoal(outputStream, argument);
outputStream << std::endl << colorlog::Function("goal") << "(";
// TODO: assert that goal is variable-free
// TODO: implement
//translatePredicateToVariable(outputStream, *predicate, isPositive);
outputStream << ").";
};
goal.match(handleAtomicFormula, handleNot, handleAnd, handleUnsupported);
const auto handleNegatedDerivedPredicate =
[&](const ::pddl::normalizedAST::DerivedPredicatePointer &derivedPredicate)
{
handleDerivedPredicate(derivedPredicate, false);
};
const auto handleAtomicFormula =
[&](const ::pddl::normalizedAST::AtomicFormula &atomicFormula)
{
atomicFormula.match(handlePredicate, handleDerivedPredicate);
};
const auto handleNot =
[&](const ::pddl::normalizedAST::NotPointer<::pddl::normalizedAST::AtomicFormula> &not_)
{
not_->argument.match(handleNegatedPredicate, handleNegatedDerivedPredicate);
};
const auto handleLiteral =
[&](const ::pddl::normalizedAST::Literal &literal)
{
literal.match(handleAtomicFormula, handleNot);
};
const auto handleAnd =
[&](const ::pddl::normalizedAST::AndPointer<::pddl::normalizedAST::Literal> &and_)
{
for (const auto &argument : and_->arguments)
handleLiteral(argument);
};
goal.match(handleLiteral, handleAnd);
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -3,7 +3,7 @@
#include <colorlog/Formatting.h>
#include <pddlparse/AST.h>
#include <pddlparse/NormalizedAST.h>
#include <plasp/TranslatorException.h>
@ -22,16 +22,10 @@ namespace pddl
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename PrintObjectName>
inline void translatePrecondition(colorlog::ColorStream &outputStream, const ::pddl::ast::Precondition &precondition, const std::string &objectType, PrintObjectName printObjectName)
inline void translatePrecondition(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::Precondition &precondition, const std::string &objectType, PrintObjectName printObjectName)
{
const auto handleUnsupported =
[](const auto &)
{
throw TranslatorException("only “and” expressions and (negated) predicates supported as action preconditions currently");
};
const auto handlePredicate =
[&](const ::pddl::ast::PredicatePointer &predicate, bool isPositive = true)
[&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true)
{
outputStream << std::endl << colorlog::Function("precondition") << "(";
printObjectName();
@ -42,31 +36,57 @@ inline void translatePrecondition(colorlog::ColorStream &outputStream, const ::p
outputStream << ").";
};
const auto handleAtomicFormula =
[&](const ::pddl::ast::AtomicFormula &atomicFormula)
const auto handleNegatedPredicate =
[&](const ::pddl::normalizedAST::PredicatePointer &predicate)
{
atomicFormula.match(handlePredicate, handleUnsupported);
};
const auto handleNot =
[&](const ::pddl::ast::NotPointer<::pddl::ast::Precondition> &not_)
{
if (!not_->argument.is<::pddl::ast::AtomicFormula>() || !not_->argument.get<::pddl::ast::AtomicFormula>().is<::pddl::ast::PredicatePointer>())
handleUnsupported(not_);
const auto &predicate = not_->argument.get<::pddl::ast::AtomicFormula>().get<::pddl::ast::PredicatePointer>();
handlePredicate(predicate, false);
};
const auto handleAnd =
[&](const ::pddl::ast::AndPointer<::pddl::ast::Precondition> &and_)
const auto handleDerivedPredicate =
[&](const ::pddl::normalizedAST::DerivedPredicatePointer &, bool = true)
{
for (const auto &argument : and_->arguments)
translatePrecondition(outputStream, argument, objectType, printObjectName);
outputStream << std::endl << colorlog::Function("precondition") << "(";
printObjectName();
outputStream << ", ";
// TODO: implement
/*translatePredicateToVariable(outputStream, *predicate, isPositive);
outputStream << ") :- " << output::Function(objectType.c_str()) << "(";
printObjectName();
outputStream << ").";*/
};
precondition.match(handleAtomicFormula, handleNot, handleAnd, handleUnsupported);
const auto handleNegatedDerivedPredicate =
[&](const ::pddl::normalizedAST::DerivedPredicatePointer &derivedPredicate)
{
handleDerivedPredicate(derivedPredicate, false);
};
const auto handleAtomicFormula =
[&](const ::pddl::normalizedAST::AtomicFormula &atomicFormula)
{
atomicFormula.match(handlePredicate, handleDerivedPredicate);
};
const auto handleNot =
[&](const ::pddl::normalizedAST::NotPointer<::pddl::normalizedAST::AtomicFormula> &not_)
{
not_->argument.match(handleNegatedPredicate, handleNegatedDerivedPredicate);
};
const auto handleLiteral =
[&](const ::pddl::normalizedAST::Literal &literal)
{
literal.match(handleAtomicFormula, handleNot);
};
const auto handleAnd =
[&](const ::pddl::normalizedAST::AndPointer<::pddl::normalizedAST::Literal> &and_)
{
for (const auto &argument : and_->arguments)
handleLiteral(argument);
};
precondition.match(handleLiteral, handleAnd);
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -3,7 +3,7 @@
#include <colorlog/Formatting.h>
#include <pddlparse/AST.h>
#include <pddlparse/NormalizedAST.h>
#include <pddlparse/Parse.h>
#include <plasp/pddl/translation/Primitives.h>
@ -20,12 +20,12 @@ namespace pddl
//
////////////////////////////////////////////////////////////////////////////////////////////////////
void translatePredicate(colorlog::ColorStream &outputStream, const ::pddl::ast::Predicate &predicate);
void translatePredicateDeclaration(colorlog::ColorStream &outputStream, const ::pddl::ast::PredicateDeclaration &predicateDeclaration);
void translatePredicate(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::Predicate &predicate);
void translatePredicateDeclaration(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::PredicateDeclaration &predicateDeclaration);
////////////////////////////////////////////////////////////////////////////////////////////////////
inline void translatePredicate(colorlog::ColorStream &outputStream, const ::pddl::ast::Predicate &predicate)
inline void translatePredicate(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::Predicate &predicate)
{
const auto &arguments = predicate.arguments;
@ -42,13 +42,13 @@ inline void translatePredicate(colorlog::ColorStream &outputStream, const ::pddl
outputStream << ", ";
const auto handleConstant =
[&](const ::pddl::ast::ConstantPointer &constant)
[&](const ::pddl::normalizedAST::ConstantPointer &constant)
{
outputStream << colorlog::Keyword("constant") << "(" << *constant << ")";
};
const auto handleVariable =
[&](const ::pddl::ast::VariablePointer &variable)
[&](const ::pddl::normalizedAST::VariablePointer &variable)
{
outputStream << *variable;
};
@ -67,7 +67,7 @@ inline void translatePredicate(colorlog::ColorStream &outputStream, const ::pddl
////////////////////////////////////////////////////////////////////////////////////////////////////
inline void translatePredicateDeclaration(colorlog::ColorStream &outputStream, const ::pddl::ast::PredicateDeclaration &predicateDeclaration)
inline void translatePredicateDeclaration(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::PredicateDeclaration &predicateDeclaration)
{
outputStream << colorlog::Keyword("variable") << "(";
@ -84,7 +84,7 @@ inline void translatePredicateDeclaration(colorlog::ColorStream &outputStream, c
////////////////////////////////////////////////////////////////////////////////////////////////////
void translatePredicateToVariable(colorlog::ColorStream &outputStream, const ::pddl::ast::Predicate &predicate, bool isPositive = true)
void translatePredicateToVariable(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::Predicate &predicate, bool isPositive = true)
{
outputStream << colorlog::Keyword("variable") << "(";
translatePredicate(outputStream, predicate);

View File

@ -3,7 +3,7 @@
#include <colorlog/Formatting.h>
#include <pddlparse/AST.h>
#include <pddlparse/NormalizedAST.h>
#include <plasp/TranslatorException.h>
@ -18,7 +18,7 @@ namespace pddl
//
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::pddl::ast::ConstantDeclaration &constantDeclaration)
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::pddl::normalizedAST::ConstantDeclaration &constantDeclaration)
{
assert(!constantDeclaration.name.empty());
@ -27,7 +27,7 @@ inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::pddl::ast::Constant &constant)
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::pddl::normalizedAST::Constant &constant)
{
assert(constant.declaration != nullptr);
@ -36,7 +36,7 @@ inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::pddl::ast::PrimitiveTypeDeclaration &primitiveTypeDeclaration)
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::pddl::normalizedAST::PrimitiveTypeDeclaration &primitiveTypeDeclaration)
{
assert(!primitiveTypeDeclaration.name.empty());
@ -45,7 +45,7 @@ inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::pddl::ast::PrimitiveType &primitiveType)
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::pddl::normalizedAST::PrimitiveType &primitiveType)
{
assert(primitiveType.declaration != nullptr);
@ -54,7 +54,7 @@ inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, ::pddl::ast::VariableDeclaration &variableDeclaration)
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, ::pddl::normalizedAST::VariableDeclaration &variableDeclaration)
{
assert(!variableDeclaration.name.empty());
assert(std::isalpha(variableDeclaration.name[0]));
@ -70,7 +70,7 @@ inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, ::pddl::
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, ::pddl::ast::Variable &variable)
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, ::pddl::normalizedAST::Variable &variable)
{
assert(variable.declaration != nullptr);
@ -80,7 +80,7 @@ inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, ::pddl::
////////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: move to appropriate header
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::pddl::ast::Action &action)
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::pddl::normalizedAST::Action &action)
{
return (stream << colorlog::String(action.name.c_str()));
}
@ -88,7 +88,7 @@ inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::
////////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: move to appropriate header
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::pddl::ast::PredicateDeclaration &predicateDeclaration)
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::pddl::normalizedAST::PredicateDeclaration &predicateDeclaration)
{
return (stream << colorlog::String(predicateDeclaration.name.c_str()));
}
@ -96,13 +96,29 @@ inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::
////////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: move to appropriate header
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::pddl::ast::Predicate &predicate)
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::pddl::normalizedAST::Predicate &predicate)
{
return (stream << *predicate.declaration);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: move to appropriate header
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::pddl::normalizedAST::DerivedPredicateDeclaration &derivedPredicateDeclaration)
{
return (stream << colorlog::String(derivedPredicateDeclaration.name.c_str()));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: move to appropriate header
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const ::pddl::normalizedAST::DerivedPredicate &derivedPredicate)
{
return (stream << *derivedPredicate.declaration);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

@ -3,7 +3,7 @@
#include <colorlog/Formatting.h>
#include <pddlparse/AST.h>
#include <pddlparse/NormalizedAST.h>
#include <pddlparse/Parse.h>
#include <plasp/TranslatorException.h>
@ -53,10 +53,10 @@ inline void translateVariablesForRuleBody(colorlog::ColorStream &outputStream, c
if (variable->type)
{
if (!variable->type.value().template is<::pddl::ast::PrimitiveTypePointer>())
if (!variable->type.value().template is<::pddl::normalizedAST::PrimitiveTypePointer>())
throw TranslatorException("only primitive types supported currently");
const auto &type = variable->type.value().template get<::pddl::ast::PrimitiveTypePointer>();
const auto &type = variable->type.value().template get<::pddl::normalizedAST::PrimitiveTypePointer>();
outputStream << colorlog::Function("has") << "("
<< *variable << ", " << colorlog::Keyword("type") << "(" << *type << "))";

View File

@ -82,6 +82,14 @@ class ParserException : public Exception
////////////////////////////////////////////////////////////////////////////////////////////////////
class NormalizationException : public Exception
{
public:
using Exception::Exception;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
#endif

View File

@ -0,0 +1,24 @@
#ifndef __PDDL_PARSE__NORMALIZE_H
#define __PDDL_PARSE__NORMALIZE_H
#include <pddlparse/AST.h>
#include <pddlparse/Context.h>
#include <pddlparse/NormalizedAST.h>
#include <pddlparse/detail/normalization/Description.h>
namespace pddl
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Normalize
//
////////////////////////////////////////////////////////////////////////////////////////////////////
using detail::normalize;
////////////////////////////////////////////////////////////////////////////////////////////////////
}
#endif

View File

@ -133,6 +133,7 @@ struct Problem
Domain *domain;
std::string name;
DerivedPredicateDeclarations derivedPredicates;
ConstantDeclarations objects;
InitialState initialState;
std::experimental::optional<Goal> goal;

View File

@ -66,6 +66,9 @@ using ast::AtPointer;
using ast::Either;
using ast::EitherPointer;
using ast::Exists;
using ast::ExistsPointer;
using ast::ForAll;
using ast::ForAllPointer;
using ast::Not;
using ast::NotPointer;
using ast::When;
@ -170,7 +173,7 @@ namespace detail
using EffectT = Variant<
AtomicFormula,
AndPointer<Effect>,
ast::ForAllPointer<Effect>,
ForAllPointer<Effect>,
NotPointer<Effect>,
WhenPointer<Precondition, ConditionalEffect>>;
}

View File

@ -0,0 +1,26 @@
#ifndef __PDDL_PARSE__DETAIL__NORMALIZATION__ACTION_H
#define __PDDL_PARSE__DETAIL__NORMALIZATION__ACTION_H
#include <pddlparse/ASTForward.h>
#include <pddlparse/Context.h>
#include <pddlparse/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Action
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::ActionPointer normalize(ast::ActionPointer &&domain, normalizedAST::DerivedPredicateDeclarations &derivedPredicates);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@ -0,0 +1,26 @@
#ifndef __PDDL_PARSE__DETAIL__NORMALIZATION__ATOMIC_FORMULA_H
#define __PDDL_PARSE__DETAIL__NORMALIZATION__ATOMIC_FORMULA_H
#include <pddlparse/ASTForward.h>
#include <pddlparse/Context.h>
#include <pddlparse/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// AtomicFormula
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::AtomicFormula normalize(ast::AtomicFormula &&atomicFormula);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@ -0,0 +1,26 @@
#ifndef __PDDL_PARSE__DETAIL__NORMALIZATION__DESCRIPTION_H
#define __PDDL_PARSE__DETAIL__NORMALIZATION__DESCRIPTION_H
#include <pddlparse/ASTForward.h>
#include <pddlparse/Context.h>
#include <pddlparse/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Description
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::Description normalize(ast::Description &&description);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@ -0,0 +1,26 @@
#ifndef __PDDL_PARSE__DETAIL__NORMALIZATION__DOMAIN_H
#define __PDDL_PARSE__DETAIL__NORMALIZATION__DOMAIN_H
#include <pddlparse/ASTForward.h>
#include <pddlparse/Context.h>
#include <pddlparse/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Domain
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::DomainPointer normalize(ast::DomainPointer &&domain);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@ -0,0 +1,26 @@
#ifndef __PDDL_PARSE__DETAIL__NORMALIZATION__EFFECT_H
#define __PDDL_PARSE__DETAIL__NORMALIZATION__EFFECT_H
#include <pddlparse/ASTForward.h>
#include <pddlparse/Context.h>
#include <pddlparse/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Effect
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::Effect normalize(ast::Effect &&effect);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@ -0,0 +1,26 @@
#ifndef __PDDL_PARSE__DETAIL__NORMALIZATION__FACT_H
#define __PDDL_PARSE__DETAIL__NORMALIZATION__FACT_H
#include <pddlparse/ASTForward.h>
#include <pddlparse/Context.h>
#include <pddlparse/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Fact
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::Fact normalize(ast::Fact &&fact);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@ -0,0 +1,26 @@
#ifndef __PDDL_PARSE__DETAIL__NORMALIZATION__INITIAL_STATE_H
#define __PDDL_PARSE__DETAIL__NORMALIZATION__INITIAL_STATE_H
#include <pddlparse/ASTForward.h>
#include <pddlparse/Context.h>
#include <pddlparse/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// InitialState
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::InitialState normalize(ast::InitialState &&initialState);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@ -0,0 +1,26 @@
#ifndef __PDDL_PARSE__DETAIL__NORMALIZATION__LITERAL_H
#define __PDDL_PARSE__DETAIL__NORMALIZATION__LITERAL_H
#include <pddlparse/ASTForward.h>
#include <pddlparse/Context.h>
#include <pddlparse/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Literal
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::Literal normalize(ast::Literal &&literal);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@ -0,0 +1,26 @@
#ifndef __PDDL_PARSE__DETAIL__NORMALIZATION__PRECONDITION_H
#define __PDDL_PARSE__DETAIL__NORMALIZATION__PRECONDITION_H
#include <pddlparse/ASTForward.h>
#include <pddlparse/Context.h>
#include <pddlparse/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Precondition
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::Precondition normalize(ast::Precondition &&precondition, normalizedAST::DerivedPredicateDeclarations &derivedPredicates);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@ -0,0 +1,26 @@
#ifndef __PDDL_PARSE__DETAIL__NORMALIZATION__PROBLEM_H
#define __PDDL_PARSE__DETAIL__NORMALIZATION__PROBLEM_H
#include <pddlparse/ASTForward.h>
#include <pddlparse/Context.h>
#include <pddlparse/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Problem
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::ProblemPointer normalize(ast::ProblemPointer &&problem, normalizedAST::Domain *domain);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@ -9,6 +9,9 @@ file(GLOB detail_headers "../include/pddlparse/detail/*.h")
file(GLOB detail_parsing_sources "pddlparse/detail/parsing/*.cpp")
file(GLOB detail_parsing_headers "../include/pddlparse/detail/parsing/*.h")
file(GLOB detail_normalization_sources "pddlparse/detail/normalization/*.cpp")
file(GLOB detail_normalization_headers "../include/pddlparse/detail/normalization/*.h")
set(includes
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/../../lib/colorlog/include
@ -25,6 +28,9 @@ set(sources
${detail_parsing_sources}
${detail_parsing_headers}
${detail_normalization_sources}
${detail_normalization_headers}
)
set(libraries

View File

@ -0,0 +1,41 @@
#include <pddlparse/detail/normalization/Action.h>
#include <pddlparse/AST.h>
#include <pddlparse/NormalizedAST.h>
#include <pddlparse/detail/normalization/Effect.h>
#include <pddlparse/detail/normalization/Precondition.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Action
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::ActionPointer normalize(ast::ActionPointer &&action, normalizedAST::DerivedPredicateDeclarations &derivedPredicates)
{
auto normalizedAction = std::make_unique<normalizedAST::Action>();
normalizedAction->name = std::move(action->name);
normalizedAction->parameters = std::move(action->parameters);
if (action->precondition)
normalizedAction->precondition = normalize(std::move(action->precondition.value()), derivedPredicates);
// TODO: implement
/*
if (action->effect)
normalizedAction->effect = normalize(std::move(action->effect.value()));
*/
return normalizedAction;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

@ -0,0 +1,38 @@
#include <pddlparse/detail/normalization/AtomicFormula.h>
#include <pddlparse/AST.h>
#include <pddlparse/Exception.h>
#include <pddlparse/NormalizedAST.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// AtomicFormula
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::AtomicFormula normalize(ast::AtomicFormula &&atomicFormula)
{
const auto handleUnsupported =
[&](ast::UnsupportedPointer &unsupported) -> normalizedAST::AtomicFormula
{
throw NormalizationException("" + unsupported->type + "” expressions as literals cant be normalized currently");
};
const auto handlePredicate =
[&](ast::PredicatePointer &predicate) -> normalizedAST::AtomicFormula
{
return std::move(predicate);
};
return atomicFormula.match(handlePredicate, handleUnsupported);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

@ -0,0 +1,34 @@
#include <pddlparse/detail/normalization/Description.h>
#include <pddlparse/AST.h>
#include <pddlparse/NormalizedAST.h>
#include <pddlparse/detail/normalization/Domain.h>
#include <pddlparse/detail/normalization/Problem.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Description
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::Description normalize(ast::Description &&description)
{
normalizedAST::Description normalizedDescription;
normalizedDescription.domain = normalize(std::move(description.domain));
if (description.problem)
normalizedDescription.problem = normalize(std::move(description.problem.value()), normalizedDescription.domain.get());
return normalizedDescription;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

@ -0,0 +1,38 @@
#include <pddlparse/detail/normalization/Domain.h>
#include <pddlparse/AST.h>
#include <pddlparse/NormalizedAST.h>
#include <pddlparse/detail/normalization/Action.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Description
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::DomainPointer normalize(ast::DomainPointer &&domain)
{
auto normalizedDomain = std::make_unique<normalizedAST::Domain>();
normalizedDomain->types = std::move(domain->types);
normalizedDomain->name = std::move(domain->name);
normalizedDomain->constants = std::move(domain->constants);
normalizedDomain->predicates = std::move(domain->predicates);
normalizedDomain->actions.reserve(domain->actions.size());
for (auto &&action : domain->actions)
normalizedDomain->actions.emplace_back(normalize(std::move(action), normalizedDomain->derivedPredicates));
return normalizedDomain;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

@ -0,0 +1,28 @@
#include <pddlparse/detail/normalization/Effect.h>
#include <pddlparse/AST.h>
#include <pddlparse/Exception.h>
#include <pddlparse/NormalizedAST.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Effect
//
////////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: implement
/*
normalizedAST::Effect normalize(ast::Effect &&)
{
}
*/
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

@ -0,0 +1,39 @@
#include <pddlparse/detail/normalization/Fact.h>
#include <pddlparse/AST.h>
#include <pddlparse/Exception.h>
#include <pddlparse/NormalizedAST.h>
#include <pddlparse/detail/normalization/Literal.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Fact
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::Fact normalize(ast::Fact &&fact)
{
const auto handleLiteral =
[&](ast::Literal &literal) -> normalizedAST::Literal
{
return normalize(std::move(literal));
};
const auto handleAt =
[&](ast::AtPointer<ast::Literal> &) -> normalizedAST::Literal
{
throw NormalizationException("“at” expressions in preconditions cant be normalized currently");
};
return fact.match(handleLiteral, handleAt);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

@ -0,0 +1,33 @@
#include <pddlparse/detail/normalization/InitialState.h>
#include <pddlparse/AST.h>
#include <pddlparse/Exception.h>
#include <pddlparse/NormalizedAST.h>
#include <pddlparse/detail/normalization/Fact.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// InitialState
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::InitialState normalize(ast::InitialState &&initialState)
{
normalizedAST::InitialState normalizedInitialState;
normalizedInitialState.facts.reserve(initialState.facts.size());
for (auto &fact : initialState.facts)
normalizedInitialState.facts.emplace_back(normalize(std::move(fact)));
return normalizedInitialState;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

@ -0,0 +1,39 @@
#include <pddlparse/detail/normalization/Literal.h>
#include <pddlparse/AST.h>
#include <pddlparse/Exception.h>
#include <pddlparse/NormalizedAST.h>
#include <pddlparse/detail/normalization/AtomicFormula.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Literal
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::Literal normalize(ast::Literal &&literal)
{
const auto handleAtomicFormula =
[&](ast::AtomicFormula &atomicFormula) -> normalizedAST::Literal
{
return normalize(std::move(atomicFormula));
};
const auto handleNot =
[&](ast::NotPointer<ast::AtomicFormula> &not_) -> normalizedAST::Literal
{
return std::make_unique<normalizedAST::Not<normalizedAST::AtomicFormula>>(normalize(std::move(not_->argument)));
};
return literal.match(handleAtomicFormula, handleNot);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

@ -0,0 +1,218 @@
#include <pddlparse/detail/normalization/Precondition.h>
#include <pddlparse/AST.h>
#include <pddlparse/Exception.h>
#include <pddlparse/NormalizedAST.h>
#include <pddlparse/detail/normalization/AtomicFormula.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Precondition
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::DerivedPredicatePointer addDerivedPredicate(normalizedAST::DerivedPredicateDeclarations &derivedPredicates, const std::string &type)
{
auto name = "derived-" + type + "-" + std::to_string(derivedPredicates.size() + 1);
derivedPredicates.emplace_back(std::make_unique<normalizedAST::DerivedPredicateDeclaration>(std::move(name), normalizedAST::VariableDeclarations()));
return std::make_unique<normalizedAST::DerivedPredicate>(normalizedAST::DerivedPredicate::Arguments(), derivedPredicates.back().get());
}
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::DerivedPredicatePointer normalizeNested(ast::AndPointer<ast::Precondition> &, normalizedAST::DerivedPredicateDeclarations &derivedPredicates)
{
// TODO: handle arguments recursively
return addDerivedPredicate(derivedPredicates, "and");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::DerivedPredicatePointer normalizeNested(ast::AtomicFormula &, normalizedAST::DerivedPredicateDeclarations &)
{
// TODO: add explanation
throw std::logic_error("atomic formulas should have been handled earlier");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::DerivedPredicatePointer normalizeNested(ast::ExistsPointer<ast::Precondition> &, normalizedAST::DerivedPredicateDeclarations &derivedPredicates)
{
// TODO: handle arguments recursively
return addDerivedPredicate(derivedPredicates, "exists");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::DerivedPredicatePointer normalizeNested(ast::ForAllPointer<ast::Precondition> &, normalizedAST::DerivedPredicateDeclarations &derivedPredicates)
{
// TODO: handle arguments recursively
return addDerivedPredicate(derivedPredicates, "forall");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::DerivedPredicatePointer normalizeNested(ast::ImplyPointer<ast::Precondition> &, normalizedAST::DerivedPredicateDeclarations &derivedPredicates)
{
// TODO: handle arguments recursively
return addDerivedPredicate(derivedPredicates, "imply");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::DerivedPredicatePointer normalizeNested(ast::NotPointer<ast::Precondition> &, normalizedAST::DerivedPredicateDeclarations &derivedPredicates)
{
// TODO: handle arguments recursively
return addDerivedPredicate(derivedPredicates, "not");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::DerivedPredicatePointer normalizeNested(ast::OrPointer<ast::Precondition> &, normalizedAST::DerivedPredicateDeclarations &derivedPredicates)
{
// TODO: handle arguments recursively
return addDerivedPredicate(derivedPredicates, "or");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::PredicatePointer normalize(ast::UnsupportedPointer &&unsupported, normalizedAST::DerivedPredicateDeclarations &)
{
throw NormalizationException("" + unsupported->type + "” expressions in preconditions cant be normalized currently");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Normalize top-level negations
normalizedAST::NotPointer<normalizedAST::AtomicFormula> normalize(ast::NotPointer<ast::Precondition> &&not_, normalizedAST::DerivedPredicateDeclarations &derivedPredicates)
{
// “not” expressions may be nested one time to form simple literals
if (not_->argument.is<ast::AtomicFormula>())
return std::make_unique<normalizedAST::Not<normalizedAST::AtomicFormula>>(normalize(std::move(not_->argument.get<ast::AtomicFormula>())));
const auto handleNested =
[&](auto &nested) -> normalizedAST::AtomicFormula
{
return normalizeNested(nested, derivedPredicates);
};
const auto handleUnsupported =
[&](ast::UnsupportedPointer &unsupported) -> normalizedAST::AtomicFormula
{
throw NormalizationException("" + unsupported->type + "” expressions in preconditions cant be normalized currently");
};
auto normalizedArgument = not_->argument.match(handleNested, handleUnsupported);
return std::make_unique<normalizedAST::Not<normalizedAST::AtomicFormula>>(std::move(normalizedArgument));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Normalize top-level conjunctions
normalizedAST::AndPointer<normalizedAST::Literal> normalize(ast::AndPointer<ast::Precondition> &&and_, normalizedAST::DerivedPredicateDeclarations &derivedPredicates)
{
normalizedAST::And<normalizedAST::Literal>::Arguments arguments;
arguments.reserve(and_->arguments.size());
const auto handleAtomicFormula =
[&](ast::AtomicFormula &atomicFormula) -> normalizedAST::Literal
{
return normalize(std::move(atomicFormula));
};
const auto handleNot =
[&](ast::NotPointer<ast::Precondition> &not_) -> normalizedAST::Literal
{
return normalize(std::move(not_), derivedPredicates);
};
const auto handleNested =
[&](auto &nested) -> normalizedAST::Literal
{
return normalizeNested(nested, derivedPredicates);
};
const auto handleUnsupported =
[&](ast::UnsupportedPointer &unsupported) -> normalizedAST::Literal
{
throw NormalizationException("" + unsupported->type + "” expressions in preconditions cant be normalized currently");
};
for (auto &&argument : and_->arguments)
{
auto normalizedArgument = argument.match(handleAtomicFormula, handleNot, handleNested, handleUnsupported);
arguments.emplace_back(std::move(normalizedArgument));
}
return std::make_unique<normalizedAST::And<normalizedAST::Literal>>(std::move(arguments));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::Precondition normalize(ast::Precondition &&precondition, normalizedAST::DerivedPredicateDeclarations &derivedPredicates)
{
/*
reduce
AtomicFormula,
AndPointer<Precondition>,
NotPointer<Precondition>,
UnsupportedPointer>;
ExistsPointer<Precondition>,
ForAllPointer<Precondition>,
ImplyPointer<Precondition>,
OrPointer<Precondition>,
to
Literal,
AndPointer<Literal>>;
*/
const auto handleAtomicFormula =
[&](ast::AtomicFormula &atomicFormula) -> normalizedAST::Precondition
{
return normalize(std::move(atomicFormula));
};
const auto handleNot =
[&](ast::NotPointer<ast::Precondition> &not_) -> normalizedAST::Precondition
{
return normalize(std::move(not_), derivedPredicates);
};
const auto handleAnd =
[&](ast::AndPointer<ast::Precondition> &and_) -> normalizedAST::Precondition
{
return normalize(std::move(and_), derivedPredicates);
};
const auto handleNested =
[&](auto &nested) -> normalizedAST::Precondition
{
return normalizeNested(nested, derivedPredicates);
};
const auto handleUnsupported =
[&](ast::UnsupportedPointer &unsupported) -> normalizedAST::Precondition
{
throw NormalizationException("" + unsupported->type + "” expressions in preconditions cant be normalized currently");
};
return precondition.match(handleAtomicFormula, handleNot, handleAnd, handleUnsupported, handleNested);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

@ -0,0 +1,36 @@
#include <pddlparse/detail/normalization/Problem.h>
#include <pddlparse/AST.h>
#include <pddlparse/NormalizedAST.h>
#include <pddlparse/detail/normalization/InitialState.h>
#include <pddlparse/detail/normalization/Precondition.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Problem
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::ProblemPointer normalize(ast::ProblemPointer &&problem, normalizedAST::Domain *domain)
{
auto normalizedProblem = std::make_unique<normalizedAST::Problem>(domain);
normalizedProblem->name = std::move(problem->name);
normalizedProblem->objects = std::move(problem->objects);
normalizedProblem->initialState = normalize(std::move(problem->initialState));
if (problem->goal)
normalizedProblem->goal = normalize(std::move(problem->goal.value()), normalizedProblem->derivedPredicates);
return normalizedProblem;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

@ -4,7 +4,7 @@
#include <colorlog/Formatting.h>
#include <pddlparse/AST.h>
#include <pddlparse/NormalizedAST.h>
#include <plasp/TranslatorException.h>
@ -26,7 +26,7 @@ namespace pddl
//
////////////////////////////////////////////////////////////////////////////////////////////////////
TranslatorASP::TranslatorASP(const ::pddl::ast::Description &description, colorlog::ColorStream &outputStream)
TranslatorASP::TranslatorASP(const ::pddl::normalizedAST::Description &description, colorlog::ColorStream &outputStream)
: m_description{description},
m_outputStream(outputStream)
{
@ -216,7 +216,7 @@ void TranslatorASP::translateActions() const
////////////////////////////////////////////////////////////////////////////////////////////////////
void TranslatorASP::translateConstants(const std::string &heading, const ::pddl::ast::ConstantDeclarations &constants) const
void TranslatorASP::translateConstants(const std::string &heading, const ::pddl::normalizedAST::ConstantDeclarations &constants) const
{
m_outputStream << colorlog::Heading2(heading.c_str());
@ -232,10 +232,10 @@ void TranslatorASP::translateConstants(const std::string &heading, const ::pddl:
if (type)
{
if (!type.value().is<::pddl::ast::PrimitiveTypePointer>())
if (!type.value().is<::pddl::normalizedAST::PrimitiveTypePointer>())
throw TranslatorException("only primitive types supported currently");
const auto &primitveType = type.value().get<::pddl::ast::PrimitiveTypePointer>();
const auto &primitveType = type.value().get<::pddl::normalizedAST::PrimitiveTypePointer>();
m_outputStream << colorlog::Function("has") << "("
<< colorlog::Keyword("constant") << "(" << *constant << "), "
@ -294,36 +294,44 @@ void TranslatorASP::translateInitialState() const
{
m_outputStream << std::endl << colorlog::Function("initialState") << "(";
const auto handleUnsupported =
[&](const auto &)
const auto handlePredicate =
[&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true)
{
throw TranslatorException("only predicates and their negations supported in initial state currently");
};
const auto handleAtomicFormula =
[&](const ::pddl::ast::AtomicFormula &atomicFormula, bool isPositive = true)
{
if (!atomicFormula.is<::pddl::ast::PredicatePointer>())
handleUnsupported(atomicFormula);
const auto &predicate = atomicFormula.get<::pddl::ast::PredicatePointer>();
translatePredicateToVariable(m_outputStream, *predicate, isPositive);
};
const auto handleNegatedPredicate =
[&](const ::pddl::normalizedAST::PredicatePointer &predicate)
{
return handlePredicate(predicate, false);
};
const auto handleDerivedPredicate =
[&](const ::pddl::normalizedAST::DerivedPredicatePointer &, bool = true)
{
// TODO: implement
//translatePredicateToVariable(m_outputStream, *predicate, true);
};
const auto handleNegatedDerivedPredicate =
[&](const ::pddl::normalizedAST::DerivedPredicatePointer &derivedPredicate)
{
return handleDerivedPredicate(derivedPredicate, false);
};
const auto handleAtomicFormula =
[&](const ::pddl::normalizedAST::AtomicFormula &atomicFormula)
{
atomicFormula.match(handlePredicate, handleDerivedPredicate);
};
const auto handleNot =
[&](const ::pddl::ast::NotPointer<::pddl::ast::AtomicFormula> &not_)
[&](const ::pddl::normalizedAST::NotPointer<::pddl::normalizedAST::AtomicFormula> &not_)
{
handleAtomicFormula(not_->argument, false);
not_->argument.match(handleNegatedPredicate, handleNegatedDerivedPredicate);
};
const auto handleLiteral =
[&](const ::pddl::ast::Literal &literal)
{
literal.match(handleAtomicFormula, handleNot);
};
fact.match(handleLiteral, handleUnsupported);
fact.match(handleAtomicFormula, handleNot);
m_outputStream << ").";
}

View File

@ -6,6 +6,7 @@
#include <colorlog/Logger.h>
#include <pddlparse/AST.h>
#include <pddlparse/Normalize.h>
#include <pddlparse/Parse.h>
#include <plasp/pddl/TranslatorASP.h>
@ -25,7 +26,7 @@ TEST_CASE("[PDDL translation] Former issues are fixed", "[PDDL translation]")
SECTION("translating domains without typing information works")
{
context.tokenizer.read("data/issues/issue-4.pddl");
auto description = pddl::parseDescription(context);
auto description = pddl::normalize(pddl::parseDescription(context));
const auto translator = plasp::pddl::TranslatorASP(std::move(description), logger.outputStream());
CHECK_NOTHROW(translator.translate());
}
@ -33,7 +34,7 @@ TEST_CASE("[PDDL translation] Former issues are fixed", "[PDDL translation]")
SECTION("translating the simple blocks world domain works")
{
context.tokenizer.read("data/issues/issue-5.pddl");
auto description = pddl::parseDescription(context);
auto description = pddl::normalize(pddl::parseDescription(context));
const auto translator = plasp::pddl::TranslatorASP(std::move(description), logger.outputStream());
CHECK_NOTHROW(translator.translate());
}