souffle  2.0.2-371-g6315b36
Relation.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 Relation.h
12  *
13  * Defines the class for ram relations
14  ***********************************************************************/
15 
16 #pragma once
17 
18 #include "RelationTag.h"
19 #include "ram/Node.h"
21 #include <cassert>
22 #include <cstddef>
23 #include <memory>
24 #include <ostream>
25 #include <string>
26 #include <utility>
27 #include <vector>
28 
29 namespace souffle::ram {
30 
31 /**
32  * @class Relation
33  * @brief A RAM Relation in the RAM intermediate representation.
34  */
35 class Relation : public Node {
36 public:
37  Relation(std::string name, size_t arity, size_t auxiliaryArity, std::vector<std::string> attributeNames,
38  std::vector<std::string> attributeTypes, RelationRepresentation representation)
42  assert(this->attributeNames.size() == arity && "arity mismatch for attributes");
43  assert(this->attributeTypes.size() == arity && "arity mismatch for types");
44  for (std::size_t i = 0; i < arity; i++) {
45  assert(!this->attributeNames[i].empty() && "no attribute name specified");
46  assert(!this->attributeTypes[i].empty() && "no attribute type specified");
47  }
48  }
49 
50  /** @brief Get name */
51  const std::string& getName() const {
52  return name;
53  }
54 
55  /** @brief Get attribute types */
56  const std::vector<std::string>& getAttributeTypes() const {
57  return attributeTypes;
58  }
59 
60  /** @brief Get attribute names */
61  const std::vector<std::string>& getAttributeNames() const {
62  return attributeNames;
63  }
64 
65  /** @brief Is nullary relation */
66  bool isNullary() const {
67  return arity == 0;
68  }
69 
70  /** @brief Relation representation type */
72  return representation;
73  }
74 
75  /** @brief Is temporary relation (for semi-naive evaluation) */
76  bool isTemp() const {
77  return name.at(0) == '@';
78  }
79 
80  /** @brief Get arity of relation */
81  unsigned getArity() const {
82  return arity;
83  }
84 
85  /** @brief Get number of auxiliary attributes */
86  unsigned getAuxiliaryArity() const {
87  return auxiliaryArity;
88  }
89 
90  /** @brief Compare two relations via their name */
91  bool operator<(const Relation& other) const {
92  return name < other.name;
93  }
94 
95  Relation* clone() const override {
97  }
98 
99 protected:
100  void print(std::ostream& out) const override {
101  out << name;
102  if (arity > 0) {
103  out << "(" << attributeNames[0] << ":" << attributeTypes[0];
104  for (unsigned i = 1; i < arity; i++) {
105  out << ",";
106  out << attributeNames[i] << ":" << attributeTypes[i];
107  if (i >= arity - auxiliaryArity) {
108  out << " auxiliary";
109  }
110  }
111  out << ")";
112  out << " " << representation;
113  } else {
114  out << " nullary";
115  }
116  }
117 
118  bool equal(const Node& node) const override {
119  assert(isA<Relation>(&node));
120  const auto& other = static_cast<const Relation&>(node);
121  return representation == other.representation && name == other.name && arity == other.arity &&
122  auxiliaryArity == other.auxiliaryArity && attributeNames == other.attributeNames &&
123  attributeTypes == other.attributeTypes;
124  }
125 
126 protected:
127  /** Data-structure representation */
129 
130  /** Name of relation */
131  const std::string name;
132 
133  /** Arity, i.e., number of attributes */
134  const size_t arity;
135 
136  /** Number of auxiliary attributes (e.g. provenance attributes etc) */
137  const size_t auxiliaryArity;
138 
139  /** Name of attributes */
140  const std::vector<std::string> attributeNames;
141 
142  /** Type of attributes */
143  const std::vector<std::string> attributeTypes;
144 };
145 
146 } // namespace souffle::ram
souffle::ram::Relation::name
const std::string name
Name of relation.
Definition: Relation.h:136
souffle::ram::Relation::isNullary
bool isNullary() const
Is nullary relation.
Definition: Relation.h:71
souffle::ram::Relation::arity
const size_t arity
Arity, i.e., number of attributes.
Definition: Relation.h:139
souffle::ram::Relation::equal
bool equal(const Node &node) const override
Equality check for two RAM nodes.
Definition: Relation.h:123
souffle::ram::Relation::attributeNames
const std::vector< std::string > attributeNames
Name of attributes.
Definition: Relation.h:145
souffle::ram::Relation::getAttributeTypes
const std::vector< std::string > & getAttributeTypes() const
Get attribute types.
Definition: Relation.h:61
souffle::ram::Relation::auxiliaryArity
const size_t auxiliaryArity
Number of auxiliary attributes (e.g.
Definition: Relation.h:142
souffle::ram::Relation::print
void print(std::ostream &out) const override
Print RAM node.
Definition: Relation.h:105
souffle::ram::Relation::Relation
Relation(std::string name, size_t arity, size_t auxiliaryArity, std::vector< std::string > attributeNames, std::vector< std::string > attributeTypes, RelationRepresentation representation)
Definition: Relation.h:42
souffle::ram::Relation::getArity
unsigned getArity() const
Get arity of relation.
Definition: Relation.h:86
souffle::ram::Relation::representation
const RelationRepresentation representation
Data-structure representation.
Definition: Relation.h:133
souffle::ram
Definition: AstToRamTranslator.h:54
i
size_t i
Definition: json11.h:663
ContainerUtil.h
RelationTag.h
souffle::ram::Relation::getAuxiliaryArity
unsigned getAuxiliaryArity() const
Get number of auxiliary attributes.
Definition: Relation.h:91
souffle::ram::Relation
An abstract class for performing indexed operations.
Definition: Relation.h:40
souffle::ram::Relation::operator<
bool operator<(const Relation &other) const
Compare two relations via their name.
Definition: Relation.h:96
souffle::ram::Relation::attributeTypes
const std::vector< std::string > attributeTypes
Type of attributes.
Definition: Relation.h:148
Node.h
std
Definition: Brie.h:3053
souffle::ram::Relation::getAttributeNames
const std::vector< std::string > & getAttributeNames() const
Get attribute names.
Definition: Relation.h:66
souffle::ram::Relation::getRepresentation
RelationRepresentation getRepresentation() const
Relation representation type.
Definition: Relation.h:76
souffle::ram::Relation::getName
const std::string & getName() const
Get name.
Definition: Relation.h:56
souffle::RelationRepresentation
RelationRepresentation
Space of internal representations that a relation can have.
Definition: RelationTag.h:56
souffle::ram::Relation::clone
Relation * clone() const override
Create a clone (i.e.
Definition: Relation.h:100
souffle::ram::Relation::isTemp
bool isTemp() const
Is temporary relation (for semi-naive evaluation)
Definition: Relation.h:81