diff --git a/include/plasp/pddl/translation/Predicate.h b/include/plasp/pddl/translation/Predicate.h index 2b19230..2fb380d 100644 --- a/include/plasp/pddl/translation/Predicate.h +++ b/include/plasp/pddl/translation/Predicate.h @@ -5,6 +5,7 @@ #include #include #include +#include #include 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 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()) + { + const auto &constant = argument->as(); + + outputStream << output::Keyword("constant") << "(" << output::String(constant.name().c_str()) << ")"; + } + else if (argument->is()) + { + const auto &variable = argument->as(); + + 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 ¶meters = derivedPredicate.parameters(); + const auto id = derivedPredicate.id(); + + if (parameters.empty()) + { + outputStream << output::Number(id); + return; + } + + outputStream << "(" << output::Number(id); + + for (const auto ¶meter : parameters) + outputStream << ", " << output::Variable(parameter->name().c_str()); + + outputStream << ")"; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + inline void printPredicateName(output::ColorStream &outputStream, const expressions::PredicateDeclaration &predicateDeclaration) { outputStream << output::Keyword("variable") << "("; diff --git a/include/plasp/pddl/translation/Primitives.h b/include/plasp/pddl/translation/Primitives.h index fca8872..51f2b29 100644 --- a/include/plasp/pddl/translation/Primitives.h +++ b/include/plasp/pddl/translation/Primitives.h @@ -6,6 +6,7 @@ #include #include #include +#include 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); //////////////////////////////////////////////////////////////////////////////////////////////////// -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()) - { - const auto &constant = argument->as(); - - outputStream << output::Keyword("constant") << "(" << output::String(constant.name().c_str()) << ")"; - } - else if (argument->is()) - { - const auto &variable = argument->as(); - - 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) { // Translate single predicate diff --git a/src/plasp/pddl/TranslatorASP.cpp b/src/plasp/pddl/TranslatorASP.cpp index 8c9470a..1fab18b 100644 --- a/src/plasp/pddl/TranslatorASP.cpp +++ b/src/plasp/pddl/TranslatorASP.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -144,31 +145,13 @@ void TranslatorASP::translatePredicates() const 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) { - m_outputStream - << std::endl - << output::Function("variable") << "(" - << output::Keyword("variable") << "("; + m_outputStream << std::endl << output::Function("variable") << "("; - printPredicateName(predicate); + translation::printPredicateName(m_outputStream, *predicate); - m_outputStream << "))"; + m_outputStream << ")"; translation::translateVariablesForRuleBody(m_outputStream, predicate->parameters()); @@ -198,27 +181,15 @@ void TranslatorASP::translateDerivedPredicates() const for (const auto &derivedPredicate : derivedPredicates) { - const auto printDerivedPredicateName = - [&derivedPredicate](output::ColorStream &outputStream) + const auto printObjectName = + [&](auto &outputStream) { - outputStream << output::Keyword("derivedVariable") << "("; - - const auto id = derivedPredicate->id(); - - if (derivedPredicate->parameters().empty()) - { - outputStream << output::Number(id) << ")"; - return; - } - - outputStream << "(" << output::Number(id); - translation::translateVariablesForRuleHead(outputStream, derivedPredicate->parameters()); - outputStream << "))"; + translation::printDerivedPredicateName(outputStream, *derivedPredicate); }; m_outputStream << std::endl << output::Function("derivedVariable") << "("; - printDerivedPredicateName(m_outputStream); + printObjectName(m_outputStream); m_outputStream << ")"; @@ -226,7 +197,7 @@ void TranslatorASP::translateDerivedPredicates() const m_outputStream << "."; - translation::translatePreconditionDisjunction(m_outputStream, "derivedVariable", printDerivedPredicateName, derivedPredicate->preconditions()); + translation::translatePreconditionDisjunction(m_outputStream, "derivedVariable", printObjectName, derivedPredicate->preconditions()); m_outputStream << std::endl; }