souffle  2.0.2-371-g6315b36
AbstractExistenceCheck.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 AbstractExistenceCheck.h
12  *
13  * Defines a class for evaluating conditions in the Relational Algebra
14  * Machine.
15  *
16  ***********************************************************************/
17 
18 #pragma once
19 
20 #include "ram/Condition.h"
21 #include "ram/Expression.h"
22 #include "ram/Node.h"
23 #include "ram/Relation.h"
24 #include "ram/utility/NodeMapper.h"
27 #include <cassert>
28 #include <memory>
29 #include <sstream>
30 #include <string>
31 #include <utility>
32 #include <vector>
33 
34 namespace souffle::ram {
35 
36 /**
37  * @class AbstractExistenceCheck
38  * @brief Abstract existence check for a tuple in a relation
39  */
40 class AbstractExistenceCheck : public Condition {
41 public:
42  AbstractExistenceCheck(std::string rel, VecOwn<Expression> vals)
43  : relation(std::move(rel)), values(std::move(vals)) {
44  for (const auto& v : values) {
45  assert(v != nullptr && "NULL value");
46  }
47  }
48 
49  /** @brief Get relation */
50  const std::string& getRelation() const {
51  return relation;
52  }
53 
54  /**
55  * @brief Get arguments of the tuple/pattern
56  * A null pointer element in the vector denotes an unspecified
57  * pattern for a tuple element.
58  */
59  const std::vector<Expression*> getValues() const {
60  return toPtrVector(values);
61  }
62 
63  std::vector<const Node*> getChildNodes() const override {
64  std::vector<const Node*> res;
65  for (const auto& cur : values) {
66  res.push_back(cur.get());
67  }
68  return res;
69  }
70 
71  void apply(const NodeMapper& map) override {
72  for (auto& val : values) {
73  val = map(std::move(val));
74  }
75  }
76 
77 protected:
78  void print(std::ostream& os) const override {
79  os << "(" << join(values, ",") << ") ∈ " << relation;
80  }
81 
82  bool equal(const Node& node) const override {
83  const auto& other = static_cast<const AbstractExistenceCheck&>(node);
84  return relation == other.relation && equal_targets(values, other.values);
85  }
86 
87  /** Relation */
88  const std::string relation;
89 
90  /** Search tuple */
92 };
93 
94 } // namespace souffle::ram
souffle::ram::AbstractExistenceCheck::getValues
const std::vector< Expression * > getValues() const
Get arguments of the tuple/pattern A null pointer element in the vector denotes an unspecified patter...
Definition: AbstractExistenceCheck.h:66
souffle::ram::AbstractExistenceCheck::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: AbstractExistenceCheck.h:78
souffle::ram::AbstractExistenceCheck::print
void print(std::ostream &os) const override
Print RAM node.
Definition: AbstractExistenceCheck.h:85
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
souffle::ram::AbstractExistenceCheck::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain list of all embedded child nodes.
Definition: AbstractExistenceCheck.h:70
souffle::ram
Definition: AstToRamTranslator.h:54
souffle::ram::Node
Node is a superclass for all RAM IR classes.
Definition: Node.h:42
NodeMapper.h
souffle::ram::NodeMapper
An abstract class for manipulating RAM Nodes by substitution.
Definition: NodeMapper.h:38
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::AbstractExistenceCheck
Abstract existence check for a tuple in a relation.
Definition: AbstractExistenceCheck.h:47
Condition.h
souffle::ram::AbstractExistenceCheck::getRelation
const std::string & getRelation() const
Get relation.
Definition: AbstractExistenceCheck.h:57
souffle::ram::AbstractExistenceCheck::relation
const std::string relation
Relation.
Definition: AbstractExistenceCheck.h:95
Node.h
std
Definition: Brie.h:3053
StreamUtil.h
souffle::ram::AbstractExistenceCheck::values
VecOwn< Expression > values
Search tuple.
Definition: AbstractExistenceCheck.h:98
Expression.h
souffle::ram::AbstractExistenceCheck::AbstractExistenceCheck
AbstractExistenceCheck(std::string rel, VecOwn< Expression > vals)
Definition: AbstractExistenceCheck.h:49
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086
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::ram::AbstractExistenceCheck::equal
bool equal(const Node &node) const override
Equality check for two RAM nodes.
Definition: AbstractExistenceCheck.h:89