souffle  2.0.2-371-g6315b36
Relation.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2018, The Souffle Developers. 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 #pragma once
10 
11 #include "ram/Relation.h"
12 #include "ram/analysis/Index.h"
13 #include <cstddef>
14 #include <cstdint>
15 #include <memory>
16 #include <ostream>
17 #include <set>
18 #include <string>
19 #include <unordered_set>
20 #include <vector>
21 
22 namespace souffle::synthesiser {
23 
24 using ram::analysis::MinIndexSelection;
25 
26 class Relation {
27 public:
28  Relation(const ram::Relation& rel, const MinIndexSelection& indices, const bool isProvenance = false)
29  : relation(rel), indices(indices), isProvenance(isProvenance) {}
30 
31  virtual ~Relation() = default;
32 
33  /** Compute the final list of indices to be used */
34  virtual void computeIndices() = 0;
35 
36  /** Get arity of relation */
37  size_t getArity() const {
38  return relation.getArity();
39  }
40 
41  /** Get data structure of relation */
42  const std::string& getDataStructure() const {
43  return dataStructure;
44  }
45 
46  /** Get list of indices used for relation,
47  * guaranteed that original indices in analysis::MinIndexSelection
48  * come before any generated indices */
50  return computedIndices;
51  }
52 
53  std::set<int> getProvenenceIndexNumbers() const {
55  }
56 
57  /** Get stored analysis::MinIndexSelection */
59  return indices;
60  }
61 
62  /** Get stored ram::Relation */
63  const ram::Relation& getRelation() const {
64  return relation;
65  }
66 
67  /** Print type name */
68  virtual std::string getTypeName() = 0;
69 
70  /** Helper function to convert attribute types to a single string */
71  std::string getTypeAttributeString(const std::vector<std::string>& attributeTypes,
72  const std::unordered_set<uint32_t>& attributesUsed) const;
73 
74  /** Generate relation type struct */
75  virtual void generateTypeStruct(std::ostream& out) = 0;
76 
77  /** Factory method to generate a SynthesiserRelation */
78  static Own<Relation> getSynthesiserRelation(
79  const ram::Relation& ramRel, const MinIndexSelection& indexSet, bool isProvenance);
80 
81 protected:
82  /** Ram relation referred to by this */
84 
85  /** Indices used for this relation */
87 
88  /** The data structure used for the relation */
89  std::string dataStructure;
90 
91  /** The final list of indices used */
93 
94  /** The list of indices added for provenance computation */
95  std::set<int> provenanceIndexNumbers;
96 
97  /** The number of the master index */
98  size_t masterIndex = -1;
99 
100  /** Is this relation used with provenance */
101  const bool isProvenance;
102 };
103 
104 class NullaryRelation : public Relation {
105 public:
106  NullaryRelation(const ram::Relation& ramRel, const MinIndexSelection& indexSet, bool isProvenance)
107  : Relation(ramRel, indexSet, isProvenance) {}
108 
109  void computeIndices() override;
110  std::string getTypeName() override;
111  void generateTypeStruct(std::ostream& out) override;
112 };
113 
114 class InfoRelation : public Relation {
115 public:
116  InfoRelation(const ram::Relation& ramRel, const MinIndexSelection& indexSet, bool isProvenance)
117  : Relation(ramRel, indexSet, isProvenance) {}
118 
119  void computeIndices() override;
120  std::string getTypeName() override;
121  void generateTypeStruct(std::ostream& out) override;
122 };
123 
124 class DirectRelation : public Relation {
125 public:
126  DirectRelation(const ram::Relation& ramRel, const MinIndexSelection& indexSet, bool isProvenance)
127  : Relation(ramRel, indexSet, isProvenance) {}
128 
129  void computeIndices() override;
130  std::string getTypeName() override;
131  void generateTypeStruct(std::ostream& out) override;
132 };
133 
134 class IndirectRelation : public Relation {
135 public:
136  IndirectRelation(const ram::Relation& ramRel, const MinIndexSelection& indexSet, bool isProvenance)
137  : Relation(ramRel, indexSet, isProvenance) {}
138 
139  void computeIndices() override;
140  std::string getTypeName() override;
141  void generateTypeStruct(std::ostream& out) override;
142 };
143 
144 class BrieRelation : public Relation {
145 public:
146  BrieRelation(const ram::Relation& ramRel, const MinIndexSelection& indexSet, bool isProvenance)
147  : Relation(ramRel, indexSet, isProvenance) {}
148 
149  void computeIndices() override;
150  std::string getTypeName() override;
151  void generateTypeStruct(std::ostream& out) override;
152 };
153 
154 class EqrelRelation : public Relation {
155 public:
156  EqrelRelation(const ram::Relation& ramRel, const MinIndexSelection& indexSet, bool isProvenance)
157  : Relation(ramRel, indexSet, isProvenance) {}
158 
159  void computeIndices() override;
160  std::string getTypeName() override;
161  void generateTypeStruct(std::ostream& out) override;
162 };
163 } // namespace souffle::synthesiser
souffle::synthesiser::Relation::computedIndices
MinIndexSelection::OrderCollection computedIndices
The final list of indices used.
Definition: Relation.h:92
souffle::synthesiser::InfoRelation::generateTypeStruct
void generateTypeStruct(std::ostream &out) override
Generate type struct of a info relation, which is empty, the actual implementation is in CompiledSouf...
Definition: Relation.cpp:92
souffle::synthesiser::InfoRelation::getTypeName
std::string getTypeName() override
Generate type name of a info relation.
Definition: Relation.cpp:86
souffle::synthesiser::Relation::provenanceIndexNumbers
std::set< int > provenanceIndexNumbers
The list of indices added for provenance computation.
Definition: Relation.h:95
souffle::synthesiser::BrieRelation::generateTypeStruct
void generateTypeStruct(std::ostream &out) override
Generate type struct of a brie relation.
Definition: Relation.cpp:904
Index.h
souffle::synthesiser::NullaryRelation::generateTypeStruct
void generateTypeStruct(std::ostream &out) override
Generate type struct of a nullary relation, which is empty, the actual implementation is in CompiledS...
Definition: Relation.cpp:110
souffle::synthesiser::IndirectRelation::getTypeName
std::string getTypeName() override
Generate type name of a indirect indexed relation.
Definition: Relation.cpp:535
souffle::synthesiser::DirectRelation
Definition: Relation.h:124
souffle::synthesiser::NullaryRelation::computeIndices
void computeIndices() override
Generate index set for a nullary relation, which should be empty.
Definition: Relation.cpp:99
souffle::synthesiser::EqrelRelation::generateTypeStruct
void generateTypeStruct(std::ostream &out) override
Generate type struct of a eqrel relation.
Definition: Relation.cpp:1157
souffle::synthesiser::Relation::getArity
size_t getArity() const
Get arity of relation.
Definition: Relation.h:37
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
relation
Relation & relation
Definition: Reader.h:130
souffle::synthesiser::DirectRelation::DirectRelation
DirectRelation(const ram::Relation &ramRel, const MinIndexSelection &indexSet, bool isProvenance)
Definition: Relation.h:126
souffle::synthesiser::NullaryRelation
Definition: Relation.h:104
souffle::synthesiser::Relation::indices
const MinIndexSelection & indices
Indices used for this relation.
Definition: Relation.h:86
souffle::synthesiser::IndirectRelation::IndirectRelation
IndirectRelation(const ram::Relation &ramRel, const MinIndexSelection &indexSet, bool isProvenance)
Definition: Relation.h:136
souffle::synthesiser::DirectRelation::getTypeName
std::string getTypeName() override
Generate type name of a direct indexed relation.
Definition: Relation.cpp:164
souffle::synthesiser::BrieRelation::BrieRelation
BrieRelation(const ram::Relation &ramRel, const MinIndexSelection &indexSet, bool isProvenance)
Definition: Relation.h:146
souffle::synthesiser::InfoRelation::computeIndices
void computeIndices() override
Generate index set for a info relation, which should be empty.
Definition: Relation.cpp:81
souffle::synthesiser::Relation::isProvenance
const bool isProvenance
Is this relation used with provenance.
Definition: Relation.h:101
souffle::synthesiser::DirectRelation::computeIndices
void computeIndices() override
Generate index set for a direct indexed relation.
Definition: Relation.cpp:117
souffle::synthesiser::Relation::getRelation
const ram::Relation & getRelation() const
Get stored ram::Relation.
Definition: Relation.h:63
souffle::ram::Relation
An abstract class for performing indexed operations.
Definition: Relation.h:40
souffle::synthesiser::InfoRelation::InfoRelation
InfoRelation(const ram::Relation &ramRel, const MinIndexSelection &indexSet, bool isProvenance)
Definition: Relation.h:116
Relation.h
souffle::synthesiser::EqrelRelation::getTypeName
std::string getTypeName() override
Generate type name of a eqrel relation.
Definition: Relation.cpp:1152
souffle::synthesiser
Souffle - A Datalog Compiler Copyright (c) 2013, 2015, Oracle and/or its affiliates.
Definition: Relation.cpp:22
souffle::ram::analysis::MinIndexSelection
Definition: Index.h:208
souffle::synthesiser::NullaryRelation::getTypeName
std::string getTypeName() override
Generate type name of a nullary relation.
Definition: Relation.cpp:104
souffle::synthesiser::IndirectRelation::computeIndices
void computeIndices() override
Generate index set for a indirect indexed relation.
Definition: Relation.cpp:513
souffle::synthesiser::InfoRelation
Definition: Relation.h:114
souffle::synthesiser::IndirectRelation::generateTypeStruct
void generateTypeStruct(std::ostream &out) override
Generate type struct of a indirect indexed relation.
Definition: Relation.cpp:559
souffle::synthesiser::DirectRelation::generateTypeStruct
void generateTypeStruct(std::ostream &out) override
Generate type struct of a direct indexed relation.
Definition: Relation.cpp:188
souffle::synthesiser::Relation::masterIndex
size_t masterIndex
The number of the master index.
Definition: Relation.h:98
souffle::synthesiser::EqrelRelation
Definition: Relation.h:154
souffle::synthesiser::Relation::dataStructure
std::string dataStructure
The data structure used for the relation.
Definition: Relation.h:89
souffle::ram::Relation::attributeTypes
const std::vector< std::string > attributeTypes
Type of attributes.
Definition: Relation.h:148
souffle::synthesiser::Relation::getIndices
MinIndexSelection::OrderCollection getIndices() const
Get list of indices used for relation, guaranteed that original indices in analysis::MinIndexSelectio...
Definition: Relation.h:49
souffle::synthesiser::BrieRelation::computeIndices
void computeIndices() override
Generate index set for a brie relation.
Definition: Relation.cpp:849
souffle::synthesiser::BrieRelation::getTypeName
std::string getTypeName() override
Generate type name of a brie relation.
Definition: Relation.cpp:880
souffle::synthesiser::Relation::getProvenenceIndexNumbers
std::set< int > getProvenenceIndexNumbers() const
Definition: Relation.h:53
souffle::synthesiser::Relation::Relation
Relation(const ram::Relation &rel, const MinIndexSelection &indices, const bool isProvenance=false)
Definition: Relation.h:28
souffle::synthesiser::Relation::relation
const ram::Relation & relation
Ram relation referred to by this.
Definition: Relation.h:83
souffle::synthesiser::Relation::getMinIndexSelection
const MinIndexSelection & getMinIndexSelection() const
Get stored analysis::MinIndexSelection.
Definition: Relation.h:58
souffle::synthesiser::EqrelRelation::computeIndices
void computeIndices() override
Generate index set for a eqrel relation.
Definition: Relation.cpp:1143
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086
souffle::synthesiser::BrieRelation
Definition: Relation.h:144
souffle::ram::analysis::MinIndexSelection::OrderCollection
std::vector< LexOrder > OrderCollection
Definition: Index.h:217
souffle::synthesiser::EqrelRelation::EqrelRelation
EqrelRelation(const ram::Relation &ramRel, const MinIndexSelection &indexSet, bool isProvenance)
Definition: Relation.h:156
souffle::synthesiser::IndirectRelation
Definition: Relation.h:134
souffle::synthesiser::NullaryRelation::NullaryRelation
NullaryRelation(const ram::Relation &ramRel, const MinIndexSelection &indexSet, bool isProvenance)
Definition: Relation.h:106
souffle::synthesiser::Relation::getDataStructure
const std::string & getDataStructure() const
Get data structure of relation.
Definition: Relation.h:42