souffle  2.0.2-371-g6315b36
Program.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2017, 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 /************************************************************************
10  *
11  * @file Program.h
12  *
13  * Defines a Program of a relational algebra query
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "ram/Node.h"
20 #include "ram/Relation.h"
21 #include "ram/Statement.h"
23 #include "ram/utility/NodeMapper.h"
26 #include <cassert>
27 #include <map>
28 #include <memory>
29 #include <ostream>
30 #include <string>
31 #include <utility>
32 #include <vector>
33 
34 namespace souffle::ram {
35 
36 /**
37  * @class Program
38  * @brief RAM program relation declaration and functions
39  *
40  * A typical example:
41  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
42  * PROGRAM
43  * DECLARATION
44  * A(x:i:number)
45  * END DECLARATION
46  * BEGIN MAIN
47  * ...
48  * END MAIN
49  * END PROGRAM
50  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
51  */
52 class Program : public Node {
53 private:
54  Program() = default;
55 
56 public:
57  Program(VecOwn<Relation> rels, Own<Statement> main, std::map<std::string, Own<Statement>> subs)
58  : relations(std::move(rels)), main(std::move(main)), subroutines(std::move(subs)) {
59  assert(this->main != nullptr && "Main program is a null-pointer");
60  for (const auto& rel : relations) {
61  assert(rel != nullptr && "Relation is a null-pointer");
62  }
63  for (const auto& sub : subroutines) {
64  assert(sub.second != nullptr && "Subroutine is a null-pointer");
65  }
66  }
67 
68  std::vector<const Node*> getChildNodes() const override {
69  std::vector<const Node*> children;
70  children = main->getChildNodes();
71  for (auto& rel : relations) {
72  children.push_back(rel.get());
73  }
74  for (auto& sub : subroutines) {
75  children.push_back(sub.second.get());
76  }
77  return children;
78  }
79 
80  /** @brief Get main program */
81  Statement& getMain() const {
82  return *main;
83  }
84 
85  /** @brief Get all relations of RAM program */
86  std::vector<Relation*> getRelations() const {
88  }
89 
90  /** @brief Get all subroutines of a RAM program */
91  const std::map<std::string, Statement*> getSubroutines() const {
92  std::map<std::string, Statement*> subroutineRefs;
93  for (auto& sub : subroutines) {
94  subroutineRefs.insert({sub.first, sub.second.get()});
95  }
96  return subroutineRefs;
97  }
98 
99  /** @brief Get a specific subroutine */
100  const Statement& getSubroutine(const std::string& name) const {
101  return *subroutines.at(name);
102  }
103 
104  Program* clone() const override {
105  auto* res = new Program();
106  res->main = souffle::clone(main);
107  for (auto& rel : relations) {
108  res->relations.push_back(souffle::clone(rel));
109  }
110  for (auto& sub : subroutines) {
111  res->subroutines[sub.first] = souffle::clone(sub.second);
112  }
113  return res;
114  }
115 
116  void apply(const NodeMapper& map) override {
117  main = map(std::move(main));
118  for (auto& rel : relations) {
119  rel = map(std::move(rel));
120  }
121  for (auto& sub : subroutines) {
122  sub.second = map(std::move(sub.second));
123  }
124  }
125 
126 protected:
127  void print(std::ostream& out) const override {
128  out << "PROGRAM" << std::endl;
129  out << " DECLARATION" << std::endl;
130  for (const auto& rel : relations) {
131  out << " " << *rel << std::endl;
132  }
133  out << " END DECLARATION" << std::endl;
134  for (const auto& sub : subroutines) {
135  out << " SUBROUTINE " << sub.first << std::endl;
136  sub.second->print(out, 2);
137  out << " END SUBROUTINE" << std::endl;
138  }
139  out << " BEGIN MAIN" << std::endl;
140  main->print(out, 2);
141  out << " END MAIN" << std::endl;
142  out << "END PROGRAM" << std::endl;
143  }
144 
145  bool equal(const Node& node) const override {
146  const auto& other = static_cast<const Program&>(node);
147 
148  return equal_targets(relations, other.relations) && equal_ptr(main, other.main) &&
149  equal_targets(subroutines, other.subroutines);
150  }
151 
152 protected:
153  /** Relations of RAM program */
155 
156  /** Main program */
158 
159  /** Subroutines for provenance system */
160  std::map<std::string, Own<Statement>> subroutines;
161 };
162 
163 } // namespace souffle::ram
souffle::ram::Program::relations
VecOwn< Relation > relations
Relations of RAM program.
Definition: Program.h:160
souffle::ram::Program::getSubroutine
const Statement & getSubroutine(const std::string &name) const
Get a specific subroutine.
Definition: Program.h:106
LambdaNodeMapper.h
souffle::ast::analysis::sub
std::shared_ptr< Constraint< Var > > sub(const Var &a, const Var &b, const std::string &symbol="⊑")
A generic factory for constraints of the form.
Definition: ConstraintSystem.h:228
souffle::ram::Program::subroutines
std::map< std::string, Own< Statement > > subroutines
Subroutines for provenance system.
Definition: Program.h:166
souffle::ram::Program::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: Program.h:122
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
souffle::ram::Program::main
Own< Statement > main
Main program.
Definition: Program.h:163
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
MiscUtil.h
souffle::ram::Program::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain list of all embedded child nodes.
Definition: Program.h:74
souffle::ram
Definition: AstToRamTranslator.h:54
souffle::ram::Program::clone
Program * clone() const override
Create a clone (i.e.
Definition: Program.h:110
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
NodeMapper.h
ContainerUtil.h
souffle::ram::Program::Program
Program()=default
souffle::ram::Program::getSubroutines
const std::map< std::string, Statement * > getSubroutines() const
Get all subroutines of a RAM program.
Definition: Program.h:97
Relation.h
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::Program::equal
bool equal(const Node &node) const override
Equality check for two RAM nodes.
Definition: Program.h:151
souffle::ram::Program::print
void print(std::ostream &out) const override
Print RAM node.
Definition: Program.h:133
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
souffle::ram::Program::getRelations
std::vector< Relation * > getRelations() const
Get all relations of RAM program
Definition: Program.h:92
souffle::ram::Statement
Abstract class for RAM statements.
Definition: Statement.h:37
Node.h
std
Definition: Brie.h:3053
Statement.h
souffle::ram::Program::getMain
Statement & getMain() const
Get main program.
Definition: Program.h:87
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