diff --git a/CHANGELOG.md b/CHANGELOG.md index 96c00ac..8a39d93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Features: -* supports input from std::cin +* supports input from `std::cin` * basic support for parsing PDDL 3.1 domains and problems * limited support for translating PDDL 3.1 domains and problems to ASP facts * automatic language detection for PDDL and SAS diff --git a/include/plasp/pddl/TranslatorASP.h b/include/plasp/pddl/TranslatorASP.h index 4bc5a1b..88a04a3 100644 --- a/include/plasp/pddl/TranslatorASP.h +++ b/include/plasp/pddl/TranslatorASP.h @@ -26,15 +26,14 @@ class TranslatorASP private: void translateDomain() const; void translateTypes() const; - void translateConstants() const; void translatePredicates() const; void translateActions() const; void translateProblem() const; - void translateObjects() const; void translateInitialState() const; void translateGoal() const; + void translateConstants(const std::string &header, const expressions::Constants &constants) const; void translateVariablesHead(const expressions::Variables &variables) const; void translateVariablesBody(const expressions::Variables &variables) const; void translateLiteral(const Expression &literal) const; diff --git a/src/plasp/pddl/TranslatorASP.cpp b/src/plasp/pddl/TranslatorASP.cpp index e8ac949..4454ed0 100644 --- a/src/plasp/pddl/TranslatorASP.cpp +++ b/src/plasp/pddl/TranslatorASP.cpp @@ -50,17 +50,14 @@ void TranslatorASP::translateDomain() const const auto &domain = m_description.domain(); // Types - if (!domain.types().empty()) - { - m_ostream << std::endl; - translateTypes(); - } + m_ostream << std::endl; + translateTypes(); // Constants if (!domain.constants().empty()) { m_ostream << std::endl; - translateConstants(); + translateConstants("constants", domain.constants()); } // Predicates @@ -82,16 +79,19 @@ void TranslatorASP::translateDomain() const void TranslatorASP::translateTypes() const { - // TODO: escape ASP identifiers - m_ostream << "% types"; + m_ostream << "% types" << std::endl; const auto &types = m_description.domain().types(); + if (types.empty()) + { + m_ostream << "type(object)." << std::endl; + return; + } + std::for_each(types.cbegin(), types.cend(), [&](const auto &type) { - m_ostream << std::endl; - const auto typeName = utils::escapeASP(type->name()); m_ostream << "type(" << typeName << ")." << std::endl; @@ -111,32 +111,6 @@ void TranslatorASP::translateTypes() const //////////////////////////////////////////////////////////////////////////////////////////////////// -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; - - const auto constantName = utils::escapeASP(constant->name()); - - m_ostream << "constant(" << constantName << ")." << std::endl; - - const auto *type = constant->type(); - - if (type == nullptr) - return; - - m_ostream << "hasType(constant(" << constantName << "), type(" << utils::escapeASP(type->name()) << "))." << std::endl; - }); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - void TranslatorASP::translatePredicates() const { m_ostream << "% predicates"; @@ -269,6 +243,28 @@ void TranslatorASP::translateActions() const //////////////////////////////////////////////////////////////////////////////////////////////////// +void TranslatorASP::translateConstants(const std::string &header, const expressions::Constants &constants) const +{ + m_ostream << "% " << header; + + std::for_each(constants.cbegin(), constants.cend(), + [&](const auto &constant) + { + const auto constantName = utils::escapeASP(constant->name()); + + m_ostream << std::endl << "constant(" << constantName << ")." << std::endl; + + const auto *type = constant->type(); + + if (type != nullptr) + m_ostream << "hasType(constant(" << constantName << "), type(" << utils::escapeASP(type->name()) << "))." << std::endl; + else + m_ostream << "hasType(constant(" << constantName << "), type(object))." << std::endl; + }); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + void TranslatorASP::translateVariablesHead(const expressions::Variables &variables) const { if (variables.empty()) @@ -300,11 +296,11 @@ void TranslatorASP::translateVariablesBody(const expressions::Variables &variabl for (auto i = variables.cbegin(); i != variables.cend(); i++) { + const auto &variable = **i; + if (i != variables.cbegin()) m_ostream << ", "; - const auto &variable = **i; - if (variable.type() != nullptr) { if (variable.type()->expressionType() != Expression::Type::PrimitiveType) @@ -314,7 +310,8 @@ void TranslatorASP::translateVariablesBody(const expressions::Variables &variabl m_ostream << "hasType(" << utils::escapeASPVariable(variable.name()) << ", type(" << type.name() << "))"; } - // TODO: handle untyped variables + else + m_ostream << "hasType(" << utils::escapeASPVariable(variable.name()) << ", type(object))"; } } @@ -398,7 +395,7 @@ void TranslatorASP::translateProblem() const if (!problem.objects().empty()) { m_ostream << std::endl; - translateObjects(); + translateConstants("objects", problem.objects()); } // Initial State @@ -412,34 +409,6 @@ void TranslatorASP::translateProblem() const //////////////////////////////////////////////////////////////////////////////////////////////////// -void TranslatorASP::translateObjects() const -{ - BOOST_ASSERT(m_description.containsProblem()); - - m_ostream << "% objects"; - - const auto &objects = m_description.problem().objects(); - - std::for_each(objects.cbegin(), objects.cend(), - [&](const auto &object) - { - m_ostream << std::endl; - - const auto objectName = utils::escapeASP(object->name()); - - m_ostream << "constant(" << objectName << ")." << std::endl; - - const auto *type = object->type(); - - if (type == nullptr) - return; - - m_ostream << "hasType(constant(" << objectName << "), type(" << utils::escapeASP(type->name()) << "))." << std::endl; - }); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - void TranslatorASP::translateInitialState() const { BOOST_ASSERT(m_description.containsProblem());