2016-11-22 03:15:52 +01:00
|
|
|
|
#ifndef __ANTHEM__STATEMENT_VISITOR_H
|
|
|
|
|
#define __ANTHEM__STATEMENT_VISITOR_H
|
|
|
|
|
|
2016-11-23 04:41:38 +01:00
|
|
|
|
#include <anthem/Body.h>
|
2016-11-23 03:29:26 +01:00
|
|
|
|
#include <anthem/Head.h>
|
2016-11-22 03:15:52 +01:00
|
|
|
|
#include <anthem/Utils.h>
|
2016-11-24 02:42:32 +01:00
|
|
|
|
#include <anthem/output/ClingoOutput.h>
|
2016-11-22 03:15:52 +01:00
|
|
|
|
|
|
|
|
|
namespace anthem
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// StatementVisitor
|
|
|
|
|
//
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
struct StatementVisitor
|
|
|
|
|
{
|
2016-11-24 00:52:28 +01:00
|
|
|
|
void visit(const Clingo::AST::Program &program, const Clingo::AST::Statement &statement, Context &context)
|
2016-11-22 03:15:52 +01:00
|
|
|
|
{
|
2016-11-24 02:42:32 +01:00
|
|
|
|
context.logger.log(output::Priority::Debug, program.name);
|
2016-11-22 03:15:52 +01:00
|
|
|
|
|
|
|
|
|
if (!program.parameters.empty())
|
2016-11-24 00:52:28 +01:00
|
|
|
|
throwErrorAtLocation(statement.location, "program parameters currently unsupported", context);
|
2016-11-22 03:15:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 00:52:28 +01:00
|
|
|
|
void visit(const Clingo::AST::Rule &rule, const Clingo::AST::Statement &, Context &context)
|
2016-11-22 03:15:52 +01:00
|
|
|
|
{
|
2016-11-24 02:42:32 +01:00
|
|
|
|
auto &outputStream = context.logger.outputStream();
|
|
|
|
|
|
2016-11-23 03:29:26 +01:00
|
|
|
|
// Concatenate all head terms
|
2016-11-24 00:52:28 +01:00
|
|
|
|
rule.head.data.accept(HeadLiteralCollectFunctionTermsVisitor(), rule.head, context);
|
2016-11-22 03:15:52 +01:00
|
|
|
|
|
2016-11-23 03:29:26 +01:00
|
|
|
|
// Print auxiliary variables replacing the head atom’s arguments
|
2016-11-24 00:52:28 +01:00
|
|
|
|
if (!context.headTerms.empty())
|
2016-11-22 17:58:18 +01:00
|
|
|
|
{
|
2016-11-24 00:52:28 +01:00
|
|
|
|
for (auto i = context.headTerms.cbegin(); i != context.headTerms.cend(); i++)
|
2016-11-22 17:58:18 +01:00
|
|
|
|
{
|
2016-11-23 03:29:26 +01:00
|
|
|
|
const auto &headTerm = **i;
|
|
|
|
|
|
2016-11-24 00:52:28 +01:00
|
|
|
|
if (i != context.headTerms.cbegin())
|
2016-11-24 02:42:32 +01:00
|
|
|
|
outputStream << ", ";
|
|
|
|
|
|
|
|
|
|
const auto variableName = std::string(AuxiliaryBodyVariablePrefix) + std::to_string(i - context.headTerms.cbegin());
|
2016-11-22 17:58:18 +01:00
|
|
|
|
|
2016-11-24 02:42:32 +01:00
|
|
|
|
outputStream
|
|
|
|
|
<< output::Variable(variableName.c_str())
|
|
|
|
|
<< " " << output::Keyword("in") << " " << headTerm;
|
2016-11-22 17:58:18 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 00:52:28 +01:00
|
|
|
|
if (rule.body.empty() && context.headTerms.empty())
|
2016-11-24 02:42:32 +01:00
|
|
|
|
outputStream << output::Boolean("true");
|
2016-11-23 04:59:04 +01:00
|
|
|
|
else
|
2016-11-23 04:41:38 +01:00
|
|
|
|
{
|
2016-11-23 04:59:04 +01:00
|
|
|
|
// Print translated body literals
|
|
|
|
|
for (auto i = rule.body.cbegin(); i != rule.body.cend(); i++)
|
|
|
|
|
{
|
|
|
|
|
const auto &bodyLiteral = *i;
|
2016-11-22 03:15:52 +01:00
|
|
|
|
|
2016-11-24 00:52:28 +01:00
|
|
|
|
if (!context.headTerms.empty())
|
2016-11-23 04:59:04 +01:00
|
|
|
|
std::cout << " and ";
|
2016-11-22 03:15:52 +01:00
|
|
|
|
|
2016-11-23 04:59:04 +01:00
|
|
|
|
if (bodyLiteral.sign != Clingo::AST::Sign::None)
|
2016-11-24 00:52:28 +01:00
|
|
|
|
throwErrorAtLocation(bodyLiteral.location, "only positive literals currently supported", context);
|
2016-11-22 03:15:52 +01:00
|
|
|
|
|
2016-11-23 04:59:04 +01:00
|
|
|
|
bodyLiteral.data.accept(BodyLiteralPrintVisitor(), bodyLiteral, context);
|
|
|
|
|
}
|
2016-11-23 04:41:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 02:42:32 +01:00
|
|
|
|
outputStream << " " << output::Operator("->") << " ";
|
2016-11-23 03:29:26 +01:00
|
|
|
|
|
|
|
|
|
// Print consequent of the implication
|
2016-11-24 00:52:28 +01:00
|
|
|
|
rule.head.data.accept(HeadLiteralPrintSubstitutedVisitor(), rule.head, context);
|
2016-11-22 03:15:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 00:52:28 +01:00
|
|
|
|
void visit(const Clingo::AST::Definition &, const Clingo::AST::Statement &statement, Context &context)
|
2016-11-22 03:15:52 +01:00
|
|
|
|
{
|
2016-11-24 00:52:28 +01:00
|
|
|
|
throwErrorAtLocation(statement.location, "“definition” statements currently unsupported", context);
|
2016-11-22 03:15:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 00:52:28 +01:00
|
|
|
|
void visit(const Clingo::AST::ShowSignature &, const Clingo::AST::Statement &statement, Context &context)
|
2016-11-22 03:15:52 +01:00
|
|
|
|
{
|
2016-11-24 00:52:28 +01:00
|
|
|
|
throwErrorAtLocation(statement.location, "“show signature” statements currently unsupported", context);
|
2016-11-22 03:15:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 00:52:28 +01:00
|
|
|
|
void visit(const Clingo::AST::ShowTerm &, const Clingo::AST::Statement &statement, Context &context)
|
2016-11-22 03:15:52 +01:00
|
|
|
|
{
|
2016-11-24 00:52:28 +01:00
|
|
|
|
throwErrorAtLocation(statement.location, "“show term” statements currently unsupported", context);
|
2016-11-22 03:15:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 00:52:28 +01:00
|
|
|
|
void visit(const Clingo::AST::Minimize &, const Clingo::AST::Statement &statement, Context &context)
|
2016-11-22 03:15:52 +01:00
|
|
|
|
{
|
2016-11-24 00:52:28 +01:00
|
|
|
|
throwErrorAtLocation(statement.location, "“minimize” statements currently unsupported", context);
|
2016-11-22 03:15:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 00:52:28 +01:00
|
|
|
|
void visit(const Clingo::AST::Script &, const Clingo::AST::Statement &statement, Context &context)
|
2016-11-22 03:15:52 +01:00
|
|
|
|
{
|
2016-11-24 00:52:28 +01:00
|
|
|
|
throwErrorAtLocation(statement.location, "“script” statements currently unsupported", context);
|
2016-11-22 03:15:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 00:52:28 +01:00
|
|
|
|
void visit(const Clingo::AST::External &, const Clingo::AST::Statement &statement, Context &context)
|
2016-11-22 03:15:52 +01:00
|
|
|
|
{
|
2016-11-24 00:52:28 +01:00
|
|
|
|
throwErrorAtLocation(statement.location, "“external” statements currently unsupported", context);
|
2016-11-22 03:15:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 00:52:28 +01:00
|
|
|
|
void visit(const Clingo::AST::Edge &, const Clingo::AST::Statement &statement, Context &context)
|
2016-11-22 03:15:52 +01:00
|
|
|
|
{
|
2016-11-24 00:52:28 +01:00
|
|
|
|
throwErrorAtLocation(statement.location, "“edge” statements currently unsupported", context);
|
2016-11-22 03:15:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 00:52:28 +01:00
|
|
|
|
void visit(const Clingo::AST::Heuristic &, const Clingo::AST::Statement &statement, Context &context)
|
2016-11-22 03:15:52 +01:00
|
|
|
|
{
|
2016-11-24 00:52:28 +01:00
|
|
|
|
throwErrorAtLocation(statement.location, "“heuristic” statements currently unsupported", context);
|
2016-11-22 03:15:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 00:52:28 +01:00
|
|
|
|
void visit(const Clingo::AST::ProjectAtom &, const Clingo::AST::Statement &statement, Context &context)
|
2016-11-22 03:15:52 +01:00
|
|
|
|
{
|
2016-11-24 00:52:28 +01:00
|
|
|
|
throwErrorAtLocation(statement.location, "“project atom” statements currently unsupported", context);
|
2016-11-22 03:15:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 00:52:28 +01:00
|
|
|
|
void visit(const Clingo::AST::ProjectSignature &, const Clingo::AST::Statement &statement, Context &context)
|
2016-11-22 03:15:52 +01:00
|
|
|
|
{
|
2016-11-24 00:52:28 +01:00
|
|
|
|
throwErrorAtLocation(statement.location, "“project signature” statements currently unsupported", context);
|
2016-11-22 03:15:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 00:52:28 +01:00
|
|
|
|
void visit(const Clingo::AST::TheoryDefinition &, const Clingo::AST::Statement &statement, Context &context)
|
2016-11-22 03:15:52 +01:00
|
|
|
|
{
|
2016-11-24 00:52:28 +01:00
|
|
|
|
throwErrorAtLocation(statement.location, "“theory definition” statements currently unsupported", context);
|
2016-11-22 03:15:52 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|