Colorized output for SAS translator.

This commit is contained in:
Patrick Lühne 2016-06-14 17:35:32 +02:00
parent 1e21457efb
commit f7cd24b67a
14 changed files with 137 additions and 120 deletions

View File

@ -10,6 +10,11 @@ Features:
* automatic language detection for PDDL and SAS
* new command-line option `--language` to explicitly specify input language
* new command-line option `--warning-level` to treat warnings as errors or to ignore warnings
* supports colorized output
Bug Fixes:
* fixes bug in translation of SAS axiom rules
## 3.0.0 (2016-05-24)

View File

@ -9,6 +9,7 @@
#include <plasp/pddl/TranslatorASP.h>
#include <plasp/sas/Description.h>
#include <plasp/sas/TranslatorASP.h>
#include <plasp/utils/LogStream.h>
#include <plasp/utils/TranslatorException.h>
int main(int argc, char **argv)
@ -123,8 +124,9 @@ int main(int argc, char **argv)
}
else if (language == plasp::Language::Type::SAS)
{
plasp::utils::LogStream logStream(plasp::utils::StandardStream::Out);
const auto description = plasp::sas::Description::fromParser(std::move(parser));
const auto translator = plasp::sas::TranslatorASP(description, std::cout);
const auto translator = plasp::sas::TranslatorASP(description, logStream);
translator.translate();
}
}

View File

@ -16,7 +16,7 @@ namespace sas
//
////////////////////////////////////////////////////////////////////////////////////////////////////
std::ostream &operator <<(std::ostream &ostream, const Description &description);
utils::LogStream &operator<<(utils::LogStream &ostream, const Description &description);
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -8,6 +8,7 @@
#include <plasp/sas/Effect.h>
#include <plasp/sas/Predicate.h>
#include <plasp/sas/Variable.h>
#include <plasp/utils/LogStream.h>
#include <plasp/utils/Parser.h>
namespace plasp
@ -35,7 +36,7 @@ class Operator
static Operator fromSAS(utils::Parser &parser, const Variables &variables);
public:
void printPredicateAsASP(std::ostream &ostream) const;
void printPredicateAsASP(utils::LogStream &ostream) const;
const Predicate &predicate() const;
const Conditions &preconditions() const;

View File

@ -5,6 +5,7 @@
#include <string>
#include <vector>
#include <plasp/utils/LogStream.h>
#include <plasp/utils/Parser.h>
namespace plasp
@ -26,8 +27,8 @@ class Predicate
using Arguments = std::vector<std::string>;
public:
void printAsSAS(std::ostream &ostream) const;
void printAsASP(std::ostream &ostream) const;
void printAsSAS(utils::LogStream &outputStream) const;
void printAsASP(utils::LogStream &outputStream) const;
const std::string &name() const;
const Arguments &arguments() const;

View File

