From 9c2f49e4a05eca72bbf8286bf6b8c024dbfdddea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Sun, 12 Jun 2016 22:39:04 +0200 Subject: [PATCH] Put PDDL translation sections into separate methods. --- include/plasp/pddl/TranslatorASP.h | 4 + src/plasp/pddl/TranslatorASP.cpp | 179 ++++++++++++++++------------- 2 files changed, 104 insertions(+), 79 deletions(-) diff --git a/include/plasp/pddl/TranslatorASP.h b/include/plasp/pddl/TranslatorASP.h index 3180afe..df25970 100644 --- a/include/plasp/pddl/TranslatorASP.h +++ b/include/plasp/pddl/TranslatorASP.h @@ -27,6 +27,10 @@ class TranslatorASP void checkSupport() const; void translateDomain() const; + void translateTypes() const; + void translateConstants() const; + void translatePredicates() const; + void translateProblem() const; const Description &m_description; diff --git a/src/plasp/pddl/TranslatorASP.cpp b/src/plasp/pddl/TranslatorASP.cpp index 1e05ddc..ab40780 100644 --- a/src/plasp/pddl/TranslatorASP.cpp +++ b/src/plasp/pddl/TranslatorASP.cpp @@ -72,102 +72,123 @@ void TranslatorASP::translateDomain() const if (!domain.types().empty()) { m_ostream << std::endl; - m_ostream << "% types"; - - const auto &types = domain.types(); - - std::for_each(types.cbegin(), types.cend(), - [&](const auto &type) - { - m_ostream << std::endl; - - m_ostream << "type(" << type->name() << ")." << std::endl; - - const auto &parentTypes = type->parentTypes(); - - std::for_each(parentTypes.cbegin(), parentTypes.cend(), - [&](const auto &parentType) - { - m_ostream << "inherits(type(" << type->name() << "), type(" << parentType->name() << "))." << std::endl; - }); - }); + translateTypes(); } // Constants if (!domain.constants().empty()) { m_ostream << std::endl; - m_ostream << "% constants"; - - const auto &constants = domain.constants(); - - std::for_each(constants.cbegin(), constants.cend(), - [&](const auto &constant) - { - m_ostream << std::endl; - - m_ostream << "constant(" << constant->name() << ")." << std::endl; - - const auto *type = constant->type(); - - if (type == nullptr) - return; - - m_ostream << "hasType(constant(" << constant->name() << "), type(" << type->name() << "))." << std::endl; - }); + translateConstants(); } // Predicates if (!domain.predicates().empty()) { m_ostream << std::endl; - m_ostream << "% predicates"; - - const auto &predicates = domain.predicates(); - - std::for_each(predicates.cbegin(), predicates.cend(), - [&](const auto &predicate) - { - m_ostream << std::endl; - - m_ostream << "predicate(" << predicate->name(); - - const auto &arguments = predicate->arguments(); - - if (arguments.empty()) - { - m_ostream << ")."; - return; - } - - m_ostream << "("; - - for (auto i = arguments.cbegin(); i != arguments.cend(); i++) - { - if (i != arguments.cbegin()) - m_ostream << ", "; - - m_ostream << utils::escapeASPVariable((*i)->name()); - } - - m_ostream << ")) :- "; - - for (auto i = arguments.cbegin(); i != arguments.cend(); i++) - { - if (i != arguments.cbegin()) - m_ostream << ", "; - - const auto &type = *dynamic_cast((*i)->type()); - m_ostream << "hasType(" << utils::escapeASPVariable((*i)->name()) << ", type(" << type.name() << "))"; - } - - m_ostream << "."; - }); + translatePredicates(); } } //////////////////////////////////////////////////////////////////////////////////////////////////// +void TranslatorASP::translateTypes() const +{ + m_ostream << "% types"; + + const auto &types = m_description.domain().types(); + + std::for_each(types.cbegin(), types.cend(), + [&](const auto &type) + { + m_ostream << std::endl; + + m_ostream << "type(" << type->name() << ")." << std::endl; + + const auto &parentTypes = type->parentTypes(); + + std::for_each(parentTypes.cbegin(), parentTypes.cend(), + [&](const auto &parentType) + { + m_ostream << "inherits(type(" << type->name() << "), type(" << parentType->name() << "))." << std::endl; + }); + }); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +void TranslatorASP::translateConstants() const +{ + m_ostream << "% constants"; + + const auto &constants = m_description.domain().constants(); + + std::for_each(constants.cbegin(), constants.cend(), + [&](const auto &constant) + { + m_ostream << std::endl; + + m_ostream << "constant(" << constant->name() << ")." << std::endl; + + const auto *type = constant->type(); + + if (type == nullptr) + return; + + m_ostream << "hasType(constant(" << constant->name() << "), type(" << type->name() << "))." << std::endl; + }); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +void TranslatorASP::translatePredicates() const +{ + m_ostream << "% predicates"; + + const auto &predicates = m_description.domain().predicates(); + + std::for_each(predicates.cbegin(), predicates.cend(), + [&](const auto &predicate) + { + m_ostream << std::endl; + + m_ostream << "predicate(" << predicate->name(); + + const auto &arguments = predicate->arguments(); + + if (arguments.empty()) + { + m_ostream << ")."; + return; + } + + m_ostream << "("; + + for (auto i = arguments.cbegin(); i != arguments.cend(); i++) + { + if (i != arguments.cbegin()) + m_ostream << ", "; + + m_ostream << utils::escapeASPVariable((*i)->name()); + } + + m_ostream << ")) :- "; + + for (auto i = arguments.cbegin(); i != arguments.cend(); i++) + { + if (i != arguments.cbegin()) + m_ostream << ", "; + + const auto &type = *dynamic_cast((*i)->type()); + m_ostream << "hasType(" << utils::escapeASPVariable((*i)->name()) << ", type(" << type.name() << "))"; + } + + m_ostream << "."; + }); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + void TranslatorASP::translateProblem() const { m_ostream << std::endl