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 49b7f4e..8370d22 100644 --- a/src/plasp/pddl/TranslatorASP.cpp +++ b/src/plasp/pddl/TranslatorASP.cpp @@ -60,7 +60,7 @@ void TranslatorASP::translateDomain() const if (!domain.constants().empty()) { m_ostream << std::endl; - translateConstants(); + translateConstants("constants", domain.constants()); } // Predicates @@ -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::endl; + + std::for_each(constants.cbegin(), constants.cend(), + [&](const auto &constant) + { + 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::translateVariablesHead(const expressions::Variables &variables) const { if (variables.empty()) @@ -283,7 +279,12 @@ void TranslatorASP::translateVariablesHead(const expressions::Variables &variabl const auto &variable = **i; - m_ostream << utils::escapeASPVariable(variable.name()); + // Handle typed variables + if (variable.type() != nullptr) + m_ostream << utils::escapeASPVariable(variable.name()); + // Allow any constant if typing is disabled + else + m_ostream << "constant(" << utils::escapeASPVariable(variable.name()) << ")"; } m_ostream << ")"; @@ -300,11 +301,12 @@ 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; - + // Handle typed variables if (variable.type() != nullptr) { if (variable.type()->expressionType() != Expression::Type::PrimitiveType) @@ -314,7 +316,9 @@ void TranslatorASP::translateVariablesBody(const expressions::Variables &variabl m_ostream << "hasType(" << utils::escapeASPVariable(variable.name()) << ", type(" << type.name() << "))"; } - // TODO: handle untyped variables + // Allow any constant if typing is disabled + else + m_ostream << "constant(" << utils::escapeASPVariable(variable.name()) << ")"; } } @@ -398,7 +402,7 @@ void TranslatorASP::translateProblem() const if (!problem.objects().empty()) { m_ostream << std::endl; - translateObjects(); + translateConstants("objects", problem.objects()); } // Initial State @@ -412,34 +416,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());