souffle  2.0.2-371-g6315b36
Clause.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file Clause.h
12  *
13  * Defines the clause class
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "ast/Atom.h"
20 #include "ast/ExecutionPlan.h"
21 #include "ast/Literal.h"
22 #include "ast/Node.h"
23 #include "ast/utility/NodeMapper.h"
24 #include "parser/SrcLocation.h"
28 #include <algorithm>
29 #include <memory>
30 #include <ostream>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 
35 namespace souffle::ast {
36 
37 /**
38  * @class Clause
39  * @brief Intermediate representation of a horn clause
40  *
41  * A clause can either be:
42  * - a fact - a clause with no body (e.g., X(a,b))
43  * - a rule - a clause with a head and a body (e.g., Y(a,b) -: X(a,b))
44  */
45 class Clause : public Node {
46 public:
47  Clause(Own<Atom> head = {}, VecOwn<Literal> bodyLiterals = {}, Own<ExecutionPlan> plan = {},
48  SrcLocation loc = {})
49  : Node(std::move(loc)), head(std::move(head)), bodyLiterals(std::move(bodyLiterals)),
50  plan(std::move(plan)) {}
51 
52  /** Add a literal to the body of the clause */
53  void addToBody(Own<Literal> literal) {
54  bodyLiterals.push_back(std::move(literal));
55  }
56 
57  /** Set the head of clause to @p h */
58  void setHead(Own<Atom> h) {
59  head = std::move(h);
60  }
61 
62  /** Set the bodyLiterals of clause to @p body */
63  void setBodyLiterals(VecOwn<Literal> body) {
64  bodyLiterals = std::move(body);
65  }
66 
67  /** Return the atom that represents the head of the clause */
68  Atom* getHead() const {
69  return head.get();
70  }
71 
72  /** Obtains a copy of the internally maintained body literals */
73  std::vector<Literal*> getBodyLiterals() const {
75  }
76 
77  /** Obtains the execution plan associated to this clause or null if there is none */
78  const ExecutionPlan* getExecutionPlan() const {
79  return plan.get();
80  }
81 
82  /** Updates the execution plan associated to this clause */
84  this->plan = std::move(plan);
85  }
86 
87  /** Resets the execution plan */
88  void clearExecutionPlan() {
89  plan = nullptr;
90  }
91 
92  Clause* clone() const override {
93  return new Clause(
95  }
96 
97  void apply(const NodeMapper& map) override {
98  head = map(std::move(head));
99  for (auto& lit : bodyLiterals) {
100  lit = map(std::move(lit));
101  }
102  }
103 
104  std::vector<const Node*> getChildNodes() const override {
105  std::vector<const Node*> res = {head.get()};
106  for (auto& cur : bodyLiterals) {
107  res.push_back(cur.get());
108  }
109  return res;
110  }
111 
112 protected:
113  void print(std::ostream& os) const override {
114  if (head != nullptr) {
115  os << *head;
116  }
117  if (!bodyLiterals.empty()) {
118  os << " :- \n " << join(bodyLiterals, ",\n ");
119  }
120  os << ".";
121  if (plan != nullptr) {
122  os << *plan;
123  }
124  }
125 
126  bool equal(const Node& node) const override {
127  const auto& other = static_cast<const Clause&>(node);
128  return equal_ptr(head, other.head) && equal_targets(bodyLiterals, other.bodyLiterals) &&
129  equal_ptr(plan, other.plan);
130  }
131 
132  /** Head of the clause */
133  Own<Atom> head;
134 
135  /** Body literals of clause */
137 
138  /** User defined execution plan (if not defined, plan is null) */
140 };
141 
142 } // namespace souffle::ast
souffle::ast::Clause::addToBody
void addToBody(Own< Literal > literal)
Add a literal to the body of the clause.
Definition: Clause.h:59
ExecutionPlan.h
SrcLocation.h
souffle::ast::Clause::getExecutionPlan
const ExecutionPlan * getExecutionPlan() const
Obtains the execution plan associated to this clause or null if there is none.
Definition: Clause.h:84
souffle::ast::NodeMapper
An abstract class for manipulating AST Nodes by substitution.
Definition: NodeMapper.h:36
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
souffle::map
auto map(const std::vector< A > &xs, F &&f)
Applies a function to each element of a vector and returns the results.
Definition: ContainerUtil.h:158
MiscUtil.h
souffle::ast::Clause::head
Own< Atom > head
Head of the clause.
Definition: Clause.h:139
souffle::ast::Clause
Intermediate representation of a horn clause.
Definition: Clause.h:51
souffle::ast::ExecutionPlan
Definition: ExecutionPlan.h:51
souffle::ast::Atom
An atom class.
Definition: Atom.h:51
souffle::ast::Clause::setHead
void setHead(Own< Atom > h)
Set the head of clause to h.
Definition: Clause.h:64
souffle::ast::Clause::getHead
Atom * getHead() const
Return the atom that represents the head of the clause.
Definition: Clause.h:74
NodeMapper.h
souffle::ast::Clause::clearExecutionPlan
void clearExecutionPlan()
Resets the execution plan.
Definition: Clause.h:94
souffle::ast::Clause::equal
bool equal(const Node &node) const override
Abstract equality check for two AST nodes.
Definition: Clause.h:132
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
ContainerUtil.h
souffle::join
detail::joined_sequence< Iter, Printer > join(const Iter &a, const Iter &b, const std::string &sep, const Printer &p)
Creates an object to be forwarded to some output stream for printing sequences of elements interspers...
Definition: StreamUtil.h:175
souffle::equal_targets
bool equal_targets(const Container &a, const Container &b, const Comparator &comp)
A function testing whether two containers are equal with the given Comparator.
Definition: ContainerUtil.h:433
Atom.h
Literal.h
souffle::equal_ptr
bool equal_ptr(const T *a, const T *b)
Compares two values referenced by a pointer where the case where both pointers are null is also consi...
Definition: MiscUtil.h:130
Node.h
souffle::ast::Clause::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: Clause.h:103
souffle::ast::Clause::setExecutionPlan
void setExecutionPlan(Own< ExecutionPlan > plan)
Updates the execution plan associated to this clause.
Definition: Clause.h:89
souffle::ast::Clause::plan
Own< ExecutionPlan > plan
User defined execution plan (if not defined, plan is null)
Definition: Clause.h:145
StreamUtil.h
souffle::ast::Clause::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain a list of all embedded AST child nodes.
Definition: Clause.h:110
souffle::ast::Node::Node
Node(SrcLocation loc={})
Definition: Node.h:42
souffle::ast::Clause::setBodyLiterals
void setBodyLiterals(VecOwn< Literal > body)
Set the bodyLiterals of clause to body.
Definition: Clause.h:69
souffle::ast::Node::getSrcLoc
const SrcLocation & getSrcLoc() const
Return source location of the Node.
Definition: Node.h:46
souffle::ast
Definition: Aggregator.h:35
souffle::ast::Clause::print
void print(std::ostream &os) const override
Output to a given output stream.
Definition: Clause.h:119
souffle::toPtrVector
std::vector< T * > toPtrVector(const std::vector< std::unique_ptr< T >> &v)
A utility function enabling the creation of a vector of pointers.
Definition: ContainerUtil.h:146
souffle::VecOwn
std::vector< Own< A > > VecOwn
Definition: ContainerUtil.h:45
souffle::ast::Clause::clone
Clause * clone() const override
Create a clone (i.e.
Definition: Clause.h:98
souffle::ast::Clause::Clause
Clause(Own< Atom > head={}, VecOwn< Literal > bodyLiterals={}, Own< ExecutionPlan > plan={}, SrcLocation loc={})
Definition: Clause.h:53
souffle::ast::Clause::getBodyLiterals
std::vector< Literal * > getBodyLiterals() const
Obtains a copy of the internally maintained body literals.
Definition: Clause.h:79
souffle::ast::Clause::bodyLiterals
VecOwn< Literal > bodyLiterals
Body literals of clause.
Definition: Clause.h:142