souffle  2.0.2-371-g6315b36
Component.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 Component.h
12  *
13  * Defines the component class
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "ast/Clause.h"
20 #include "ast/ComponentInit.h"
21 #include "ast/ComponentType.h"
22 #include "ast/Directive.h"
23 #include "ast/Node.h"
24 #include "ast/Relation.h"
25 #include "ast/Type.h"
26 #include "ast/utility/NodeMapper.h"
30 #include <algorithm>
31 #include <memory>
32 #include <ostream>
33 #include <set>
34 #include <string>
35 #include <utility>
36 #include <vector>
37 
38 namespace souffle::ast {
39 
40 /**
41  * @class Component
42  * @brief Component class
43  *
44  * Example:
45  * .comp X = {
46  * .decl A(y:number)
47  * A(1).
48  * }
49  *
50  * Component consists of type declaration, relations, rules, etc.
51  */
52 class Component : public Node {
53 public:
54  /** Get component type */
55  const ComponentType* getComponentType() const {
56  return componentType.get();
57  }
58 
59  /** Set component type */
61  componentType = std::move(other);
62  }
63 
64  /** Get base components */
65  const std::vector<ComponentType*> getBaseComponents() const {
67  }
68 
69  /** Add base components */
70  void addBaseComponent(Own<ComponentType> component) {
71  baseComponents.push_back(std::move(component));
72  }
73 
74  /** Add type */
75  void addType(Own<Type> t) {
76  types.push_back(std::move(t));
77  }
78 
79  /** Get types */
80  std::vector<Type*> getTypes() const {
81  return toPtrVector(types);
82  }
83 
84  /** Copy base components */
85  void copyBaseComponents(const Component& other) {
86  baseComponents = souffle::clone(other.baseComponents);
87  }
88 
89  /** Add relation */
90  void addRelation(Own<Relation> r) {
91  relations.push_back(std::move(r));
92  }
93 
94  /** Get relations */
95  std::vector<Relation*> getRelations() const {
97  }
98 
99  /** Add clause */
100  void addClause(Own<Clause> c) {
101  clauses.push_back(std::move(c));
102  }
103 
104  /** Get clauses */
105  std::vector<Clause*> getClauses() const {
107  }
108 
109  /** Add directive */
110  void addDirective(Own<Directive> directive) {
111  directives.push_back(std::move(directive));
112  }
113 
114  /** Get directive statements */
115  std::vector<Directive*> getDirectives() const {
117  }
118 
119  /** Add components */
120  void addComponent(Own<Component> c) {
121  components.push_back(std::move(c));
122  }
123 
124  /** Get components */
125  std::vector<Component*> getComponents() const {
127  }
128 
129  /** Add instantiation */
131  instantiations.push_back(std::move(i));
132  }
133 
134  /** Get instantiation */
135  std::vector<ComponentInit*> getInstantiations() const {
137  }
138 
139  /** Add override */
140  void addOverride(const std::string& name) {
141  overrideRules.insert(name);
142  }
143 
144  /** Get override */
145  const std::set<std::string>& getOverridden() const {
147  }
148 
149  Component* clone() const override {
150  auto* res = new Component();
151  res->componentType = souffle::clone(componentType);
152  res->baseComponents = souffle::clone(baseComponents);
153  res->components = souffle::clone(components);
154  res->instantiations = souffle::clone(instantiations);
155  res->types = souffle::clone(types);
156  res->relations = souffle::clone(relations);
157  res->clauses = souffle::clone(clauses);
158  res->directives = souffle::clone(directives);
159  res->overrideRules = overrideRules;
160  return res;
161  }
162 
163  void apply(const NodeMapper& mapper) override {
164  componentType = mapper(std::move(componentType));
165  for (auto& cur : baseComponents) {
166  cur = mapper(std::move(cur));
167  }
168  for (auto& cur : components) {
169  cur = mapper(std::move(cur));
170  }
171  for (auto& cur : instantiations) {
172  cur = mapper(std::move(cur));
173  }
174  for (auto& cur : types) {
175  cur = mapper(std::move(cur));
176  }
177  for (auto& cur : relations) {
178  cur = mapper(std::move(cur));
179  }
180  for (auto& cur : clauses) {
181  cur = mapper(std::move(cur));
182  }
183  for (auto& cur : directives) {
184  cur = mapper(std::move(cur));
185  }
186  }
187 
188  std::vector<const Node*> getChildNodes() const override {
189  std::vector<const Node*> res;
190 
191  res.push_back(componentType.get());
192  for (const auto& cur : baseComponents) {
193  res.push_back(cur.get());
194  }
195  for (const auto& cur : components) {
196  res.push_back(cur.get());
197  }
198  for (const auto& cur : instantiations) {
199  res.push_back(cur.get());
200  }
201  for (const auto& cur : types) {
202  res.push_back(cur.get());
203  }
204  for (const auto& cur : relations) {
205  res.push_back(cur.get());
206  }
207  for (const auto& cur : clauses) {
208  res.push_back(cur.get());
209  }
210  for (const auto& cur : directives) {
211  res.push_back(cur.get());
212  }
213  return res;
214  }
215 
216 protected:
217  void print(std::ostream& os) const override {
218  auto show = [&](auto&& xs, char const* sep = "\n", char const* prefix = "") {
219  if (xs.empty()) return;
220  os << prefix << join(xs, sep) << "\n";
221  };
222 
223  os << ".comp " << *componentType << " ";
224  show(baseComponents, ",", ": ");
225  os << "{\n";
226  show(components);
227  show(instantiations);
228  show(types);
229  show(relations);
230  show(overrideRules, ",", ".override ");
231  show(clauses, "\n\n");
232  show(directives, "\n\n");
233  os << "}\n";
234  }
235 
236  bool equal(const Node& node) const override {
237  const auto& other = static_cast<const Component&>(node);
238 
239  if (equal_ptr(componentType, other.componentType)) {
240  return true;
241  }
242  if (!equal_targets(baseComponents, other.baseComponents)) {
243  return false;
244  }
245  if (!equal_targets(components, other.components)) {
246  return false;
247  }
248  if (!equal_targets(instantiations, other.instantiations)) {
249  return false;
250  }
251  if (!equal_targets(types, other.types)) {
252  return false;
253  }
254  if (!equal_targets(relations, other.relations)) {
255  return false;
256  }
257  if (!equal_targets(clauses, other.clauses)) {
258  return false;
259  }
260  if (!equal_targets(directives, other.directives)) {
261  return false;
262  }
263  if (overrideRules != other.overrideRules) {
264  return false;
265  }
266  return true;
267  }
268 
269  /** Name of component and its formal component arguments. */
270  Own<ComponentType> componentType;
271 
272  /** Base components of component */
273  VecOwn<ComponentType> baseComponents;
274 
275  /** Types declarations */
277 
278  /** Relations */
280 
281  /** Clauses */
283 
284  /** I/O directives */
286 
287  /** Nested components */
289 
290  /** Nested component instantiations. */
292 
293  /** Clauses of relations that are overwritten by this component */
294  std::set<std::string> overrideRules;
295 };
296 
297 } // namespace souffle::ast
souffle::ast::Component::copyBaseComponents
void copyBaseComponents(const Component &other)
Copy base components.
Definition: Component.h:91
souffle::ast::Component::setComponentType
void setComponentType(Own< ComponentType > other)
Set component type.
Definition: Component.h:66
souffle::ast::Component::addComponent
void addComponent(Own< Component > c)
Add components.
Definition: Component.h:126
Directive.h
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
MiscUtil.h
souffle::ast::Component::getDirectives
std::vector< Directive * > getDirectives() const
Get directive statements.
Definition: Component.h:121
ComponentInit.h
Relation.h
souffle::ast::Component::components
VecOwn< Component > components
Nested components.
Definition: Component.h:294
souffle::ast::Component::getClauses
std::vector< Clause * > getClauses() const
Get clauses.
Definition: Component.h:111
souffle::ast::Component::getOverridden
const std::set< std::string > & getOverridden() const
Get override.
Definition: Component.h:151
NodeMapper.h
souffle::ast::Component::getRelations
std::vector< Relation * > getRelations() const
Get relations.
Definition: Component.h:101
souffle::ast::Component::getBaseComponents
const std::vector< ComponentType * > getBaseComponents() const
Get base components.
Definition: Component.h:71
souffle::ast::Component::addInstantiation
void addInstantiation(Own< ComponentInit > i)
Add instantiation.
Definition: Component.h:136
souffle::ast::Component::clone
Component * clone() const override
Create a clone (i.e.
Definition: Component.h:155
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
i
size_t i
Definition: json11.h:663
souffle::ast::Component::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain a list of all embedded AST child nodes.
Definition: Component.h:194
ContainerUtil.h
souffle::ast::Component::getComponentType
const ComponentType * getComponentType() const
Get component type.
Definition: Component.h:61
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::Component::equal
bool equal(const Node &node) const override
Abstract equality check for two AST nodes.
Definition: Component.h:242
souffle::ast::Component::types
VecOwn< Type > types
Types declarations.
Definition: Component.h:282
souffle::ast::Component::addRelation
void addRelation(Own< Relation > r)
Add relation.
Definition: Component.h:96
souffle::ast::Component::clauses
VecOwn< Clause > clauses
Clauses.
Definition: Component.h:288
Type.h
souffle::equal_ptr
bool equal_ptr(const T *a, const T *b)
Compares two values referenced by a pointer where the case where both pointers are null is also consi...
Definition: MiscUtil.h:130
ComponentType.h
souffle::ast::Component
Component class.
Definition: Component.h:58
souffle::ast::Component::componentType
Own< ComponentType > componentType
Name of component and its formal component arguments.
Definition: Component.h:276
souffle::ast::Component::getInstantiations
std::vector< ComponentInit * > getInstantiations() const
Get instantiation.
Definition: Component.h:141
Node.h
souffle::ast::Component::apply
void apply(const NodeMapper &mapper) override
Apply the mapper to all child nodes.
Definition: Component.h:169
souffle::ast::Component::addBaseComponent
void addBaseComponent(Own< ComponentType > component)
Add base components.
Definition: Component.h:76
souffle::ast::Component::addClause
void addClause(Own< Clause > c)
Add clause.
Definition: Component.h:106
souffle::ast::Component::addType
void addType(Own< Type > t)
Add type.
Definition: Component.h:81
souffle::ast::Node
Abstract class for syntactic elements in an input program.
Definition: Node.h:40
souffle::ast::Component::directives
VecOwn< Directive > directives
I/O directives.
Definition: Component.h:291
souffle::ast::Component::addDirective
void addDirective(Own< Directive > directive)
Add directive.
Definition: Component.h:116
StreamUtil.h
Clause.h
souffle::ast::Component::instantiations
VecOwn< ComponentInit > instantiations
Nested component instantiations.
Definition: Component.h:297
souffle::ast
Definition: Aggregator.h:35
souffle::ast::Component::overrideRules
std::set< std::string > overrideRules
Clauses of relations that are overwritten by this component.
Definition: Component.h:300
souffle::ast::Component::addOverride
void addOverride(const std::string &name)
Add override.
Definition: Component.h:146
souffle::ast::Component::getComponents
std::vector< Component * > getComponents() const
Get components.
Definition: Component.h:131
souffle::ast::Component::print
void print(std::ostream &os) const override
Output to a given output stream.
Definition: Component.h:223
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::Component::getTypes
std::vector< Type * > getTypes() const
Get types.
Definition: Component.h:86
souffle::ast::Component::baseComponents
VecOwn< ComponentType > baseComponents
Base components of component.
Definition: Component.h:279
souffle::ast::Component::relations
VecOwn< Relation > relations
Relations.
Definition: Component.h:285