Using LogStreams in the PDDL translator.

This commit is contained in:
Patrick Lühne 2016-06-14 16:14:39 +02:00
parent fd5416c94e
commit 0de2489440
4 changed files with 71 additions and 71 deletions

View File

@ -117,8 +117,8 @@ int main(int argc, char **argv)
else if (warningLevel == "ignore") else if (warningLevel == "ignore")
context.logger.setWarningLevel(plasp::utils::Logger::WarningLevel::Ignore); context.logger.setWarningLevel(plasp::utils::Logger::WarningLevel::Ignore);
const auto description = plasp::pddl::Description::fromContext(std::move(context)); auto description = plasp::pddl::Description::fromContext(std::move(context));
const auto translator = plasp::pddl::TranslatorASP(description, std::cout); const auto translator = plasp::pddl::TranslatorASP(description, description.context().logger.outputStream());
translator.translate(); translator.translate();
} }
else if (language == plasp::Language::Type::SAS) else if (language == plasp::Language::Type::SAS)

View File

@ -19,7 +19,7 @@ namespace pddl
class TranslatorASP class TranslatorASP
{ {
public: public:
explicit TranslatorASP(const Description &description, std::ostream &ostream); explicit TranslatorASP(const Description &description, utils::LogStream &outputStream);
void translate() const; void translate() const;
@ -40,7 +40,7 @@ class TranslatorASP
void translatePredicate(const expressions::Predicate &predicate) const; void translatePredicate(const expressions::Predicate &predicate) const;
const Description &m_description; const Description &m_description;
std::ostream &m_ostream; utils::LogStream &m_outputStream;
}; };
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -19,9 +19,9 @@ namespace pddl
// //
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
TranslatorASP::TranslatorASP(const Description &description, std::ostream &ostream) TranslatorASP::TranslatorASP(const Description &description, utils::LogStream &outputStream)
: m_description(description), : m_description(description),
m_ostream(ostream) m_outputStream(outputStream)
{ {
} }
@ -33,7 +33,7 @@ void TranslatorASP::translate() const
if (m_description.containsProblem()) if (m_description.containsProblem())
{ {
m_ostream << std::endl; m_outputStream << std::endl;
translateProblem(); translateProblem();
} }
} }
@ -42,7 +42,7 @@ void TranslatorASP::translate() const
void TranslatorASP::translateDomain() const void TranslatorASP::translateDomain() const
{ {
m_ostream m_outputStream
<< "%---------------------------------------" << std::endl << "%---------------------------------------" << std::endl
<< "% domain" << std::endl << "% domain" << std::endl
<< "%---------------------------------------" << std::endl; << "%---------------------------------------" << std::endl;
@ -50,27 +50,27 @@ void TranslatorASP::translateDomain() const
const auto &domain = m_description.domain(); const auto &domain = m_description.domain();
// Types // Types
m_ostream << std::endl; m_outputStream << std::endl;
translateTypes(); translateTypes();
// Constants // Constants
if (!domain.constants().empty()) if (!domain.constants().empty())
{ {
m_ostream << std::endl; m_outputStream << std::endl;
translateConstants("constants", domain.constants()); translateConstants("constants", domain.constants());
} }
// Predicates // Predicates
if (!domain.predicates().empty()) if (!domain.predicates().empty())
{ {
m_ostream << std::endl; m_outputStream << std::endl;
translatePredicates(); translatePredicates();
} }
// Actions // Actions
if (!domain.actions().empty()) if (!domain.actions().empty())
{ {
m_ostream << std::endl; m_outputStream << std::endl;
translateActions(); translateActions();
} }
} }
@ -79,13 +79,13 @@ void TranslatorASP::translateDomain() const
void TranslatorASP::translateTypes() const void TranslatorASP::translateTypes() const
{ {
m_ostream << "% types" << std::endl; m_outputStream << "% types" << std::endl;
const auto &types = m_description.domain().types(); const auto &types = m_description.domain().types();
if (types.empty()) if (types.empty())
{ {
m_ostream << "type(object)." << std::endl; m_outputStream << "type(object)." << std::endl;
return; return;
} }
@ -94,7 +94,7 @@ void TranslatorASP::translateTypes() const
{ {
const auto typeName = utils::escapeASP(type->name()); const auto typeName = utils::escapeASP(type->name());
m_ostream << "type(" << typeName << ")." << std::endl; m_outputStream << "type(" << typeName << ")." << std::endl;
const auto &parentTypes = type->parentTypes(); const auto &parentTypes = type->parentTypes();
@ -103,8 +103,8 @@ void TranslatorASP::translateTypes() const
{ {
const auto parentTypeName = utils::escapeASP(parentType->name()); const auto parentTypeName = utils::escapeASP(parentType->name());
m_ostream << "inherits(type(" << typeName << "), type(" << parentTypeName << "))." << std::endl; m_outputStream << "inherits(type(" << typeName << "), type(" << parentTypeName << "))." << std::endl;
m_ostream << "hasType(X, type(" << parentTypeName << ")) :- hasType(X, type(" << typeName << "))." << std::endl; m_outputStream << "hasType(X, type(" << parentTypeName << ")) :- hasType(X, type(" << typeName << "))." << std::endl;
}); });
}); });
} }
@ -113,45 +113,45 @@ void TranslatorASP::translateTypes() const
void TranslatorASP::translatePredicates() const void TranslatorASP::translatePredicates() const
{ {
m_ostream << "% predicates"; m_outputStream << "% predicates";
const auto &predicates = m_description.domain().predicates(); const auto &predicates = m_description.domain().predicates();
std::for_each(predicates.cbegin(), predicates.cend(), std::for_each(predicates.cbegin(), predicates.cend(),
[&](const auto &predicate) [&](const auto &predicate)
{ {
m_ostream << std::endl; m_outputStream << std::endl;
m_ostream << "predicate(" << utils::escapeASP(predicate->name()); m_outputStream << "predicate(" << utils::escapeASP(predicate->name());
this->translateVariablesHead(predicate->arguments()); this->translateVariablesHead(predicate->arguments());
m_ostream << ")"; m_outputStream << ")";
this->translateVariablesBody(predicate->arguments()); this->translateVariablesBody(predicate->arguments());
m_ostream << "."; m_outputStream << ".";
}); });
m_ostream << std::endl; m_outputStream << std::endl;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void TranslatorASP::translateActions() const void TranslatorASP::translateActions() const
{ {
m_ostream << "% actions"; m_outputStream << "% actions";
const auto &actions = m_description.domain().actions(); const auto &actions = m_description.domain().actions();
const auto printActionName = const auto printActionName =
[&](const auto &action) [&](const auto &action)
{ {
m_ostream << "action(" << utils::escapeASP(action.name()); m_outputStream << "action(" << utils::escapeASP(action.name());
this->translateVariablesHead(action.parameters()); this->translateVariablesHead(action.parameters());
m_ostream << ")"; m_outputStream << ")";
}; };
std::for_each(actions.cbegin(), actions.cend(), std::for_each(actions.cbegin(), actions.cend(),
@ -160,30 +160,30 @@ void TranslatorASP::translateActions() const
const auto translateLiteral = const auto translateLiteral =
[&](const auto &ruleHead, const auto &literal) [&](const auto &ruleHead, const auto &literal)
{ {
m_ostream << std::endl; m_outputStream << std::endl;
m_ostream << ruleHead << "("; m_outputStream << ruleHead << "(";
printActionName(*action); printActionName(*action);
m_ostream << ", "; m_outputStream << ", ";
this->translateLiteral(literal); this->translateLiteral(literal);
m_ostream << ") :- "; m_outputStream << ") :- ";
printActionName(*action); printActionName(*action);
m_ostream << "."; m_outputStream << ".";
}; };
m_ostream << std::endl; m_outputStream << std::endl;
// Name // Name
printActionName(*action); printActionName(*action);
this->translateVariablesBody(action->parameters()); this->translateVariablesBody(action->parameters());
m_ostream << "."; m_outputStream << ".";
// Precondition // Precondition
if (action->precondition()) if (action->precondition())
@ -237,7 +237,7 @@ void TranslatorASP::translateActions() const
} }
} }
m_ostream << std::endl; m_outputStream << std::endl;
}); });
} }
@ -245,21 +245,21 @@ void TranslatorASP::translateActions() const
void TranslatorASP::translateConstants(const std::string &header, const expressions::Constants &constants) const void TranslatorASP::translateConstants(const std::string &header, const expressions::Constants &constants) const
{ {
m_ostream << "% " << header; m_outputStream << "% " << header;
std::for_each(constants.cbegin(), constants.cend(), std::for_each(constants.cbegin(), constants.cend(),
[&](const auto &constant) [&](const auto &constant)
{ {
const auto constantName = utils::escapeASP(constant->name()); const auto constantName = utils::escapeASP(constant->name());
m_ostream << std::endl << "constant(" << constantName << ")." << std::endl; m_outputStream << std::endl << "constant(" << constantName << ")." << std::endl;
const auto *type = constant->type(); const auto *type = constant->type();
if (type != nullptr) if (type != nullptr)
m_ostream << "hasType(constant(" << constantName << "), type(" << utils::escapeASP(type->name()) << "))." << std::endl; m_outputStream << "hasType(constant(" << constantName << "), type(" << utils::escapeASP(type->name()) << "))." << std::endl;
else else
m_ostream << "hasType(constant(" << constantName << "), type(object))." << std::endl; m_outputStream << "hasType(constant(" << constantName << "), type(object))." << std::endl;
}); });
} }
@ -270,19 +270,19 @@ void TranslatorASP::translateVariablesHead(const expressions::Variables &variabl
if (variables.empty()) if (variables.empty())
return; return;
m_ostream << "("; m_outputStream << "(";
for (auto i = variables.cbegin(); i != variables.cend(); i++) for (auto i = variables.cbegin(); i != variables.cend(); i++)
{ {
if (i != variables.cbegin()) if (i != variables.cbegin())
m_ostream << ", "; m_outputStream << ", ";
const auto &variable = **i; const auto &variable = **i;
m_ostream << utils::escapeASPVariable(variable.name()); m_outputStream << utils::escapeASPVariable(variable.name());
} }
m_ostream << ")"; m_outputStream << ")";
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
@ -292,14 +292,14 @@ void TranslatorASP::translateVariablesBody(const expressions::Variables &variabl
if (variables.empty()) if (variables.empty())
return; return;
m_ostream << " :- "; m_outputStream << " :- ";
for (auto i = variables.cbegin(); i != variables.cend(); i++) for (auto i = variables.cbegin(); i != variables.cend(); i++)
{ {
const auto &variable = **i; const auto &variable = **i;
if (i != variables.cbegin()) if (i != variables.cbegin())
m_ostream << ", "; m_outputStream << ", ";
if (variable.type() != nullptr) if (variable.type() != nullptr)
{ {
@ -308,10 +308,10 @@ void TranslatorASP::translateVariablesBody(const expressions::Variables &variabl
const auto &type = *dynamic_cast<const expressions::PrimitiveType *>(variable.type()); const auto &type = *dynamic_cast<const expressions::PrimitiveType *>(variable.type());
m_ostream << "hasType(" << utils::escapeASPVariable(variable.name()) << ", type(" << type.name() << "))"; m_outputStream << "hasType(" << utils::escapeASPVariable(variable.name()) << ", type(" << type.name() << "))";
} }
else else
m_ostream << "hasType(" << utils::escapeASPVariable(variable.name()) << ", type(object))"; m_outputStream << "hasType(" << utils::escapeASPVariable(variable.name()) << ", type(object))";
} }
} }
@ -323,7 +323,7 @@ void TranslatorASP::translateLiteral(const Expression &literal) const
if (literal.expressionType() == Expression::Type::Predicate) if (literal.expressionType() == Expression::Type::Predicate)
{ {
this->translatePredicate(dynamic_cast<const expressions::Predicate &>(literal)); this->translatePredicate(dynamic_cast<const expressions::Predicate &>(literal));
m_ostream << ", true"; m_outputStream << ", true";
} }
// Assuming that "not" expression may only contain a predicate // Assuming that "not" expression may only contain a predicate
else if (literal.expressionType() == Expression::Type::Not) else if (literal.expressionType() == Expression::Type::Not)
@ -332,7 +332,7 @@ void TranslatorASP::translateLiteral(const Expression &literal) const
const auto &predicate = dynamic_cast<const expressions::Predicate &>(*notExpression.argument()); const auto &predicate = dynamic_cast<const expressions::Predicate &>(*notExpression.argument());
this->translatePredicate(predicate); this->translatePredicate(predicate);
m_ostream << ", false"; m_outputStream << ", false";
} }
else else
throw utils::TranslatorException("Only primitive predicates and their negations supported as literals currently"); throw utils::TranslatorException("Only primitive predicates and their negations supported as literals currently");
@ -342,40 +342,40 @@ void TranslatorASP::translateLiteral(const Expression &literal) const
void TranslatorASP::translatePredicate(const expressions::Predicate &predicate) const void TranslatorASP::translatePredicate(const expressions::Predicate &predicate) const
{ {
m_ostream << "predicate(" << utils::escapeASP(predicate.name()); m_outputStream << "predicate(" << utils::escapeASP(predicate.name());
const auto &arguments = predicate.arguments(); const auto &arguments = predicate.arguments();
if (arguments.empty()) if (arguments.empty())
{ {
m_ostream << ")"; m_outputStream << ")";
return; return;
} }
m_ostream << "("; m_outputStream << "(";
for (auto i = arguments.cbegin(); i != arguments.cend(); i++) for (auto i = arguments.cbegin(); i != arguments.cend(); i++)
{ {
if (i != arguments.cbegin()) if (i != arguments.cbegin())
m_ostream << ", "; m_outputStream << ", ";
if ((*i)->expressionType() == Expression::Type::Constant) if ((*i)->expressionType() == Expression::Type::Constant)
{ {
const auto &constant = dynamic_cast<const expressions::Constant &>(**i); const auto &constant = dynamic_cast<const expressions::Constant &>(**i);
m_ostream << "constant(" << utils::escapeASP(constant.name()) << ")"; m_outputStream << "constant(" << utils::escapeASP(constant.name()) << ")";
} }
else if ((*i)->expressionType() == Expression::Type::Variable) else if ((*i)->expressionType() == Expression::Type::Variable)
{ {
const auto &variable = dynamic_cast<const expressions::Variable &>(**i); const auto &variable = dynamic_cast<const expressions::Variable &>(**i);
m_ostream << utils::escapeASPVariable(variable.name()); m_outputStream << utils::escapeASPVariable(variable.name());
} }
else else
throw utils::TranslatorException("Only variables and constants supported in predicates currently"); throw utils::TranslatorException("Only variables and constants supported in predicates currently");
} }
m_ostream << "))"; m_outputStream << "))";
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
@ -384,7 +384,7 @@ void TranslatorASP::translateProblem() const
{ {
BOOST_ASSERT(m_description.containsProblem()); BOOST_ASSERT(m_description.containsProblem());
m_ostream m_outputStream
<< "%---------------------------------------" << std::endl << "%---------------------------------------" << std::endl
<< "% problem" << std::endl << "% problem" << std::endl
<< "%---------------------------------------" << std::endl; << "%---------------------------------------" << std::endl;
@ -394,16 +394,16 @@ void TranslatorASP::translateProblem() const
// Objects // Objects
if (!problem.objects().empty()) if (!problem.objects().empty())
{ {
m_ostream << std::endl; m_outputStream << std::endl;
translateConstants("objects", problem.objects()); translateConstants("objects", problem.objects());
} }
// Initial State // Initial State
m_ostream << std::endl; m_outputStream << std::endl;
translateInitialState(); translateInitialState();
// Goal // Goal
m_ostream << std::endl; m_outputStream << std::endl;
translateGoal(); translateGoal();
} }
@ -413,14 +413,14 @@ void TranslatorASP::translateInitialState() const
{ {
BOOST_ASSERT(m_description.containsProblem()); BOOST_ASSERT(m_description.containsProblem());
m_ostream << "% initial state"; m_outputStream << "% initial state";
const auto &initialStateFacts = m_description.problem().initialState().facts(); const auto &initialStateFacts = m_description.problem().initialState().facts();
std::for_each(initialStateFacts.cbegin(), initialStateFacts.cend(), std::for_each(initialStateFacts.cbegin(), initialStateFacts.cend(),
[&](const auto &fact) [&](const auto &fact)
{ {
m_ostream << std::endl << "initialState("; m_outputStream << std::endl << "initialState(";
// Translate single predicate // Translate single predicate
if (fact->expressionType() == Expression::Type::Predicate) if (fact->expressionType() == Expression::Type::Predicate)
@ -436,10 +436,10 @@ void TranslatorASP::translateInitialState() const
else else
throw utils::TranslatorException("Only predicates and their negations supported in initial state currently"); throw utils::TranslatorException("Only predicates and their negations supported in initial state currently");
m_ostream << ")."; m_outputStream << ").";
}); });
m_ostream << std::endl; m_outputStream << std::endl;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
@ -448,18 +448,18 @@ void TranslatorASP::translateGoal() const
{ {
BOOST_ASSERT(m_description.containsProblem()); BOOST_ASSERT(m_description.containsProblem());
m_ostream << "% goal"; m_outputStream << "% goal";
const auto &goal = m_description.problem().goal(); const auto &goal = m_description.problem().goal();
if (goal.expressionType() == Expression::Type::Predicate if (goal.expressionType() == Expression::Type::Predicate
|| goal.expressionType() == Expression::Type::Not) || goal.expressionType() == Expression::Type::Not)
{ {
m_ostream << std::endl << "goal("; m_outputStream << std::endl << "goal(";
translateLiteral(goal); translateLiteral(goal);
m_ostream << ")."; m_outputStream << ").";
} }
else if (goal.expressionType() == Expression::Type::And) else if (goal.expressionType() == Expression::Type::And)
{ {
@ -468,17 +468,17 @@ void TranslatorASP::translateGoal() const
std::for_each(andExpression.arguments().cbegin(), andExpression.arguments().cend(), std::for_each(andExpression.arguments().cbegin(), andExpression.arguments().cend(),
[&](const auto *argument) [&](const auto *argument)
{ {
m_ostream << std::endl << "goal("; m_outputStream << std::endl << "goal(";
this->translateLiteral(*argument); this->translateLiteral(*argument);
m_ostream << ")."; m_outputStream << ").";
}); });
} }
else else
throw utils::TranslatorException("Only single predicates, their negations, and conjunctions are currently supported in the goal"); throw utils::TranslatorException("Only single predicates, their negations, and conjunctions are currently supported in the goal");
m_ostream << std::endl; m_outputStream << std::endl;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -15,7 +15,7 @@ boost::iostreams::stream<boost::iostreams::null_sink> nullStream((boost::iostrea
TEST(PDDLTranslationTests, CheckIssues) TEST(PDDLTranslationTests, CheckIssues)
{ {
// Check that translating domains without typing information works // Check that translating domains without typing information works
const auto description = Description::fromFile("data/issues/issue-4.pddl"); auto description = Description::fromFile("data/issues/issue-4.pddl");
const auto translator = TranslatorASP(description, nullStream); const auto translator = TranslatorASP(description, description.context().logger.outputStream());
ASSERT_NO_THROW(translator.translate()); ASSERT_NO_THROW(translator.translate());
} }