From 39a32a6e0a64c4d8488ad5e2dfa9b277530f6000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Tue, 14 Jun 2016 13:21:41 +0200 Subject: [PATCH 1/5] Handling untyped variables correctly in the PDDL translator. --- include/plasp/pddl/TranslatorASP.h | 3 +- src/plasp/pddl/TranslatorASP.cpp | 96 +++++++++++------------------- 2 files changed, 37 insertions(+), 62 deletions(-) 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()); From c62f3f88f10fbb720cfefc5135a1209858e83781 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Tue, 14 Jun 2016 13:53:09 +0200 Subject: [PATCH 2/5] =?UTF-8?q?Handling=20untyped=20variables=20as=20varia?= =?UTF-8?q?bles=20of=20type=20=C2=BBobject=C2=AB=20in=20PDDL=20translator?= =?UTF-8?q?=20for=20simplicity.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plasp/pddl/TranslatorASP.cpp | 37 ++++++++++++++------------------ 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/src/plasp/pddl/TranslatorASP.cpp b/src/plasp/pddl/TranslatorASP.cpp index 8370d22..4df45e4 100644 --- a/src/plasp/pddl/TranslatorASP.cpp +++ b/src/plasp/pddl/TranslatorASP.cpp @@ -50,11 +50,8 @@ 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()) @@ -82,11 +79,16 @@ void TranslatorASP::translateDomain() const void TranslatorASP::translateTypes() const { - // TODO: escape ASP identifiers m_ostream << "% types"; const auto &types = m_description.domain().types(); + if (types.empty()) + { + m_ostream << std::endl << "type(object)." << std::endl; + return; + } + std::for_each(types.cbegin(), types.cend(), [&](const auto &type) { @@ -245,21 +247,21 @@ void TranslatorASP::translateActions() const void TranslatorASP::translateConstants(const std::string &header, const expressions::Constants &constants) const { - m_ostream << "% " << header << std::endl; + m_ostream << "% " << header; std::for_each(constants.cbegin(), constants.cend(), [&](const auto &constant) { const auto constantName = utils::escapeASP(constant->name()); - m_ostream << "constant(" << constantName << ")." << std::endl; + m_ostream << std::endl << "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; + 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; }); } @@ -279,12 +281,7 @@ void TranslatorASP::translateVariablesHead(const expressions::Variables &variabl const auto &variable = **i; - // 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 << utils::escapeASPVariable(variable.name()); } m_ostream << ")"; @@ -306,7 +303,6 @@ void TranslatorASP::translateVariablesBody(const expressions::Variables &variabl if (i != variables.cbegin()) m_ostream << ", "; - // Handle typed variables if (variable.type() != nullptr) { if (variable.type()->expressionType() != Expression::Type::PrimitiveType) @@ -316,9 +312,8 @@ void TranslatorASP::translateVariablesBody(const expressions::Variables &variabl m_ostream << "hasType(" << utils::escapeASPVariable(variable.name()) << ", type(" << type.name() << "))"; } - // Allow any constant if typing is disabled else - m_ostream << "constant(" << utils::escapeASPVariable(variable.name()) << ")"; + m_ostream << "hasType(" << utils::escapeASPVariable(variable.name()) << ", type(object))"; } } From 94d448d438a6b1fae4313c51dfdfcc30f9616b27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Tue, 14 Jun 2016 14:05:38 +0200 Subject: [PATCH 3/5] Minor formatting. --- src/plasp/pddl/TranslatorASP.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/plasp/pddl/TranslatorASP.cpp b/src/plasp/pddl/TranslatorASP.cpp index 4df45e4..1a7a637 100644 --- a/src/plasp/pddl/TranslatorASP.cpp +++ b/src/plasp/pddl/TranslatorASP.cpp @@ -79,7 +79,7 @@ void TranslatorASP::translateDomain() const void TranslatorASP::translateTypes() const { - m_ostream << "% types"; + m_ostream << "% types" << std::endl; const auto &types = m_description.domain().types(); @@ -92,8 +92,6 @@ void TranslatorASP::translateTypes() const 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; From a7cf696357c974d3f1b5fb6611f1259f8555d105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Tue, 14 Jun 2016 14:18:38 +0200 Subject: [PATCH 4/5] Minor formatting. --- src/plasp/pddl/TranslatorASP.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plasp/pddl/TranslatorASP.cpp b/src/plasp/pddl/TranslatorASP.cpp index 1a7a637..d11c263 100644 --- a/src/plasp/pddl/TranslatorASP.cpp +++ b/src/plasp/pddl/TranslatorASP.cpp @@ -85,7 +85,7 @@ void TranslatorASP::translateTypes() const if (types.empty()) { - m_ostream << std::endl << "type(object)." << std::endl; + m_ostream << "type(object)." << std::endl; return; } From a23063fd4f9c1c5fab0af4f0767f1c40f9732afe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Tue, 14 Jun 2016 14:22:41 +0200 Subject: [PATCH 5/5] Minor formatting. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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