Put translation of PDDL variable lists into separate method.

This commit is contained in:
Patrick Lühne 2016-06-12 22:55:35 +02:00
parent 87ca54a253
commit 0756d63769
2 changed files with 39 additions and 58 deletions

View File

@ -34,6 +34,8 @@ class TranslatorASP
void translateProblem() const; void translateProblem() const;
void translateVariables(const expressions::Variables &variables) const;
const Description &m_description; const Description &m_description;
std::ostream &m_ostream; std::ostream &m_ostream;
}; };

View File

@ -177,36 +177,9 @@ void TranslatorASP::translatePredicates() const
m_ostream << "predicate(" << predicate->name(); m_ostream << "predicate(" << predicate->name();
const auto &arguments = predicate->arguments(); translateVariables(predicate->arguments());
if (arguments.empty())
{
m_ostream << ")."; m_ostream << ").";
return;
}
m_ostream << "(";
for (auto i = arguments.cbegin(); i != arguments.cend(); i++)
{
if (i != arguments.cbegin())
m_ostream << ", ";
m_ostream << utils::escapeASPVariable((*i)->name());
}
m_ostream << ")) :- ";
for (auto i = arguments.cbegin(); i != arguments.cend(); i++)
{
if (i != arguments.cbegin())
m_ostream << ", ";
const auto &type = *dynamic_cast<const expressions::PrimitiveType *>((*i)->type());
m_ostream << "hasType(" << utils::escapeASPVariable((*i)->name()) << ", type(" << type.name() << "))";
}
m_ostream << ".";
}); });
m_ostream << std::endl; m_ostream << std::endl;
@ -227,39 +200,45 @@ void TranslatorASP::translateActions() const
m_ostream << "action(" << action->name(); m_ostream << "action(" << action->name();
const auto &parameters = action->parameters(); translateVariables(action->parameters());
if (parameters.empty())
{
m_ostream << ")."; m_ostream << ").";
return; });
m_ostream << std::endl;
} }
////////////////////////////////////////////////////////////////////////////////////////////////////
void TranslatorASP::translateVariables(const expressions::Variables &variables) const
{
if (variables.empty())
return;
m_ostream << "("; m_ostream << "(";
for (auto i = parameters.cbegin(); i != parameters.cend(); i++) for (auto i = variables.cbegin(); i != variables.cend(); i++)
{ {
if (i != parameters.cbegin()) if (i != variables.cbegin())
m_ostream << ", "; m_ostream << ", ";
m_ostream << utils::escapeASPVariable((*i)->name()); const auto &variable = *dynamic_cast<const expressions::Variable *>(i->get());
m_ostream << utils::escapeASPVariable(variable.name());
} }
m_ostream << ")) :- "; m_ostream << ")) :- ";
for (auto i = parameters.cbegin(); i != parameters.cend(); i++) for (auto i = variables.cbegin(); i != variables.cend(); i++)
{ {
if (i != parameters.cbegin()) if (i != variables.cbegin())
m_ostream << ", "; m_ostream << ", ";
const auto &type = *dynamic_cast<const expressions::PrimitiveType *>((*i)->type()); const auto &variable = *dynamic_cast<const expressions::Variable *>(i->get());
m_ostream << "hasType(" << utils::escapeASPVariable((*i)->name()) << ", type(" << type.name() << "))"; const auto &type = *dynamic_cast<const expressions::PrimitiveType *>(variable.type());
m_ostream << "hasType(" << utils::escapeASPVariable(variable.name()) << ", type(" << type.name() << "))";
} }
m_ostream << ".";
});
m_ostream << std::endl;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////