Restructured functions related to translation of predicates.

This commit is contained in:
Patrick Lühne 2016-12-08 01:27:38 +01:00
parent 1561aa5eb9
commit 586f53a788
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
3 changed files with 73 additions and 77 deletions

View File

@ -5,6 +5,7 @@
#include <plasp/pddl/expressions/DerivedPredicate.h> #include <plasp/pddl/expressions/DerivedPredicate.h>
#include <plasp/pddl/expressions/Predicate.h> #include <plasp/pddl/expressions/Predicate.h>
#include <plasp/pddl/expressions/PredicateDeclaration.h> #include <plasp/pddl/expressions/PredicateDeclaration.h>
#include <plasp/pddl/expressions/Variable.h>
#include <plasp/pddl/translation/Variables.h> #include <plasp/pddl/translation/Variables.h>
namespace plasp namespace plasp
@ -20,11 +21,73 @@ namespace translation
// //
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: refactor (overly redundant)
void translatePredicate(output::ColorStream &outputStream, const expressions::Predicate &predicate);
void translateDerivedPredicate(output::ColorStream &outputStream, const expressions::DerivedPredicate &derivedPredicate);
void printPredicateName(output::ColorStream &outputStream, const expressions::PredicateDeclaration &predicateDeclaration); void printPredicateName(output::ColorStream &outputStream, const expressions::PredicateDeclaration &predicateDeclaration);
void printDerivedPredicateName(output::ColorStream &outputStream, const expressions::DerivedPredicate &derivedPredicate); void printDerivedPredicateName(output::ColorStream &outputStream, const expressions::DerivedPredicate &derivedPredicate);
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
inline void translatePredicate(output::ColorStream &outputStream, const expressions::Predicate &predicate)
{
const auto &arguments = predicate.arguments();
if (arguments.empty())
{
outputStream << output::String(predicate.name().c_str());
return;
}
outputStream << "(" << output::String(predicate.name().c_str());
for (const auto &argument : arguments)
{
outputStream << ", ";
if (argument->is<expressions::Constant>())
{
const auto &constant = argument->as<expressions::Constant>();
outputStream << output::Keyword("constant") << "(" << output::String(constant.name().c_str()) << ")";
}
else if (argument->is<expressions::Variable>())
{
const auto &variable = argument->as<expressions::Variable>();
outputStream << output::Variable(variable.name().c_str());
}
else
throw output::TranslatorException("only variables and constants supported in predicates currently");
}
outputStream << ")";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline void translateDerivedPredicate(output::ColorStream &outputStream, const expressions::DerivedPredicate &derivedPredicate)
{
const auto &parameters = derivedPredicate.parameters();
const auto id = derivedPredicate.id();
if (parameters.empty())
{
outputStream << output::Number<decltype(id)>(id);
return;
}
outputStream << "(" << output::Number<decltype(id)>(id);
for (const auto &parameter : parameters)
outputStream << ", " << output::Variable(parameter->name().c_str());
outputStream << ")";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline void printPredicateName(output::ColorStream &outputStream, const expressions::PredicateDeclaration &predicateDeclaration) inline void printPredicateName(output::ColorStream &outputStream, const expressions::PredicateDeclaration &predicateDeclaration)
{ {
outputStream << output::Keyword("variable") << "("; outputStream << output::Keyword("variable") << "(";

View File

@ -6,6 +6,7 @@
#include <plasp/pddl/Description.h> #include <plasp/pddl/Description.h>
#include <plasp/pddl/expressions/Not.h> #include <plasp/pddl/expressions/Not.h>
#include <plasp/pddl/expressions/Predicate.h> #include <plasp/pddl/expressions/Predicate.h>
#include <plasp/pddl/translation/Predicate.h>
namespace plasp namespace plasp
{ {
@ -20,49 +21,10 @@ namespace translation
// //
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void translatePredicate(output::ColorStream &outputStream, const expressions::Predicate &predicate);
void translateLiteral(output::ColorStream &outputStream, const Expression &literal); void translateLiteral(output::ColorStream &outputStream, const Expression &literal);
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
inline void translatePredicate(output::ColorStream &outputStream, const expressions::Predicate &predicate)
{
const auto &arguments = predicate.arguments();
if (arguments.empty())
{
outputStream << output::String(predicate.name().c_str());
return;
}
outputStream << "(" << output::String(predicate.name().c_str());
for (const auto &argument : arguments)
{
outputStream << ", ";
if (argument->is<expressions::Constant>())
{
const auto &constant = argument->as<expressions::Constant>();
outputStream << output::Keyword("constant") << "(" << output::String(constant.name().c_str()) << ")";
}
else if (argument->is<expressions::Variable>())
{
const auto &variable = argument->as<expressions::Variable>();
outputStream << output::Variable(variable.name().c_str());
}
else
throw output::TranslatorException("only variables and constants supported in predicates currently");
}
outputStream << ")";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline void translateLiteral(output::ColorStream &outputStream, const Expression &literal) inline void translateLiteral(output::ColorStream &outputStream, const Expression &literal)
{ {
// Translate single predicate // Translate single predicate

View File

@ -8,6 +8,7 @@
#include <plasp/pddl/expressions/Not.h> #include <plasp/pddl/expressions/Not.h>
#include <plasp/pddl/expressions/Predicate.h> #include <plasp/pddl/expressions/Predicate.h>
#include <plasp/pddl/translation/Precondition.h> #include <plasp/pddl/translation/Precondition.h>
#include <plasp/pddl/translation/Predicate.h>
#include <plasp/pddl/translation/Primitives.h> #include <plasp/pddl/translation/Primitives.h>
#include <plasp/pddl/translation/Variables.h> #include <plasp/pddl/translation/Variables.h>
@ -144,31 +145,13 @@ void TranslatorASP::translatePredicates() const
const auto &predicates = m_description.domain().predicates(); const auto &predicates = m_description.domain().predicates();
const auto printPredicateName =
[&](const auto &predicate)
{
if (predicate->parameters().empty())
{
m_outputStream << output::String(predicate->name().c_str());
return;
}
m_outputStream << "(" << output::String(predicate->name().c_str());
translation::translateVariablesForRuleHead(m_outputStream, predicate->parameters());
m_outputStream << ")";
};
for (const auto &predicate : predicates) for (const auto &predicate : predicates)
{ {
m_outputStream m_outputStream << std::endl << output::Function("variable") << "(";
<< std::endl
<< output::Function("variable") << "("
<< output::Keyword("variable") << "(";
printPredicateName(predicate); translation::printPredicateName(m_outputStream, *predicate);
m_outputStream << "))"; m_outputStream << ")";
translation::translateVariablesForRuleBody(m_outputStream, predicate->parameters()); translation::translateVariablesForRuleBody(m_outputStream, predicate->parameters());
@ -198,27 +181,15 @@ void TranslatorASP::translateDerivedPredicates() const
for (const auto &derivedPredicate : derivedPredicates) for (const auto &derivedPredicate : derivedPredicates)
{ {
const auto printDerivedPredicateName = const auto printObjectName =
[&derivedPredicate](output::ColorStream &outputStream) [&](auto &outputStream)
{ {
outputStream << output::Keyword("derivedVariable") << "("; translation::printDerivedPredicateName(outputStream, *derivedPredicate);
const auto id = derivedPredicate->id();
if (derivedPredicate->parameters().empty())
{
outputStream << output::Number<decltype(id)>(id) << ")";
return;
}
outputStream << "(" << output::Number<decltype(id)>(id);
translation::translateVariablesForRuleHead(outputStream, derivedPredicate->parameters());
outputStream << "))";
}; };
m_outputStream << std::endl << output::Function("derivedVariable") << "("; m_outputStream << std::endl << output::Function("derivedVariable") << "(";
printDerivedPredicateName(m_outputStream); printObjectName(m_outputStream);
m_outputStream << ")"; m_outputStream << ")";
@ -226,7 +197,7 @@ void TranslatorASP::translateDerivedPredicates() const
m_outputStream << "."; m_outputStream << ".";
translation::translatePreconditionDisjunction(m_outputStream, "derivedVariable", printDerivedPredicateName, derivedPredicate->preconditions()); translation::translatePreconditionDisjunction(m_outputStream, "derivedVariable", printObjectName, derivedPredicate->preconditions());
m_outputStream << std::endl; m_outputStream << std::endl;
} }