souffle  2.0.2-371-g6315b36
ReorderConditions.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 ReorderConditions.cpp
12  *
13  ***********************************************************************/
14 
16 #include "ram/Condition.h"
17 #include "ram/Node.h"
18 #include "ram/Operation.h"
19 #include "ram/Program.h"
20 #include "ram/Statement.h"
22 #include "ram/utility/Utils.h"
23 #include "ram/utility/Visitor.h"
25 #include <algorithm>
26 #include <functional>
27 #include <memory>
28 #include <vector>
29 
30 namespace souffle::ram::transform {
31 
33  bool changed = false;
34  visitDepthFirst(program, [&](const Query& query) {
35  std::function<Own<Node>(Own<Node>)> filterRewriter = [&](Own<Node> node) -> Own<Node> {
36  if (const Filter* filter = dynamic_cast<Filter*>(node.get())) {
37  const Condition* condition = &filter->getCondition();
38  VecOwn<Condition> sortedConds;
39  VecOwn<Condition> condList = toConjunctionList(condition);
40  for (auto& cond : condList) {
41  sortedConds.emplace_back(cond->clone());
42  }
43  std::sort(sortedConds.begin(), sortedConds.end(), [&](Own<Condition>& a, Own<Condition>& b) {
44  return rca->getComplexity(a.get()) < rca->getComplexity(b.get());
45  });
46 
47  if (!std::equal(sortedConds.begin(), sortedConds.end(), condList.begin(),
48  [](Own<Condition>& a, Own<Condition>& b) { return *a == *b; })) {
49  changed = true;
50  node = mk<Filter>(Own<Condition>(toCondition(sortedConds)),
51  souffle::clone(&filter->getOperation()));
52  }
53  }
54  node->apply(makeLambdaRamMapper(filterRewriter));
55  return node;
56  };
57  const_cast<Query*>(&query)->apply(makeLambdaRamMapper(filterRewriter));
58  });
59  return changed;
60 }
61 
62 } // namespace souffle::ram::transform
souffle::ram::transform
Definition: ChoiceConversion.cpp:30
souffle::ram::Query
A relational algebra query.
Definition: Query.h:50
souffle::ram::toCondition
Own< Condition > toCondition(const VecOwn< Condition > &conds)
Convert list of conditions to a conjunction.
Definition: Utils.h:84
souffle::ram::toConjunctionList
VecOwn< Condition > toConjunctionList(const Condition *condition)
Convert terms of a conjunction to a list.
Definition: Utils.h:57
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
MiscUtil.h
souffle::ram::Condition
Abstract class for conditions and boolean values in RAM.
Definition: Condition.h:35
Program.h
souffle::ram::Filter
Checks whether a given condition holds.
Definition: Filter.h:49
Complexity.h
Visitor.h
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
souffle::filter
std::vector< A > filter(std::vector< A > xs, F &&f)
Filter a vector to include certain elements.
Definition: FunctionalUtil.h:155
souffle::ram::transform::ReorderConditionsTransformer::reorderConditions
bool reorderConditions(Program &program)
Reorder conjunctive terms in filter operations.
Definition: ReorderConditions.cpp:36
Condition.h
Utils.h
ReorderConditions.h
Node.h
b
l j a showGridBackground &&c b raw series this eventEmitter b
Definition: htmlJsChartistMin.h:15
Operation.h
Statement.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::VecOwn
std::vector< Own< A > > VecOwn
Definition: ContainerUtil.h:45
souffle::ram::makeLambdaRamMapper
LambdaNodeMapper< Lambda > makeLambdaRamMapper(const Lambda &lambda)
Creates a node mapper based on a corresponding lambda expression.
Definition: LambdaNodeMapper.h:67