souffle  2.0.2-371-g6315b36
TupleId.cpp
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2018, 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 TupleId.cpp
12  *
13  ***********************************************************************/
14 
15 #include "ram/transform/TupleId.h"
16 #include "ram/Expression.h"
17 #include "ram/Node.h"
18 #include "ram/Operation.h"
19 #include "ram/Program.h"
20 #include "ram/Statement.h"
21 #include "ram/utility/Visitor.h"
22 #include <functional>
23 #include <map>
24 #include <memory>
25 #include <vector>
26 
27 namespace souffle::ram::transform {
28 
29 bool TupleIdTransformer::reorderOperations(Program& program) {
30  bool changed = false;
31  visitDepthFirst(program, [&](const Query& query) {
32  // Maps old tupleIds to new tupleIds
33  std::map<int, int> reorder;
34  int ctr = 0;
35 
36  visitDepthFirst(query, [&](const TupleOperation& search) {
37  if (ctr != search.getTupleId()) {
38  changed = true;
39  }
40  reorder[search.getTupleId()] = ctr;
41  const_cast<TupleOperation*>(&search)->setTupleId(ctr);
42  ctr++;
43  });
44 
45  std::function<Own<Node>(Own<Node>)> elementRewriter = [&](Own<Node> node) -> Own<Node> {
46  if (auto* element = dynamic_cast<TupleElement*>(node.get())) {
47  if (reorder[element->getTupleId()] != element->getTupleId()) {
48  changed = true;
49  node = mk<TupleElement>(reorder[element->getTupleId()], element->getElement());
50  }
51  }
52  node->apply(makeLambdaRamMapper(elementRewriter));
53  return node;
54  };
55  const_cast<Query*>(&query)->apply(makeLambdaRamMapper(elementRewriter));
56  });
57 
58  return changed;
59 }
60 
61 } // namespace souffle::ram::transform
souffle::ram::transform
Definition: ChoiceConversion.cpp:30
souffle::ram::transform::Transformer::apply
bool apply(TranslationUnit &translationUnit)
@Brief apply the transformer to a translation unit @Param translationUnit that will be transformed.
Definition: Transformer.cpp:39
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
souffle::ram::TupleElement
Access element from the current tuple in a tuple environment.
Definition: TupleElement.h:42
Program.h
Visitor.h
souffle::ram::TupleOperation::getTupleId
int getTupleId() const
Get identifier.
Definition: TupleOperation.h:43
souffle::ram::TupleOperation
Abstract class for relation searches and lookups.
Definition: TupleOperation.h:35
Node.h
Operation.h
TupleId.h
Statement.h
souffle::ram::TupleOperation::setTupleId
void setTupleId(int id)
Set identifier.
Definition: TupleOperation.h:48
souffle::ram::transform::TupleIdTransformer::reorderOperations
bool reorderOperations(Program &program)
Apply tupleId reordering to the whole program.
Definition: TupleId.cpp:33
Expression.h
souffle::ram::visitDepthFirst
void visitDepthFirst(const Node &root, Visitor< R, Ps... > &visitor, Args &... args)
A utility function visiting all nodes within the RAM fragments rooted by the given node recursively i...
Definition: Visitor.h:357
souffle::ram::makeLambdaRamMapper
LambdaNodeMapper< Lambda > makeLambdaRamMapper(const Lambda &lambda)
Creates a node mapper based on a corresponding lambda expression.
Definition: LambdaNodeMapper.h:67