souffle  2.0.2-371-g6315b36
ReorderFilterBreak.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 ReorderFilterBreak.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"
21 #include "ram/utility/Visitor.h"
23 #include <functional>
24 #include <memory>
25 #include <vector>
26 
27 namespace souffle::ram::transform {
28 
29 bool ReorderFilterBreak::reorderFilterBreak(Program& program) {
30  bool changed = false;
31  visitDepthFirst(program, [&](const Query& query) {
32  std::function<Own<Node>(Own<Node>)> filterRewriter = [&](Own<Node> node) -> Own<Node> {
33  // find filter-break nesting
34  if (const Filter* filter = dynamic_cast<Filter*>(node.get())) {
35  if (const Break* br = dynamic_cast<Break*>(&filter->getOperation())) {
36  changed = true;
37  // convert to break-filter nesting
38  node = mk<Break>(souffle::clone(&br->getCondition()),
39  mk<Filter>(souffle::clone(&filter->getCondition()),
40  souffle::clone(&br->getOperation())));
41  }
42  }
43  node->apply(makeLambdaRamMapper(filterRewriter));
44  return node;
45  };
46  const_cast<Query*>(&query)->apply(makeLambdaRamMapper(filterRewriter));
47  });
48  return changed;
49 }
50 
51 } // namespace souffle::ram::transform
souffle::ram::transform
Definition: ChoiceConversion.cpp:30
souffle::ram::Break
Breaks out of the loop if a condition holds.
Definition: Break.h:49
souffle::ram::Query
A relational algebra query.
Definition: Query.h:50
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
MiscUtil.h
Program.h
souffle::ram::Filter
Checks whether a given condition holds.
Definition: Filter.h:49
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
ReorderFilterBreak.h
Condition.h
Node.h
Operation.h
Statement.h
souffle::ram::transform::ReorderFilterBreak::reorderFilterBreak
bool reorderFilterBreak(Program &program)
reorder filter-break nesting to break-filter nesting
Definition: ReorderFilterBreak.cpp:33
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