From 920f3ab2105802d73b5ffbeac47e920456ce5f15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Tue, 22 Nov 2016 17:58:18 +0100 Subject: [PATCH] Started implementing translation of head literals. --- include/anthem/HeadLiteralVisitor.h | 40 +++++++++- include/anthem/LiteralVisitor.h | 82 +++++++++++++++++++ include/anthem/StatementVisitor.h | 27 +++++-- include/anthem/TermVisitor.h | 117 ++++++++++++++++++++++++++++ 4 files changed, 257 insertions(+), 9 deletions(-) create mode 100644 include/anthem/LiteralVisitor.h create mode 100644 include/anthem/TermVisitor.h diff --git a/include/anthem/HeadLiteralVisitor.h b/include/anthem/HeadLiteralVisitor.h index d83d1f7..285630e 100644 --- a/include/anthem/HeadLiteralVisitor.h +++ b/include/anthem/HeadLiteralVisitor.h @@ -1,6 +1,7 @@ #ifndef __ANTHEM__HEAD_LITERAL_VISITOR_H #define __ANTHEM__HEAD_LITERAL_VISITOR_H +#include #include 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 &variables) + { + literal.data.accept(LiteralCollectVariablesVisitor(), literal, variables); + } + + void visit(const Clingo::AST::Disjunction &, const Clingo::AST::HeadLiteral &headLiteral, std::vector &) + { + // TODO: implement + throwErrorUnsupportedHeadLiteral("disjunction", headLiteral); + } + + void visit(const Clingo::AST::Aggregate &, const Clingo::AST::HeadLiteral &headLiteral, std::vector &) + { + throwErrorUnsupportedHeadLiteral("aggregate", headLiteral); + } + + void visit(const Clingo::AST::HeadAggregate &, const Clingo::AST::HeadLiteral &headLiteral, std::vector &) + { + throwErrorUnsupportedHeadLiteral("head aggregate", headLiteral); + } + + void visit(const Clingo::AST::TheoryAtom &, const Clingo::AST::HeadLiteral &headLiteral, std::vector &) + { + throwErrorUnsupportedHeadLiteral("theory", headLiteral); + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + } #endif diff --git a/include/anthem/LiteralVisitor.h b/include/anthem/LiteralVisitor.h new file mode 100644 index 0000000..aed8b4a --- /dev/null +++ b/include/anthem/LiteralVisitor.h @@ -0,0 +1,82 @@ +#ifndef __ANTHEM__LITERAL_VISITOR_H +#define __ANTHEM__LITERAL_VISITOR_H + +#include +#include + +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 &) + { + } + + void visit(const Clingo::AST::Term &term, const Clingo::AST::Literal &, std::vector &variables) + { + term.data.accept(TermCollectVariablesVisitor(), term, variables); + } + + void visit(const Clingo::AST::Comparison &comparison, const Clingo::AST::Literal &, std::vector &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 &) + { + throwErrorUnsupportedLiteral("CSP literal", literal); + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} + +#endif diff --git a/include/anthem/StatementVisitor.h b/include/anthem/StatementVisitor.h index e1bbc97..ef0fc77 100644 --- a/include/anthem/StatementVisitor.h +++ b/include/anthem/StatementVisitor.h @@ -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 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) diff --git a/include/anthem/TermVisitor.h b/include/anthem/TermVisitor.h new file mode 100644 index 0000000..5ed7e18 --- /dev/null +++ b/include/anthem/TermVisitor.h @@ -0,0 +1,117 @@ +#ifndef __ANTHEM__TERM_VISITOR_H +#define __ANTHEM__TERM_VISITOR_H + +#include + +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 &) + { + } + + void visit(const Clingo::AST::Variable &variable, const Clingo::AST::Term &, std::vector &variables) + { + variables.push_back(variable); + } + + void visit(const Clingo::AST::UnaryOperation &unaryOperation, const Clingo::AST::Term &, std::vector &variables) + { + unaryOperation.argument.data.accept(*this, unaryOperation.argument, variables); + } + + void visit(const Clingo::AST::BinaryOperation &binaryOperation, const Clingo::AST::Term &, std::vector &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 &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 &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 &variables) + { + for (const auto &argument : pool.arguments) + argument.data.accept(*this, argument, variables); + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} + +#endif