Improved output format and highlighting.
This commit is contained in:
parent
9c76ce7174
commit
cdb06fa5bf
@ -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:
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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<char>(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)
|
||||
|
@ -1,57 +0,0 @@
|
||||
#ifndef __PLASP__UTILS__IO_H
|
||||
#define __PLASP__UTILS__IO_H
|
||||
|
||||
#include <boost/algorithm/string/replace.hpp>
|
||||
|
||||
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
|
@ -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<CharacterType, TraitsType> *sb);
|
||||
inline LogStream &operator<<(std::ios_base &(*func)(std::ios_base &));
|
||||
inline LogStream &operator<<(std::basic_ios<CharacterType, TraitsType> &(*func)(std::basic_ios<CharacterType, TraitsType> &));
|
||||
inline LogStream &operator<<(std::basic_ostream<CharacterType, TraitsType> &(*func)(std::basic_ostream<CharacterType, TraitsType> &));
|
||||
|
||||
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<CharacterType, TraitsType>* sb)
|
||||
{
|
||||
ostream() << sb;
|
||||
@ -283,6 +305,30 @@ inline LogStream &operator<<(LogStream &stream, const std::basic_string<Characte
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
LogStream &LogStream::operator<<(char value)
|
||||
{
|
||||
ostream() << value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
LogStream &LogStream::operator<<(signed char value)
|
||||
{
|
||||
ostream() << value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
LogStream &LogStream::operator<<(unsigned char value)
|
||||
{
|
||||
ostream() << value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,6 @@
|
||||
#include <plasp/pddl/Domain.h>
|
||||
#include <plasp/pddl/ExpressionContext.h>
|
||||
#include <plasp/pddl/expressions/Type.h>
|
||||
#include <plasp/utils/IO.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
|
@ -8,7 +8,6 @@
|
||||
#include <plasp/pddl/expressions/PredicateDeclaration.h>
|
||||
#include <plasp/pddl/expressions/PrimitiveType.h>
|
||||
#include <plasp/pddl/expressions/Variable.h>
|
||||
#include <plasp/utils/IO.h>
|
||||
#include <plasp/utils/ParserException.h>
|
||||
|
||||
namespace plasp
|
||||
|
@ -11,7 +11,6 @@
|
||||
#include <plasp/pddl/expressions/Predicate.h>
|
||||
#include <plasp/pddl/expressions/PredicateDeclaration.h>
|
||||
#include <plasp/pddl/expressions/Unsupported.h>
|
||||
#include <plasp/utils/IO.h>
|
||||
#include <plasp/utils/ParserException.h>
|
||||
|
||||
namespace plasp
|
||||
|
@ -6,7 +6,6 @@
|
||||
#include <plasp/pddl/ExpressionContext.h>
|
||||
#include <plasp/pddl/IO.h>
|
||||
#include <plasp/pddl/expressions/Constant.h>
|
||||
#include <plasp/utils/IO.h>
|
||||
#include <plasp/utils/ParserException.h>
|
||||
|
||||
namespace plasp
|
||||
|
@ -4,7 +4,6 @@
|
||||
#include <boost/assign.hpp>
|
||||
#include <boost/bimap.hpp>
|
||||
|
||||
#include <plasp/utils/IO.h>
|
||||
#include <plasp/utils/ParserException.h>
|
||||
|
||||
namespace plasp
|
||||
|
@ -6,7 +6,6 @@
|
||||
#include <plasp/pddl/expressions/Not.h>
|
||||
#include <plasp/pddl/expressions/Predicate.h>
|
||||
#include <plasp/utils/Formatting.h>
|
||||
#include <plasp/utils/IO.h>
|
||||
#include <plasp/utils/TranslatorException.h>
|
||||
|
||||
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<const expressions::PrimitiveType *>(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<const expressions::Constant &>(**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<const expressions::Variable &>(**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);
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include <limits>
|
||||
#include <sstream>
|
||||
|
||||
#include <plasp/utils/IO.h>
|
||||
#include <plasp/utils/Formatting.h>
|
||||
#include <plasp/utils/ParserException.h>
|
||||
|
||||
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 << ")";
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -4,7 +4,6 @@
|
||||
|
||||
#include <plasp/sas/Variable.h>
|
||||
#include <plasp/utils/Formatting.h>
|
||||
#include <plasp/utils/IO.h>
|
||||
#include <plasp/utils/ParserException.h>
|
||||
|
||||
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")) << ")";
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -3,7 +3,6 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <plasp/utils/Formatting.h>
|
||||
#include <plasp/utils/IO.h>
|
||||
#include <plasp/utils/ParserException.h>
|
||||
|
||||
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) << ")";
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -1,6 +1,5 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <plasp/utils/IO.h>
|
||||
#include <plasp/utils/Parser.h>
|
||||
#include <plasp/utils/ParserException.h>
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user