2016-06-10 17:40:32 +02:00
|
|
|
#include <plasp/pddl/TranslatorASP.h>
|
|
|
|
|
2016-06-12 23:38:44 +02:00
|
|
|
#include <plasp/pddl/expressions/And.h>
|
|
|
|
#include <plasp/pddl/expressions/Not.h>
|
2016-06-12 22:25:12 +02:00
|
|
|
#include <plasp/utils/IO.h>
|
2016-06-10 17:40:32 +02:00
|
|
|
#include <plasp/utils/TranslatorException.h>
|
|
|
|
|
|
|
|
namespace plasp
|
|
|
|
{
|
|
|
|
namespace pddl
|
|
|
|
{
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// TranslatorASP
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-12 22:31:18 +02:00
|
|
|
TranslatorASP::TranslatorASP(const Description &description, std::ostream &ostream)
|
|
|
|
: m_description(description),
|
|
|
|
m_ostream(ostream)
|
2016-06-10 17:40:32 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-12 22:19:55 +02:00
|
|
|
void TranslatorASP::checkSupport() const
|
|
|
|
{
|
2016-06-12 23:38:44 +02:00
|
|
|
// Check for "either" types in predicate declarations
|
2016-06-12 22:19:55 +02:00
|
|
|
const auto &predicates = m_description.domain().predicates();
|
|
|
|
|
|
|
|
std::for_each(predicates.cbegin(), predicates.cend(),
|
|
|
|
[&](const auto &predicate)
|
|
|
|
{
|
|
|
|
const auto &arguments = predicate->arguments();
|
|
|
|
|
|
|
|
std::for_each(arguments.cbegin(), arguments.cend(),
|
|
|
|
[&](const auto ¶meter)
|
|
|
|
{
|
|
|
|
if (parameter->type()->expressionType() != Expression::Type::PrimitiveType)
|
|
|
|
throw utils::TranslatorException("Only primitive types supported currently");
|
|
|
|
});
|
|
|
|
});
|
2016-06-12 22:47:39 +02:00
|
|
|
|
|
|
|
const auto &actions = m_description.domain().actions();
|
|
|
|
|
|
|
|
std::for_each(actions.cbegin(), actions.cend(),
|
|
|
|
[&](const auto &action)
|
|
|
|
{
|
|
|
|
const auto ¶meters = action->parameters();
|
|
|
|
|
2016-06-12 23:38:44 +02:00
|
|
|
// Check for "either" types in action parameters
|
2016-06-12 22:47:39 +02:00
|
|
|
std::for_each(parameters.cbegin(), parameters.cend(),
|
|
|
|
[&](const auto ¶meter)
|
|
|
|
{
|
|
|
|
if (parameter->type()->expressionType() != Expression::Type::PrimitiveType)
|
|
|
|
throw utils::TranslatorException("Only primitive types supported currently");
|
|
|
|
});
|
2016-06-12 23:38:44 +02:00
|
|
|
|
|
|
|
// Check that all preconditions are "and" expressions or single predicates
|
|
|
|
if (action->precondition().expressionType() != Expression::Type::And
|
|
|
|
&& action->precondition().expressionType() != Expression::Type::Predicate)
|
|
|
|
{
|
|
|
|
throw utils::TranslatorException("Only \"and\" expressions and single predicates supported as action preconditions currently");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that "and" expression in preconditions contains single predicates only
|
|
|
|
if (action->precondition().expressionType() == Expression::Type::And)
|
|
|
|
{
|
|
|
|
const auto &precondition = dynamic_cast<const expressions::And &>(action->precondition());
|
|
|
|
const auto &preconditionArguments = precondition.arguments();
|
|
|
|
|
|
|
|
std::for_each(preconditionArguments.cbegin(), preconditionArguments.cend(),
|
|
|
|
[&](const auto &argument)
|
|
|
|
{
|
|
|
|
if (argument->expressionType() != Expression::Type::Predicate)
|
|
|
|
throw utils::TranslatorException("Only predicates supported in preconditions currently");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that all effects are "and" expressions
|
|
|
|
if (action->effect().expressionType() != Expression::Type::And
|
|
|
|
&& action->effect().expressionType() != Expression::Type::Predicate)
|
|
|
|
{
|
|
|
|
throw utils::TranslatorException("Only \"and\" expressions and single predicates supported as action effects currently");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that "and" expression in effect contains single predicates or negated predicates only
|
|
|
|
if (action->effect().expressionType() == Expression::Type::And)
|
|
|
|
{
|
|
|
|
const auto &effect = dynamic_cast<const expressions::And &>(action->effect());
|
|
|
|
const auto &effectArguments = effect.arguments();
|
|
|
|
|
|
|
|
std::for_each(effectArguments.cbegin(), effectArguments.cend(),
|
|
|
|
[&](const auto &argument)
|
|
|
|
{
|
|
|
|
const Expression *expression = argument.get();
|
|
|
|
|
|
|
|
if (expression->expressionType() == Expression::Type::Not)
|
|
|
|
{
|
|
|
|
const auto ¬Expression = dynamic_cast<const expressions::Not &>(*expression);
|
|
|
|
expression = ¬Expression.argument();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expression->expressionType() != Expression::Type::Predicate)
|
|
|
|
throw utils::TranslatorException("Only predicates and negated predicates supported in effects currently");
|
|
|
|
});
|
|
|
|
}
|
2016-06-12 22:47:39 +02:00
|
|
|
});
|
2016-06-12 22:19:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-12 22:31:18 +02:00
|
|
|
void TranslatorASP::translate() const
|
2016-06-10 17:40:32 +02:00
|
|
|
{
|
2016-06-12 22:19:55 +02:00
|
|
|
checkSupport();
|
|
|
|
|
2016-06-12 22:31:18 +02:00
|
|
|
translateDomain();
|
2016-06-10 17:40:32 +02:00
|
|
|
|
|
|
|
if (m_description.containsProblem())
|
2016-06-12 22:25:12 +02:00
|
|
|
{
|
2016-06-12 22:31:18 +02:00
|
|
|
m_ostream << std::endl;
|
2016-06-12 22:25:12 +02:00
|
|
|
|
2016-06-12 22:31:18 +02:00
|
|
|
translateProblem();
|
2016-06-12 22:25:12 +02:00
|
|
|
}
|
2016-06-10 17:40:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-12 22:31:18 +02:00
|
|
|
void TranslatorASP::translateDomain() const
|
2016-06-10 17:40:32 +02:00
|
|
|
{
|
2016-06-12 22:31:18 +02:00
|
|
|
m_ostream
|
2016-06-10 17:40:32 +02:00
|
|
|
<< "%---------------------------------------" << std::endl
|
|
|
|
<< "% domain" << std::endl
|
2016-06-10 17:52:19 +02:00
|
|
|
<< "%---------------------------------------" << std::endl;
|
2016-06-10 17:40:32 +02:00
|
|
|
|
|
|
|
const auto &domain = m_description.domain();
|
|
|
|
|
|
|
|
// Types
|
2016-06-12 22:19:24 +02:00
|
|
|
if (!domain.types().empty())
|
|
|
|
{
|
2016-06-12 22:31:18 +02:00
|
|
|
m_ostream << std::endl;
|
2016-06-12 22:39:04 +02:00
|
|
|
translateTypes();
|
2016-06-12 22:19:24 +02:00
|
|
|
}
|
2016-06-10 17:52:19 +02:00
|
|
|
|
|
|
|
// Constants
|
2016-06-12 22:19:24 +02:00
|
|
|
if (!domain.constants().empty())
|
|
|
|
{
|
2016-06-12 22:31:18 +02:00
|
|
|
m_ostream << std::endl;
|
2016-06-12 22:39:04 +02:00
|
|
|
translateConstants();
|
2016-06-12 22:19:24 +02:00
|
|
|
}
|
2016-06-12 22:25:12 +02:00
|
|
|
|
|
|
|
// Predicates
|
|
|
|
if (!domain.predicates().empty())
|
|
|
|
{
|
2016-06-12 22:31:18 +02:00
|
|
|
m_ostream << std::endl;
|
2016-06-12 22:39:04 +02:00
|
|
|
translatePredicates();
|
|
|
|
}
|
2016-06-12 22:47:39 +02:00
|
|
|
|
|
|
|
// Actions
|
|
|
|
if (!domain.actions().empty())
|
|
|
|
{
|
|
|
|
m_ostream << std::endl;
|
|
|
|
translateActions();
|
|
|
|
}
|
2016-06-12 22:39:04 +02:00
|
|
|
}
|
2016-06-12 22:25:12 +02:00
|
|
|
|
2016-06-12 22:39:04 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2016-06-12 22:25:12 +02:00
|
|
|
|
2016-06-12 22:39:04 +02:00
|
|
|
void TranslatorASP::translateTypes() const
|
|
|
|
{
|
2016-06-12 22:47:39 +02:00
|
|
|
// TODO: escape ASP identifiers
|
2016-06-12 22:39:04 +02:00
|
|
|
m_ostream << "% types";
|
2016-06-12 22:25:12 +02:00
|
|
|
|
2016-06-12 22:39:04 +02:00
|
|
|
const auto &types = m_description.domain().types();
|
2016-06-12 22:25:12 +02:00
|
|
|
|
2016-06-12 22:39:04 +02:00
|
|
|
std::for_each(types.cbegin(), types.cend(),
|
|
|
|
[&](const auto &type)
|
|
|
|
{
|
|
|
|
m_ostream << std::endl;
|
2016-06-12 22:25:12 +02:00
|
|
|
|
2016-06-12 22:39:04 +02:00
|
|
|
m_ostream << "type(" << type->name() << ")." << std::endl;
|
2016-06-12 22:25:12 +02:00
|
|
|
|
2016-06-12 22:39:04 +02:00
|
|
|
const auto &parentTypes = type->parentTypes();
|
2016-06-12 22:25:12 +02:00
|
|
|
|
2016-06-12 22:39:04 +02:00
|
|
|
std::for_each(parentTypes.cbegin(), parentTypes.cend(),
|
|
|
|
[&](const auto &parentType)
|
2016-06-12 22:25:12 +02:00
|
|
|
{
|
2016-06-12 22:39:04 +02:00
|
|
|
m_ostream << "inherits(type(" << type->name() << "), type(" << parentType->name() << "))." << std::endl;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2016-06-12 22:25:12 +02:00
|
|
|
|
2016-06-12 22:39:04 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2016-06-12 22:25:12 +02:00
|
|
|
|
2016-06-12 22:39:04 +02:00
|
|
|
void TranslatorASP::translateConstants() const
|
|
|
|
{
|
|
|
|
m_ostream << "% constants";
|
2016-06-12 22:25:12 +02:00
|
|
|
|
2016-06-12 22:39:04 +02:00
|
|
|
const auto &constants = m_description.domain().constants();
|
2016-06-12 22:25:12 +02:00
|
|
|
|
2016-06-12 22:39:04 +02:00
|
|
|
std::for_each(constants.cbegin(), constants.cend(),
|
|
|
|
[&](const auto &constant)
|
|
|
|
{
|
|
|
|
m_ostream << std::endl;
|
2016-06-12 22:25:12 +02:00
|
|
|
|
2016-06-12 22:39:04 +02:00
|
|
|
m_ostream << "constant(" << constant->name() << ")." << std::endl;
|
|
|
|
|
|
|
|
const auto *type = constant->type();
|
|
|
|
|
|
|
|
if (type == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_ostream << "hasType(constant(" << constant->name() << "), type(" << type->name() << "))." << std::endl;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void TranslatorASP::translatePredicates() const
|
|
|
|
{
|
|
|
|
m_ostream << "% predicates";
|
|
|
|
|
|
|
|
const auto &predicates = m_description.domain().predicates();
|
|
|
|
|
|
|
|
std::for_each(predicates.cbegin(), predicates.cend(),
|
|
|
|
[&](const auto &predicate)
|
|
|
|
{
|
|
|
|
m_ostream << std::endl;
|
|
|
|
|
|
|
|
m_ostream << "predicate(" << predicate->name();
|
|
|
|
|
2016-06-12 22:58:17 +02:00
|
|
|
this->translateVariables(predicate->arguments());
|
2016-06-12 22:39:04 +02:00
|
|
|
|
2016-06-12 22:55:35 +02:00
|
|
|
m_ostream << ").";
|
2016-06-12 22:39:04 +02:00
|
|
|
});
|
2016-06-12 22:47:39 +02:00
|
|
|
|
|
|
|
m_ostream << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void TranslatorASP::translateActions() const
|
|
|
|
{
|
|
|
|
m_ostream << "% actions";
|
|
|
|
|
|
|
|
const auto &actions = m_description.domain().actions();
|
|
|
|
|
|
|
|
std::for_each(actions.cbegin(), actions.cend(),
|
|
|
|
[&](const auto &action)
|
|
|
|
{
|
|
|
|
m_ostream << std::endl;
|
|
|
|
|
|
|
|
m_ostream << "action(" << action->name();
|
|
|
|
|
2016-06-12 22:58:17 +02:00
|
|
|
this->translateVariables(action->parameters());
|
2016-06-12 22:47:39 +02:00
|
|
|
|
2016-06-12 22:55:35 +02:00
|
|
|
m_ostream << ").";
|
|
|
|
});
|
2016-06-12 22:47:39 +02:00
|
|
|
|
2016-06-12 22:55:35 +02:00
|
|
|
m_ostream << std::endl;
|
|
|
|
}
|
2016-06-12 22:47:39 +02:00
|
|
|
|
2016-06-12 22:55:35 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2016-06-12 22:47:39 +02:00
|
|
|
|
2016-06-12 22:55:35 +02:00
|
|
|
void TranslatorASP::translateVariables(const expressions::Variables &variables) const
|
|
|
|
{
|
|
|
|
if (variables.empty())
|
|
|
|
return;
|
2016-06-12 22:47:39 +02:00
|
|
|
|
2016-06-12 22:55:35 +02:00
|
|
|
m_ostream << "(";
|
2016-06-12 22:47:39 +02:00
|
|
|
|
2016-06-12 22:55:35 +02:00
|
|
|
for (auto i = variables.cbegin(); i != variables.cend(); i++)
|
|
|
|
{
|
|
|
|
if (i != variables.cbegin())
|
|
|
|
m_ostream << ", ";
|
2016-06-12 22:47:39 +02:00
|
|
|
|
2016-06-12 22:55:35 +02:00
|
|
|
const auto &variable = *dynamic_cast<const expressions::Variable *>(i->get());
|
2016-06-12 22:47:39 +02:00
|
|
|
|
2016-06-12 22:55:35 +02:00
|
|
|
m_ostream << utils::escapeASPVariable(variable.name());
|
|
|
|
}
|
2016-06-12 22:47:39 +02:00
|
|
|
|
2016-06-12 22:55:35 +02:00
|
|
|
m_ostream << ")) :- ";
|
|
|
|
|
|
|
|
for (auto i = variables.cbegin(); i != variables.cend(); i++)
|
|
|
|
{
|
|
|
|
if (i != variables.cbegin())
|
|
|
|
m_ostream << ", ";
|
|
|
|
|
|
|
|
const auto &variable = *dynamic_cast<const expressions::Variable *>(i->get());
|
|
|
|
const auto &type = *dynamic_cast<const expressions::PrimitiveType *>(variable.type());
|
|
|
|
|
|
|
|
m_ostream << "hasType(" << utils::escapeASPVariable(variable.name()) << ", type(" << type.name() << "))";
|
|
|
|
}
|
2016-06-10 17:40:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-12 22:31:18 +02:00
|
|
|
void TranslatorASP::translateProblem() const
|
2016-06-10 17:40:32 +02:00
|
|
|
{
|
2016-06-12 22:47:39 +02:00
|
|
|
m_ostream
|
2016-06-10 17:40:32 +02:00
|
|
|
<< "%---------------------------------------" << std::endl
|
|
|
|
<< "% problem" << std::endl
|
2016-06-10 17:52:19 +02:00
|
|
|
<< "%---------------------------------------" << std::endl;
|
2016-06-10 17:40:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|