souffle  2.0.2-371-g6315b36
Project.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 Project.h
12  *
13  ***********************************************************************/
14 
15 #pragma once
16 
17 #include "ram/Expression.h"
18 #include "ram/Node.h"
19 #include "ram/Operation.h"
20 #include "ram/Relation.h"
21 #include "ram/utility/NodeMapper.h"
25 #include <cassert>
26 #include <iosfwd>
27 #include <memory>
28 #include <ostream>
29 #include <string>
30 #include <utility>
31 #include <vector>
32 
33 namespace souffle::ram {
34 
35 /**
36  * @class Project
37  * @brief Project a result into the target relation.
38  *
39  * For example:
40  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
41  * FOR t0 IN A
42  * ...
43  * PROJECT (t0.a, t0.b, t0.c) INTO @new_X
44  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
45  */
46 class Project : public Operation {
47 public:
48  Project(std::string rel, VecOwn<Expression> expressions)
49  : relation(std::move(rel)), expressions(std::move(expressions)) {
50  for (auto const& expr : expressions) {
51  assert(expr != nullptr && "Expression is a null-pointer");
52  }
53  }
54 
55  /** @brief Get relation */
56  const std::string& getRelation() const {
57  return relation;
58  }
59 
60  /** @brief Get expressions */
61  std::vector<Expression*> getValues() const {
62  return toPtrVector(expressions);
63  }
64 
65  std::vector<const Node*> getChildNodes() const override {
66  std::vector<const Node*> res;
67  for (const auto& expr : expressions) {
68  res.push_back(expr.get());
69  }
70  return res;
71  }
72 
73  Project* clone() const override {
74  VecOwn<Expression> newValues;
75  for (auto& expr : expressions) {
76  newValues.emplace_back(expr->clone());
77  }
78  return new Project(relation, std::move(newValues));
79  }
80 
81  void apply(const NodeMapper& map) override {
82  for (auto& expr : expressions) {
83  expr = map(std::move(expr));
84  }
85  }
86 
87 protected:
88  void print(std::ostream& os, int tabpos) const override {
89  os << times(" ", tabpos);
90  os << "PROJECT (" << join(expressions, ", ", print_deref<Own<Expression>>()) << ") INTO " << relation
91  << std::endl;
92  }
93 
94  bool equal(const Node& node) const override {
95  const auto& other = static_cast<const Project&>(node);
96  return relation == other.relation && equal_targets(expressions, other.expressions);
97  }
98 
99  /** Relation name */
100  std::string relation;
101 
102  /* Values (expressions) for projection */
104 };
105 
106 } // namespace souffle::ram
souffle::ram::Project::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain list of all embedded child nodes.
Definition: Project.h:69
souffle::ram::Project::expressions
VecOwn< Expression > expressions
Definition: Project.h:107
souffle::ram::Project::equal
bool equal(const Node &node) const override
Equality check for two RAM nodes.
Definition: Project.h:98
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::print_deref
A functor printing elements after dereferencing it.
Definition: StreamUtil.h:166
souffle::ram::Project::relation
std::string relation
Relation name.
Definition: Project.h:104
souffle::ram
Definition: AstToRamTranslator.h:54
souffle::ram::Project::clone
Project * clone() const override
Create a clone (i.e.
Definition: Project.h:77
souffle::ram::Project::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: Project.h:85
souffle::ram::Node
Node is a superclass for all RAM IR classes.
Definition: Node.h:42
souffle::ram::Project::Project
Project(std::string rel, VecOwn< Expression > expressions)
Definition: Project.h:52
NodeMapper.h
souffle::times
detail::multiplying_printer< T > times(const T &value, unsigned num)
A utility printing a given value multiple times.
Definition: StreamUtil.h:322
souffle::ram::Project
Project a result into the target relation.
Definition: Project.h:50
ContainerUtil.h
Relation.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
souffle::ram::Project::getValues
std::vector< Expression * > getValues() const
Get expressions.
Definition: Project.h:65
souffle::ram::Project::getRelation
const std::string & getRelation() const
Get relation.
Definition: Project.h:60
Node.h
std
Definition: Brie.h:3053
Operation.h
StreamUtil.h
Expression.h
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086
souffle::ram::Project::print
void print(std::ostream &os, int tabpos) const override
Pretty print with indentation.
Definition: Project.h:92
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