@ -2,6 +2,7 @@
#define __PLASP__SAS__TRANSLATOR_ASP_H
#include <plasp/sas/Description.h>
#include <plasp/utils/LogStream.h>
#include <iosfwd>
@ -19,7 +20,7 @@ namespace sas
class TranslatorASP
{
public:
explicit TranslatorASP(const Description &description, std::ostream &ostream);
explicit TranslatorASP(const Description &description, utils::LogStream &outputStream);
void translate() const;
@ -33,7 +34,7 @@ class TranslatorASP
void translateAxiomRules() const;
const Description &m_description;
std::ostream &m_ostream;
utils::LogStream &m_outputStream;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -5,6 +5,7 @@
#include <string>
#include <vector>
#include <plasp/utils/LogStream.h>
#include <plasp/utils/Parser.h>
namespace plasp
@ -44,9 +45,9 @@ struct Value
public:
Value negated() const;
void printAsSAS(std::ostream &ostream) const;
void printAsASP(std::ostream &ostream) const;
void printAsASPPredicate(std::ostream &ostream) const;
void printAsSAS(utils::LogStream &outputStream) const;
void printAsASP(utils::LogStream &outputStream) const;
void printAsASPPredicate(utils::LogStream &outputStream) const;
Sign sign() const;
const std::string &name() const;

View File

@ -6,6 +6,7 @@
#include <vector>
#include <plasp/sas/Value.h>
#include <plasp/utils/LogStream.h>
#include <plasp/utils/Parser.h>
namespace plasp
@ -31,7 +32,7 @@ class Variable
static const Variable &referenceFromSAS(utils::Parser &parser, const Variables &variables);
public:
void printNameAsASPPredicate(std::ostream &ostream) const;
void printNameAsASPPredicate(utils::LogStream &outputStream) const;
const std::string &name() const;
int axiomLayer() const;

View File

@ -13,7 +13,7 @@ namespace sas
//
////////////////////////////////////////////////////////////////////////////////////////////////////
std::ostream &operator <<(std::ostream &ostream, const Description &description)
utils::LogStream &operator<<(utils::LogStream &ostream, const Description &description)
{
// Metric section
ostream << "uses action costs: " << (description.usesActionCosts() ? "yes" : "no") << std::endl;

View File

@ -4,6 +4,7 @@
#include <limits>
#include <plasp/sas/VariableTransition.h>
#include <plasp/utils/Formatting.h>
namespace plasp
{
@ -45,11 +46,11 @@ Operator Operator::fromSAS(utils::Parser &parser, const Variables &variables)
////////////////////////////////////////////////////////////////////////////////////////////////////
void Operator::printPredicateAsASP(std::ostream &ostream) const
void Operator::printPredicateAsASP(utils::LogStream &outputStream) const
{
ostream << "action(";
m_predicate.printAsASP(ostream);
ostream << ")";
outputStream << utils::Keyword("action") << "(";
m_predicate.printAsASP(outputStream);
outputStream << ")";
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -68,43 +68,43 @@ const Predicate::Arguments &Predicate::arguments() const
////////////////////////////////////////////////////////////////////////////////////////////////////
void Predicate::printAsSAS(std::ostream &ostream) const
void Predicate::printAsSAS(utils::LogStream &outputStream) const
{
if (m_arguments.empty())
{
ostream << m_name;
outputStream << m_name;
return;
}
for (size_t i = 0; i < m_arguments.size(); i++)
{
if (i > 0)
ostream << " ";
outputStream << " ";
ostream << m_arguments[i];
outputStream << m_arguments[i];
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Predicate::printAsASP(std::ostream &ostream) const
void Predicate::printAsASP(utils::LogStream &outputStream) const
{
if (m_arguments.empty())
{
ostream << utils::escapeASP(m_name);
outputStream << utils::escapeASP(m_name);
return;
}
ostream << utils::escapeASP(m_name) << "(";
outputStream << utils::escapeASP(m_name) << "(";
for (size_t i = 0; i < m_arguments.size(); i++)
{
if (i > 0)
ostream << ", ";
outputStream << ", ";
ostream << utils::escapeASP(m_arguments[i]);
outputStream << utils::escapeASP(m_arguments[i]);
}
ostream << ")";
outputStream << ")";
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -1,5 +1,7 @@
#include <plasp/sas/TranslatorASP.h>
#include <plasp/utils/Formatting.h>
namespace plasp
{
namespace sas
@ -11,9 +13,9 @@ namespace sas
//
////////////////////////////////////////////////////////////////////////////////////////////////////
TranslatorASP::TranslatorASP(const Description &description, std::ostream &ostream)
TranslatorASP::TranslatorASP(const Description &description, utils::LogStream &ostream)
: m_description(description),
m_ostream(ostream)
m_outputStream(ostream)
{
}
@ -34,67 +36,67 @@ void TranslatorASP::translate() const
void TranslatorASP::translateRequirements() const
{
m_ostream << "% feature requirements" << std::endl;
m_outputStream << utils::Heading2("feature requirements") << std::endl;
if (m_description.usesActionCosts())
m_ostream << "requiresFeature(actionCosts)." << std::endl;
m_outputStream << utils::Keyword("requiresFeature") << "(actionCosts)." << std::endl;
if (m_description.usesAxiomRules())
m_ostream << "requiresFeature(axiomRules)." << std::endl;
m_outputStream << utils::Keyword("requiresFeature") << "(axiomRules)." << std::endl;
if (m_description.usesConditionalEffects())
m_ostream << "requiresFeature(conditionalEffects)." << std::endl;
m_outputStream << utils::Keyword("requiresFeature") << "(conditionalEffects)." << std::endl;
m_ostream << std::endl;
m_outputStream << std::endl;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void TranslatorASP::translateInitialState() const
{
m_ostream << "% initial state" << std::endl;
m_outputStream << utils::Heading2("initial state") << std::endl;
const auto &initialStateFacts = m_description.initialState().facts();
std::for_each(initialStateFacts.cbegin(), initialStateFacts.cend(),
[&](const auto &fact)
{
m_ostream << "initialState(";
fact.variable().printNameAsASPPredicate(m_ostream);
m_ostream << ", ";
fact.value().printAsASPPredicate(m_ostream);
m_ostream << ")." << std::endl;
m_outputStream << utils::Keyword("initialState") << "(";
fact.variable().printNameAsASPPredicate(m_outputStream);
m_outputStream << ", ";
fact.value().printAsASPPredicate(m_outputStream);
m_outputStream << ")." << std::endl;
});
m_ostream << std::endl;
m_outputStream << std::endl;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void TranslatorASP::translateGoal() const
{
m_ostream << "% goal" << std::endl;
m_outputStream << utils::Heading2("goal") << std::endl;
const auto &goalFacts = m_description.goal().facts();
std::for_each(goalFacts.cbegin(), goalFacts.cend(),
[&](const auto &fact)
{
m_ostream << "goal(";
fact.variable().printNameAsASPPredicate(m_ostream);
m_ostream << ", ";
fact.value().printAsASPPredicate(m_ostream);
m_ostream << ")." << std::endl;
m_outputStream << utils::Keyword("goal") << "(";
fact.variable().printNameAsASPPredicate(m_outputStream);
m_outputStream << ", ";
fact.value().printAsASPPredicate(m_outputStream);
m_outputStream << ")." << std::endl;
});
m_ostream << std::endl;
m_outputStream << std::endl;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void TranslatorASP::translateVariables() const
{
m_ostream << "% variables";
m_outputStream << utils::Heading2("variables");
const auto &variables = m_description.variables();
@ -105,51 +107,51 @@ void TranslatorASP::translateVariables() const
BOOST_ASSERT(!values.empty());
m_ostream << std::endl;
variable.printNameAsASPPredicate(m_ostream);
m_ostream << "." << std::endl;
m_outputStream << std::endl;
variable.printNameAsASPPredicate(m_outputStream);
m_outputStream << "." << std::endl;
std::for_each(values.cbegin(), values.cend(),
[&](const auto &value)
{
m_ostream << "contains(";
variable.printNameAsASPPredicate(m_ostream);
m_ostream << ", ";
value.printAsASPPredicate(m_ostream);
m_ostream << ")." << std::endl;
m_outputStream << utils::Keyword("contains") << "(";
variable.printNameAsASPPredicate(m_outputStream);
m_outputStream << ", ";
value.printAsASPPredicate(m_outputStream);
m_outputStream << ")." << std::endl;
});
});
m_ostream << std::endl;
m_outputStream << std::endl;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void TranslatorASP::translateActions() const
{
m_ostream << "% actions";
m_outputStream << utils::Heading2("actions");
const auto &operators = m_description.operators();
std::for_each(operators.cbegin(), operators.cend(),
[&](const auto &operator_)
{
m_ostream << std::endl;
operator_.printPredicateAsASP(m_ostream);
m_ostream << "." << std::endl;
m_outputStream << std::endl;
operator_.printPredicateAsASP(m_outputStream);
m_outputStream << "." << std::endl;
const auto &preconditions = operator_.preconditions();
std::for_each(preconditions.cbegin(), preconditions.cend(),
[&](const auto &precondition)
{
m_ostream << "precondition(";
operator_.printPredicateAsASP(m_ostream);
m_ostream << ", ";
precondition.variable().printNameAsASPPredicate(m_ostream);
m_ostream << ", ";
precondition.value().printAsASPPredicate(m_ostream);
m_ostream << ")." << std::endl;
m_outputStream << utils::Keyword("precondition") << "(";
operator_.printPredicateAsASP(m_outputStream);
m_outputStream << ", ";
precondition.variable().printNameAsASPPredicate(m_outputStream);
m_outputStream << ", ";
precondition.value().printAsASPPredicate(m_outputStream);
m_outputStream << ")." << std::endl;
});
const auto &effects = operator_.effects();
@ -164,39 +166,39 @@ void TranslatorASP::translateActions() const
std::for_each(conditions.cbegin(), conditions.cend(),
[&](const auto &condition)
{
m_ostream << "effectCondition(";
operator_.printPredicateAsASP(m_ostream);
m_ostream << ", effect(" << currentEffectID << "), ";
condition.variable().printNameAsASPPredicate(m_ostream);
m_ostream << ", ";
condition.value().printAsASPPredicate(m_ostream);
m_ostream << ")." << std::endl;
m_outputStream << utils::Keyword("effectCondition") << "(";
operator_.printPredicateAsASP(m_outputStream);
m_outputStream << ", " << utils::Keyword("effect") << "(" << currentEffectID << "), ";
condition.variable().printNameAsASPPredicate(m_outputStream);
m_outputStream << ", ";
condition.value().printAsASPPredicate(m_outputStream);
m_outputStream << ")." << std::endl;
});
m_ostream << "postcondition(";
operator_.printPredicateAsASP(m_ostream);
m_ostream << ", effect(" << currentEffectID << "), ";
effect.postcondition().variable().printNameAsASPPredicate(m_ostream);
m_ostream << ", ";
effect.postcondition().value().printAsASPPredicate(m_ostream);
m_ostream << ")." << std::endl;
m_outputStream << utils::Keyword("postcondition") << "(";
operator_.printPredicateAsASP(m_outputStream);
m_outputStream << ", " << utils::Keyword("effect") << "(" << currentEffectID << "), ";
effect.postcondition().variable().printNameAsASPPredicate(m_outputStream);
m_outputStream << ", ";
effect.postcondition().value().printAsASPPredicate(m_outputStream);
m_outputStream << ")." << std::endl;
currentEffectID++;
});
m_ostream << "costs(";
operator_.printPredicateAsASP(m_ostream);
m_ostream << ", " << operator_.costs() << ")." << std::endl;
m_outputStream << utils::Keyword("costs") << "(";
operator_.printPredicateAsASP(m_outputStream);
m_outputStream << ", " << operator_.costs() << ")." << std::endl;
});
m_ostream << std::endl;
m_outputStream << std::endl;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void TranslatorASP::translateMutexes() const
{
m_ostream << "% mutex groups";
m_outputStream << utils::Heading2("mutex groups");
const auto &mutexGroups = m_description.mutexGroups();
@ -208,18 +210,18 @@ void TranslatorASP::translateMutexes() const
const auto mutexGroupID = std::to_string(currentMutexGroupID);
currentMutexGroupID++;
m_ostream << std::endl << "mutexGroup(" << mutexGroupID << ")." << std::endl;
m_outputStream << std::endl << utils::Keyword("mutexGroup") << "(" << mutexGroupID << ")." << std::endl;
const auto &facts = mutexGroup.facts();
std::for_each(facts.cbegin(), facts.cend(),
[&](const auto &fact)
{
m_ostream << "contains(mutexGroup(" << mutexGroupID << "), ";
fact.variable().printNameAsASPPredicate(m_ostream);
m_ostream << ", ";
fact.value().printAsASPPredicate(m_ostream);
m_ostream << ")." << std::endl;
m_outputStream << utils::Keyword("contains") << "(" << utils::Keyword("mutexGroup") << "(" << mutexGroupID << "), ";
fact.variable().printNameAsASPPredicate(m_outputStream);
m_outputStream << ", ";
fact.value().printAsASPPredicate(m_outputStream);
m_outputStream << ")." << std::endl;
});
});
}
@ -231,8 +233,8 @@ void TranslatorASP::translateAxiomRules() const
if (!m_description.usesActionCosts())
return;
m_ostream << std::endl;
m_ostream << "% axiom rules";
m_outputStream << std::endl;
m_outputStream << utils::Heading2("axiom rules");
const auto &axiomRules = m_description.axiomRules();
@ -244,27 +246,27 @@ void TranslatorASP::translateAxiomRules() const
const auto axiomRuleID = std::to_string(currentAxiomRuleID);
currentAxiomRuleID++;
m_ostream << std::endl << "axiomRule(" << axiomRuleID << ")." << std::endl;
m_outputStream << std::endl << utils::Keyword("axiomRule") << "(" << axiomRuleID << ")." << std::endl;
const auto &conditions = axiomRule.conditions();
std::for_each(conditions.cbegin(), conditions.cend(),
[&](const auto &condition)
{
m_ostream << "condition(axiomRule(" << axiomRuleID << "), ";
condition.variable().printNameAsASPPredicate(m_ostream);
m_ostream << ", ";
condition.value().printAsASPPredicate(m_ostream);
m_ostream << ")." << std::endl;
m_outputStream << utils::Keyword("condition") << "(" << utils::Keyword("axiomRule") << "(" << axiomRuleID << "), ";
condition.variable().printNameAsASPPredicate(m_outputStream);
m_outputStream << ", ";
condition.value().printAsASPPredicate(m_outputStream);
m_outputStream << ")." << std::endl;
});
const auto &postcondition = axiomRule.postcondition();
m_ostream << "postcondition(axiomRule(axiomRule" << axiomRuleID << "), ";
postcondition.variable().printNameAsASPPredicate(m_ostream);
m_ostream << ", ";
postcondition.value().printAsASPPredicate(m_ostream);
m_ostream << ")." << std::endl;
m_outputStream << utils::Keyword("postcondition") << "(" << utils::Keyword("axiomRule") << "(" << axiomRuleID << "), ";
postcondition.variable().printNameAsASPPredicate(m_outputStream);
m_outputStream << ", ";
postcondition.value().printAsASPPredicate(m_outputStream);
m_outputStream << ")." << std::endl;
});
}

View File

@ -3,6 +3,7 @@
#include <iostream>
#include <plasp/sas/Variable.h>
#include <plasp/utils/Formatting.h>
#include <plasp/utils/IO.h>
#include <plasp/utils/ParserException.h>
@ -127,42 +128,42 @@ const std::string &Value::name() const
////////////////////////////////////////////////////////////////////////////////////////////////////
void Value::printAsASP(std::ostream &ostream) const
void Value::printAsASP(utils::LogStream &outputStream) const
{
if (m_sign == Value::Sign::Negative)
ostream << "not ";
outputStream << utils::Keyword("not") << " ";
ostream << utils::escapeASP(m_name);
outputStream << utils::escapeASP(m_name);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Value::printAsASPPredicate(std::ostream &ostream) const
void Value::printAsASPPredicate(utils::LogStream &outputStream) const
{
// TODO: do not compare by value
if (*this == Value::None)
{
ostream << "value(none)";
outputStream << utils::Keyword("value") << "(" << utils::Keyword("none") << ")";
return;
}
ostream << "value(" << utils::escapeASP(m_name) << ", "
<< (m_sign == Sign::Positive ? "true" : "false") << ")";
outputStream << utils::Keyword("value") << "(" << utils::escapeASP(m_name) << ", "
<< (m_sign == Sign::Positive ? utils::Keyword("true") : utils::Keyword("false")) << ")";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Value::printAsSAS(std::ostream &ostream) const
void Value::printAsSAS(utils::LogStream &outputStream) const
{
if (m_sign == Value::Sign::Positive)
ostream << "Atom ";
outputStream << "Atom ";
else
ostream << "NegatedAtom ";
outputStream << "NegatedAtom ";
ostream << m_name;
outputStream << m_name;
if (!m_hasArguments)
ostream << "()";
outputStream << "()";
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -2,6 +2,7 @@
#include <iostream>
#include <plasp/utils/Formatting.h>
#include <plasp/utils/IO.h>
#include <plasp/utils/ParserException.h>
@ -51,9 +52,9 @@ Variable Variable::fromSAS(utils::Parser &parser)
////////////////////////////////////////////////////////////////////////////////////////////////////
void Variable::printNameAsASPPredicate(std::ostream &ostream) const
void Variable::printNameAsASPPredicate(utils::LogStream &outputStream) const
{
ostream << "variable(" << utils::escapeASP(m_name) << ")";
outputStream << utils::Keyword("variable") << "(" << utils::escapeASP(m_name) << ")";
}
////////////////////////////////////////////////////////////////////////////////////////////////////