Put functions related to translation of variables in separate file.

This commit is contained in:
Patrick Lühne 2016-12-08 01:05:39 +01:00
parent 66cb09c8e4
commit 8587aa23c4
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
2 changed files with 135 additions and 110 deletions

View File

@ -0,0 +1,87 @@
#ifndef __PLASP__PDDL__TRANSLATION__VARIABLES_H
#define __PLASP__PDDL__TRANSLATION__VARIABLES_H
#include <plasp/output/Formatting.h>
#include <plasp/output/TranslatorException.h>
#include <plasp/pddl/Description.h>
#include <plasp/pddl/expressions/Not.h>
#include <plasp/pddl/expressions/Predicate.h>
namespace plasp
{
namespace pddl
{
namespace translation
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Variables
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T>
void translateVariablesForRuleHead(output::ColorStream &outputStream, const T &variables);
template<class T>
void translateVariablesForRuleBody(output::ColorStream &outputStream, const T &variables);
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T>
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<class T>
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<expressions::PrimitiveType>();
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

View File

@ -9,6 +9,7 @@
#include <plasp/pddl/expressions/Predicate.h> #include <plasp/pddl/expressions/Predicate.h>
#include <plasp/pddl/translation/Precondition.h> #include <plasp/pddl/translation/Precondition.h>
#include <plasp/pddl/translation/Primitives.h> #include <plasp/pddl/translation/Primitives.h>
#include <plasp/pddl/translation/Variables.h>
namespace plasp namespace plasp
{ {
@ -21,13 +22,6 @@ namespace pddl
// //
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T>
void translateVariablesHead(output::ColorStream &outputStream, const T &variables);
template<class T>
void translateVariablesBody(output::ColorStream &outputStream, const T &variables);
////////////////////////////////////////////////////////////////////////////////////////////////////
TranslatorASP::TranslatorASP(Description &description, output::ColorStream &outputStream) TranslatorASP::TranslatorASP(Description &description, output::ColorStream &outputStream)
: m_description(description), : m_description(description),
m_outputStream(outputStream) m_outputStream(outputStream)
@ -108,8 +102,7 @@ void TranslatorASP::translateTypes() const
return; return;
} }
std::for_each(types.cbegin(), types.cend(), for (const auto &type : types)
[&](const auto &type)
{ {
m_outputStream m_outputStream
<< output::Function("type") << "(" << output::Function("type") << "("
@ -127,7 +120,7 @@ void TranslatorASP::translateTypes() const
<< "(" << output::String(type->name().c_str()) << "), " << output::Keyword("type") << "(" << output::String(type->name().c_str()) << "), " << output::Keyword("type")
<< "(" << output::String(parentType->name().c_str()) << "))." << std::endl; << "(" << output::String(parentType->name().c_str()) << "))." << std::endl;
}); });
}); }
m_outputStream m_outputStream
<< std::endl << std::endl
@ -162,12 +155,11 @@ void TranslatorASP::translatePredicates() const
} }
m_outputStream << "(" << output::String(predicate->name().c_str()); m_outputStream << "(" << output::String(predicate->name().c_str());
translateVariablesHead(m_outputStream, predicate->parameters()); translation::translateVariablesForRuleHead(m_outputStream, predicate->parameters());
m_outputStream << ")"; m_outputStream << ")";
}; };
std::for_each(predicates.cbegin(), predicates.cend(), for (const auto &predicate : predicates)
[&](const auto &predicate)
{ {
m_outputStream m_outputStream
<< std::endl << std::endl
@ -178,10 +170,10 @@ void TranslatorASP::translatePredicates() const
m_outputStream << "))"; m_outputStream << "))";
translateVariablesBody(m_outputStream, predicate->parameters()); translation::translateVariablesForRuleBody(m_outputStream, predicate->parameters());
m_outputStream << "."; m_outputStream << ".";
}); }
m_outputStream m_outputStream
<< std::endl << std::endl << std::endl << std::endl
@ -220,7 +212,7 @@ void TranslatorASP::translateDerivedPredicates() const
} }
outputStream << "(" << output::Number<decltype(id)>(id); outputStream << "(" << output::Number<decltype(id)>(id);
translateVariablesHead(outputStream, derivedPredicate->parameters()); translation::translateVariablesForRuleHead(outputStream, derivedPredicate->parameters());
outputStream << "))"; outputStream << "))";
}; };
@ -230,7 +222,7 @@ void TranslatorASP::translateDerivedPredicates() const
m_outputStream << ")"; m_outputStream << ")";
translateVariablesBody(m_outputStream, derivedPredicate->parameters()); translation::translateVariablesForRuleBody(m_outputStream, derivedPredicate->parameters());
m_outputStream << "."; m_outputStream << ".";
@ -270,7 +262,7 @@ void TranslatorASP::translateActions() const
} }
m_outputStream << "(" << output::String(action.name().c_str()); m_outputStream << "(" << output::String(action.name().c_str());
translateVariablesHead(m_outputStream, action.parameters()); translation::translateVariablesForRuleHead(m_outputStream, action.parameters());
m_outputStream << "))"; m_outputStream << "))";
}; };
@ -305,7 +297,7 @@ void TranslatorASP::translateActions() const
printActionName(*action); printActionName(*action);
m_outputStream << ")"; m_outputStream << ")";
translateVariablesBody(m_outputStream, action->parameters()); translation::translateVariablesForRuleBody(m_outputStream, action->parameters());
m_outputStream << "."; m_outputStream << ".";
@ -375,8 +367,7 @@ void TranslatorASP::translateConstants(const std::string &heading, const express
{ {
m_outputStream << output::Heading2(heading.c_str()); m_outputStream << output::Heading2(heading.c_str());
std::for_each(constants.cbegin(), constants.cend(), for (const auto &constant : constants)
[&](const auto &constant)
{ {
m_outputStream << std::endl m_outputStream << std::endl
<< output::Function("constant") << "(" << output::Function("constant") << "("
@ -398,59 +389,6 @@ void TranslatorASP::translateConstants(const std::string &heading, const express
<< output::Keyword("constant") << "(" << output::String(constant->name().c_str()) << "), " << output::Keyword("constant") << "(" << output::String(constant->name().c_str()) << "), "
<< output::Keyword("type") << "(" << output::String("object") << "))." << std::endl; << output::Keyword("type") << "(" << output::String("object") << "))." << std::endl;
} }
});
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T>
void translateVariablesHead(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<class T>
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 (variable.type()->expressionType() != Expression::Type::PrimitiveType)
throw output::TranslatorException("only primitive types supported currently");
const auto &type = variable.type()->template as<expressions::PrimitiveType>();
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") << "))";
}
} }
} }