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>
|
|
|
|
|
|
|
|
|
|
namespace anthem
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// StatementVisitor
|
|
|
|
|
//
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
void throwErrorUnsupportedStatement(const char *statementType, const Clingo::AST::Statement &statement)
|
|
|
|
|
{
|
2016-11-23 03:29:26 +01:00
|
|
|
|
const auto errorMessage = std::string("“") + statementType + "” statements currently unsupported";
|
2016-11-22 03:15:52 +01:00
|
|
|
|
|
|
|
|
|
throwErrorAtLocation(statement.location, errorMessage.c_str());
|
|
|
|
|
|
|
|
|
|
throw std::runtime_error(errorMessage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
struct StatementVisitor
|
|
|
|
|
{
|
|
|
|
|
void visit(const Clingo::AST::Program &program, const Clingo::AST::Statement &statement)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "[program] " << program.name << std::endl;
|
|
|
|
|
|
|
|
|
|
if (!program.parameters.empty())
|
2016-11-23 03:29:26 +01:00
|
|
|
|
throwErrorAtLocation(statement.location, "program parameters currently unsupported");
|
2016-11-22 03:15:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void visit(const Clingo::AST::Rule &rule, const Clingo::AST::Statement &)
|
|
|
|
|
{
|
2016-11-23 04:41:38 +01:00
|
|
|
|
Context context;
|
|
|
|
|
|
2016-11-23 03:29:26 +01:00
|
|
|
|
// Concatenate all head terms
|
|
|
|
|
std::vector<const Clingo::AST::Term *> headTerms;
|
|
|
|
|
rule.head.data.accept(HeadLiteralCollectFunctionTermsVisitor(), rule.head, headTerms);
|
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-22 18:12:02 +01:00
|
|
|
|
if (!headTerms.empty())
|
2016-11-22 17:58:18 +01:00
|
|
|
|
{
|
2016-11-22 18:12:02 +01:00
|
|
|
|
for (auto i = headTerms.cbegin(); i != 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-22 18:12:02 +01:00
|
|
|
|
if (i != headTerms.cbegin())
|
2016-11-22 17:58:18 +01:00
|
|
|
|
std::cout << ", ";
|
|
|
|
|
|
2016-11-23 03:29:26 +01:00
|
|
|
|
std::cout
|
2016-11-23 04:41:38 +01:00
|
|
|
|
<< AuxiliaryHeadVariablePrefix << (i - headTerms.cbegin())
|
2016-11-23 03:29:26 +01:00
|
|
|
|
<< " in " << headTerm;
|
2016-11-22 17:58:18 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-23 04:41:38 +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-23 04:41:38 +01:00
|
|
|
|
if (!headTerms.empty())
|
|
|
|
|
std::cout << " and ";
|
2016-11-22 03:15:52 +01:00
|
|
|
|
|
|
|
|
|
if (bodyLiteral.sign != Clingo::AST::Sign::None)
|
|
|
|
|
throwErrorAtLocation(bodyLiteral.location, "only positive literals currently supported");
|
|
|
|
|
|
2016-11-23 04:41:38 +01:00
|
|
|
|
bodyLiteral.data.accept(BodyLiteralPrintVisitor(), bodyLiteral, context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::cout << " -> ";
|
2016-11-23 03:29:26 +01:00
|
|
|
|
|
|
|
|
|
// Print consequent of the implication
|
|
|
|
|
rule.head.data.accept(HeadLiteralPrintSubstitutedVisitor(), rule.head, headTerms);
|
2016-11-22 03:15:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void visit(const Clingo::AST::Definition &, const Clingo::AST::Statement &statement)
|
|
|
|
|
{
|
|
|
|
|
throwErrorUnsupportedStatement("definition", statement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void visit(const Clingo::AST::ShowSignature &, const Clingo::AST::Statement &statement)
|
|
|
|
|
{
|
|
|
|
|
throwErrorUnsupportedStatement("show signature", statement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void visit(const Clingo::AST::ShowTerm &, const Clingo::AST::Statement &statement)
|
|
|
|
|
{
|
|
|
|
|
throwErrorUnsupportedStatement("show term", statement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void visit(const Clingo::AST::Minimize &, const Clingo::AST::Statement &statement)
|
|
|
|
|
{
|
|
|
|
|
throwErrorUnsupportedStatement("minimize", statement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void visit(const Clingo::AST::Script &, const Clingo::AST::Statement &statement)
|
|
|
|
|
{
|
|
|
|
|
throwErrorUnsupportedStatement("script", statement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void visit(const Clingo::AST::External &, const Clingo::AST::Statement &statement)
|
|
|
|
|
{
|
|
|
|
|
throwErrorUnsupportedStatement("external", statement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void visit(const Clingo::AST::Edge &, const Clingo::AST::Statement &statement)
|
|
|
|
|
{
|
|
|
|
|
throwErrorUnsupportedStatement("edge", statement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void visit(const Clingo::AST::Heuristic &, const Clingo::AST::Statement &statement)
|
|
|
|
|
{
|
|
|
|
|
throwErrorUnsupportedStatement("heuristic", statement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void visit(const Clingo::AST::ProjectAtom &, const Clingo::AST::Statement &statement)
|
|
|
|
|
{
|
|
|
|
|
throwErrorUnsupportedStatement("project atom", statement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void visit(const Clingo::AST::ProjectSignature &, const Clingo::AST::Statement &statement)
|
|
|
|
|
{
|
|
|
|
|
throwErrorUnsupportedStatement("project signature", statement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void visit(const Clingo::AST::TheoryDefinition &, const Clingo::AST::Statement &statement)
|
|
|
|
|
{
|
|
|
|
|
throwErrorUnsupportedStatement("theory definition", statement);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|