souffle  2.0.2-371-g6315b36
CompiledSouffle.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2015, 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 CompiledSouffle.h
12  *
13  * Main include file for generated C++ classes of Souffle
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "souffle/RamTypes.h"
20 #include "souffle/RecordTable.h"
21 #include "souffle/SignalHandler.h"
23 #include "souffle/SymbolTable.h"
27 #include "souffle/io/IOSystem.h"
28 #include "souffle/io/WriteStream.h"
38 #ifndef __EMBEDDED_SOUFFLE__
40 #include "souffle/profile/Logger.h"
42 #endif
43 #include <array>
44 #include <atomic>
45 #include <cassert>
46 #include <cmath>
47 #include <cstdint>
48 #include <cstdlib>
49 #include <exception>
50 #include <iostream>
51 #include <iterator>
52 #include <memory>
53 #include <regex>
54 #include <string>
55 #include <utility>
56 #include <vector>
57 
58 #if defined(_OPENMP)
59 #include <omp.h>
60 #endif
61 
62 namespace souffle {
63 
64 extern "C" {
65 inline souffle::SouffleProgram* getInstance(const char* p) {
67 }
68 }
69 
70 /**
71  * Relation wrapper used internally in the generated Datalog program
72  */
73 template <class RelType>
74 class RelationWrapper : public souffle::Relation {
75 public:
76  static constexpr arity_type Arity = RelType::Arity;
78  using AttrStrSeq = std::array<const char*, Arity>;
79 
80 private:
81  RelType& relation;
83  std::string name;
86  const uint32_t id;
88 
89  // NB: internal wrapper. does not satisfy the `iterator` concept.
91  typename RelType::iterator it;
94 
95  public:
96  iterator_wrapper(uint32_t arg_id, const Relation* rel, typename RelType::iterator arg_it)
97  : iterator_base(arg_id), it(std::move(arg_it)), relation(rel), t(rel) {}
98  void operator++() override {
99  ++it;
100  }
101  tuple& operator*() override {
102  auto&& value = *it;
103  t.rewind();
104  for (size_t i = 0; i < Arity; i++)
105  t[i] = value[i];
106  return t;
107  }
108  iterator_base* clone() const override {
109  return new iterator_wrapper(*this);
110  }
111 
112  protected:
113  bool equal(const iterator_base& o) const override {
114  const auto& casted = static_cast<const iterator_wrapper&>(o);
115  return it == casted.it;
116  }
117  };
118 
119 public:
120  RelationWrapper(uint32_t id, RelType& r, SouffleProgram& p, std::string name, const AttrStrSeq& t,
122  : relation(r), program(p), name(std::move(name)), attrTypes(t), attrNames(n), id(id),
124 
125  iterator begin() const override {
126  return iterator(mk<iterator_wrapper>(id, this, relation.begin()));
127  }
128  iterator end() const override {
129  return iterator(mk<iterator_wrapper>(id, this, relation.end()));
130  }
131 
132  void insert(const tuple& arg) override {
133  TupleType t;
134  assert(&arg.getRelation() == this && "wrong relation");
135  assert(arg.size() == Arity && "wrong tuple arity");
136  for (size_t i = 0; i < Arity; i++) {
137  t[i] = arg[i];
138  }
139  relation.insert(t);
140  }
141  bool contains(const tuple& arg) const override {
142  TupleType t;
143  assert(arg.size() == Arity && "wrong tuple arity");
144  for (size_t i = 0; i < Arity; i++) {
145  t[i] = arg[i];
146  }
147  return relation.contains(t);
148  }
149  std::size_t size() const override {
150  return relation.size();
151  }
152  std::string getName() const override {
153  return name;
154  }
155  const char* getAttrType(size_t arg) const override {
156  assert(arg < Arity && "attribute out of bound");
157  return attrTypes[arg];
158  }
159  const char* getAttrName(size_t arg) const override {
160  assert(arg < Arity && "attribute out of bound");
161  return attrNames[arg];
162  }
163  arity_type getArity() const override {
164  return Arity;
165  }
166  arity_type getAuxiliaryArity() const override {
167  return numAuxAttribs;
168  }
169  SymbolTable& getSymbolTable() const override {
170  return program.getSymbolTable();
171  }
172 
173  /** Eliminate all the tuples in relation*/
174  void purge() override {
175  relation.purge();
176  }
177 };
178 
179 /** Nullary relations */
180 class t_nullaries {
181 private:
182  std::atomic<bool> data{false};
183 
184 public:
185  static constexpr Relation::arity_type Arity = 0;
186 
187  t_nullaries() = default;
189  struct context {};
190  context createContext() {
191  return context();
192  }
193  class iterator {
194  bool value;
195 
196  public:
197  typedef std::forward_iterator_tag iterator_category;
198  using value_type = RamDomain*;
199  using difference_type = ptrdiff_t;
200  using pointer = value_type*;
201  using reference = value_type&;
202 
203  iterator(bool v = false) : value(v) {}
204 
205  const RamDomain* operator*() {
206  return nullptr;
207  }
208 
209  bool operator==(const iterator& other) const {
210  return other.value == value;
211  }
212 
213  bool operator!=(const iterator& other) const {
214  return other.value != value;
215  }
216 
217  iterator& operator++() {
218  if (value) {
219  value = false;
220  }
221  return *this;
222  }
223  };
224  iterator begin() const {
225  return iterator(data);
226  }
227  iterator end() const {
228  return iterator();
229  }
230  void insert(const t_tuple& /* t */) {
231  data = true;
232  }
233  void insert(const t_tuple& /* t */, context& /* ctxt */) {
234  data = true;
235  }
236  void insert(const RamDomain* /* ramDomain */) {
237  data = true;
238  }
239  bool insert() {
240  bool result = data;
241  data = true;
242  return !result;
243  }
244  bool contains(const t_tuple& /* t */) const {
245  return data;
246  }
247  bool contains(const t_tuple& /* t */, context& /* ctxt */) const {
248  return data;
249  }
250  std::size_t size() const {
251  return data ? 1 : 0;
252  }
253  bool empty() const {
254  return !data;
255  }
256  void purge() {
257  data = false;
258  }
259  void printStatistics(std::ostream& /* o */) const {}
260 };
261 
262 /** info relations */
263 template <Relation::arity_type Arity_>
264 class t_info {
265 public:
266  static constexpr Relation::arity_type Arity = Arity_;
267 
268  t_info() = default;
270  struct context {};
271  context createContext() {
272  return context();
273  }
274  class iterator : public std::iterator<std::forward_iterator_tag, Tuple<RamDomain, Arity>> {
275  typename std::vector<Tuple<RamDomain, Arity>>::const_iterator it;
276 
277  public:
278  iterator(const typename std::vector<t_tuple>::const_iterator& o) : it(o) {}
279 
280  const t_tuple operator*() {
281  return *it;
282  }
283 
284  bool operator==(const iterator& other) const {
285  return other.it == it;
286  }
287 
288  bool operator!=(const iterator& other) const {
289  return !(*this == other);
290  }
291 
292  iterator& operator++() {
293  it++;
294  return *this;
295  }
296  };
297  iterator begin() const {
298  return iterator(data.begin());
299  }
300  iterator end() const {
301  return iterator(data.end());
302  }
303  void insert(const t_tuple& t) {
304  insert_lock.lock();
305  if (!contains(t)) {
306  data.push_back(t);
307  }
309  }
310  void insert(const t_tuple& t, context& /* ctxt */) {
311  insert(t);
312  }
313  void insert(const RamDomain* ramDomain) {
314  insert_lock.lock();
315  t_tuple t;
316  for (size_t i = 0; i < Arity; ++i) {
317  t.data[i] = ramDomain[i];
318  }
319  data.push_back(t);
321  }
322  bool contains(const t_tuple& t) const {
323  for (const auto& o : data) {
324  if (t == o) {
325  return true;
326  }
327  }
328  return false;
329  }
330  bool contains(const t_tuple& t, context& /* ctxt */) const {
331  return contains(t);
332  }
333  std::size_t size() const {
334  return data.size();
335  }
336  bool empty() const {
337  return data.size() == 0;
338  }
339  void purge() {
340  data.clear();
341  }
342  void printStatistics(std::ostream& /* o */) const {}
343 
344 private:
345  std::vector<Tuple<RamDomain, Arity>> data;
347 };
348 
349 } // namespace souffle
souffle::t_info::iterator
Definition: CompiledSouffle.h:280
souffle::RelationWrapper::attrTypes
AttrStrSeq attrTypes
Definition: CompiledSouffle.h:90
souffle::RelationWrapper::AttrStrSeq
std::array< const char *, Arity > AttrStrSeq
Definition: CompiledSouffle.h:84
souffle::RelationWrapper::program
SouffleProgram & program
Definition: CompiledSouffle.h:88
souffle::Lock::unlock
void unlock()
Definition: ParallelUtil.h:481
souffle::Relation::iterator
Wrapper class for abstract iterator.
Definition: SouffleInterface.h:158
souffle::t_nullaries::iterator::operator++
iterator & operator++()
Definition: CompiledSouffle.h:223
souffle::ProgramFactory::newInstance
virtual SouffleProgram * newInstance()=0
Create new instance (abstract).
souffle::RelationWrapper::end
iterator end() const override
Return an iterator pointing to next to the last tuple of the relation.
Definition: CompiledSouffle.h:134
souffle::t_info::Arity
static constexpr Relation::arity_type Arity
Definition: CompiledSouffle.h:272
souffle::RelationWrapper::getSymbolTable
SymbolTable & getSymbolTable() const override
Get the symbol table of a relation.
Definition: CompiledSouffle.h:175
souffle::RamDomain
int32_t RamDomain
Definition: RamTypes.h:56
souffle::t_nullaries::iterator::reference
value_type & reference
Definition: CompiledSouffle.h:207
souffle::Lock::lock
void lock()
Definition: ParallelUtil.h:475
souffle::t_nullaries::createContext
context createContext()
Definition: CompiledSouffle.h:196
souffle::RelationWrapper::iterator_wrapper::equal
bool equal(const iterator_base &o) const override
Definition: CompiledSouffle.h:119
souffle::id
A functor representing the identity function for a generic type T.
Definition: StreamUtil.h:136
souffle::t_nullaries::iterator
Definition: CompiledSouffle.h:199
souffle::t_info::createContext
context createContext()
Definition: CompiledSouffle.h:277
souffle::t_info::context
Definition: CompiledSouffle.h:276
SymbolTable.h
souffle::RelationWrapper::iterator_wrapper::iterator_wrapper
iterator_wrapper(uint32_t arg_id, const Relation *rel, typename RelType::iterator arg_it)
Definition: CompiledSouffle.h:102
souffle::t_info::iterator::operator*
const t_tuple operator*()
Definition: CompiledSouffle.h:286
souffle::t_info::end
iterator end() const
Definition: CompiledSouffle.h:306
souffle::t_nullaries::iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: CompiledSouffle.h:203
souffle::RelationWrapper::id
const uint32_t id
Definition: CompiledSouffle.h:92
souffle::t_nullaries::purge
void purge()
Definition: CompiledSouffle.h:262
ParallelUtil.h
souffle::Lock
A small utility class for implementing simple locks.
Definition: ParallelUtil.h:467
souffle::t_nullaries::t_tuple
Tuple< RamDomain, 0 > t_tuple
Definition: CompiledSouffle.h:194
souffle::t_info::iterator::operator==
bool operator==(const iterator &other) const
Definition: CompiledSouffle.h:290
souffle::RelationWrapper::iterator_wrapper::operator++
void operator++() override
Definition: CompiledSouffle.h:104
MiscUtil.h
souffle::RelationWrapper::iterator_wrapper::it
RelType::iterator it
Definition: CompiledSouffle.h:97
Brie.h
souffle::t_info::insert_lock
Lock insert_lock
Definition: CompiledSouffle.h:352
IOSystem.h
souffle::t_nullaries::empty
bool empty() const
Definition: CompiledSouffle.h:259
souffle::t_nullaries::insert
void insert(const t_tuple &)
Definition: CompiledSouffle.h:236
souffle::t_nullaries::insert
void insert(const t_tuple &, context &)
Definition: CompiledSouffle.h:239
n
var n
Definition: htmlJsChartistMin.h:15
souffle::t_nullaries::insert
bool insert()
Definition: CompiledSouffle.h:245
souffle::Relation::iterator_base
Abstract iterator class.
Definition: SouffleInterface.h:64
ProfileEvent.h
Logger.h
souffle::RelationWrapper::Arity
static constexpr arity_type Arity
Definition: CompiledSouffle.h:82
souffle::RelationWrapper::attrNames
AttrStrSeq attrNames
Definition: CompiledSouffle.h:91
souffle::t_nullaries::t_nullaries
t_nullaries()=default
souffle::Relation
Object-oriented wrapper class for Souffle's templatized relations.
Definition: SouffleInterface.h:49
Table.h
souffle::t_info::t_info
t_info()=default
souffle::t_info::iterator::iterator
iterator(const typename std::vector< t_tuple >::const_iterator &o)
Definition: CompiledSouffle.h:284
souffle::SouffleProgram
Abstract base class for generated Datalog programs.
Definition: SouffleInterface.h:693
souffle::t_nullaries::begin
iterator begin() const
Definition: CompiledSouffle.h:230
souffle::RelationWrapper::getName
std::string getName() const override
Get the name of a relation.
Definition: CompiledSouffle.h:158
souffle::tuple::getRelation
const Relation & getRelation() const
Get the reference to the relation to which the tuple belongs.
Definition: SouffleInterface.h:505
i
size_t i
Definition: json11.h:663
souffle::t_info::empty
bool empty() const
Definition: CompiledSouffle.h:342
souffle::RelationWrapper::iterator_wrapper
Definition: CompiledSouffle.h:96
ContainerUtil.h
souffle::RelationWrapper::iterator_wrapper::clone
iterator_base * clone() const override
Definition: CompiledSouffle.h:114
souffle::t_nullaries::size
std::size_t size() const
Definition: CompiledSouffle.h:256
souffle::RelationWrapper::purge
void purge() override
Eliminate all the tuples in relation.
Definition: CompiledSouffle.h:180
souffle::RelationWrapper::contains
bool contains(const tuple &arg) const override
Check whether a tuple exists in a relation.
Definition: CompiledSouffle.h:147
souffle::RelationWrapper::getArity
arity_type getArity() const override
Return the arity of a relation.
Definition: CompiledSouffle.h:169
souffle::t_info::t_tuple
Tuple< RamDomain, Arity > t_tuple
Definition: CompiledSouffle.h:275
souffle::t_info::size
std::size_t size() const
Definition: CompiledSouffle.h:339
souffle::RelationWrapper::iterator_wrapper::operator*
tuple & operator*() override
Definition: CompiledSouffle.h:107
StringUtil.h
souffle::RelationWrapper::begin
iterator begin() const override
Return an iterator pointing to the first tuple of the relation.
Definition: CompiledSouffle.h:131
o
var o
Definition: htmlJsChartistMin.h:15
souffle::t_nullaries::iterator::operator*
const RamDomain * operator*()
Definition: CompiledSouffle.h:211
souffle::t_nullaries::iterator::difference_type
ptrdiff_t difference_type
Definition: CompiledSouffle.h:205
souffle::SymbolTable
Definition: SymbolTable.h:48
souffle::RelationWrapper::numAuxAttribs
const arity_type numAuxAttribs
Definition: CompiledSouffle.h:93
souffle::t_nullaries::contains
bool contains(const t_tuple &) const
Definition: CompiledSouffle.h:250
souffle::RelationWrapper::name
std::string name
Definition: CompiledSouffle.h:89
souffle::t_info::printStatistics
void printStatistics(std::ostream &) const
Definition: CompiledSouffle.h:348
iterator_base
souffle::RelationWrapper::iterator_wrapper::relation
const Relation * relation
Definition: CompiledSouffle.h:98
souffle::t_nullaries::iterator::operator==
bool operator==(const iterator &other) const
Definition: CompiledSouffle.h:215
souffle::RelationWrapper
Relation wrapper used internally in the generated Datalog program.
Definition: CompiledSouffle.h:80
souffle::Tuple
std::array< A, N > Tuple
Definition: RamTypes.h:36
souffle::t_info
info relations
Definition: CompiledSouffle.h:270
souffle::RelationWrapper::getAttrName
const char * getAttrName(size_t arg) const override
Get the attribute name of a relation at the column specified by the parameter.
Definition: CompiledSouffle.h:165
souffle::RelationWrapper::TupleType
Tuple< RamDomain, Arity > TupleType
Definition: CompiledSouffle.h:83
RecordTable.h
EquivalenceRelation.h
souffle::RelationWrapper::RelationWrapper
RelationWrapper(uint32_t id, RelType &r, SouffleProgram &p, std::string name, const AttrStrSeq &t, const AttrStrSeq &n, arity_type numAuxAttribs)
Definition: CompiledSouffle.h:126
souffle::t_nullaries::iterator::value_type
RamDomain * value_type
Definition: CompiledSouffle.h:204
WriteStream.h
souffle::t_info::iterator::operator++
iterator & operator++()
Definition: CompiledSouffle.h:298
souffle::tuple::size
Relation::arity_type size() const
Return the number of elements in the tuple.
Definition: SouffleInterface.h:514
souffle::t_nullaries::printStatistics
void printStatistics(std::ostream &) const
Definition: CompiledSouffle.h:265
SignalHandler.h
EvaluatorUtil.h
souffle::t_nullaries
Nullary relations.
Definition: CompiledSouffle.h:186
souffle::t_info::iterator::it
std::vector< Tuple< RamDomain, Arity > >::const_iterator it
Definition: CompiledSouffle.h:281
souffle::t_nullaries::context
Definition: CompiledSouffle.h:195
std
Definition: Brie.h:3053
souffle::t_info::data
std::vector< Tuple< RamDomain, Arity > > data
Definition: CompiledSouffle.h:351
RamTypes.h
souffle::t_nullaries::iterator::iterator
iterator(bool v=false)
Definition: CompiledSouffle.h:209
StreamUtil.h
souffle::RelationWrapper::getAttrType
const char * getAttrType(size_t arg) const override
Get the attribute type of a relation at the column specified by the parameter.
Definition: CompiledSouffle.h:161
souffle::t_nullaries::iterator::value
bool value
Definition: CompiledSouffle.h:200
FileUtil.h
souffle
Definition: AggregateOp.h:25
souffle::t_nullaries::iterator::pointer
value_type * pointer
Definition: CompiledSouffle.h:206
souffle::t_info::contains
bool contains(const t_tuple &t) const
Definition: CompiledSouffle.h:328
souffle::t_info::begin
iterator begin() const
Definition: CompiledSouffle.h:303
souffle::t_nullaries::Arity
static constexpr Relation::arity_type Arity
Definition: CompiledSouffle.h:191
souffle::tuple::rewind
void rewind()
Reset the index giving the "current element" of the tuple to zero.
Definition: SouffleInterface.h:554
CompiledOptions.h
souffle::RelationWrapper::size
std::size_t size() const override
Get the number of tuples in a relation.
Definition: CompiledSouffle.h:155
FunctionalUtil.h
souffle::Relation::arity_type
uint32_t arity_type
Definition: SouffleInterface.h:51
CacheUtil.h
souffle::t_nullaries::data
std::atomic< bool > data
Definition: CompiledSouffle.h:188
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086
souffle::RelationWrapper::insert
void insert(const tuple &arg) override
Insert a new tuple into the relation.
Definition: CompiledSouffle.h:138
souffle::tuple
Defines a tuple for the OO interface such that relations with varying columns can be accessed.
Definition: SouffleInterface.h:443
souffle::RelationWrapper::getAuxiliaryArity
arity_type getAuxiliaryArity() const override
Return the number of auxiliary attributes.
Definition: CompiledSouffle.h:172
souffle::t_info::purge
void purge()
Definition: CompiledSouffle.h:345
SouffleInterface.h
souffle::t_nullaries::end
iterator end() const
Definition: CompiledSouffle.h:233
souffle::RelationWrapper::iterator_wrapper::t
tuple t
Definition: CompiledSouffle.h:99
souffle::t_nullaries::iterator::operator!=
bool operator!=(const iterator &other) const
Definition: CompiledSouffle.h:219
souffle::t_info::insert
void insert(const t_tuple &t)
Definition: CompiledSouffle.h:309
souffle::t_info::iterator::operator!=
bool operator!=(const iterator &other) const
Definition: CompiledSouffle.h:294
souffle::SouffleProgram::getSymbolTable
virtual SymbolTable & getSymbolTable()=0
Get the symbol table of the program.
souffle::RelationWrapper::relation
RelType & relation
Definition: CompiledSouffle.h:87
p
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 p
Definition: htmlJsChartistMin.h:15
souffle::getInstance
souffle::SouffleProgram * getInstance(const char *p)
Definition: CompiledSouffle.h:71