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, 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 relation class and helper its classes
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "RelationTag.h"
20 #include "ast/Attribute.h"
21 #include "ast/Node.h"
22 #include "ast/QualifiedName.h"
23 #include "ast/utility/NodeMapper.h"
24 #include "parser/SrcLocation.h"
27 #include <algorithm>
28 #include <cassert>
29 #include <cstddef>
30 #include <memory>
31 #include <ostream>
32 #include <set>
33 #include <string>
34 #include <utility>
35 #include <vector>
36 
37 namespace souffle::ast {
38 
39 /**
40  * @class Relation
41  * @brief Defines a relation with a name, attributes, qualifiers, and internal representation.
42  */
43 class Relation : public Node {
44 public:
45  Relation() = default;
46  Relation(QualifiedName name, SrcLocation loc = {}) : name(std::move(name)) {
47  setSrcLoc(std::move(loc));
48  }
49 
50  /** Get qualified relation name */
51  const QualifiedName& getQualifiedName() const {
52  return name;
53  }
54 
55  /** Set name for this relation */
57  name = std::move(n);
58  }
59 
60  /** Add a new used type to this relation */
61  void addAttribute(Own<Attribute> attr) {
62  assert(attr && "Undefined attribute");
63  attributes.push_back(std::move(attr));
64  }
65 
66  /** Return the arity of this relation */
67  size_t getArity() const {
68  return attributes.size();
69  }
70 
71  /** Set relation attributes */
72  void setAttributes(VecOwn<Attribute> attrs) {
73  attributes = std::move(attrs);
74  }
75 
76  /** Get relation attributes */
77  std::vector<Attribute*> getAttributes() const {
79  }
80 
81  /** Get relation qualifiers */
82  const std::set<RelationQualifier>& getQualifiers() const {
83  return qualifiers;
84  }
85 
86  /** Add qualifier to this relation */
88  qualifiers.insert(q);
89  }
90 
91  /** Remove qualifier from this relation */
93  qualifiers.erase(q);
94  }
95 
96  /** Get relation representation */
99  }
100 
101  /** Set relation representation */
103  this->representation = representation;
104  }
105 
106  /** Check for a relation qualifier */
107  bool hasQualifier(RelationQualifier q) const {
108  return qualifiers.find(q) != qualifiers.end();
109  }
110 
111  Relation* clone() const override {
112  auto res = new Relation(name, getSrcLoc());
113  res->attributes = souffle::clone(attributes);
114  res->qualifiers = qualifiers;
115  res->representation = representation;
116  return res;
117  }
118 
119  void apply(const NodeMapper& map) override {
120  for (auto& cur : attributes) {
121  cur = map(std::move(cur));
122  }
123  }
124 
125  std::vector<const Node*> getChildNodes() const override {
126  std::vector<const Node*> res;
127  for (const auto& cur : attributes) {
128  res.push_back(cur.get());
129  }
130  return res;
131  }
132 
133 protected:
134  void print(std::ostream& os) const override {
135  os << ".decl " << getQualifiedName() << "(" << join(attributes, ", ") << ")" << join(qualifiers, " ")
136  << " " << representation;
137  }
138 
139  bool equal(const Node& node) const override {
140  const auto& other = static_cast<const Relation&>(node);
141  return name == other.name && equal_targets(attributes, other.attributes);
142  }
143 
144  /** Name of relation */
146 
147  /** Attributes of the relation */
149 
150  /** Qualifiers of relation */
151  std::set<RelationQualifier> qualifiers;
152 
153  /** Datastructure to use for this relation */
155 };
156 
157 /**
158  * @class NameComparison
159  * @brief Comparator for relations
160  *
161  * Lexicographical order for Relation
162  * using the qualified name as an ordering criteria.
163  */
164 struct NameComparison {
165  bool operator()(const Relation* x, const Relation* y) const {
166  if (x != nullptr && y != nullptr) {
167  return x->getQualifiedName() < y->getQualifiedName();
168  }
169  return y != nullptr;
170  }
171 };
172 
173 /** Relation set */
174 using RelationSet = std::set<const Relation*, NameComparison>;
175 
176 } // namespace souffle::ast
souffle::ast::Relation::setAttributes
void setAttributes(VecOwn< Attribute > attrs)
Set relation attributes.
Definition: Relation.h:78
souffle::ast::Relation::name
QualifiedName name
Name of relation.
Definition: Relation.h:151
souffle::ast::Relation::attributes
VecOwn< Attribute > attributes
Attributes of the relation.
Definition: Relation.h:154
souffle::ast::Relation::getRepresentation
RelationRepresentation getRepresentation() const
Get relation representation.
Definition: Relation.h:103
souffle::ast::Relation::print
void print(std::ostream &os) const override
Output to a given output stream.
Definition: Relation.h:140
SrcLocation.h
souffle::ast::NameComparison
Comparator for relations.
Definition: Relation.h:170
souffle::ast::NodeMapper
An abstract class for manipulating AST Nodes by substitution.
Definition: NodeMapper.h:36
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
souffle::ast::NameComparison::operator()
bool operator()(const Relation *x, const Relation *y) const
Definition: Relation.h:171
souffle::ast::Relation
Defines a relation with a name, attributes, qualifiers, and internal representation.
Definition: Relation.h:49
souffle::ast::Relation::getAttributes
std::vector< Attribute * > getAttributes() const
Get relation attributes.
Definition: Relation.h:83
souffle::ast::Relation::setRepresentation
void setRepresentation(RelationRepresentation representation)
Set relation representation.
Definition: Relation.h:108
souffle::ast::Relation::addQualifier
void addQualifier(RelationQualifier q)
Add qualifier to this relation.
Definition: Relation.h:93
n
var n
Definition: htmlJsChartistMin.h:15
souffle::ast::Relation::addAttribute
void addAttribute(Own< Attribute > attr)
Add a new used type to this relation.
Definition: Relation.h:67
NodeMapper.h
souffle::ast::Relation::getQualifiers
const std::set< RelationQualifier > & getQualifiers() const
Get relation qualifiers.
Definition: Relation.h:88
Attribute.h
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
ContainerUtil.h
souffle::ast::Relation::equal
bool equal(const Node &node) const override
Abstract equality check for two AST nodes.
Definition: Relation.h:145
RelationTag.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::ast::Relation::Relation
Relation()=default
souffle::ast::RelationSet
std::set< const Relation *, NameComparison > RelationSet
Relation set.
Definition: Relation.h:180
q
a horizontalBars(j=m=void 0===a.axisX.type?new c.AutoScaleAxis(c.Axis.units.x, b.normalized.series, o, c.extend({}, a.axisX,{highLow:d, referenceValue:0})):a.axisX.type.call(c, c.Axis.units.x, b.normalized.series, o, c.extend({}, a.axisX,{highLow:d, referenceValue:0})), l=n=void 0===a.axisY.type?new c.StepAxis(c.Axis.units.y, b.normalized.series, o,{ticks:k}):a.axisY.type.call(c, c.Axis.units.y, b.normalized.series, o, a.axisY)) var q
Definition: htmlJsChartistMin.h:15
souffle::ast::Relation::hasQualifier
bool hasQualifier(RelationQualifier q) const
Check for a relation qualifier.
Definition: Relation.h:113
Node.h
souffle::ast::Relation::representation
RelationRepresentation representation
Datastructure to use for this relation.
Definition: Relation.h:160
souffle::ast::Relation::getArity
size_t getArity() const
Return the arity of this relation.
Definition: Relation.h:73
QualifiedName.h
souffle::RelationRepresentation::DEFAULT
@ DEFAULT
souffle::ast::Relation::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain a list of all embedded AST child nodes.
Definition: Relation.h:131
souffle::ast::Relation::getQualifiedName
const QualifiedName & getQualifiedName() const
Get qualified relation name.
Definition: Relation.h:57
souffle::ast::Node::setSrcLoc
void setSrcLoc(SrcLocation l)
Set source location for the Node.
Definition: Node.h:51
StreamUtil.h
souffle::ast::Relation::qualifiers
std::set< RelationQualifier > qualifiers
Qualifiers of relation.
Definition: Relation.h:157
souffle::ast::Relation::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: Relation.h:125
souffle::ast::Node::Node
Node(SrcLocation loc={})
Definition: Node.h:42
souffle::ast::Node::getSrcLoc
const SrcLocation & getSrcLoc() const
Return source location of the Node.
Definition: Node.h:46
souffle::ast
Definition: Aggregator.h:35
souffle::ast::QualifiedName
Qualified Name class defines fully/partially qualified names to identify objects in components.
Definition: QualifiedName.h:39
souffle::RelationRepresentation
RelationRepresentation
Space of internal representations that a relation can have.
Definition: RelationTag.h:56
souffle::ast::Relation::setQualifiedName
void setQualifiedName(QualifiedName n)
Set name for this relation.
Definition: Relation.h:62
souffle::RelationQualifier
RelationQualifier
Space of qualifiers that a relation can have.
Definition: RelationTag.h:45
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::ast::Relation::removeQualifier
void removeQualifier(RelationQualifier q)
Remove qualifier from this relation.
Definition: Relation.h:98
souffle::ast::Relation::clone
Relation * clone() const override
Create a clone (i.e.
Definition: Relation.h:117