From f7cd24b67a2b405d4b5039368f48c7346d3197d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Tue, 14 Jun 2016 17:35:32 +0200 Subject: [PATCH] Colorized output for SAS translator. --- CHANGELOG.md | 5 + apps/plasp-app/main.cpp | 4 +- include/plasp/sas/Debugging.h | 2 +- include/plasp/sas/Operator.h | 3 +- include/plasp/sas/Predicate.h | 5 +- include/plasp/sas/TranslatorASP.h | 5 +- include/plasp/sas/Value.h | 7 +- include/plasp/sas/Variable.h | 3 +- src/plasp/sas/Debugging.cpp | 2 +- src/plasp/sas/Operator.cpp | 9 +- src/plasp/sas/Predicate.cpp | 20 ++-- src/plasp/sas/TranslatorASP.cpp | 162 +++++++++++++++--------------- src/plasp/sas/Value.cpp | 25 ++--- src/plasp/sas/Variable.cpp | 5 +- 14 files changed, 137 insertions(+), 120 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a39d93..5687d67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/apps/plasp-app/main.cpp b/apps/plasp-app/main.cpp index d690e36..9f0bc8d 100644 --- a/apps/plasp-app/main.cpp +++ b/apps/plasp-app/main.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include 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(); } } diff --git a/include/plasp/sas/Debugging.h b/include/plasp/sas/Debugging.h index 811ec13..f3d817b 100644 --- a/include/plasp/sas/Debugging.h +++ b/include/plasp/sas/Debugging.h @@ -16,7 +16,7 @@ namespace sas // //////////////////////////////////////////////////////////////////////////////////////////////////// -std::ostream &operator <<(std::ostream &ostream, const Description &description); +utils::LogStream &operator<<(utils::LogStream &ostream, const Description &description); //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/plasp/sas/Operator.h b/include/plasp/sas/Operator.h index 2913df4..96d6a58 100644 --- a/include/plasp/sas/Operator.h +++ b/include/plasp/sas/Operator.h @@ -8,6 +8,7 @@ #include #include #include +#include #include 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; diff --git a/include/plasp/sas/Predicate.h b/include/plasp/sas/Predicate.h index 9052a46..46ab5ea 100644 --- a/include/plasp/sas/Predicate.h +++ b/include/plasp/sas/Predicate.h @@ -5,6 +5,7 @@ #include #include +#include #include namespace plasp @@ -26,8 +27,8 @@ class Predicate using Arguments = std::vector; 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; diff --git a/include/plasp/sas/TranslatorASP.h b/include/plasp/sas/TranslatorASP.h index 0a01987..96692d9 100644 --- a/include/plasp/sas/TranslatorASP.h +++ b/include/plasp/sas/TranslatorASP.h @@ -2,6 +2,7 @@ #define __PLASP__SAS__TRANSLATOR_ASP_H #include +#include #include @@ -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; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/plasp/sas/Value.h b/include/plasp/sas/Value.h index 6cdddfb..8de7423 100644 --- a/include/plasp/sas/Value.h +++ b/include/plasp/sas/Value.h @@ -5,6 +5,7 @@ #include #include +#include #include 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; diff --git a/include/plasp/sas/Variable.h b/include/plasp/sas/Variable.h index 0fcc83c..085df10 100644 --- a/include/plasp/sas/Variable.h +++ b/include/plasp/sas/Variable.h @@ -6,6 +6,7 @@ #include #include +#include #include 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; diff --git a/src/plasp/sas/Debugging.cpp b/src/plasp/sas/Debugging.cpp index 1fb2ffa..1b30d1e 100644 --- a/src/plasp/sas/Debugging.cpp +++ b/src/plasp/sas/Debugging.cpp @@ -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; diff --git a/src/plasp/sas/Operator.cpp b/src/plasp/sas/Operator.cpp index cf69f73..3d390d7 100644 --- a/src/plasp/sas/Operator.cpp +++ b/src/plasp/sas/Operator.cpp @@ -4,6 +4,7 @@ #include #include +#include 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 << ")"; } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/plasp/sas/Predicate.cpp b/src/plasp/sas/Predicate.cpp index 14ecf50..7ac6595 100644 --- a/src/plasp/sas/Predicate.cpp +++ b/src/plasp/sas/Predicate.cpp @@ -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 << ")"; } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/plasp/sas/TranslatorASP.cpp b/src/plasp/sas/TranslatorASP.cpp index d60a577..6713de5 100644 --- a/src/plasp/sas/TranslatorASP.cpp +++ b/src/plasp/sas/TranslatorASP.cpp @@ -1,5 +1,7 @@ #include +#include + 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; }); } diff --git a/src/plasp/sas/Value.cpp b/src/plasp/sas/Value.cpp index f5ad4b6..049c64a 100644 --- a/src/plasp/sas/Value.cpp +++ b/src/plasp/sas/Value.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -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 << "()"; } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/plasp/sas/Variable.cpp b/src/plasp/sas/Variable.cpp index 48dc140..6c1e8c9 100644 --- a/src/plasp/sas/Variable.cpp +++ b/src/plasp/sas/Variable.cpp @@ -2,6 +2,7 @@ #include +#include #include #include @@ -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) << ")"; } ////////////////////////////////////////////////////////////////////////////////////////////////////