| 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"
 | 
| 22 | #include "souffle/SouffleInterface.h"
 | 
| 23 | #include "souffle/SymbolTable.h"
 | 
| 24 | #include "souffle/datastructure/BTreeDelete.h"
 | 
| 25 | #include "souffle/datastructure/Brie.h"
 | 
| 26 | #include "souffle/datastructure/ConcurrentCache.h"
 | 
| 27 | #include "souffle/datastructure/EqRel.h"
 | 
| 28 | #include "souffle/datastructure/Info.h"
 | 
| 29 | #include "souffle/datastructure/Nullaries.h"
 | 
| 30 | #include "souffle/datastructure/RecordTableImpl.h"
 | 
| 31 | #include "souffle/datastructure/SymbolTableImpl.h"
 | 
| 32 | #include "souffle/datastructure/Table.h"
 | 
| 33 | #include "souffle/io/IOSystem.h"
 | 
| 34 | #include "souffle/io/WriteStream.h"
 | 
| 35 | #include "souffle/utility/EvaluatorUtil.h"
 | 
| 36 | 
 | 
| 37 | #if defined(_OPENMP)
 | 
| 38 | #include <omp.h>
 | 
| 39 | #endif
 | 
| 40 | 
 | 
| 41 | namespace souffle {
 | 
| 42 | 
 | 
| 43 | extern "C" {
 | 
| 44 | inline souffle::SouffleProgram* getInstance(const char* p) {
 | 
| 45 |     return souffle::ProgramFactory::newInstance(p);
 | 
| 46 | }
 | 
| 47 | }
 | 
| 48 | 
 | 
| 49 | /**
 | 
| 50 |  * Relation wrapper used internally in the generated Datalog program
 | 
| 51 |  */
 | 
| 52 | template <class RelType>
 | 
| 53 | class RelationWrapper : public souffle::Relation {
 | 
| 54 | public:
 | 
| 55 |     static constexpr arity_type Arity = RelType::Arity;
 | 
| 56 |     using TupleType = Tuple<RamDomain, Arity>;
 | 
| 57 |     using AttrStrSeq = std::array<const char*, Arity>;
 | 
| 58 | 
 | 
| 59 | private:
 | 
| 60 |     RelType& relation;
 | 
| 61 |     SouffleProgram& program;
 | 
| 62 |     std::string name;
 | 
| 63 |     AttrStrSeq attrTypes;
 | 
| 64 |     AttrStrSeq attrNames;
 | 
| 65 |     const uint32_t id;
 | 
| 66 |     const arity_type numAuxAttribs;
 | 
| 67 | 
 | 
| 68 |     // NB: internal wrapper. does not satisfy the `iterator` concept.
 | 
| 69 |     class iterator_wrapper : public iterator_base {
 | 
| 70 |         typename RelType::iterator it;
 | 
| 71 |         const Relation* relation;
 | 
| 72 |         tuple t;
 | 
| 73 | 
 | 
| 74 |     public:
 | 
| 75 |         iterator_wrapper(uint32_t arg_id, const Relation* rel, typename RelType::iterator arg_it)
 | 
| 76 |                 : iterator_base(arg_id), it(std::move(arg_it)), relation(rel), t(rel) {}
 | 
| 77 |         void operator++() override {
 | 
| 78 |             ++it;
 | 
| 79 |         }
 | 
| 80 |         tuple& operator*() override {
 | 
| 81 |             auto&& value = *it;
 | 
| 82 |             t.rewind();
 | 
| 83 |             for (std::size_t i = 0; i < Arity; i++)
 | 
| 84 |                 t[i] = value[i];
 | 
| 85 |             return t;
 | 
| 86 |         }
 | 
| 87 |         iterator_base* clone() const override {
 | 
| 88 |             return new iterator_wrapper(*this);
 | 
| 89 |         }
 | 
| 90 | 
 | 
| 91 |     protected:
 | 
| 92 |         bool equal(const iterator_base& o) const override {
 | 
| 93 |             const auto& casted = asAssert<iterator_wrapper>(o);
 | 
| 94 |             return it == casted.it;
 | 
| 95 |         }
 | 
| 96 |     };
 | 
| 97 | 
 | 
| 98 | public:
 | 
| 99 |     RelationWrapper(uint32_t id, RelType& r, SouffleProgram& p, std::string name, const AttrStrSeq& t,
 | 
| 100 |             const AttrStrSeq& n, arity_type numAuxAttribs)
 | 
| 101 |             : relation(r), program(p), name(std::move(name)), attrTypes(t), attrNames(n), id(id),
 | 
| 102 |               numAuxAttribs(numAuxAttribs) {}
 | 
| 103 | 
 | 
| 104 |     iterator begin() const override {
 | 
| 105 |         return iterator(mk<iterator_wrapper>(id, this, relation.begin()));
 | 
| 106 |     }
 | 
| 107 |     iterator end() const override {
 | 
| 108 |         return iterator(mk<iterator_wrapper>(id, this, relation.end()));
 | 
| 109 |     }
 | 
| 110 | 
 | 
| 111 |     void insert(const tuple& arg) override {
 | 
| 112 |         TupleType t;
 | 
| 113 |         assert(&arg.getRelation() == this && "wrong relation");
 | 
| 114 |         assert(arg.size() == Arity && "wrong tuple arity");
 | 
| 115 |         for (std::size_t i = 0; i < Arity; i++) {
 | 
| 116 |             t[i] = arg[i];
 | 
| 117 |         }
 | 
| 118 |         relation.insert(t);
 | 
| 119 |     }
 | 
| 120 |     bool contains(const tuple& arg) const override {
 | 
| 121 |         TupleType t;
 | 
| 122 |         assert(arg.size() == Arity && "wrong tuple arity");
 | 
| 123 |         for (std::size_t i = 0; i < Arity; i++) {
 | 
| 124 |             t[i] = arg[i];
 | 
| 125 |         }
 | 
| 126 |         return relation.contains(t);
 | 
| 127 |     }
 | 
| 128 |     std::size_t size() const override {
 | 
| 129 |         return relation.size();
 | 
| 130 |     }
 | 
| 131 |     std::string getName() const override {
 | 
| 132 |         return name;
 | 
| 133 |     }
 | 
| 134 |     const char* getAttrType(std::size_t arg) const override {
 | 
| 135 |         assert(arg < Arity && "attribute out of bound");
 | 
| 136 |         return attrTypes[arg];
 | 
| 137 |     }
 | 
| 138 |     const char* getAttrName(std::size_t arg) const override {
 | 
| 139 |         assert(arg < Arity && "attribute out of bound");
 | 
| 140 |         return attrNames[arg];
 | 
| 141 |     }
 | 
| 142 |     arity_type getArity() const override {
 | 
| 143 |         return Arity;
 | 
| 144 |     }
 | 
| 145 |     arity_type getAuxiliaryArity() const override {
 | 
| 146 |         return numAuxAttribs;
 | 
| 147 |     }
 | 
| 148 |     SymbolTable& getSymbolTable() const override {
 | 
| 149 |         return program.getSymbolTable();
 | 
| 150 |     }
 | 
| 151 | 
 | 
| 152 |     /** Eliminate all the tuples in relation*/
 | 
| 153 |     void purge() override {
 | 
| 154 |         relation.purge();
 | 
| 155 |     }
 | 
| 156 | };
 | 
| 157 | 
 | 
| 158 | }  // namespace souffle
 |