From 87ca54a253495235103a0265fe63158f1e6c193f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Sun, 12 Jun 2016 22:47:39 +0200 Subject: [PATCH] Translating names of PDDL actions. --- include/plasp/pddl/TranslatorASP.h | 1 + src/plasp/pddl/TranslatorASP.cpp | 77 +++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/include/plasp/pddl/TranslatorASP.h b/include/plasp/pddl/TranslatorASP.h index df25970..8e3bd7c 100644 --- a/include/plasp/pddl/TranslatorASP.h +++ b/include/plasp/pddl/TranslatorASP.h @@ -30,6 +30,7 @@ class TranslatorASP void translateTypes() const; void translateConstants() const; void translatePredicates() const; + void translateActions() const; void translateProblem() const; diff --git a/src/plasp/pddl/TranslatorASP.cpp b/src/plasp/pddl/TranslatorASP.cpp index ab40780..16bc146 100644 --- a/src/plasp/pddl/TranslatorASP.cpp +++ b/src/plasp/pddl/TranslatorASP.cpp @@ -39,6 +39,21 @@ void TranslatorASP::checkSupport() const throw utils::TranslatorException("Only primitive types supported currently"); }); }); + + const auto &actions = m_description.domain().actions(); + + std::for_each(actions.cbegin(), actions.cend(), + [&](const auto &action) + { + const auto ¶meters = action->parameters(); + + std::for_each(parameters.cbegin(), parameters.cend(), + [&](const auto ¶meter) + { + if (parameter->type()->expressionType() != Expression::Type::PrimitiveType) + throw utils::TranslatorException("Only primitive types supported currently"); + }); + }); } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -88,12 +103,20 @@ void TranslatorASP::translateDomain() const m_ostream << std::endl; translatePredicates(); } + + // Actions + if (!domain.actions().empty()) + { + m_ostream << std::endl; + translateActions(); + } } //////////////////////////////////////////////////////////////////////////////////////////////////// void TranslatorASP::translateTypes() const { + // TODO: escape ASP identifiers m_ostream << "% types"; const auto &types = m_description.domain().types(); @@ -185,13 +208,65 @@ void TranslatorASP::translatePredicates() const m_ostream << "."; }); + + m_ostream << std::endl; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +void TranslatorASP::translateActions() const +{ + m_ostream << "% actions"; + + const auto &actions = m_description.domain().actions(); + + std::for_each(actions.cbegin(), actions.cend(), + [&](const auto &action) + { + m_ostream << std::endl; + + m_ostream << "action(" << action->name(); + + const auto ¶meters = action->parameters(); + + if (parameters.empty()) + { + m_ostream << ")."; + return; + } + + m_ostream << "("; + + for (auto i = parameters.cbegin(); i != parameters.cend(); i++) + { + if (i != parameters.cbegin()) + m_ostream << ", "; + + m_ostream << utils::escapeASPVariable((*i)->name()); + } + + m_ostream << ")) :- "; + + for (auto i = parameters.cbegin(); i != parameters.cend(); i++) + { + if (i != parameters.cbegin()) + m_ostream << ", "; + + const auto &type = *dynamic_cast((*i)->type()); + m_ostream << "hasType(" << utils::escapeASPVariable((*i)->name()) << ", type(" << type.name() << "))"; + } + + m_ostream << "."; + }); + + m_ostream << std::endl; } //////////////////////////////////////////////////////////////////////////////////////////////////// void TranslatorASP::translateProblem() const { - m_ostream << std::endl + m_ostream << "%---------------------------------------" << std::endl << "% problem" << std::endl << "%---------------------------------------" << std::endl;