Started implementing translation of head literals.

This commit is contained in:
Patrick Lühne 2016-11-22 17:58:18 +01:00
parent 7e7baa1aab
commit 920f3ab210
No known key found for this signature in database
GPG Key ID: 05F3611E97A70ABF
4 changed files with 257 additions and 9 deletions

View File

@ -1,6 +1,7 @@
#ifndef __ANTHEM__HEAD_LITERAL_VISITOR_H
#define __ANTHEM__HEAD_LITERAL_VISITOR_H
#include <anthem/LiteralVisitor.h>
#include <anthem/Utils.h>
namespace anthem
@ -25,13 +26,17 @@ void throwErrorUnsupportedHeadLiteral(const char *statementType, const Clingo::A
struct HeadLiteralVisitor
{
void visit(const Clingo::AST::Literal &, const Clingo::AST::HeadLiteral &)
void visit(const Clingo::AST::Literal &literal, const Clingo::AST::HeadLiteral &)
{
std::cout << "[literal]" << std::endl;
if (literal.sign != Clingo::AST::Sign::None)
throwErrorAtLocation(literal.location, "only positive literals currently supported");
literal.data.accept(LiteralVisitor(), literal);
}
void visit(const Clingo::AST::Disjunction &, const Clingo::AST::HeadLiteral &headLiteral)
{
// TODO: implement
throwErrorUnsupportedHeadLiteral("disjunction", headLiteral);
}
@ -53,6 +58,37 @@ struct HeadLiteralVisitor
////////////////////////////////////////////////////////////////////////////////////////////////////
struct HeadLiteralCollectVariablesVisitor
{
void visit(const Clingo::AST::Literal &literal, const Clingo::AST::HeadLiteral &, std::vector<Clingo::AST::Variable> &variables)
{
literal.data.accept(LiteralCollectVariablesVisitor(), literal, variables);
}
void visit(const Clingo::AST::Disjunction &, const Clingo::AST::HeadLiteral &headLiteral, std::vector<Clingo::AST::Variable> &)
{
// TODO: implement
throwErrorUnsupportedHeadLiteral("disjunction", headLiteral);
}
void visit(const Clingo::AST::Aggregate &, const Clingo::AST::HeadLiteral &headLiteral, std::vector<Clingo::AST::Variable> &)
{
throwErrorUnsupportedHeadLiteral("aggregate", headLiteral);
}
void visit(const Clingo::AST::HeadAggregate &, const Clingo::AST::HeadLiteral &headLiteral, std::vector<Clingo::AST::Variable> &)
{
throwErrorUnsupportedHeadLiteral("head aggregate", headLiteral);
}
void visit(const Clingo::AST::TheoryAtom &, const Clingo::AST::HeadLiteral &headLiteral, std::vector<Clingo::AST::Variable> &)
{
throwErrorUnsupportedHeadLiteral("theory", headLiteral);
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
#endif

View File

@ -0,0 +1,82 @@
#ifndef __ANTHEM__LITERAL_VISITOR_H
#define __ANTHEM__LITERAL_VISITOR_H
#include <anthem/TermVisitor.h>
#include <anthem/Utils.h>
namespace anthem
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// LiteralVisitor
//
////////////////////////////////////////////////////////////////////////////////////////////////////
void throwErrorUnsupportedLiteral(const char *statementType, const Clingo::AST::Literal &literal)
{
const auto errorMessage = std::string("") + statementType + "” literals currently not supported";
throwErrorAtLocation(literal.location, errorMessage.c_str());
throw std::runtime_error(errorMessage);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
struct LiteralVisitor
{
void visit(const Clingo::AST::Boolean &boolean, const Clingo::AST::Literal &)
{
if (boolean.value == true)
std::cout << "true";
else
std::cout << "false";
}
void visit(const Clingo::AST::Term &term, const Clingo::AST::Literal &)
{
term.data.accept(TermVisitor(), term);
}
void visit(const Clingo::AST::Comparison &, const Clingo::AST::Literal &literal)
{
throwErrorUnsupportedLiteral("comparison", literal);
}
void visit(const Clingo::AST::CSPLiteral &, const Clingo::AST::Literal &literal)
{
throwErrorUnsupportedLiteral("CSP literal", literal);
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////
struct LiteralCollectVariablesVisitor
{
void visit(const Clingo::AST::Boolean &, const Clingo::AST::Literal &, std::vector<Clingo::AST::Variable> &)
{
}
void visit(const Clingo::AST::Term &term, const Clingo::AST::Literal &, std::vector<Clingo::AST::Variable> &variables)
{
term.data.accept(TermCollectVariablesVisitor(), term, variables);
}
void visit(const Clingo::AST::Comparison &comparison, const Clingo::AST::Literal &, std::vector<Clingo::AST::Variable> &variables)
{
comparison.left.data.accept(TermCollectVariablesVisitor(), comparison.left, variables);
comparison.right.data.accept(TermCollectVariablesVisitor(), comparison.right, variables);
}
void visit(const Clingo::AST::CSPLiteral &, const Clingo::AST::Literal &literal, std::vector<Clingo::AST::Variable> &)
{
throwErrorUnsupportedLiteral("CSP literal", literal);
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
#endif

View File

@ -37,22 +37,35 @@ struct StatementVisitor
void visit(const Clingo::AST::Rule &rule, const Clingo::AST::Statement &)
{
std::cout << "[rule]" << std::endl;
std::cout << "[head literal]" << std::endl;
std::vector<Clingo::AST::Variable> headVariables;
rule.head.data.accept(HeadLiteralCollectVariablesVisitor(), rule.head, headVariables);
rule.head.data.accept(HeadLiteralVisitor(), rule.head);
if (!headVariables.empty())
{
std::cout << "exists ";
std::cout << "[body]" << std::endl;
for (auto i = headVariables.cbegin(); i != headVariables.cend(); i++)
{
if (i != headVariables.cbegin())
std::cout << ", ";
std::cout << *i;
}
std::cout << ": ";
}
std::cout << "body -> ";
/*rule.head.data.accept(HeadLiteralVisitor(), rule.head);
for (const auto &bodyLiteral : rule.body)
{
std::cout << "[body literal]" << std::endl;
if (bodyLiteral.sign != Clingo::AST::Sign::None)
throwErrorAtLocation(bodyLiteral.location, "only positive literals currently supported");
bodyLiteral.data.accept(BodyLiteralVisitor(), bodyLiteral);
}
}*/
}
void visit(const Clingo::AST::Definition &, const Clingo::AST::Statement &statement)

View File

@ -0,0 +1,117 @@
#ifndef __ANTHEM__TERM_VISITOR_H
#define __ANTHEM__TERM_VISITOR_H
#include <anthem/Utils.h>
namespace anthem
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// TermVisitor
//
////////////////////////////////////////////////////////////////////////////////////////////////////
void throwErrorUnsupportedTerm(const char *statementType, const Clingo::AST::Term &term)
{
const auto errorMessage = std::string("") + statementType + "” terms currently not supported";
throwErrorAtLocation(term.location, errorMessage.c_str());
throw std::runtime_error(errorMessage);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
struct TermVisitor
{
void visit(const Clingo::Symbol &symbol, const Clingo::AST::Term &)
{
std::cout << symbol;
}
void visit(const Clingo::AST::Variable &, const Clingo::AST::Term &term)
{
throwErrorUnsupportedTerm("variable", term);
}
void visit(const Clingo::AST::UnaryOperation &, const Clingo::AST::Term &term)
{
throwErrorUnsupportedTerm("unary operation", term);
}
void visit(const Clingo::AST::BinaryOperation &, const Clingo::AST::Term &term)
{
throwErrorUnsupportedTerm("binary operation", term);
}
void visit(const Clingo::AST::Interval &, const Clingo::AST::Term &term)
{
throwErrorUnsupportedTerm("interval", term);
}
void visit(const Clingo::AST::Function &function, const Clingo::AST::Term &term)
{
std::cout << "[" << function.name << "]";
throwErrorUnsupportedTerm("function", term);
}
void visit(const Clingo::AST::Pool &, const Clingo::AST::Term &term)
{
throwErrorUnsupportedTerm("pool", term);
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////
struct TermCollectVariablesVisitor
{
void visit(const Clingo::Symbol &, const Clingo::AST::Term &, std::vector<Clingo::AST::Variable> &)
{
}
void visit(const Clingo::AST::Variable &variable, const Clingo::AST::Term &, std::vector<Clingo::AST::Variable> &variables)
{
variables.push_back(variable);
}
void visit(const Clingo::AST::UnaryOperation &unaryOperation, const Clingo::AST::Term &, std::vector<Clingo::AST::Variable> &variables)
{
unaryOperation.argument.data.accept(*this, unaryOperation.argument, variables);
}
void visit(const Clingo::AST::BinaryOperation &binaryOperation, const Clingo::AST::Term &, std::vector<Clingo::AST::Variable> &variables)
{
binaryOperation.left.data.accept(*this, binaryOperation.left, variables);
binaryOperation.right.data.accept(*this, binaryOperation.right, variables);
}
void visit(const Clingo::AST::Interval &interval, const Clingo::AST::Term &, std::vector<Clingo::AST::Variable> &variables)
{
interval.left.data.accept(*this, interval.left, variables);
interval.right.data.accept(*this, interval.right, variables);
}
void visit(const Clingo::AST::Function &function, const Clingo::AST::Term &term, std::vector<Clingo::AST::Variable> &variables)
{
if (function.external)
throwErrorAtLocation(term.location, "external functions currently not supported");
for (const auto &argument : function.arguments)
argument.data.accept(*this, argument, variables);
}
void visit(const Clingo::AST::Pool &pool, const Clingo::AST::Term &, std::vector<Clingo::AST::Variable> &variables)
{
for (const auto &argument : pool.arguments)
argument.data.accept(*this, argument, variables);
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
#endif