anthem/include/anthem/StatementVisitor.h

142 lines
4.8 KiB
C
Raw Normal View History

2016-11-22 03:15:52 +01:00
#ifndef __ANTHEM__STATEMENT_VISITOR_H
#define __ANTHEM__STATEMENT_VISITOR_H
#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>
#include <anthem/output/ClingoOutput.h>
2016-11-22 03:15:52 +01:00
namespace anthem
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// StatementVisitor
//
////////////////////////////////////////////////////////////////////////////////////////////////////
struct StatementVisitor
{
void visit(const Clingo::AST::Program &program, const Clingo::AST::Statement &statement, Context &context)
2016-11-22 03:15:52 +01:00
{
context.logger.log(output::Priority::Debug, program.name);
2016-11-22 03:15:52 +01:00
if (!program.parameters.empty())
throwErrorAtLocation(statement.location, "program parameters currently unsupported", context);
2016-11-22 03:15:52 +01:00
}
void visit(const Clingo::AST::Rule &rule, const Clingo::AST::Statement &, Context &context)
2016-11-22 03:15:52 +01:00
{
// TODO: implement more nicely
context.headTerms.clear();
auto &outputStream = context.logger.outputStream();
2016-11-23 03:29:26 +01:00
// Concatenate all head terms
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 atoms arguments
if (!context.headTerms.empty())
{
for (auto i = context.headTerms.cbegin(); i != context.headTerms.cend(); i++)
{
2016-11-23 03:29:26 +01:00
const auto &headTerm = **i;
if (i != context.headTerms.cbegin())
outputStream << ", ";
const auto variableName = std::string(AuxiliaryHeadVariablePrefix) + std::to_string(i - context.headTerms.cbegin());
outputStream
<< output::Variable(variableName.c_str())
<< " " << output::Keyword("in") << " " << headTerm;
}
}
if (rule.body.empty() && context.headTerms.empty())
outputStream << output::Boolean("true");
else
{
// 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
if (!context.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", context);
2016-11-22 03:15:52 +01:00
bodyLiteral.data.accept(BodyLiteralPrintVisitor(), bodyLiteral, context);
}
}
outputStream << " " << output::Operator("->") << " ";
2016-11-23 03:29:26 +01:00
// Print consequent of the implication
rule.head.data.accept(HeadLiteralPrintSubstitutedVisitor(), rule.head, context);
2016-11-22 03:15:52 +01:00
}
void visit(const Clingo::AST::Definition &, const Clingo::AST::Statement &statement, Context &context)
2016-11-22 03:15:52 +01:00
{
throwErrorAtLocation(statement.location, "“definition” statements currently unsupported", context);
2016-11-22 03:15:52 +01:00
}
void visit(const Clingo::AST::ShowSignature &, const Clingo::AST::Statement &statement, Context &context)
2016-11-22 03:15:52 +01:00
{
throwErrorAtLocation(statement.location, "“show signature” statements currently unsupported", context);
2016-11-22 03:15:52 +01:00
}
void visit(const Clingo::AST::ShowTerm &, const Clingo::AST::Statement &statement, Context &context)
2016-11-22 03:15:52 +01:00
{
throwErrorAtLocation(statement.location, "“show term” statements currently unsupported", context);
2016-11-22 03:15:52 +01:00
}
void visit(const Clingo::AST::Minimize &, const Clingo::AST::Statement &statement, Context &context)
2016-11-22 03:15:52 +01:00
{
throwErrorAtLocation(statement.location, "“minimize” statements currently unsupported", context);
2016-11-22 03:15:52 +01:00
}
void visit(const Clingo::AST::Script &, const Clingo::AST::Statement &statement, Context &context)
2016-11-22 03:15:52 +01:00
{
throwErrorAtLocation(statement.location, "“script” statements currently unsupported", context);
2016-11-22 03:15:52 +01:00
}
void visit(const Clingo::AST::External &, const Clingo::AST::Statement &statement, Context &context)
2016-11-22 03:15:52 +01:00
{
throwErrorAtLocation(statement.location, "“external” statements currently unsupported", context);
2016-11-22 03:15:52 +01:00
}
void visit(const Clingo::AST::Edge &, const Clingo::AST::Statement &statement, Context &context)
2016-11-22 03:15:52 +01:00
{
throwErrorAtLocation(statement.location, "“edge” statements currently unsupported", context);
2016-11-22 03:15:52 +01:00
}
void visit(const Clingo::AST::Heuristic &, const Clingo::AST::Statement &statement, Context &context)
2016-11-22 03:15:52 +01:00
{
throwErrorAtLocation(statement.location, "“heuristic” statements currently unsupported", context);
2016-11-22 03:15:52 +01:00
}
void visit(const Clingo::AST::ProjectAtom &, const Clingo::AST::Statement &statement, Context &context)
2016-11-22 03:15:52 +01:00
{
throwErrorAtLocation(statement.location, "“project atom” statements currently unsupported", context);
2016-11-22 03:15:52 +01:00
}
void visit(const Clingo::AST::ProjectSignature &, const Clingo::AST::Statement &statement, Context &context)
2016-11-22 03:15:52 +01:00
{
throwErrorAtLocation(statement.location, "“project signature” statements currently unsupported", context);
2016-11-22 03:15:52 +01:00
}
void visit(const Clingo::AST::TheoryDefinition &, const Clingo::AST::Statement &statement, Context &context)
2016-11-22 03:15:52 +01:00
{
throwErrorAtLocation(statement.location, "“theory definition” statements currently unsupported", context);
2016-11-22 03:15:52 +01:00
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
#endif