souffle  2.0.2-371-g6315b36
Utils.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2020, 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 Utils.h
12  *
13  * A collection of utilities operating on RAM constructs.
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "ram/Condition.h"
20 #include "ram/Conjunction.h"
21 #include "ram/Expression.h"
22 #include "ram/True.h"
23 #include "ram/UndefValue.h"
25 #include <algorithm>
26 #include <memory>
27 #include <queue>
28 #include <utility>
29 #include <vector>
30 
31 namespace souffle::ram {
32 
33 /** @brief Determines if an expression represents an undefined value */
34 inline bool isUndefValue(const Expression* expr) {
35  return isA<UndefValue>(expr);
36 }
37 
38 /** @brief Determines if a condition represents true */
39 inline bool isTrue(const Condition* cond) {
40  return isA<True>(cond);
41 }
42 
43 /**
44  * @brief Convert terms of a conjunction to a list
45  * @param conds A RAM condition
46  * @return A list of RAM conditions
47  *
48  * Convert a condition of the format C1 /\ C2 /\ ... /\ Cn
49  * to a list {C1, C2, ..., Cn}.
50  */
51 inline VecOwn<Condition> toConjunctionList(const Condition* condition) {
52  VecOwn<Condition> conditionList;
53  std::queue<const Condition*> conditionsToProcess;
54  if (condition != nullptr) {
55  conditionsToProcess.push(condition);
56  while (!conditionsToProcess.empty()) {
57  condition = conditionsToProcess.front();
58  conditionsToProcess.pop();
59  if (const auto* ramConj = dynamic_cast<const Conjunction*>(condition)) {
60  conditionsToProcess.push(&ramConj->getLHS());
61  conditionsToProcess.push(&ramConj->getRHS());
62  } else {
63  conditionList.emplace_back(condition->clone());
64  }
65  }
66  }
67  return conditionList;
68 }
69 
70 /**
71  * @brief Convert list of conditions to a conjunction
72  * @param A list of RAM conditions
73  * @param A RAM condition
74  *
75  * Convert a list {C1, C2, ..., Cn} to a condition of
76  * the format C1 /\ C2 /\ ... /\ Cn.
77  */
78 inline Own<Condition> toCondition(const VecOwn<Condition>& conds) {
79  Own<Condition> result;
80  for (auto const& cur : conds) {
81  if (result == nullptr) {
82  result = souffle::clone(cur);
83  } else {
84  result = mk<Conjunction>(std::move(result), souffle::clone(cur));
85  }
86  }
87  return result;
88 }
89 
90 /**
91  * @brief store terms of a conjunction in an array of pointers without cloning
92  *
93  * Convert a condition of the format C1 /\ C2 /\ ... /\ Cn
94  * to a list {C1, C2, ..., Cn}.
95  */
96 inline std::vector<const ram::Condition*> findConjunctiveTerms(const ram::Condition* condition) {
97  std::vector<const ram::Condition*> conditionList;
98  std::queue<const ram::Condition*> conditionsToProcess;
99  if (condition != nullptr) {
100  conditionsToProcess.push(condition);
101  while (!conditionsToProcess.empty()) {
102  condition = conditionsToProcess.front();
103  conditionsToProcess.pop();
104  if (const auto* ramConj = dynamic_cast<const ram::Conjunction*>(condition)) {
105  conditionsToProcess.push(&ramConj->getLHS());
106  conditionsToProcess.push(&ramConj->getRHS());
107  } else {
108  conditionList.emplace_back(condition);
109  }
110  }
111  }
112  return conditionList;
113 }
114 
115 } // namespace souffle::ram
souffle::ram::toCondition
Own< Condition > toCondition(const VecOwn< Condition > &conds)
Convert list of conditions to a conjunction.
Definition: Utils.h:84
souffle::ram::isUndefValue
bool isUndefValue(const Expression *expr)
Determines if an expression represents an undefined value.
Definition: Utils.h:40
souffle::ram::toConjunctionList
VecOwn< Condition > toConjunctionList(const Condition *condition)
Convert terms of a conjunction to a list.
Definition: Utils.h:57
MiscUtil.h
souffle::ram::Condition
Abstract class for conditions and boolean values in RAM.
Definition: Condition.h:35
True.h
souffle::ram
Definition: AstToRamTranslator.h:54
souffle::ram::Condition::clone
Condition * clone() const override=0
Create a clone (i.e.
souffle::ram::findConjunctiveTerms
std::vector< const ram::Condition * > findConjunctiveTerms(const ram::Condition *condition)
store terms of a conjunction in an array of pointers without cloning
Definition: Utils.h:102
souffle::ram::Conjunction
A conjunction of conditions.
Definition: Conjunction.h:54
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
Condition.h
souffle::ram::isTrue
bool isTrue(const Condition *cond)
Determines if a condition represents true.
Definition: Utils.h:45
Expression.h
UndefValue.h
souffle::VecOwn
std::vector< Own< A > > VecOwn
Definition: ContainerUtil.h:45
Conjunction.h