Implemented translation of derived predicates.
This commit is contained in:
@@ -28,6 +28,7 @@ class TranslatorASP
|
||||
void translateDomain() const;
|
||||
void translateTypes() const;
|
||||
void translatePredicates() const;
|
||||
void translateDerivedPredicates() const;
|
||||
void translateActions() const;
|
||||
|
||||
void translateProblem() const;
|
||||
|
120
include/plasp/pddl/translation/DerivedPredicate.h
Normal file
120
include/plasp/pddl/translation/DerivedPredicate.h
Normal file
@@ -0,0 +1,120 @@
|
||||
#ifndef __PLASP__PDDL__TRANSLATION__DERIVED_PREDICATE_H
|
||||
#define __PLASP__PDDL__TRANSLATION__DERIVED_PREDICATE_H
|
||||
|
||||
#include <colorlog/Formatting.h>
|
||||
|
||||
#include <pddlparse/NormalizedAST.h>
|
||||
#include <pddlparse/Parse.h>
|
||||
|
||||
#include <plasp/pddl/translation/Primitives.h>
|
||||
#include <plasp/pddl/translation/Variables.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
namespace pddl
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// DerivedPredicate
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void translateDerivedPredicate(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicate &derivedPredicate);
|
||||
void translateDerivedPredicateDeclaration(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicateDeclaration &derivedPredicateDeclaration);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline void translateDerivedPredicate(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicate &derivedPredicate)
|
||||
{
|
||||
const auto &arguments = derivedPredicate.arguments;
|
||||
|
||||
if (arguments.empty())
|
||||
{
|
||||
outputStream << *derivedPredicate.declaration;
|
||||
return;
|
||||
}
|
||||
|
||||
outputStream << "(" << *derivedPredicate.declaration;
|
||||
|
||||
for (const auto &argument : arguments)
|
||||
{
|
||||
outputStream << ", ";
|
||||
|
||||
const auto handleConstant =
|
||||
[&](const ::pddl::normalizedAST::ConstantPointer &constant)
|
||||
{
|
||||
outputStream << colorlog::Keyword("constant") << "(" << *constant << ")";
|
||||
};
|
||||
|
||||
const auto handleVariable =
|
||||
[&](const ::pddl::normalizedAST::VariablePointer &variable)
|
||||
{
|
||||
outputStream << *variable;
|
||||
};
|
||||
|
||||
argument.match(handleConstant, handleVariable);
|
||||
}
|
||||
|
||||
outputStream << ")";
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline void translateDerivedPredicateDeclaration(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicateDeclaration &derivedPredicateDeclaration)
|
||||
{
|
||||
outputStream << colorlog::Keyword("derivedVariable") << "(";
|
||||
|
||||
if (derivedPredicateDeclaration.parameters.empty())
|
||||
{
|
||||
outputStream << derivedPredicateDeclaration << ")";
|
||||
return;
|
||||
}
|
||||
|
||||
outputStream << "(" << derivedPredicateDeclaration;
|
||||
translateVariablesForRuleHead(outputStream, derivedPredicateDeclaration.parameters);
|
||||
outputStream << "))";
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline void translateDerivedPredicateToVariable(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicate &derivedPredicate, bool isPositive = true)
|
||||
{
|
||||
outputStream << colorlog::Keyword("derivedVariable") << "(";
|
||||
translateDerivedPredicate(outputStream, derivedPredicate);
|
||||
outputStream << "), " << colorlog::Keyword("value") << "(";
|
||||
translateDerivedPredicate(outputStream, derivedPredicate);
|
||||
outputStream << ", ";
|
||||
|
||||
if (isPositive)
|
||||
outputStream << colorlog::Boolean("true");
|
||||
else
|
||||
outputStream << colorlog::Boolean("false");
|
||||
|
||||
outputStream << ")";
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline void translateDerivedPredicateDeclarationToVariable(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicateDeclaration &derivedPredicateDeclaration, bool isPositive = true)
|
||||
{
|
||||
outputStream << colorlog::Keyword("derivedVariable") << "(";
|
||||
translateDerivedPredicateDeclaration(outputStream, derivedPredicateDeclaration);
|
||||
outputStream << "), " << colorlog::Keyword("value") << "(";
|
||||
translateDerivedPredicateDeclaration(outputStream, derivedPredicateDeclaration);
|
||||
outputStream << ", ";
|
||||
|
||||
if (isPositive)
|
||||
outputStream << colorlog::Boolean("true");
|
||||
else
|
||||
outputStream << colorlog::Boolean("false");
|
||||
|
||||
outputStream << ")";
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
104
include/plasp/pddl/translation/DerivedPredicatePrecondition.h
Normal file
104
include/plasp/pddl/translation/DerivedPredicatePrecondition.h
Normal file
@@ -0,0 +1,104 @@
|
||||
#ifndef __PLASP__PDDL__TRANSLATION__DERIVED_PREDICATE_PRECONDITION_H
|
||||
#define __PLASP__PDDL__TRANSLATION__DERIVED_PREDICATE_PRECONDITION_H
|
||||
|
||||
#include <colorlog/Formatting.h>
|
||||
|
||||
#include <pddlparse/NormalizedAST.h>
|
||||
|
||||
#include <plasp/TranslatorException.h>
|
||||
|
||||
#include <plasp/pddl/translation/DerivedPredicate.h>
|
||||
#include <plasp/pddl/translation/Predicate.h>
|
||||
#include <plasp/pddl/translation/Primitives.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
namespace pddl
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// DerivedPredicatePrecondition
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<typename PrintObjectName>
|
||||
inline void translateDerivedPredicatePrecondition(colorlog::ColorStream &outputStream, const ::pddl::normalizedAST::DerivedPredicatePrecondition &derivedPredicatePrecondition, const std::string &objectType, PrintObjectName printObjectName)
|
||||
{
|
||||
const auto handlePredicate =
|
||||
[&](const ::pddl::normalizedAST::PredicatePointer &predicate, bool isPositive = true)
|
||||
{
|
||||
outputStream << std::endl << colorlog::Function("precondition") << "(";
|
||||
printObjectName();
|
||||
outputStream << ", ";
|
||||
translatePredicateToVariable(outputStream, *predicate, isPositive);
|
||||
outputStream << ") :- " << colorlog::Function(objectType.c_str()) << "(";
|
||||
printObjectName();
|
||||
outputStream << ").";
|
||||
};
|
||||
|
||||
const auto handleNegatedPredicate =
|
||||
[&](const ::pddl::normalizedAST::PredicatePointer &predicate)
|
||||
{
|
||||
handlePredicate(predicate, false);
|
||||
};
|
||||
|
||||
const auto handleDerivedPredicate =
|
||||
[&](const ::pddl::normalizedAST::DerivedPredicatePointer &derivedPredicate, bool isPositive = true)
|
||||
{
|
||||
outputStream << std::endl << colorlog::Function("precondition") << "(";
|
||||
printObjectName();
|
||||
outputStream << ", ";
|
||||
translateDerivedPredicateToVariable(outputStream, *derivedPredicate, isPositive);
|
||||
outputStream << ") :- " << colorlog::Function(objectType.c_str()) << "(";
|
||||
printObjectName();
|
||||
outputStream << ").";
|
||||
};
|
||||
|
||||
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_->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);
|
||||
};
|
||||
|
||||
const auto handleOr =
|
||||
[&](const ::pddl::normalizedAST::OrPointer<::pddl::normalizedAST::Literal> &or_)
|
||||
{
|
||||
for (const auto &argument : or_->arguments)
|
||||
handleLiteral(argument);
|
||||
};
|
||||
|
||||
derivedPredicatePrecondition.match(handleLiteral, handleAnd, handleOr);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@@ -7,6 +7,7 @@
|
||||
|
||||
#include <plasp/TranslatorException.h>
|
||||
|
||||
#include <plasp/pddl/translation/DerivedPredicate.h>
|
||||
#include <plasp/pddl/translation/Predicate.h>
|
||||
#include <plasp/pddl/translation/Primitives.h>
|
||||
|
||||
@@ -43,16 +44,15 @@ inline void translatePrecondition(colorlog::ColorStream &outputStream, const ::p
|
||||
};
|
||||
|
||||
const auto handleDerivedPredicate =
|
||||
[&](const ::pddl::normalizedAST::DerivedPredicatePointer &, bool = true)
|
||||
[&](const ::pddl::normalizedAST::DerivedPredicatePointer &derivedPredicate, bool isPositive = true)
|
||||
{
|
||||
outputStream << std::endl << colorlog::Function("precondition") << "(";
|
||||
printObjectName();
|
||||
outputStream << ", ";
|
||||
// TODO: implement
|
||||
/*translatePredicateToVariable(outputStream, *predicate, isPositive);
|
||||
outputStream << ") :- " << output::Function(objectType.c_str()) << "(";
|
||||
translateDerivedPredicateToVariable(outputStream, *derivedPredicate, isPositive);
|
||||
outputStream << ") :- " << colorlog::Function(objectType.c_str()) << "(";
|
||||
printObjectName();
|
||||
outputStream << ").";*/
|
||||
outputStream << ").";
|
||||
};
|
||||
|
||||
const auto handleNegatedDerivedPredicate =
|
||||
|
@@ -29,9 +29,6 @@ void translateVariablesForRuleBody(colorlog::ColorStream &outputStream, const T
|
||||
template<class T>
|
||||
inline void translateVariablesForRuleHead(colorlog::ColorStream &outputStream, const T &variables)
|
||||
{
|
||||
if (variables.empty())
|
||||
return;
|
||||
|
||||
for (const auto &variable : variables)
|
||||
outputStream << ", " << *variable;
|
||||
}
|
||||
@@ -39,16 +36,11 @@ inline void translateVariablesForRuleHead(colorlog::ColorStream &outputStream, c
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class T>
|
||||
inline void translateVariablesForRuleBody(colorlog::ColorStream &outputStream, const T &variables)
|
||||
void translateVariablesForRuleBody(colorlog::ColorStream &outputStream, const T &variables)
|
||||
{
|
||||
if (variables.empty())
|
||||
return;
|
||||
|
||||
outputStream << " :- ";
|
||||
|
||||
for (const auto &variable : variables)
|
||||
{
|
||||
if (variable.get() != variables.begin()->get())
|
||||
if (&variable != &*variables.begin())
|
||||
outputStream << ", ";
|
||||
|
||||
if (variable->type)
|
||||
|
Reference in New Issue
Block a user