souffle  2.0.2-371-g6315b36
ExecutionPlan.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, Oracle and/or its affiliates. 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 ExecutionPlan.h
12  *
13  * Defines an execution plan class
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "ast/ExecutionOrder.h"
20 #include "ast/Node.h"
21 #include "ast/utility/NodeMapper.h"
24 #include <algorithm>
25 #include <map>
26 #include <memory>
27 #include <ostream>
28 #include <utility>
29 #include <vector>
30 
31 namespace souffle::ast {
32 
33 /**
34  * @brief ExecutionPlan
35  * @class Defines a user-defined execution plan for a clause.
36  *
37  * An user-defined execution plan consists of one or more
38  * execution orders. An execution order is a permutation
39  * of atoms in a clause.
40  *
41  * Example:
42  * .plan 0:(1,2,3), 2:(3,2,1)
43  *
44  */
45 class ExecutionPlan : public Node {
46 public:
47  /** Set execution order for a given rule version */
48  void setOrderFor(int version, Own<ExecutionOrder> plan) {
49  plans[version] = std::move(plan);
50  }
51 
52  /** Get orders */
53  std::map<int, const ExecutionOrder*> getOrders() const {
54  std::map<int, const ExecutionOrder*> result;
55  for (auto& plan : plans) {
56  result.insert(std::make_pair(plan.first, plan.second.get()));
57  }
58  return result;
59  }
60 
61  ExecutionPlan* clone() const override {
62  auto res = new ExecutionPlan();
63  res->setSrcLoc(getSrcLoc());
64  for (auto& plan : plans) {
65  res->setOrderFor(plan.first, Own<ExecutionOrder>(plan.second->clone()));
66  }
67  return res;
68  }
69 
70  void apply(const NodeMapper& map) override {
71  for (auto& plan : plans) {
72  plan.second = map(std::move(plan.second));
73  }
74  }
75 
76  std::vector<const Node*> getChildNodes() const override {
77  std::vector<const Node*> childNodes;
78  for (auto& plan : plans) {
79  childNodes.push_back(plan.second.get());
80  }
81  return childNodes;
82  }
83 
84 protected:
85  void print(std::ostream& out) const override {
86  if (!plans.empty()) {
87  out << " .plan ";
88  out << join(plans, ", ",
89  [](std::ostream& os, const auto& arg) { os << arg.first << ":" << *arg.second; });
90  }
91  }
92 
93  bool equal(const Node& node) const override {
94  const auto& other = static_cast<const ExecutionPlan&>(node);
95  return equal_targets(plans, other.plans);
96  }
97 
98 private:
99  /** Mapping versions of clauses to execution orders */
100  std::map<int, Own<ExecutionOrder>> plans;
101 };
102 
103 } // namespace souffle::ast
souffle::ast::ExecutionPlan::setOrderFor
void setOrderFor(int version, Own< ExecutionOrder > plan)
Set execution order for a given rule version.
Definition: ExecutionPlan.h:54
souffle::ast::ExecutionPlan::equal
bool equal(const Node &node) const override
Abstract equality check for two AST nodes.
Definition: ExecutionPlan.h:99
souffle::ast::ExecutionPlan::plans
std::map< int, Own< ExecutionOrder > > plans
Mapping versions of clauses to execution orders.
Definition: ExecutionPlan.h:106
souffle::ast::ExecutionPlan::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: ExecutionPlan.h:76
souffle::ast::NodeMapper
An abstract class for manipulating AST Nodes by substitution.
Definition: NodeMapper.h:36
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
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::ast::ExecutionPlan
Definition: ExecutionPlan.h:51
NodeMapper.h
ContainerUtil.h
souffle::ast::ExecutionPlan::print
void print(std::ostream &out) const override
Output to a given output stream.
Definition: ExecutionPlan.h:91
souffle::join
detail::joined_sequence< Iter, Printer > join(const Iter &a, const Iter &b, const std::string &sep, const Printer &p)
Creates an object to be forwarded to some output stream for printing sequences of elements interspers...
Definition: StreamUtil.h:175
souffle::equal_targets
bool equal_targets(const Container &a, const Container &b, const Comparator &comp)
A function testing whether two containers are equal with the given Comparator.
Definition: ContainerUtil.h:433
souffle::ast::ExecutionPlan::clone
ExecutionPlan * clone() const override
Create a clone (i.e.
Definition: ExecutionPlan.h:67
ExecutionOrder.h
Node.h
souffle::ast::ExecutionPlan::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain a list of all embedded AST child nodes.
Definition: ExecutionPlan.h:82
souffle::ast::Node
Abstract class for syntactic elements in an input program.
Definition: Node.h:40
StreamUtil.h
souffle::ast::ExecutionPlan::getOrders
std::map< int, const ExecutionOrder * > getOrders() const
Get orders.
Definition: ExecutionPlan.h:59
souffle::ast::Node::getSrcLoc
const SrcLocation & getSrcLoc() const
Return source location of the Node.
Definition: Node.h:46
souffle::ast
Definition: Aggregator.h:35