souffle  2.0.2-371-g6315b36
ExpandFilter.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 ExpandFilter.cpp
12  *
13  ***********************************************************************/
14 
16 #include "ram/Node.h"
17 #include "ram/Operation.h"
18 #include "ram/Program.h"
19 #include "ram/Statement.h"
20 #include "ram/utility/Utils.h"
21 #include "ram/utility/Visitor.h"
23 #include <functional>
24 #include <memory>
25 #include <utility>
26 #include <vector>
27 
28 namespace souffle {
29 class Condition;
30 
31 namespace ram::transform {
32 
33 bool ExpandFilterTransformer::expandFilters(Program& program) {
34  bool changed = false;
35  visitDepthFirst(program, [&](const Query& query) {
36  std::function<Own<Node>(Own<Node>)> filterRewriter = [&](Own<Node> node) -> Own<Node> {
37  if (const Filter* filter = dynamic_cast<Filter*>(node.get())) {
38  const Condition* condition = &filter->getCondition();
39  VecOwn<Condition> conditionList = toConjunctionList(condition);
40  if (conditionList.size() > 1) {
41  changed = true;
42  VecOwn<Filter> filters;
43  for (auto& cond : conditionList) {
44  if (filters.empty()) {
45  filters.emplace_back(mk<Filter>(
46  souffle::clone(cond), souffle::clone(&filter->getOperation())));
47  } else {
48  filters.emplace_back(mk<Filter>(souffle::clone(cond), std::move(filters.back())));
49  }
50  }
51  node = std::move(filters.back());
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 ram::transform
63 } // end of namespace souffle
souffle::ram::Query
A relational algebra query.
Definition: Query.h:50
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
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
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
Utils.h
Node.h
Operation.h
Statement.h
souffle
Definition: AggregateOp.h:25
ExpandFilter.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::transform::ExpandFilterTransformer::expandFilters
bool expandFilters(Program &program)
Expand filter operations.
Definition: ExpandFilter.cpp:37
souffle::ram::makeLambdaRamMapper
LambdaNodeMapper< Lambda > makeLambdaRamMapper(const Lambda &lambda)
Creates a node mapper based on a corresponding lambda expression.
Definition: LambdaNodeMapper.h:67