Put functions related to translation of variables in separate file.
This commit is contained in:
parent
66cb09c8e4
commit
8587aa23c4
87
include/plasp/pddl/translation/Variables.h
Normal file
87
include/plasp/pddl/translation/Variables.h
Normal 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
|
@ -9,6 +9,7 @@
|
||||
#include <plasp/pddl/expressions/Predicate.h>
|
||||
#include <plasp/pddl/translation/Precondition.h>
|
||||
#include <plasp/pddl/translation/Primitives.h>
|
||||
#include <plasp/pddl/translation/Variables.h>
|
||||
|
||||
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)
|
||||
: m_description(description),
|
||||
m_outputStream(outputStream)
|
||||
@ -108,8 +102,7 @@ void TranslatorASP::translateTypes() const
|
||||
return;
|
||||
}
|
||||
|
||||
std::for_each(types.cbegin(), types.cend(),
|
||||
[&](const auto &type)
|
||||
for (const auto &type : types)
|
||||
{
|
||||
m_outputStream
|
||||
<< output::Function("type") << "("
|
||||
@ -127,7 +120,7 @@ void TranslatorASP::translateTypes() const
|
||||
<< "(" << output::String(type->name().c_str()) << "), " << output::Keyword("type")
|
||||
<< "(" << output::String(parentType->name().c_str()) << "))." << std::endl;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
m_outputStream
|
||||
<< std::endl
|
||||
@ -162,12 +155,11 @@ 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)
|
||||
for (const auto &predicate : predicates)
|
||||
{
|
||||
m_outputStream
|
||||
<< std::endl
|
||||
@ -178,10 +170,10 @@ void TranslatorASP::translatePredicates() const
|
||||
|
||||
m_outputStream << "))";
|
||||
|
||||
translateVariablesBody(m_outputStream, predicate->parameters());
|
||||
translation::translateVariablesForRuleBody(m_outputStream, predicate->parameters());
|
||||
|
||||
m_outputStream << ".";
|
||||
});
|
||||
}
|
||||
|
||||
m_outputStream
|
||||
<< std::endl << std::endl
|
||||
@ -220,7 +212,7 @@ void TranslatorASP::translateDerivedPredicates() const
|
||||
}
|
||||
|
||||
outputStream << "(" << output::Number<decltype(id)>(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,8 +367,7 @@ 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)
|
||||
for (const auto &constant : constants)
|
||||
{
|
||||
m_outputStream << std::endl
|
||||
<< 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("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") << "))";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user