From cdb06fa5bf988d8c5b13134dbb7b626ae7742c54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Tue, 16 Aug 2016 18:35:55 +0200 Subject: [PATCH] Improved output format and highlighting. --- CHANGELOG.md | 2 + include/plasp/sas/Value.h | 1 - include/plasp/utils/Formatting.h | 110 +++++++++++++++++++++++- include/plasp/utils/IO.h | 57 ------------- include/plasp/utils/LogStream.h | 46 ++++++++++ src/plasp/pddl/Action.cpp | 1 - src/plasp/pddl/Domain.cpp | 1 - src/plasp/pddl/Expression.cpp | 1 - src/plasp/pddl/Problem.cpp | 1 - src/plasp/pddl/Requirement.cpp | 1 - src/plasp/pddl/TranslatorASP.cpp | 139 ++++++++++++++++--------------- src/plasp/sas/Predicate.cpp | 13 +-- src/plasp/sas/TranslatorASP.cpp | 40 ++++----- src/plasp/sas/Value.cpp | 17 +--- src/plasp/sas/Variable.cpp | 4 +- tests/TestUtils.cpp | 15 ---- 16 files changed, 257 insertions(+), 192 deletions(-) delete mode 100644 include/plasp/utils/IO.h diff --git a/CHANGELOG.md b/CHANGELOG.md index f3b29de..16b7248 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ Features: * unified translation format for SAS and PDDL files * documentation of `plasp`’s output format +* improved output syntax highlighting +* uses ASP string literals to avoid escaping PDDL identifiers Bug Fixes: diff --git a/include/plasp/sas/Value.h b/include/plasp/sas/Value.h index cd92e64..5cc1df4 100644 --- a/include/plasp/sas/Value.h +++ b/include/plasp/sas/Value.h @@ -46,7 +46,6 @@ struct Value Value negated() const; void printAsSAS(utils::LogStream &outputStream) const; - void printAsASP(utils::LogStream &outputStream) const; void printAsASPPredicate(utils::LogStream &outputStream) const; Sign sign() const; diff --git a/include/plasp/utils/Formatting.h b/include/plasp/utils/Formatting.h index 90c6a2e..35034cf 100644 --- a/include/plasp/utils/Formatting.h +++ b/include/plasp/utils/Formatting.h @@ -93,6 +93,26 @@ struct Token //////////////////////////////////////////////////////////////////////////////////////////////////// +struct RuleName: public Token +{ + RuleName(const std::string &name) + : Token(name) + { + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline LogStream &operator<<(LogStream &stream, const RuleName &keyword) +{ + return (stream + << utils::Format(utils::Color::White, utils::FontWeight::Bold) + << keyword.name + << utils::ResetFormat()); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + struct Keyword: public Token { Keyword(const std::string &name) @@ -106,11 +126,10 @@ struct Keyword: public Token inline LogStream &operator<<(LogStream &stream, const Keyword &keyword) { return (stream - << utils::Format(utils::Color::White, utils::FontWeight::Bold) + << utils::Format(utils::Color::Blue, utils::FontWeight::Normal) << keyword.name << utils::ResetFormat()); } - //////////////////////////////////////////////////////////////////////////////////////////////////// struct Number: public Token @@ -126,7 +145,7 @@ struct Number: public Token inline LogStream &operator<<(LogStream &stream, const Number &number) { return (stream - << utils::Format(utils::Color::Yellow, utils::FontWeight::Bold) + << utils::Format(utils::Color::Yellow, utils::FontWeight::Normal) << number.name << utils::ResetFormat()); } @@ -153,6 +172,91 @@ inline LogStream &operator<<(LogStream &stream, const Variable &variable) //////////////////////////////////////////////////////////////////////////////////////////////////// +struct ASPVariable: public Token +{ + ASPVariable(const std::string &name) + : Token(name) + { + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline LogStream &operator<<(LogStream &stream, const ASPVariable &aspVariable) +{ + if (aspVariable.name.empty()) + return stream; + + // TODO: check that char cast is safe + return (stream + << utils::Format(utils::Color::Green, utils::FontWeight::Bold) + << static_cast(std::toupper(aspVariable.name.front())) + << aspVariable.name.c_str() + 1 + << utils::ResetFormat()); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +struct String: public Token +{ + String(const std::string &name) + : Token(name) + { + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline LogStream &operator<<(LogStream &stream, const String &string) +{ + return (stream + << utils::Format(utils::Color::Green, utils::FontWeight::Normal) + << "\"" << string.name << "\"" + << utils::ResetFormat()); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +struct Boolean: public Token +{ + Boolean(const std::string &name) + : Token(name) + { + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline LogStream &operator<<(LogStream &stream, const Boolean &string) +{ + return (stream + << utils::Format(utils::Color::Red, utils::FontWeight::Normal) + << string.name + << utils::ResetFormat()); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +struct Reserved: public Token +{ + Reserved(const std::string &name) + : Token(name) + { + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline LogStream &operator<<(LogStream &stream, const Reserved &string) +{ + return (stream + << utils::Format(utils::Color::White, utils::FontWeight::Normal) + << string.name + << utils::ResetFormat()); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + struct Heading1: public Token { Heading1(const std::string &name) diff --git a/include/plasp/utils/IO.h b/include/plasp/utils/IO.h deleted file mode 100644 index 75005f7..0000000 --- a/include/plasp/utils/IO.h +++ /dev/null @@ -1,57 +0,0 @@ -#ifndef __PLASP__UTILS__IO_H -#define __PLASP__UTILS__IO_H - -#include - -namespace plasp -{ -namespace utils -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// IO -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -inline std::string escapeASP(const std::string &string) -{ - auto escaped = string; - - boost::replace_all(escaped, "_", "__"); - boost::replace_all(escaped, "-", "_h"); - boost::replace_all(escaped, "@", "_a"); - - return escaped; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -inline std::string unescapeASP(const std::string &string) -{ - auto unescaped = string; - - boost::replace_all(unescaped, "_a", "@"); - boost::replace_all(unescaped, "_h", "-"); - boost::replace_all(unescaped, "__", "_"); - - return unescaped; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -inline std::string escapeASPVariable(const std::string &string) -{ - auto escaped = escapeASP(string); - - escaped.front() = std::toupper(escaped.front()); - - return escaped; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} - -#endif diff --git a/include/plasp/utils/LogStream.h b/include/plasp/utils/LogStream.h index e44ceab..ce8434a 100644 --- a/include/plasp/utils/LogStream.h +++ b/include/plasp/utils/LogStream.h @@ -118,11 +118,17 @@ class LogStream inline LogStream &operator<<(bool value); inline LogStream &operator<<(const void *value); inline LogStream &operator<<(const char *value); + inline LogStream &operator<<(const signed char *value); + inline LogStream &operator<<(const unsigned char *value); inline LogStream &operator<<(std::basic_streambuf *sb); inline LogStream &operator<<(std::ios_base &(*func)(std::ios_base &)); inline LogStream &operator<<(std::basic_ios &(*func)(std::basic_ios &)); inline LogStream &operator<<(std::basic_ostream &(*func)(std::basic_ostream &)); + inline LogStream &operator<<(char value); + inline LogStream &operator<<(signed char value); + inline LogStream &operator<<(unsigned char value); + private: StandardStream m_standardStream; ColorPolicy m_colorPolicy; @@ -242,6 +248,22 @@ LogStream &LogStream::operator<<(const char *value) //////////////////////////////////////////////////////////////////////////////////////////////////// +LogStream &LogStream::operator<<(const signed char *value) +{ + ostream() << value; + return *this; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +LogStream &LogStream::operator<<(const unsigned char *value) +{ + ostream() << value; + return *this; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + LogStream &LogStream::operator<<(std::basic_streambuf* sb) { ostream() << sb; @@ -283,6 +305,30 @@ inline LogStream &operator<<(LogStream &stream, const std::basic_string #include #include -#include namespace plasp { diff --git a/src/plasp/pddl/Domain.cpp b/src/plasp/pddl/Domain.cpp index f8e8513..98427af 100644 --- a/src/plasp/pddl/Domain.cpp +++ b/src/plasp/pddl/Domain.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include namespace plasp diff --git a/src/plasp/pddl/Expression.cpp b/src/plasp/pddl/Expression.cpp index af9b59b..0e3be3b 100644 --- a/src/plasp/pddl/Expression.cpp +++ b/src/plasp/pddl/Expression.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include namespace plasp diff --git a/src/plasp/pddl/Problem.cpp b/src/plasp/pddl/Problem.cpp index 90b87e2..7aecb00 100644 --- a/src/plasp/pddl/Problem.cpp +++ b/src/plasp/pddl/Problem.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include namespace plasp diff --git a/src/plasp/pddl/Requirement.cpp b/src/plasp/pddl/Requirement.cpp index 74a7994..75e61eb 100644 --- a/src/plasp/pddl/Requirement.cpp +++ b/src/plasp/pddl/Requirement.cpp @@ -4,7 +4,6 @@ #include #include -#include #include namespace plasp diff --git a/src/plasp/pddl/TranslatorASP.cpp b/src/plasp/pddl/TranslatorASP.cpp index 801429b..9120370 100644 --- a/src/plasp/pddl/TranslatorASP.cpp +++ b/src/plasp/pddl/TranslatorASP.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include namespace plasp @@ -86,8 +85,8 @@ void TranslatorASP::translateTypes() const if (types.empty()) { m_outputStream - << utils::Keyword("type") << "(" - << utils::Keyword("type") << "(object))." << std::endl; + << utils::RuleName("type") << "(" + << utils::Keyword("type") << "(" << utils::String("object") << ")." << std::endl; return; } @@ -95,31 +94,36 @@ void TranslatorASP::translateTypes() const std::for_each(types.cbegin(), types.cend(), [&](const auto &type) { - const auto typeName = utils::escapeASP(type->name()); - m_outputStream + << utils::RuleName("type") << "(" << utils::Keyword("type") << "(" - << utils::Keyword("type") << "(" - << typeName << "))." << std::endl; + << utils::String(type->name()) + << "))." << std::endl; const auto &parentTypes = type->parentTypes(); std::for_each(parentTypes.cbegin(), parentTypes.cend(), [&](const auto &parentType) { - const auto parentTypeName = utils::escapeASP(parentType->name()); - m_outputStream - << utils::Keyword("inherits") << "(" << utils::Keyword("type") - << "(" << typeName << "), " << utils::Keyword("type") - << "(" << parentTypeName << "))." << std::endl - - << utils::Keyword("has") << "(" << utils::Variable("X") << ", " - << utils::Keyword("type") << "(" << parentTypeName << ")) :- " - << utils::Keyword("has") << "(" << utils::Variable("X") << ", " - << utils::Keyword("type") << "(" << typeName << "))." << std::endl; + << utils::RuleName("inherits") << "(" << utils::Keyword("type") + << "(" << utils::String(type->name()) << "), " << utils::Keyword("type") + << "(" << utils::String(parentType->name()) << "))." << std::endl; }); }); + + m_outputStream + << std::endl + << utils::RuleName("has") << "(" + << utils::Variable("X") << ", " + << utils::Keyword("type") << "(" << utils::Variable("T2") << ")) :- " + << utils::RuleName("has") << "(" + << utils::Variable("X") << ", " + << utils::Keyword("type") << "(" << utils::Variable("T1") << ")), " + << utils::RuleName("inherits") << "(" + << utils::Keyword("type") << "(" << utils::Variable("T1") << "), " + << utils::Keyword("type") << "(" << utils::Variable("T2") << "))." + << std::endl; } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -133,17 +137,23 @@ void TranslatorASP::translatePredicates() const const auto printPredicateName = [&](const auto &predicate) { - m_outputStream - << utils::escapeASP(predicate->name()); + if (predicate->arguments().empty()) + { + m_outputStream << utils::String(predicate->name()); + return; + } + + m_outputStream << "(" << utils::String(predicate->name()); this->translateVariablesHead(predicate->arguments()); + m_outputStream << ")"; }; const auto printValueRule = [&](const auto &predicate, const auto &value) { m_outputStream - << utils::Keyword("contains") << "(" + << utils::RuleName("contains") << "(" << utils::Keyword("variable") << "("; printPredicateName(predicate); @@ -153,7 +163,7 @@ void TranslatorASP::translatePredicates() const printPredicateName(predicate); - m_outputStream << ", " << utils::Keyword(value) << "))"; + m_outputStream << ", " << utils::Boolean(value) << "))"; this->translateVariablesBody(predicate->arguments()); @@ -165,7 +175,7 @@ void TranslatorASP::translatePredicates() const { m_outputStream << std::endl - << utils::Keyword("variable") << "(" + << utils::RuleName("variable") << "(" << utils::Keyword("variable") << "("; printPredicateName(predicate); @@ -192,11 +202,18 @@ void TranslatorASP::translateActions() const const auto printActionName = [&](const auto &action) { - m_outputStream << utils::Keyword("action") << "(" << utils::escapeASP(action.name()); + m_outputStream << utils::Keyword("action") << "("; + if (action.parameters().empty()) + { + m_outputStream << utils::String(action.name()) << ")"; + + return; + } + + m_outputStream << "(" << utils::String(action.name()); this->translateVariablesHead(action.parameters()); - - m_outputStream << ")"; + m_outputStream << "))"; }; std::for_each(actions.cbegin(), actions.cend(), @@ -206,19 +223,19 @@ void TranslatorASP::translateActions() const const auto translateLiteral = [&](const auto &ruleHead, const auto &literal, bool enumerateEffects = false) { - m_outputStream << std::endl << utils::Keyword(ruleHead) << "("; + m_outputStream << std::endl << utils::RuleName(ruleHead) << "("; printActionName(*action); // TODO: implement conditional effects if (enumerateEffects) - m_outputStream << ", " << utils::Keyword("effect") << "(" << utils::Keyword("unconditional") << ")"; + m_outputStream << ", " << utils::Keyword("effect") << "(" << utils::Reserved("unconditional") << ")"; m_outputStream << ", "; this->translateLiteral(literal); - m_outputStream << ") :- " << utils::Keyword("action") << "("; + m_outputStream << ") :- " << utils::RuleName("action") << "("; printActionName(*action); @@ -228,7 +245,7 @@ void TranslatorASP::translateActions() const m_outputStream << std::endl; // Name - m_outputStream << utils::Keyword("action") << "("; + m_outputStream << utils::RuleName("action") << "("; printActionName(*action); m_outputStream << ")"; @@ -301,27 +318,25 @@ void TranslatorASP::translateConstants(const std::string &heading, const express std::for_each(constants.cbegin(), constants.cend(), [&](const auto &constant) { - const auto constantName = utils::escapeASP(constant->name()); - m_outputStream << std::endl + << utils::RuleName("constant") << "(" << utils::Keyword("constant") << "(" - << utils::Keyword("constant") << "(" - << constantName + << utils::String(constant->name()) << "))." << std::endl; const auto *type = constant->type(); if (type != nullptr) { - m_outputStream << utils::Keyword("has") << "(" - << utils::Keyword("constant") << "(" << constantName << "), " - << utils::Keyword("type") << "(" << utils::escapeASP(type->name()) << "))." << std::endl; + m_outputStream << utils::RuleName("has") << "(" + << utils::Keyword("constant") << "(" << utils::String(constant->name()) << "), " + << utils::Keyword("type") << "(" << utils::String(type->name()) << "))." << std::endl; } else { - m_outputStream << utils::Keyword("has") << "(" - << utils::Keyword("constant") << "(" << constantName << "), " - << utils::Keyword("type") << "(object))." << std::endl; + m_outputStream << utils::RuleName("has") << "(" + << utils::Keyword("constant") << "(" << utils::String(constant->name()) << "), " + << utils::Keyword("type") << "(" << utils::String("object") << "))." << std::endl; } }); } @@ -333,19 +348,12 @@ void TranslatorASP::translateVariablesHead(const expressions::Variables &variabl if (variables.empty()) return; - m_outputStream << "("; - for (auto i = variables.cbegin(); i != variables.cend(); i++) { - if (i != variables.cbegin()) - m_outputStream << ", "; - const auto &variable = **i; - m_outputStream << utils::Variable(utils::escapeASPVariable(variable.name())); + m_outputStream << ", " << utils::ASPVariable(variable.name()); } - - m_outputStream << ")"; } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -371,15 +379,15 @@ void TranslatorASP::translateVariablesBody(const expressions::Variables &variabl const auto &type = *dynamic_cast(variable.type()); - m_outputStream << utils::Keyword("has") << "(" - << utils::Variable(utils::escapeASPVariable(variable.name())) << ", " - << utils::Keyword("type") << "(" << utils::escapeASP(type.name()) << "))"; + m_outputStream << utils::RuleName("has") << "(" + << utils::ASPVariable(variable.name()) << ", " + << utils::Keyword("type") << "(" << utils::String(type.name()) << "))"; } else { - m_outputStream << utils::Keyword("has") << "(" - << utils::Variable(utils::escapeASPVariable(variable.name())) << ", " - << utils::Keyword("type") << "(object))"; + m_outputStream << utils::RuleName("has") << "(" + << utils::ASPVariable(variable.name()) << ", " + << utils::Keyword("type") << "(" << utils::String("object") << "))"; } } } @@ -397,7 +405,7 @@ void TranslatorASP::translateLiteral(const Expression &literal) const this->translatePredicate(predicate); m_outputStream << "), " << utils::Keyword("value") << "("; this->translatePredicate(predicate); - m_outputStream << ", " << utils::Keyword("true") << ")"; + m_outputStream << ", " << utils::Boolean("true") << ")"; } // Assuming that "not" expression may only contain a predicate else if (literal.expressionType() == Expression::Type::Not) @@ -409,7 +417,7 @@ void TranslatorASP::translateLiteral(const Expression &literal) const this->translatePredicate(predicate); m_outputStream << "), " << utils::Keyword("value") << "("; this->translatePredicate(predicate); - m_outputStream << ", " << utils::Keyword("false") << ")"; + m_outputStream << ", " << utils::Boolean("false") << ")"; } else throw utils::TranslatorException("only primitive predicates and their negations supported as literals currently"); @@ -419,33 +427,32 @@ void TranslatorASP::translateLiteral(const Expression &literal) const void TranslatorASP::translatePredicate(const expressions::Predicate &predicate) const { - m_outputStream << utils::escapeASP(predicate.name()); - const auto &arguments = predicate.arguments(); if (arguments.empty()) { + m_outputStream << utils::String(predicate.name()); + return; } - m_outputStream << "("; + m_outputStream << "(" << utils::String(predicate.name()); for (auto i = arguments.cbegin(); i != arguments.cend(); i++) { - if (i != arguments.cbegin()) - m_outputStream << ", "; + m_outputStream << ", "; if ((*i)->expressionType() == Expression::Type::Constant) { const auto &constant = dynamic_cast(**i); - m_outputStream << utils::Keyword("constant") << "(" << utils::escapeASP(constant.name()) << ")"; + m_outputStream << utils::Keyword("constant") << "(" << utils::String(constant.name()) << ")"; } else if ((*i)->expressionType() == Expression::Type::Variable) { const auto &variable = dynamic_cast(**i); - m_outputStream << utils::Variable(utils::escapeASPVariable(variable.name())); + m_outputStream << utils::ASPVariable(variable.name()); } else throw utils::TranslatorException("only variables and constants supported in predicates currently"); @@ -493,7 +500,7 @@ void TranslatorASP::translateInitialState() const std::for_each(initialStateFacts.cbegin(), initialStateFacts.cend(), [&](const auto &fact) { - m_outputStream << std::endl << utils::Keyword("initialState") << "("; + m_outputStream << std::endl << utils::RuleName("initialState") << "("; // Translate single predicate if (fact->expressionType() == Expression::Type::Predicate) @@ -504,7 +511,7 @@ void TranslatorASP::translateInitialState() const this->translatePredicate(predicate); m_outputStream << "), " << utils::Keyword("value") << "("; this->translatePredicate(predicate); - m_outputStream << ", " << utils::Keyword("true") << ")"; + m_outputStream << ", " << utils::Boolean("true") << ")"; } // Assuming that "not" expression may only contain a predicate else if (fact->expressionType() == Expression::Type::Not) @@ -536,7 +543,7 @@ void TranslatorASP::translateGoal() const if (goal.expressionType() == Expression::Type::Predicate || goal.expressionType() == Expression::Type::Not) { - m_outputStream << std::endl << utils::Keyword("goal") << "("; + m_outputStream << std::endl << utils::RuleName("goal") << "("; translateLiteral(goal); @@ -549,7 +556,7 @@ void TranslatorASP::translateGoal() const std::for_each(andExpression.arguments().cbegin(), andExpression.arguments().cend(), [&](const auto *argument) { - m_outputStream << std::endl << utils::Keyword("goal") << "("; + m_outputStream << std::endl << utils::RuleName("goal") << "("; this->translateLiteral(*argument); diff --git a/src/plasp/sas/Predicate.cpp b/src/plasp/sas/Predicate.cpp index d4b802e..8860ff5 100644 --- a/src/plasp/sas/Predicate.cpp +++ b/src/plasp/sas/Predicate.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include #include namespace plasp @@ -91,19 +91,14 @@ void Predicate::printAsASP(utils::LogStream &outputStream) const { if (m_arguments.empty()) { - outputStream << utils::escapeASP(m_name); + outputStream << utils::String(m_name); return; } - outputStream << utils::escapeASP(m_name) << "("; + outputStream << "(" << utils::String(m_name); for (size_t i = 0; i < m_arguments.size(); i++) - { - if (i > 0) - outputStream << ", "; - - outputStream << utils::escapeASP(m_arguments[i]); - } + outputStream << ", " << utils::String(m_arguments[i]); outputStream << ")"; } diff --git a/src/plasp/sas/TranslatorASP.cpp b/src/plasp/sas/TranslatorASP.cpp index 63eb9b5..09e96b5 100644 --- a/src/plasp/sas/TranslatorASP.cpp +++ b/src/plasp/sas/TranslatorASP.cpp @@ -55,13 +55,13 @@ void TranslatorASP::translateRequirements() const m_outputStream << utils::Heading2("feature requirements") << std::endl; if (m_description.usesActionCosts()) - m_outputStream << utils::Keyword("requires") << "(" << utils::Keyword("feature") << "(actionCosts))." << std::endl; + m_outputStream << utils::RuleName("requires") << "(" << utils::Keyword("feature") << "(" << utils::Reserved("actionCosts") << "))." << std::endl; if (m_description.usesAxiomRules()) - m_outputStream << utils::Keyword("requires") << "(" << utils::Keyword("feature") << "(axiomRules))." << std::endl; + m_outputStream << utils::RuleName("requires") << "(" << utils::Keyword("feature") << "(" << utils::Reserved("axiomRules") << "))." << std::endl; if (m_description.usesConditionalEffects()) - m_outputStream << utils::Keyword("requires") << "(" << utils::Keyword("feature") << "(conditionalEffects))." << std::endl; + m_outputStream << utils::RuleName("requires") << "(" << utils::Keyword("feature") << "(" << utils::Reserved("conditionalEffects") << "))." << std::endl; } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -75,7 +75,7 @@ void TranslatorASP::translateInitialState() const std::for_each(initialStateFacts.cbegin(), initialStateFacts.cend(), [&](const auto &fact) { - m_outputStream << utils::Keyword("initialState") << "("; + m_outputStream << utils::RuleName("initialState") << "("; fact.variable().printNameAsASPPredicate(m_outputStream); m_outputStream << ", "; fact.value().printAsASPPredicate(m_outputStream); @@ -94,7 +94,7 @@ void TranslatorASP::translateGoal() const std::for_each(goalFacts.cbegin(), goalFacts.cend(), [&](const auto &fact) { - m_outputStream << utils::Keyword("goal") << "("; + m_outputStream << utils::RuleName("goal") << "("; fact.variable().printNameAsASPPredicate(m_outputStream); m_outputStream << ", "; fact.value().printAsASPPredicate(m_outputStream); @@ -117,14 +117,14 @@ void TranslatorASP::translateVariables() const BOOST_ASSERT(!values.empty()); - m_outputStream << std::endl << utils::Keyword("variable") << "("; + m_outputStream << std::endl << utils::RuleName("variable") << "("; variable.printNameAsASPPredicate(m_outputStream); m_outputStream << ")." << std::endl; std::for_each(values.cbegin(), values.cend(), [&](const auto &value) { - m_outputStream << utils::Keyword("contains") << "("; + m_outputStream << utils::RuleName("contains") << "("; variable.printNameAsASPPredicate(m_outputStream); m_outputStream << ", "; value.printAsASPPredicate(m_outputStream); @@ -146,7 +146,7 @@ void TranslatorASP::translateActions() const std::for_each(operators.cbegin(), operators.cend(), [&](const auto &operator_) { - m_outputStream << std::endl << utils::Keyword("action") << "("; + m_outputStream << std::endl << utils::RuleName("action") << "("; operator_.printPredicateAsASP(m_outputStream); m_outputStream << ")." << std::endl; @@ -155,7 +155,7 @@ void TranslatorASP::translateActions() const std::for_each(preconditions.cbegin(), preconditions.cend(), [&](const auto &precondition) { - m_outputStream << utils::Keyword("precondition") << "("; + m_outputStream << utils::RuleName("precondition") << "("; operator_.printPredicateAsASP(m_outputStream); m_outputStream << ", "; precondition.variable().printNameAsASPPredicate(m_outputStream); @@ -175,7 +175,7 @@ void TranslatorASP::translateActions() const [&](const auto &condition) { // Conditions of conditional effects - m_outputStream << utils::Keyword("precondition") << "("; + m_outputStream << utils::RuleName("precondition") << "("; operator_.printPredicateAsASP(m_outputStream); m_outputStream << ", " << utils::Keyword("effect") << "(" << utils::Number(std::to_string(currentEffectID)) << "), "; condition.variable().printNameAsASPPredicate(m_outputStream); @@ -184,11 +184,11 @@ void TranslatorASP::translateActions() const m_outputStream << ")." << std::endl; }); - m_outputStream << utils::Keyword("postcondition") << "("; + m_outputStream << utils::RuleName("postcondition") << "("; operator_.printPredicateAsASP(m_outputStream); if (conditions.empty()) - m_outputStream << ", " << utils::Keyword("effect") << "(" << utils::Keyword("unconditional") << "), "; + m_outputStream << ", " << utils::Keyword("effect") << "(" << utils::Reserved("unconditional") << "), "; else { m_outputStream << ", " << utils::Keyword("effect") << "(" << utils::Number(std::to_string(currentEffectID)) << "), "; @@ -201,9 +201,9 @@ void TranslatorASP::translateActions() const m_outputStream << ")." << std::endl; }); - m_outputStream << utils::Keyword("costs") << "("; + m_outputStream << utils::RuleName("costs") << "("; operator_.printPredicateAsASP(m_outputStream); - m_outputStream << ", " << operator_.costs() << ")." << std::endl; + m_outputStream << ", " << utils::Number(std::to_string(operator_.costs())) << ")." << std::endl; }); } @@ -225,7 +225,7 @@ void TranslatorASP::translateMutexes() const m_outputStream << std::endl - << utils::Keyword("mutexGroup") << "(" + << utils::RuleName("mutexGroup") << "(" << utils::Keyword("mutexGroup") << "(" << utils::Number(mutexGroupID) << "))." << std::endl; @@ -235,7 +235,7 @@ void TranslatorASP::translateMutexes() const std::for_each(facts.cbegin(), facts.cend(), [&](const auto &fact) { - m_outputStream << utils::Keyword("contains") << "(" << utils::Keyword("mutexGroup") << "(" << utils::Number(mutexGroupID) << "), "; + m_outputStream << utils::RuleName("contains") << "(" << utils::Keyword("mutexGroup") << "(" << utils::Number(mutexGroupID) << "), "; fact.variable().printNameAsASPPredicate(m_outputStream); m_outputStream << ", "; fact.value().printAsASPPredicate(m_outputStream); @@ -262,7 +262,7 @@ void TranslatorASP::translateAxiomRules() const m_outputStream << std::endl - << utils::Keyword("axiomRule") << "(" + << utils::RuleName("axiomRule") << "(" << utils::Keyword("axiomRule") << "(" << utils::Number(axiomRuleID) << "))." << std::endl; @@ -273,7 +273,7 @@ void TranslatorASP::translateAxiomRules() const [&](const auto &condition) { m_outputStream - << utils::Keyword("precondition") << "(" + << utils::RuleName("precondition") << "(" << utils::Keyword("axiomRule") << "(" << utils::Number(axiomRuleID) << "), "; condition.variable().printNameAsASPPredicate(m_outputStream); m_outputStream << ", "; @@ -284,9 +284,9 @@ void TranslatorASP::translateAxiomRules() const const auto &postcondition = axiomRule.postcondition(); m_outputStream - << utils::Keyword("postcondition") << "(" + << utils::RuleName("postcondition") << "(" << utils::Keyword("axiomRule") << "(" << utils::Number(axiomRuleID) << "), " - << utils::Keyword("effect") << "(" << utils::Keyword("unconditional") << "), "; + << utils::Keyword("effect") << "(" << utils::Reserved("unconditional") << "), "; postcondition.variable().printNameAsASPPredicate(m_outputStream); m_outputStream << ", "; postcondition.value().printAsASPPredicate(m_outputStream); diff --git a/src/plasp/sas/Value.cpp b/src/plasp/sas/Value.cpp index 346e29e..c266008 100644 --- a/src/plasp/sas/Value.cpp +++ b/src/plasp/sas/Value.cpp @@ -4,7 +4,6 @@ #include #include -#include #include namespace plasp @@ -128,27 +127,17 @@ const std::string &Value::name() const //////////////////////////////////////////////////////////////////////////////////////////////////// -void Value::printAsASP(utils::LogStream &outputStream) const -{ - if (m_sign == Value::Sign::Negative) - outputStream << utils::Keyword("not") << " "; - - outputStream << utils::escapeASP(m_name); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - void Value::printAsASPPredicate(utils::LogStream &outputStream) const { // TODO: do not compare by value if (*this == Value::None) { - outputStream << utils::Keyword("value") << "(" << utils::Keyword("none") << ")"; + outputStream << utils::Keyword("value") << "(" << utils::Reserved("none") << ")"; return; } - outputStream << utils::Keyword("value") << "(" << utils::escapeASP(m_name) << ", " - << (m_sign == Sign::Positive ? utils::Keyword("true") : utils::Keyword("false")) << ")"; + outputStream << utils::Keyword("value") << "(" << utils::String(m_name) << ", " + << (m_sign == Sign::Positive ? utils::Boolean("true") : utils::Boolean("false")) << ")"; } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/plasp/sas/Variable.cpp b/src/plasp/sas/Variable.cpp index 16a2a2d..70542b8 100644 --- a/src/plasp/sas/Variable.cpp +++ b/src/plasp/sas/Variable.cpp @@ -3,7 +3,6 @@ #include #include -#include #include namespace plasp @@ -55,7 +54,8 @@ Variable Variable::fromSAS(utils::Parser<> &parser) void Variable::printNameAsASPPredicate(utils::LogStream &outputStream) const { - outputStream << utils::Keyword("variable") << "(" << utils::Number(utils::escapeASP(m_name)) << ")"; + // TODO: assert that name is a number indeed + outputStream << utils::Keyword("variable") << "(" << utils::Number(m_name) << ")"; } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/tests/TestUtils.cpp b/tests/TestUtils.cpp index de3ad48..a363bef 100644 --- a/tests/TestUtils.cpp +++ b/tests/TestUtils.cpp @@ -1,6 +1,5 @@ #include -#include #include #include @@ -331,17 +330,3 @@ TEST(UtilsTests, ParserRemoveComments) ASSERT_TRUE(p3.atEnd()); } - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -TEST(UtilsTests, EscapeASP) -{ - const std::string predicate = "action(stack_on(block-1, block-2, value@3, value@4))"; - - const auto escaped = plasp::utils::escapeASP(predicate); - const auto unescaped = plasp::utils::unescapeASP(escaped); - - ASSERT_EQ(escaped.find("-"), std::string::npos); - ASSERT_EQ(escaped.find("@"), std::string::npos); - ASSERT_EQ(predicate, unescaped); -}