souffle  2.0.2-371-g6315b36
Aggregator.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 Aggregator.h
12  *
13  * Defines the aggregator class
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "AggregateOp.h"
20 #include "ast/Argument.h"
21 #include "ast/Literal.h"
22 #include "ast/Node.h"
23 #include "ast/utility/NodeMapper.h"
24 #include "parser/SrcLocation.h"
28 #include <memory>
29 #include <optional>
30 #include <ostream>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 
35 namespace souffle::ast {
36 
37 /**
38  * @class Aggregator
39  * @brief Defines the aggregator class
40  *
41  * Example:
42  * sum y+x: {A(y),B(x)}
43  *
44  * Aggregates over a sub-query using an aggregate operator
45  * and an expression.
46  */
47 class Aggregator : public Argument {
48 public:
50  SrcLocation loc = {})
51  : Argument(std::move(loc)), baseOperator(baseOperator), targetExpression(std::move(expr)),
52  body(std::move(body)) {}
53 
54  /** Return the (base type) operator of the aggregator */
56  return baseOperator;
57  }
58 
59  /** Return target expression */
60  const Argument* getTargetExpression() const {
61  return targetExpression.get();
62  }
63 
64  /** Return body literals */
65  std::vector<Literal*> getBodyLiterals() const {
66  return toPtrVector(body);
67  }
68 
69  /** Set body */
70  void setBody(VecOwn<Literal> bodyLiterals) {
71  body = std::move(bodyLiterals);
72  }
73 
74  std::vector<const Node*> getChildNodes() const override {
75  auto res = Argument::getChildNodes();
77  res.push_back(targetExpression.get());
78  }
79  for (auto& cur : body) {
80  res.push_back(cur.get());
81  }
82  return res;
83  }
84 
85  Aggregator* clone() const override {
86  auto* copy = new Aggregator(
88  if (finalTranslatorType.has_value()) {
89  copy->setFinalType(finalTranslatorType.value());
90  }
91  return copy;
92  }
93 
94  void apply(const NodeMapper& map) override {
95  if (targetExpression) {
97  }
98  for (auto& cur : body) {
99  cur = map(std::move(cur));
100  }
101  }
102 
103  void setFinalType(AggregateOp newType) {
104  finalTranslatorType = newType;
105  }
106 
107  std::optional<AggregateOp> getFinalType() const {
108  return finalTranslatorType;
109  }
110 
111 protected:
112  void print(std::ostream& os) const override {
114  if (targetExpression) {
115  os << " " << *targetExpression;
116  }
117  os << " : { " << join(body) << " }";
118  }
119 
120  bool equal(const Node& node) const override {
121  const auto& other = static_cast<const Aggregator&>(node);
122  return baseOperator == other.baseOperator && equal_ptr(targetExpression, other.targetExpression) &&
123  equal_targets(body, other.body);
124  }
125 
126 private:
127  /** Aggregate (base type) operator */
129 
130  /** Aggregate expression */
132 
133  /** Body literal of sub-query */
135 
136  // TODO (azreika): remove after refactoring translator
137  std::optional<AggregateOp> finalTranslatorType;
138 };
139 
140 } // namespace souffle::ast
souffle::ast::Aggregator::setFinalType
void setFinalType(AggregateOp newType)
Definition: Aggregator.h:109
souffle::ast::Aggregator::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain a list of all embedded AST child nodes.
Definition: Aggregator.h:80
AggregateOp.h
SrcLocation.h
souffle::AggregateOp
AggregateOp
Types of aggregation functions.
Definition: AggregateOp.h:34
souffle::ast::Aggregator::body
VecOwn< Literal > body
Body literal of sub-query.
Definition: Aggregator.h:140
souffle::ast::NodeMapper
An abstract class for manipulating AST Nodes by substitution.
Definition: NodeMapper.h:36
souffle::ast::Aggregator::baseOperator
AggregateOp baseOperator
Aggregate (base type) operator.
Definition: Aggregator.h:134
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::Aggregator::equal
bool equal(const Node &node) const override
Abstract equality check for two AST nodes.
Definition: Aggregator.h:126
NodeMapper.h
souffle::ast::Argument
An abstract class for arguments.
Definition: Argument.h:33
Argument.h
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
Literal.h
souffle::ast::Aggregator::finalTranslatorType
std::optional< AggregateOp > finalTranslatorType
Definition: Aggregator.h:143
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
souffle::ast::Aggregator::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: Aggregator.h:100
Node.h
souffle::ast::Node::getChildNodes
virtual std::vector< const Node * > getChildNodes() const
Obtain a list of all embedded AST child nodes.
Definition: Node.h:82
souffle::detail::brie::copy
auto copy(span< A, arity > s)
Definition: Brie.h:98
souffle::ast::Node
Abstract class for syntactic elements in an input program.
Definition: Node.h:40
souffle::ast::Aggregator::getBodyLiterals
std::vector< Literal * > getBodyLiterals() const
Return body literals.
Definition: Aggregator.h:71
souffle::ast::Aggregator
Defines the aggregator class.
Definition: Aggregator.h:53
souffle::ast::Aggregator::clone
Aggregator * clone() const override
Create clone.
Definition: Aggregator.h:91
souffle::ast::Aggregator::getBaseOperator
AggregateOp getBaseOperator() const
Return the (base type) operator of the aggregator.
Definition: Aggregator.h:61
souffle::ast::Aggregator::print
void print(std::ostream &os) const override
Output to a given output stream.
Definition: Aggregator.h:118
souffle::ast::Aggregator::targetExpression
Own< Argument > targetExpression
Aggregate expression.
Definition: Aggregator.h:137
souffle::SrcLocation
A class describing a range in an input file.
Definition: SrcLocation.h:32
souffle::ast::Aggregator::getTargetExpression
const Argument * getTargetExpression() const
Return target expression.
Definition: Aggregator.h:66
StreamUtil.h
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::Aggregator::getFinalType
std::optional< AggregateOp > getFinalType() const
Definition: Aggregator.h:113
souffle::ast::Aggregator::setBody
void setBody(VecOwn< Literal > bodyLiterals)
Set body.
Definition: Aggregator.h:76
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::Aggregator::Aggregator
Aggregator(AggregateOp baseOperator, Own< Argument > expr=nullptr, VecOwn< Literal > body={}, SrcLocation loc={})
Definition: Aggregator.h:55