diff --git a/include/plasp/pddl/translation/Variables.h b/include/plasp/pddl/translation/Variables.h new file mode 100644 index 0000000..5f37c30 --- /dev/null +++ b/include/plasp/pddl/translation/Variables.h @@ -0,0 +1,87 @@ +#ifndef __PLASP__PDDL__TRANSLATION__VARIABLES_H +#define __PLASP__PDDL__TRANSLATION__VARIABLES_H + +#include +#include +#include +#include +#include + +namespace plasp +{ +namespace pddl +{ +namespace translation +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Variables +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +void translateVariablesForRuleHead(output::ColorStream &outputStream, const T &variables); +template +void translateVariablesForRuleBody(output::ColorStream &outputStream, const T &variables); + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +inline void translateVariablesForRuleHead(output::ColorStream &outputStream, const T &variables) +{ + if (variables.empty()) + return; + + for (auto i = variables.cbegin(); i != variables.cend(); i++) + { + const auto &variable = **i; + + outputStream << ", " << output::Variable(variable.name().c_str()); + } +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +inline void translateVariablesForRuleBody(output::ColorStream &outputStream, const T &variables) +{ + if (variables.empty()) + return; + + outputStream << " :- "; + + for (auto i = variables.cbegin(); i != variables.cend(); i++) + { + const auto &variable = **i; + + if (i != variables.cbegin()) + outputStream << ", "; + + if (variable.type() != nullptr) + { + if (variable.type()->expressionType() != Expression::Type::PrimitiveType) + throw output::TranslatorException("only primitive types supported currently"); + + const auto &type = variable.type()->template as(); + + outputStream << output::Function("has") << "(" + << output::Variable(variable.name().c_str()) << ", " + << output::Keyword("type") << "(" << output::String(type.name().c_str()) << "))"; + } + else + { + outputStream << output::Function("has") << "(" + << output::Variable(variable.name().c_str()) << ", " + << output::Keyword("type") << "(" << output::String("object") << "))"; + } + } +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} +} + +#endif diff --git a/src/plasp/pddl/TranslatorASP.cpp b/src/plasp/pddl/TranslatorASP.cpp index 91f4570..8c9470a 100644 --- a/src/plasp/pddl/TranslatorASP.cpp +++ b/src/plasp/pddl/TranslatorASP.cpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace plasp { @@ -21,13 +22,6 @@ namespace pddl // //////////////////////////////////////////////////////////////////////////////////////////////////// -template -void translateVariablesHead(output::ColorStream &outputStream, const T &variables); -template -void translateVariablesBody(output::ColorStream &outputStream, const T &variables); - -//////////////////////////////////////////////////////////////////////////////////////////////////// - TranslatorASP::TranslatorASP(Description &description, output::ColorStream &outputStream) : m_description(description), m_outputStream(outputStream) @@ -108,26 +102,25 @@ void TranslatorASP::translateTypes() const return; } - std::for_each(types.cbegin(), types.cend(), - [&](const auto &type) - { - m_outputStream - << output::Function("type") << "(" - << output::Keyword("type") << "(" - << output::String(type->name().c_str()) - << "))." << std::endl; + for (const auto &type : types) + { + m_outputStream + << output::Function("type") << "(" + << output::Keyword("type") << "(" + << output::String(type->name().c_str()) + << "))." << std::endl; - const auto &parentTypes = type->parentTypes(); + const auto &parentTypes = type->parentTypes(); - std::for_each(parentTypes.cbegin(), parentTypes.cend(), - [&](const auto &parentType) - { - m_outputStream - << output::Function("inherits") << "(" << output::Keyword("type") - << "(" << output::String(type->name().c_str()) << "), " << output::Keyword("type") - << "(" << output::String(parentType->name().c_str()) << "))." << std::endl; - }); - }); + std::for_each(parentTypes.cbegin(), parentTypes.cend(), + [&](const auto &parentType) + { + m_outputStream + << output::Function("inherits") << "(" << output::Keyword("type") + << "(" << output::String(type->name().c_str()) << "), " << output::Keyword("type") + << "(" << output::String(parentType->name().c_str()) << "))." << std::endl; + }); + } m_outputStream << std::endl @@ -162,26 +155,25 @@ void TranslatorASP::translatePredicates() const } m_outputStream << "(" << output::String(predicate->name().c_str()); - translateVariablesHead(m_outputStream, predicate->parameters()); + translation::translateVariablesForRuleHead(m_outputStream, predicate->parameters()); m_outputStream << ")"; }; - std::for_each(predicates.cbegin(), predicates.cend(), - [&](const auto &predicate) - { - m_outputStream - << std::endl - << output::Function("variable") << "(" - << output::Keyword("variable") << "("; + for (const auto &predicate : predicates) + { + m_outputStream + << std::endl + << output::Function("variable") << "(" + << output::Keyword("variable") << "("; - printPredicateName(predicate); + printPredicateName(predicate); - m_outputStream << "))"; + m_outputStream << "))"; - translateVariablesBody(m_outputStream, predicate->parameters()); + translation::translateVariablesForRuleBody(m_outputStream, predicate->parameters()); - m_outputStream << "."; - }); + m_outputStream << "."; + } m_outputStream << std::endl << std::endl @@ -220,7 +212,7 @@ void TranslatorASP::translateDerivedPredicates() const } outputStream << "(" << output::Number(id); - translateVariablesHead(outputStream, derivedPredicate->parameters()); + translation::translateVariablesForRuleHead(outputStream, derivedPredicate->parameters()); outputStream << "))"; }; @@ -230,7 +222,7 @@ void TranslatorASP::translateDerivedPredicates() const m_outputStream << ")"; - translateVariablesBody(m_outputStream, derivedPredicate->parameters()); + translation::translateVariablesForRuleBody(m_outputStream, derivedPredicate->parameters()); m_outputStream << "."; @@ -270,7 +262,7 @@ void TranslatorASP::translateActions() const } m_outputStream << "(" << output::String(action.name().c_str()); - translateVariablesHead(m_outputStream, action.parameters()); + translation::translateVariablesForRuleHead(m_outputStream, action.parameters()); m_outputStream << "))"; }; @@ -305,7 +297,7 @@ void TranslatorASP::translateActions() const printActionName(*action); m_outputStream << ")"; - translateVariablesBody(m_outputStream, action->parameters()); + translation::translateVariablesForRuleBody(m_outputStream, action->parameters()); m_outputStream << "."; @@ -375,81 +367,27 @@ void TranslatorASP::translateConstants(const std::string &heading, const express { m_outputStream << output::Heading2(heading.c_str()); - std::for_each(constants.cbegin(), constants.cend(), - [&](const auto &constant) - { - m_outputStream << std::endl - << output::Function("constant") << "(" - << output::Keyword("constant") << "(" - << output::String(constant->name().c_str()) - << "))." << std::endl; - - const auto type = constant->type(); - - if (type != nullptr) - { - m_outputStream << output::Function("has") << "(" - << output::Keyword("constant") << "(" << output::String(constant->name().c_str()) << "), " - << output::Keyword("type") << "(" << output::String(type->name().c_str()) << "))." << std::endl; - } - else - { - m_outputStream << output::Function("has") << "(" - << output::Keyword("constant") << "(" << output::String(constant->name().c_str()) << "), " - << output::Keyword("type") << "(" << output::String("object") << "))." << std::endl; - } - }); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -void translateVariablesHead(output::ColorStream &outputStream, const T &variables) -{ - if (variables.empty()) - return; - - for (auto i = variables.cbegin(); i != variables.cend(); i++) + for (const auto &constant : constants) { - const auto &variable = **i; + m_outputStream << std::endl + << output::Function("constant") << "(" + << output::Keyword("constant") << "(" + << output::String(constant->name().c_str()) + << "))." << std::endl; - outputStream << ", " << output::Variable(variable.name().c_str()); - } -} + const auto type = constant->type(); -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -void translateVariablesBody(output::ColorStream &outputStream, const T &variables) -{ - if (variables.empty()) - return; - - outputStream << " :- "; - - for (auto i = variables.cbegin(); i != variables.cend(); i++) - { - const auto &variable = **i; - - if (i != variables.cbegin()) - outputStream << ", "; - - if (variable.type() != nullptr) + if (type != nullptr) { - if (variable.type()->expressionType() != Expression::Type::PrimitiveType) - throw output::TranslatorException("only primitive types supported currently"); - - const auto &type = variable.type()->template as(); - - outputStream << output::Function("has") << "(" - << output::Variable(variable.name().c_str()) << ", " - << output::Keyword("type") << "(" << output::String(type.name().c_str()) << "))"; + m_outputStream << output::Function("has") << "(" + << output::Keyword("constant") << "(" << output::String(constant->name().c_str()) << "), " + << output::Keyword("type") << "(" << output::String(type->name().c_str()) << "))." << std::endl; } else { - outputStream << output::Function("has") << "(" - << output::Variable(variable.name().c_str()) << ", " - << output::Keyword("type") << "(" << output::String("object") << "))"; + m_outputStream << output::Function("has") << "(" + << output::Keyword("constant") << "(" << output::String(constant->name().c_str()) << "), " + << output::Keyword("type") << "(" << output::String("object") << "))." << std::endl; } } }