souffle  2.0.2-371-g6315b36
Aggregate.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, 2014, 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 Aggregate.h
12  *
13  ***********************************************************************/
14 
15 #pragma once
16 
17 #include "AggregateOp.h"
18 #include "ram/AbstractAggregate.h"
19 #include "ram/Condition.h"
20 #include "ram/Expression.h"
21 #include "ram/Node.h"
22 #include "ram/Operation.h"
23 #include "ram/Relation.h"
24 #include "ram/RelationOperation.h"
25 #include "ram/utility/NodeMapper.h"
26 #include "ram/utility/Utils.h"
29 #include <iosfwd>
30 #include <memory>
31 #include <ostream>
32 #include <string>
33 #include <utility>
34 #include <vector>
35 
36 namespace souffle::ram {
37 
38 /**
39  * @class Aggregate
40  * @brief Aggregation function applied on some relation
41  *
42  * For example:
43  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
44  * t0.0 = COUNT FOR ALL t0 IN A
45  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
46  * Applies the function COUNT to determine the number
47  * of elements in A.
48  */
49 class Aggregate : public RelationOperation, public AbstractAggregate {
50 public:
51  Aggregate(Own<Operation> nested, AggregateOp fun, std::string rel, Own<Expression> expression,
52  Own<Condition> condition, int ident)
53  : RelationOperation(rel, ident, std::move(nested)),
54  AbstractAggregate(fun, std::move(expression), std::move(condition)) {}
55 
56  std::vector<const Node*> getChildNodes() const override {
58  auto children = AbstractAggregate::getChildNodes();
59  res.insert(res.end(), children.begin(), children.end());
60  return res;
61  }
62 
63  Aggregate* clone() const override {
66  }
67 
68  void apply(const NodeMapper& map) override {
70  condition = map(std::move(condition));
71  expression = map(std::move(expression));
72  }
73 
74 protected:
75  void print(std::ostream& os, int tabpos) const override {
76  os << times(" ", tabpos);
77  os << "t" << getTupleId() << ".0=";
78  AbstractAggregate::print(os, tabpos);
79  os << "FOR ALL t" << getTupleId() << " ∈ " << getRelation();
80  if (!isTrue(condition.get())) {
81  os << " WHERE " << getCondition();
82  }
83  os << std::endl;
84  RelationOperation::print(os, tabpos + 1);
85  }
86 
87  bool equal(const Node& node) const override {
88  const auto& other = static_cast<const Aggregate&>(node);
90  }
91 };
92 
93 } // namespace souffle::ram
souffle::ram::AbstractAggregate::equal
bool equal(const Node &node) const
Definition: AbstractAggregate.h:94
souffle::ram::TupleOperation::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain list of all embedded child nodes.
Definition: TupleOperation.h:52
AggregateOp.h
souffle::ram::AbstractAggregate::getCondition
const Condition & getCondition() const
Get condition.
Definition: AbstractAggregate.h:53
souffle::ram::NestedOperation::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: NestedOperation.h:75
souffle::AggregateOp
AggregateOp
Types of aggregation functions.
Definition: AggregateOp.h:34
souffle::ram::NestedOperation::getOperation
Operation & getOperation() const
Get nested operation.
Definition: NestedOperation.h:62
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::ram::Aggregate::Aggregate
Aggregate(Own< Operation > nested, AggregateOp fun, std::string rel, Own< Expression > expression, Own< Condition > condition, int ident)
Definition: Aggregate.h:55
souffle::ram::Aggregate::equal
bool equal(const Node &node) const override
Equality check for two RAM nodes.
Definition: Aggregate.h:91
souffle::ram::RelationOperation::getRelation
const std::string & getRelation() const
Get search relation.
Definition: RelationOperation.h:49
souffle::ram::Aggregate::clone
Aggregate * clone() const override
Create a clone (i.e.
Definition: Aggregate.h:67
RelationOperation.h
souffle::ram::AbstractAggregate::print
void print(std::ostream &os, int) const
Definition: AbstractAggregate.h:74
souffle::ram
Definition: AstToRamTranslator.h:54
souffle::ram::RelationOperation::relation
const std::string relation
Search relation.
Definition: RelationOperation.h:60
souffle::ram::AbstractAggregate
Abstract class for aggregation.
Definition: AbstractAggregate.h:42
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
NodeMapper.h
souffle::ram::NodeMapper
An abstract class for manipulating RAM Nodes by substitution.
Definition: NodeMapper.h:38
souffle::times
detail::multiplying_printer< T > times(const T &value, unsigned num)
A utility printing a given value multiple times.
Definition: StreamUtil.h:322
Relation.h
souffle::ram::AbstractAggregate::expression
Own< Expression > expression
Aggregation expression.
Definition: AbstractAggregate.h:104
souffle::ram::Aggregate
Aggregation function applied on some relation.
Definition: Aggregate.h:53
Condition.h
souffle::ram::Aggregate::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: Aggregate.h:72
souffle::ram::RelationOperation::equal
bool equal(const Node &node) const override
Equality check for two RAM nodes.
Definition: RelationOperation.h:54
souffle::ram::RelationOperation
Abstract class for operations on relations.
Definition: RelationOperation.h:41
souffle::ram::TupleOperation::getTupleId
int getTupleId() const
Get identifier.
Definition: TupleOperation.h:43
Utils.h
souffle::ram::Aggregate::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain list of all embedded child nodes.
Definition: Aggregate.h:60
souffle::ram::isTrue
bool isTrue(const Condition *cond)
Determines if a condition represents true.
Definition: Utils.h:45
souffle::ram::Aggregate::print
void print(std::ostream &os, int tabpos) const override
Pretty print with indentation.
Definition: Aggregate.h:79
Node.h
std
Definition: Brie.h:3053
Operation.h
StreamUtil.h
AbstractAggregate.h
Expression.h
souffle::ram::AbstractAggregate::getChildNodes
std::vector< const Node * > getChildNodes() const
Definition: AbstractAggregate.h:69
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086
souffle::ram::NestedOperation::print
void print(std::ostream &os, int tabpos) const override
Pretty print with indentation.
Definition: NestedOperation.h:80
souffle::ram::AbstractAggregate::condition
Own< Condition > condition
Aggregation tuple condition.
Definition: AbstractAggregate.h:107