souffle  2.0.2-371-g6315b36
ProgInterface.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2019, 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 ProgInterface.h
12  *
13  * Defines classes that implement the SouffleInterface abstract class
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "interpreter/Engine.h"
20 #include "interpreter/Index.h"
21 #include "interpreter/Relation.h"
22 #include "ram/IO.h"
23 #include "ram/Node.h"
24 #include "ram/Program.h"
25 #include "ram/Relation.h"
26 #include "ram/TranslationUnit.h"
27 #include "ram/utility/Visitor.h"
28 #include "souffle/RamTypes.h"
30 #include "souffle/SymbolTable.h"
31 #include <cassert>
32 #include <cstddef>
33 #include <cstdint>
34 #include <iosfwd>
35 #include <map>
36 #include <memory>
37 #include <string>
38 #include <typeinfo>
39 #include <utility>
40 #include <vector>
41 
42 namespace souffle::interpreter {
43 
44 /**
45  * Wrapper class for interpreter relations
46  */
47 class RelInterface : public souffle::Relation {
48 public:
49  RelInterface(RelationWrapper& r, SymbolTable& s, std::string n, std::vector<std::string> t,
50  std::vector<std::string> an, uint32_t i)
51  : relation(r), symTable(s), name(std::move(n)), types(std::move(t)), attrNames(std::move(an)),
52  id(i) {}
53  ~RelInterface() override = default;
54 
55  /** Insert tuple */
56  void insert(const tuple& t) override {
57  relation.insert(t.data);
58  }
59 
60  /** Check whether tuple exists */
61  bool contains(const tuple& t) const override {
62  return relation.contains(t.data);
63  }
64 
65  /** Iterator to first tuple */
66  iterator begin() const override {
67  return RelInterface::iterator(mk<RelInterface::iterator_base>(id, this, relation.begin()));
68  }
69 
70  /** Iterator to last tuple */
71  iterator end() const override {
72  return RelInterface::iterator(mk<RelInterface::iterator_base>(id, this, relation.end()));
73  }
74 
75  /** Get name */
76  std::string getName() const override {
77  return name;
78  }
79 
80  /** Get arity */
81  arity_type getArity() const override {
82  return relation.getArity();
83  }
84 
85  /** Get arity */
86  arity_type getAuxiliaryArity() const override {
87  return relation.getAuxiliaryArity();
88  }
89 
90  /** Get symbol table */
91  SymbolTable& getSymbolTable() const override {
92  return symTable;
93  }
94 
95  /** Get attribute type */
96  const char* getAttrType(size_t idx) const override {
97  assert(idx < getArity() && "exceeded tuple size");
98  return types[idx].c_str();
99  }
100 
101  /** Get attribute name */
102  const char* getAttrName(size_t idx) const override {
103  assert(idx < getArity() && "exceeded tuple size");
104  return attrNames[idx].c_str();
105  }
106 
107  /** Get number of tuples in relation */
108  size_t size() const override {
109  return relation.size();
110  }
111 
112  /** Eliminate all the tuples in relation*/
113  void purge() override {
115  }
116 
117 protected:
118  /**
119  * Iterator wrapper class
120  */
122  public:
123  iterator_base(uint32_t arg_id, const RelInterface* r, RelationWrapper::Iterator i)
124  : Relation::iterator_base(arg_id), ramRelationInterface(r), it(i), tup(r) {}
125  ~iterator_base() override = default;
126 
127  /** Increment iterator */
128  void operator++() override {
129  ++it;
130  }
131 
132  /** Get current tuple */
133  tuple& operator*() override {
134  // set tuple stream to first element
136 
137  // construct the tuple to return
138  for (size_t i = 0; i < ramRelationInterface->getArity(); i++) {
139  switch (*(ramRelationInterface->getAttrType(i))) {
140  case 's': {
141  std::string s = ramRelationInterface->getSymbolTable().resolve((*it)[i]);
142  tup << s;
143  break;
144  }
145  case 'f': {
146  tup << ramBitCast<RamFloat>((*it)[i]);
147  break;
148  }
149  case 'u': {
150  tup << ramBitCast<RamUnsigned>((*it)[i]);
151  break;
152  }
153  default: {
154  tup << (*it)[i];
155  break;
156  }
157  }
158  }
159  tup.rewind();
160  return tup;
161  }
162 
163  /** Clone iterator */
164  iterator_base* clone() const override {
165  return new RelInterface::iterator_base(getId(), ramRelationInterface, it);
166  }
167 
168  protected:
169  /** Check equivalence */
170  bool equal(const souffle::Relation::iterator_base& o) const override {
171  try {
172  auto& iter = dynamic_cast<const RelInterface::iterator_base&>(o);
173  return ramRelationInterface == iter.ramRelationInterface && it == iter.it;
174  } catch (const std::bad_cast& e) {
175  return false;
176  }
177  }
178 
179  private:
183  };
184 
185 private:
186  /** Wrapped interpreter relation */
188 
189  /** Symbol table */
191 
192  /** Name of relation */
193  std::string name;
194 
195  /** Attribute type */
196  std::vector<std::string> types;
197 
198  /** Attribute Names */
199  std::vector<std::string> attrNames;
200 
201  /** Unique id for wrapper */
202  uint32_t id;
203 };
204 
205 /**
206  * Implementation of SouffleProgram interface for an interpreter instance
207  */
209 public:
210  explicit ProgInterface(Engine& interp)
211  : prog(interp.getTranslationUnit().getProgram()), exec(interp), symTable(interp.getSymbolTable()),
212  recordTable(interp.getRecordTable()) {
213  uint32_t id = 0;
214 
215  // Retrieve AST Relations and store them in a map
216  std::map<std::string, const ram::Relation*> map;
217  visitDepthFirst(prog, [&](const ram::Relation& rel) { map[rel.getName()] = &rel; });
218 
219  // Build wrapper relations for Souffle's interface
220  for (auto& relHandler : exec.getRelationMap()) {
221  if (relHandler == nullptr) {
222  // Skip droped relation.
223  continue;
224  }
225  auto& interpreterRel = **relHandler;
226  auto& name = interpreterRel.getName();
227  assert(map[name]);
228  const ram::Relation& rel = *map[name];
229 
230  // construct types and names vectors
231  std::vector<std::string> types = rel.getAttributeTypes();
232  std::vector<std::string> attrNames = rel.getAttributeNames();
233 
234  auto* interface = new RelInterface(interpreterRel, symTable, rel.getName(), types, attrNames, id);
235  interfaces.push_back(interface);
236  bool input = false;
237  bool output = false;
238  visitDepthFirst(prog, [&](const ram::IO& io) {
239  if (map[io.getRelation()] == &rel) {
240  const std::string& op = io.get("operation");
241  if (op == "input") {
242  input = true;
243  } else if (op == "output" || op == "printsize") {
244  output = true;
245  } else {
246  assert("wrong i/o operation");
247  }
248  }
249  });
250  addRelation(rel.getName(), *interface, input, output);
251  id++;
252  }
253  }
254  ~ProgInterface() override {
255  for (auto* interface : interfaces) {
256  delete interface;
257  }
258  }
259 
260  /** Run program instance: not implemented */
261  void run() override {}
262 
263  /** Load data, run program instance, store data: not implemented */
264  void runAll(std::string, std::string) override {}
265 
266  /** Load input data: not implemented */
267  void loadAll(std::string) override {}
268 
269  /** Print output data: not implemented */
270  void printAll(std::string) override {}
271 
272  /** Dump inputs: not implemented */
273  void dumpInputs() override {}
274 
275  /** Dump outputs: not implemented */
276  void dumpOutputs() override {}
277 
278  /** Run subroutine */
279  void executeSubroutine(
280  std::string name, const std::vector<RamDomain>& args, std::vector<RamDomain>& ret) override {
281  exec.executeSubroutine(name, args, ret);
282  }
283 
284  /** Get symbol table */
285  SymbolTable& getSymbolTable() override {
286  return symTable;
287  }
288 
289  RecordTable& getRecordTable() override {
290  return recordTable;
291  }
292 
293 private:
294  const ram::Program& prog;
295  Engine& exec;
296  SymbolTable& symTable;
297  RecordTable& recordTable;
298  std::vector<RelInterface*> interfaces;
299 };
300 
301 } // namespace souffle::interpreter
souffle::interpreter::RelationWrapper::contains
virtual bool contains(const RamDomain *) const =0
souffle::interpreter::RelInterface::iterator_base::clone
iterator_base * clone() const override
Clone iterator.
Definition: ProgInterface.h:176
souffle::Relation::iterator
Wrapper class for abstract iterator.
Definition: SouffleInterface.h:158
souffle::interpreter::RelInterface::iterator_base::~iterator_base
~iterator_base() override=default
Destructor.
souffle::interpreter::RelInterface::attrNames
std::vector< std::string > attrNames
Attribute Names.
Definition: ProgInterface.h:211
souffle::interpreter::RelInterface::contains
bool contains(const tuple &t) const override
Check whether tuple exists.
Definition: ProgInterface.h:73
souffle::ram::Program
RAM program relation declaration and functions.
Definition: Program.h:58
souffle::interpreter::RelationWrapper::end
virtual Iterator end() const =0
souffle::SymbolTable::resolve
const std::string & resolve(const RamDomain index) const
Find a symbol in the table by its index, note that this gives an error if the index is out of bounds.
Definition: SymbolTable.h:154
souffle::interpreter::RelationWrapper::Iterator
The iterator interface.
Definition: Relation.h:90
Relation.h
souffle::id
A functor representing the identity function for a generic type T.
Definition: StreamUtil.h:136
souffle::interpreter::RelInterface::begin
iterator begin() const override
Iterator to first tuple.
Definition: ProgInterface.h:78
SymbolTable.h
souffle::interpreter::ProgInterface::symTable
SymbolTable & symTable
Definition: ProgInterface.h:302
souffle::interpreter::RelInterface::types
std::vector< std::string > types
Attribute type.
Definition: ProgInterface.h:208
souffle::interpreter::RelInterface::getName
std::string getName() const override
Get name.
Definition: ProgInterface.h:88
souffle::interpreter::ProgInterface::interfaces
std::vector< RelInterface * > interfaces
Definition: ProgInterface.h:304
souffle::RecordTable
Definition: RecordTable.h:114
e
l j a showGridBackground &&c b raw series this eventEmitter e
Definition: htmlJsChartistMin.h:15
souffle::interpreter::RelInterface::iterator_base::ramRelationInterface
const RelInterface * ramRelationInterface
Definition: ProgInterface.h:192
souffle::interpreter::RelInterface::getArity
arity_type getArity() const override
Get arity.
Definition: ProgInterface.h:93
types
std::vector< Own< ast::Type > > types
Definition: ComponentInstantiation.cpp:64
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::interpreter::Relation::iterator_base
Definition: Relation.h:239
souffle::ram::RelationStatement::getRelation
const std::string & getRelation() const
Get RAM relation.
Definition: RelationStatement.h:42
Index.h
souffle::interpreter::ProgInterface::ProgInterface
ProgInterface(Engine &interp)
Definition: ProgInterface.h:216
Engine.h
n
var n
Definition: htmlJsChartistMin.h:15
souffle::Relation::iterator_base
Abstract iterator class.
Definition: SouffleInterface.h:64
souffle::interpreter::RelInterface::iterator_base::tup
tuple tup
Definition: ProgInterface.h:194
Program.h
souffle::interpreter::RelInterface::iterator_base::operator*
tuple & operator*() override
Get current tuple.
Definition: ProgInterface.h:145
souffle::interpreter::RelInterface::getSymbolTable
SymbolTable & getSymbolTable() const override
Get symbol table.
Definition: ProgInterface.h:103
souffle::interpreter
Definition: BrieIndex.cpp:22
souffle::Relation
Object-oriented wrapper class for Souffle's templatized relations.
Definition: SouffleInterface.h:49
souffle::interpreter::RelInterface::iterator_base::operator++
void operator++() override
Increment iterator.
Definition: ProgInterface.h:140
Visitor.h
souffle::SouffleProgram
Abstract base class for generated Datalog programs.
Definition: SouffleInterface.h:693
i
size_t i
Definition: json11.h:663
souffle::tuple::data
const RamDomain * data
Allows printing using WriteStream.
Definition: SouffleInterface.h:498
souffle::ram::Relation
An abstract class for performing indexed operations.
Definition: Relation.h:40
Relation.h
souffle::interpreter::RelInterface::getAttrName
const char * getAttrName(size_t idx) const override
Get attribute name.
Definition: ProgInterface.h:114
souffle::interpreter::ProgInterface::prog
const ram::Program & prog
Definition: ProgInterface.h:300
souffle::interpreter::RelInterface::relation
RelationWrapper & relation
Wrapped interpreter relation.
Definition: ProgInterface.h:199
souffle::interpreter::RelationWrapper::begin
virtual Iterator begin() const =0
souffle::interpreter::Engine
This class translate the RAM Program into executable format and interpreter it.
Definition: Engine.h:56
o
var o
Definition: htmlJsChartistMin.h:15
souffle::interpreter::Engine::getRelationMap
VecOwn< RelationHandle > & getRelationMap()
Return the relation map.
Definition: Engine.cpp:194
souffle::SymbolTable
Definition: SymbolTable.h:48
iterator_base
souffle::ram::IO
I/O statement for a relation.
Definition: IO.h:42
IO.h
souffle::interpreter::ProgInterface::getRecordTable
RecordTable & getRecordTable() override
Get the record table of the program.
Definition: ProgInterface.h:295
souffle::SouffleProgram::addRelation
void addRelation(const std::string &name, Relation &rel, bool isInput, bool isOutput)
Add the relation to relationMap (with its name) and allRelations, depends on the properties of the re...
Definition: SouffleInterface.h:739
souffle::interpreter::RelationWrapper::getAuxiliaryArity
arity_type getAuxiliaryArity() const
Definition: Relation.h:135
souffle::interpreter::RelInterface::getAttrType
const char * getAttrType(size_t idx) const override
Get attribute type.
Definition: ProgInterface.h:108
souffle::interpreter::RelationWrapper::getArity
arity_type getArity() const
Definition: Relation.h:131
TranslationUnit.h
souffle::interpreter::ProgInterface::recordTable
RecordTable & recordTable
Definition: ProgInterface.h:303
souffle::interpreter::RelInterface::purge
void purge() override
Eliminate all the tuples in relation.
Definition: ProgInterface.h:125
souffle::interpreter::RelInterface::name
std::string name
Name of relation.
Definition: ProgInterface.h:205
souffle::interpreter::RelInterface::iterator_base::equal
bool equal(const souffle::Relation::iterator_base &o) const override
Check equivalence.
Definition: ProgInterface.h:182
souffle::interpreter::ProgInterface
Implementation of SouffleProgram interface for an interpreter instance.
Definition: ProgInterface.h:214
Node.h
std
Definition: Brie.h:3053
souffle::interpreter::RelInterface::symTable
SymbolTable & symTable
Symbol table.
Definition: ProgInterface.h:202
RamTypes.h
souffle::interpreter::RelInterface
Wrapper class for interpreter relations.
Definition: ProgInterface.h:53
souffle::interpreter::RelationWrapper
Wrapper for InterpreterRelation.
Definition: Relation.h:48
souffle::interpreter::RelInterface::iterator_base
Iterator wrapper class.
Definition: ProgInterface.h:133
souffle::interpreter::RelInterface::iterator_base::it
RelationWrapper::Iterator it
Definition: ProgInterface.h:193
souffle::ast::visitDepthFirst
void visitDepthFirst(const Node &root, Visitor< R, Ps... > &visitor, Args &... args)
A utility function visiting all nodes within the ast rooted by the given node recursively in a depth-...
Definition: Visitor.h:273
souffle::interpreter::RelInterface::RelInterface
RelInterface(RelationWrapper &r, SymbolTable &s, std::string n, std::vector< std::string > t, std::vector< std::string > an, uint32_t i)
Definition: ProgInterface.h:61
souffle::tuple::rewind
void rewind()
Reset the index giving the "current element" of the tuple to zero.
Definition: SouffleInterface.h:554
souffle::interpreter::ProgInterface::exec
Engine & exec
Definition: ProgInterface.h:301
souffle::Relation::arity_type
uint32_t arity_type
Definition: SouffleInterface.h:51
souffle::interpreter::RelInterface::insert
void insert(const tuple &t) override
Insert tuple.
Definition: ProgInterface.h:68
souffle::interpreter::ProgInterface::getSymbolTable
SymbolTable & getSymbolTable() override
Get symbol table.
Definition: ProgInterface.h:291
souffle::interpreter::RelInterface::~RelInterface
~RelInterface() override=default
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086
souffle::interpreter::RelationWrapper::size
virtual size_t size() const =0
souffle::interpreter::RelInterface::getAuxiliaryArity
arity_type getAuxiliaryArity() const override
Get arity.
Definition: ProgInterface.h:98
souffle::tuple
Defines a tuple for the OO interface such that relations with varying columns can be accessed.
Definition: SouffleInterface.h:443
SouffleInterface.h
souffle::Relation::iterator_base::getId
virtual uint32_t getId() const
Get the ID of the iterator_base object.
Definition: SouffleInterface.h:80
souffle::interpreter::RelationWrapper::insert
virtual void insert(const RamDomain *)=0
souffle::interpreter::RelationWrapper::purge
virtual void purge()=0
souffle::interpreter::RelInterface::size
size_t size() const override
Get number of tuples in relation.
Definition: ProgInterface.h:120
souffle::interpreter::RelInterface::end
iterator end() const override
Iterator to last tuple.
Definition: ProgInterface.h:83
souffle::interpreter::RelInterface::iterator_base::iterator_base
iterator_base(uint32_t arg_id, const RelInterface *r, RelationWrapper::Iterator i)
Definition: ProgInterface.h:135
souffle::interpreter::RelInterface::id
uint32_t id
Unique id for wrapper.
Definition: ProgInterface.h:214