Refactored color output of variables and keywords.
This commit is contained in:
parent
6fec1cc409
commit
9969281b11
@ -65,7 +65,7 @@ inline LogStream &operator<<(LogStream &stream, const Format &format)
|
|||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
class ResetFormat
|
struct ResetFormat
|
||||||
{
|
{
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -81,6 +81,50 @@ inline LogStream &operator<<(LogStream &stream, const ResetFormat &)
|
|||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
struct Keyword
|
||||||
|
{
|
||||||
|
Keyword(const std::string &name)
|
||||||
|
: name(name)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string &name;
|
||||||
|
};
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
inline LogStream &operator<<(LogStream &stream, const Keyword &keyword)
|
||||||
|
{
|
||||||
|
return (stream
|
||||||
|
<< utils::Format(utils::Color::White, utils::FontWeight::Bold)
|
||||||
|
<< keyword.name
|
||||||
|
<< utils::ResetFormat());
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
struct Variable
|
||||||
|
{
|
||||||
|
Variable(const std::string &name)
|
||||||
|
: name(name)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string &name;
|
||||||
|
};
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
inline LogStream &operator<<(LogStream &stream, const Variable &variable)
|
||||||
|
{
|
||||||
|
return (stream
|
||||||
|
<< utils::Format(utils::Color::Green, utils::FontWeight::Bold)
|
||||||
|
<< variable.name
|
||||||
|
<< utils::ResetFormat());
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,8 +85,7 @@ void TranslatorASP::translateTypes() const
|
|||||||
|
|
||||||
if (types.empty())
|
if (types.empty())
|
||||||
{
|
{
|
||||||
printKeyword("type");
|
m_outputStream << utils::Keyword("type") << "(object)." << std::endl;
|
||||||
m_outputStream << "(object)." << std::endl;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,8 +94,7 @@ void TranslatorASP::translateTypes() const
|
|||||||
{
|
{
|
||||||
const auto typeName = utils::escapeASP(type->name());
|
const auto typeName = utils::escapeASP(type->name());
|
||||||
|
|
||||||
this->printKeyword("type");
|
m_outputStream << utils::Keyword("type") << "(" << typeName << ")." << std::endl;
|
||||||
m_outputStream << "(" << typeName << ")." << std::endl;
|
|
||||||
|
|
||||||
const auto &parentTypes = type->parentTypes();
|
const auto &parentTypes = type->parentTypes();
|
||||||
|
|
||||||
@ -105,25 +103,15 @@ void TranslatorASP::translateTypes() const
|
|||||||
{
|
{
|
||||||
const auto parentTypeName = utils::escapeASP(parentType->name());
|
const auto parentTypeName = utils::escapeASP(parentType->name());
|
||||||
|
|
||||||
this->printKeyword("inherits");
|
m_outputStream
|
||||||
m_outputStream << "(";
|
<< utils::Keyword("inherits") << "(" << utils::Keyword("type")
|
||||||
this->printKeyword("type");
|
<< "(" << typeName << "), " << utils::Keyword("type")
|
||||||
m_outputStream << "(" << typeName << "), ";
|
<< "(" << parentTypeName << "))." << std::endl
|
||||||
this->printKeyword("type");
|
|
||||||
m_outputStream << "(" << parentTypeName << "))." << std::endl;
|
|
||||||
|
|
||||||
this->printKeyword("hasType");
|
<< utils::Keyword("hasType") << "(" << utils::Variable("X") << ", "
|
||||||
m_outputStream << "(";
|
<< utils::Keyword("type") << "(" << parentTypeName << ")) :- "
|
||||||
this->printVariable("X");
|
<< utils::Keyword("hasType") << "(" << utils::Variable("X") << ", "
|
||||||
m_outputStream << ", ";
|
<< utils::Keyword("type") << "(" << typeName << "))." << std::endl;
|
||||||
this->printKeyword("type");
|
|
||||||
m_outputStream << "(" << parentTypeName << ")) :- ";
|
|
||||||
this->printKeyword("hasType");
|
|
||||||
m_outputStream << "(";
|
|
||||||
this->printVariable("X");
|
|
||||||
m_outputStream << ", ";
|
|
||||||
this->printKeyword("type");
|
|
||||||
m_outputStream << "(" << typeName << "))." << std::endl;
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -141,8 +129,7 @@ void TranslatorASP::translatePredicates() const
|
|||||||
{
|
{
|
||||||
m_outputStream << std::endl;
|
m_outputStream << std::endl;
|
||||||
|
|
||||||
this->printKeyword("predicate");
|
m_outputStream << utils::Keyword("predicate") << "(" << utils::escapeASP(predicate->name());
|
||||||
m_outputStream << "(" << utils::escapeASP(predicate->name());
|
|
||||||
|
|
||||||
this->translateVariablesHead(predicate->arguments());
|
this->translateVariablesHead(predicate->arguments());
|
||||||
|
|
||||||
@ -167,8 +154,7 @@ void TranslatorASP::translateActions() const
|
|||||||
const auto printActionName =
|
const auto printActionName =
|
||||||
[&](const auto &action)
|
[&](const auto &action)
|
||||||
{
|
{
|
||||||
this->printKeyword("action");
|
m_outputStream << utils::Keyword("action") << "(" << utils::escapeASP(action.name());
|
||||||
m_outputStream << "(" << utils::escapeASP(action.name());
|
|
||||||
|
|
||||||
this->translateVariablesHead(action.parameters());
|
this->translateVariablesHead(action.parameters());
|
||||||
|
|
||||||
@ -181,9 +167,7 @@ void TranslatorASP::translateActions() const
|
|||||||
const auto translateLiteral =
|
const auto translateLiteral =
|
||||||
[&](const auto &ruleHead, const auto &literal)
|
[&](const auto &ruleHead, const auto &literal)
|
||||||
{
|
{
|
||||||
m_outputStream << std::endl;
|
m_outputStream << std::endl << utils::Keyword(ruleHead) << "(";
|
||||||
this->printKeyword(ruleHead);
|
|
||||||
m_outputStream << "(";
|
|
||||||
|
|
||||||
printActionName(*action);
|
printActionName(*action);
|
||||||
|
|
||||||
@ -274,29 +258,22 @@ void TranslatorASP::translateConstants(const std::string &heading, const express
|
|||||||
{
|
{
|
||||||
const auto constantName = utils::escapeASP(constant->name());
|
const auto constantName = utils::escapeASP(constant->name());
|
||||||
|
|
||||||
m_outputStream << std::endl;
|
m_outputStream << std::endl << utils::Keyword("constant")
|
||||||
this->printKeyword("constant");
|
<< "(" << constantName << ")." << std::endl;
|
||||||
m_outputStream << "(" << constantName << ")." << std::endl;
|
|
||||||
|
|
||||||
const auto *type = constant->type();
|
const auto *type = constant->type();
|
||||||
|
|
||||||
if (type != nullptr)
|
if (type != nullptr)
|
||||||
{
|
{
|
||||||
this->printKeyword("hasType");
|
m_outputStream << utils::Keyword("hasType") << "("
|
||||||
m_outputStream << "(";
|
<< utils::Keyword("constant") << "(" << constantName << "), "
|
||||||
this->printKeyword("constant");
|
<< utils::Keyword("hastype") << "(" << utils::escapeASP(type->name()) << "))." << std::endl;
|
||||||
m_outputStream << "(" << constantName << "), ";
|
|
||||||
this->printKeyword("type");
|
|
||||||
m_outputStream << "(" << utils::escapeASP(type->name()) << "))." << std::endl;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this->printKeyword("hasType");
|
m_outputStream << utils::Keyword("hasType") << "("
|
||||||
m_outputStream << "(";
|
<< utils::Keyword("constant") << "(" << constantName << "), "
|
||||||
this->printKeyword("constant");
|
<< utils::Keyword("hastype") << "(object))." << std::endl;
|
||||||
m_outputStream << "(" << constantName << "), ";
|
|
||||||
this->printKeyword("type");
|
|
||||||
m_outputStream << "(object))." << std::endl;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -317,7 +294,7 @@ void TranslatorASP::translateVariablesHead(const expressions::Variables &variabl
|
|||||||
|
|
||||||
const auto &variable = **i;
|
const auto &variable = **i;
|
||||||
|
|
||||||
printVariable(utils::escapeASPVariable(variable.name()));
|
m_outputStream << utils::Variable(utils::escapeASPVariable(variable.name()));
|
||||||
}
|
}
|
||||||
|
|
||||||
m_outputStream << ")";
|
m_outputStream << ")";
|
||||||
@ -346,21 +323,15 @@ void TranslatorASP::translateVariablesBody(const expressions::Variables &variabl
|
|||||||
|
|
||||||
const auto &type = *dynamic_cast<const expressions::PrimitiveType *>(variable.type());
|
const auto &type = *dynamic_cast<const expressions::PrimitiveType *>(variable.type());
|
||||||
|
|
||||||
printKeyword("hasType");
|
m_outputStream << utils::Keyword("hasType") << "("
|
||||||
m_outputStream << "(";
|
<< utils::Variable(utils::escapeASPVariable(variable.name())) << ", "
|
||||||
printVariable(utils::escapeASPVariable(variable.name()));
|
<< utils::Keyword("type") << "(" << type.name() << "))";
|
||||||
m_outputStream << ", ";
|
|
||||||
printKeyword("type");
|
|
||||||
m_outputStream << "(" << type.name() << "))";
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printKeyword("hasType");
|
m_outputStream << utils::Keyword("hasType") << "("
|
||||||
m_outputStream << "(";
|
<< utils::Variable(utils::escapeASPVariable(variable.name())) << ", "
|
||||||
printVariable(utils::escapeASPVariable(variable.name()));
|
<< utils::Keyword("type") << "(object))";
|
||||||
m_outputStream << ", ";
|
|
||||||
printKeyword("type");
|
|
||||||
m_outputStream << "(object))";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -373,8 +344,7 @@ void TranslatorASP::translateLiteral(const Expression &literal) const
|
|||||||
if (literal.expressionType() == Expression::Type::Predicate)
|
if (literal.expressionType() == Expression::Type::Predicate)
|
||||||
{
|
{
|
||||||
this->translatePredicate(dynamic_cast<const expressions::Predicate &>(literal));
|
this->translatePredicate(dynamic_cast<const expressions::Predicate &>(literal));
|
||||||
m_outputStream << ", ";
|
m_outputStream << ", " << utils::Keyword("true");
|
||||||
printKeyword("true");
|
|
||||||
}
|
}
|
||||||
// Assuming that "not" expression may only contain a predicate
|
// Assuming that "not" expression may only contain a predicate
|
||||||
else if (literal.expressionType() == Expression::Type::Not)
|
else if (literal.expressionType() == Expression::Type::Not)
|
||||||
@ -383,8 +353,7 @@ void TranslatorASP::translateLiteral(const Expression &literal) const
|
|||||||
const auto &predicate = dynamic_cast<const expressions::Predicate &>(*notExpression.argument());
|
const auto &predicate = dynamic_cast<const expressions::Predicate &>(*notExpression.argument());
|
||||||
|
|
||||||
this->translatePredicate(predicate);
|
this->translatePredicate(predicate);
|
||||||
m_outputStream << ", ";
|
m_outputStream << ", " << utils::Keyword("false");
|
||||||
printKeyword("false");
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
throw utils::TranslatorException("only primitive predicates and their negations supported as literals currently");
|
throw utils::TranslatorException("only primitive predicates and their negations supported as literals currently");
|
||||||
@ -394,8 +363,7 @@ void TranslatorASP::translateLiteral(const Expression &literal) const
|
|||||||
|
|
||||||
void TranslatorASP::translatePredicate(const expressions::Predicate &predicate) const
|
void TranslatorASP::translatePredicate(const expressions::Predicate &predicate) const
|
||||||
{
|
{
|
||||||
printKeyword("predicate");
|
m_outputStream << utils::Keyword("predicate") << "(" << utils::escapeASP(predicate.name());
|
||||||
m_outputStream << "(" << utils::escapeASP(predicate.name());
|
|
||||||
|
|
||||||
const auto &arguments = predicate.arguments();
|
const auto &arguments = predicate.arguments();
|
||||||
|
|
||||||
@ -416,14 +384,13 @@ void TranslatorASP::translatePredicate(const expressions::Predicate &predicate)
|
|||||||
{
|
{
|
||||||
const auto &constant = dynamic_cast<const expressions::Constant &>(**i);
|
const auto &constant = dynamic_cast<const expressions::Constant &>(**i);
|
||||||
|
|
||||||
printKeyword("constant");
|
m_outputStream << utils::Keyword("constant") << "(" << utils::escapeASP(constant.name()) << ")";
|
||||||
m_outputStream << "(" << utils::escapeASP(constant.name()) << ")";
|
|
||||||
}
|
}
|
||||||
else if ((*i)->expressionType() == Expression::Type::Variable)
|
else if ((*i)->expressionType() == Expression::Type::Variable)
|
||||||
{
|
{
|
||||||
const auto &variable = dynamic_cast<const expressions::Variable &>(**i);
|
const auto &variable = dynamic_cast<const expressions::Variable &>(**i);
|
||||||
|
|
||||||
printVariable(utils::escapeASPVariable(variable.name()));
|
m_outputStream << utils::Variable(utils::escapeASPVariable(variable.name()));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
throw utils::TranslatorException("only variables and constants supported in predicates currently");
|
throw utils::TranslatorException("only variables and constants supported in predicates currently");
|
||||||
@ -471,9 +438,7 @@ void TranslatorASP::translateInitialState() const
|
|||||||
std::for_each(initialStateFacts.cbegin(), initialStateFacts.cend(),
|
std::for_each(initialStateFacts.cbegin(), initialStateFacts.cend(),
|
||||||
[&](const auto &fact)
|
[&](const auto &fact)
|
||||||
{
|
{
|
||||||
m_outputStream << std::endl;
|
m_outputStream << std::endl << utils::Keyword("initialState") << "(";
|
||||||
this->printKeyword("initialState");
|
|
||||||
m_outputStream << "(";
|
|
||||||
|
|
||||||
// Translate single predicate
|
// Translate single predicate
|
||||||
if (fact->expressionType() == Expression::Type::Predicate)
|
if (fact->expressionType() == Expression::Type::Predicate)
|
||||||
@ -508,9 +473,7 @@ void TranslatorASP::translateGoal() const
|
|||||||
if (goal.expressionType() == Expression::Type::Predicate
|
if (goal.expressionType() == Expression::Type::Predicate
|
||||||
|| goal.expressionType() == Expression::Type::Not)
|
|| goal.expressionType() == Expression::Type::Not)
|
||||||
{
|
{
|
||||||
m_outputStream << std::endl;
|
m_outputStream << std::endl << utils::Keyword("goal") << "(";
|
||||||
printKeyword("goal");
|
|
||||||
m_outputStream << "(";
|
|
||||||
|
|
||||||
translateLiteral(goal);
|
translateLiteral(goal);
|
||||||
|
|
||||||
@ -523,9 +486,7 @@ void TranslatorASP::translateGoal() const
|
|||||||
std::for_each(andExpression.arguments().cbegin(), andExpression.arguments().cend(),
|
std::for_each(andExpression.arguments().cbegin(), andExpression.arguments().cend(),
|
||||||
[&](const auto *argument)
|
[&](const auto *argument)
|
||||||
{
|
{
|
||||||
m_outputStream << std::endl;
|
m_outputStream << std::endl << utils::Keyword("goal") << "(";
|
||||||
this->printKeyword("goal");
|
|
||||||
m_outputStream << "(";
|
|
||||||
|
|
||||||
this->translateLiteral(*argument);
|
this->translateLiteral(*argument);
|
||||||
|
|
||||||
@ -563,25 +524,5 @@ void TranslatorASP::printHeading2(const std::string &heading) const
|
|||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
void TranslatorASP::printKeyword(const std::string &keyword) const
|
|
||||||
{
|
|
||||||
m_outputStream
|
|
||||||
<< utils::Format(utils::Color::White, utils::FontWeight::Bold)
|
|
||||||
<< keyword
|
|
||||||
<< utils::ResetFormat();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
void TranslatorASP::printVariable(const std::string &variable) const
|
|
||||||
{
|
|
||||||
m_outputStream
|
|
||||||
<< utils::Format(utils::Color::Green, utils::FontWeight::Bold)
|
|
||||||
<< variable
|
|
||||||
<< utils::ResetFormat();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user