/
MagicSet.h
234 lines (190 loc) · 6.76 KB
/
MagicSet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Souffle - A Datalog Compiler
* Copyright (c) 2017, The Souffle Developers. All rights reserved.
* Licensed under the Universal Permissive License v 1.0 as shown at:
* - https://opensource.org/licenses/UPL
* - <souffle root>/licenses/SOUFFLE-UPL.txt
*/
/************************************************************************
*
* @file MagicSet.h
*
* Define classes and functionality related to the magic set transformation.
*
***********************************************************************/
#pragma once
#include "ast/AstAbstract.h"
#include "ast/AstArgument.h"
#include "ast/AstClause.h"
#include "ast/AstLiteral.h"
#include "ast/AstQualifiedName.h"
#include "ast/AstVisitor.h"
#include "ast/analysis/AstAnalysis.h"
#include "ast/transform/AstTransformer.h"
#include "utility/StreamUtil.h"
#include <cstddef>
#include <map>
#include <memory>
#include <ostream>
#include <set>
#include <string>
#include <utility>
#include <vector>
namespace souffle {
class AstTranslationUnit;
/**
* Magic Set Transformation
*/
class MagicSetTransformer : public AstTransformer {
public:
std::string getName() const override {
return "MagicSetTransformer";
}
MagicSetTransformer* clone() const override {
return new MagicSetTransformer();
}
private:
bool transform(AstTranslationUnit& translationUnit) override;
};
class AdornedPredicate {
private:
AstQualifiedName predicateName;
std::string adornment;
public:
AdornedPredicate(AstQualifiedName name, std::string adornment)
: predicateName(std::move(name)), adornment(std::move(adornment)) {}
~AdornedPredicate() = default;
AstQualifiedName getQualifiedName() const {
return predicateName;
}
std::string getAdornment() const {
return adornment;
}
friend std::ostream& operator<<(std::ostream& out, const AdornedPredicate& arg) {
out << "(" << arg.predicateName << ", " << arg.adornment << ")";
return out;
}
friend bool operator<(const AdornedPredicate& p1, const AdornedPredicate& p2) {
if (p1.getQualifiedName() != p2.getQualifiedName()) {
return p1.getQualifiedName() < p2.getQualifiedName();
} else {
return p1.getAdornment() < p2.getAdornment();
}
}
};
class AdornedClause {
private:
AstClause* clause;
std::string headAdornment;
std::vector<std::string> bodyAdornment;
std::vector<unsigned int> ordering;
public:
AdornedClause(AstClause* clause, std::string headAdornment, std::vector<std::string> bodyAdornment,
std::vector<unsigned int> ordering)
: clause(clause), headAdornment(std::move(headAdornment)),
bodyAdornment(std::move(bodyAdornment)), ordering(std::move(ordering)) {}
AstClause* getClause() const {
return clause;
}
std::string getHeadAdornment() const {
return headAdornment;
}
std::vector<std::string> getBodyAdornment() const {
return bodyAdornment;
}
std::vector<unsigned int> getOrdering() const {
return ordering;
}
friend std::ostream& operator<<(std::ostream& out, const AdornedClause& arg) {
size_t currpos = 0;
bool firstAdded = true;
out << arg.clause->getHead()->getQualifiedName() << "{" << arg.headAdornment << "} :- ";
std::vector<AstLiteral*> bodyLiterals = arg.clause->getBodyLiterals();
for (AstLiteral* literal : bodyLiterals) {
if (auto* atom = dynamic_cast<AstAtom*>(literal)) {
if (!firstAdded) {
out << ", ";
}
firstAdded = false;
out << atom->getQualifiedName() << "{_}";
} else if (auto* neg = dynamic_cast<AstNegation*>(literal)) {
if (!firstAdded) {
out << ", ";
}
firstAdded = false;
if (currpos < arg.bodyAdornment.size()) {
out << neg->getAtom()->getQualifiedName() << "{" << arg.bodyAdornment[currpos++] << "}";
} else {
out << neg->getAtom()->getQualifiedName() << "{__}";
++currpos;
}
}
}
out << ". [order: " << arg.ordering << "]";
return out;
}
};
class BindingStore {
private:
std::map<std::string, std::unique_ptr<AstArgument>> originalArguments;
std::map<std::string, std::set<std::string>> varDependencies;
std::set<std::string> variableBoundComposites;
public:
AstArgument* cloneOriginalArgument(const std::string& argName) const {
return originalArguments.at(argName)->clone();
}
const std::set<std::string>& getVariableDependencies(const std::string& argName) const {
return varDependencies.at(argName);
}
void addBinding(const std::string& newVariableName, const AstArgument* arg) {
originalArguments[newVariableName] = souffle::clone(arg);
// find the variable dependencies
std::set<std::string> dependencies;
visitDepthFirst(*arg, [&](const AstVariable& var) { dependencies.insert(var.getName()); });
varDependencies[newVariableName] = dependencies;
}
void addVariableBoundComposite(const std::string& functorName) {
variableBoundComposites.insert(functorName);
}
bool isVariableBoundComposite(const std::string& functorName) const {
return (variableBoundComposites.find(functorName) != variableBoundComposites.end());
}
};
class Adornment : public AstAnalysis {
public:
static constexpr const char* name = "adorned-clauses";
Adornment() : AstAnalysis(name) {}
~Adornment() override = default;
void run(const AstTranslationUnit& translationUnit) override;
void print(std::ostream& os) const override;
const std::vector<std::vector<AdornedClause>>& getAdornedClauses() const {
return adornmentClauses;
}
const std::vector<AstQualifiedName>& getRelations() const {
return adornmentRelations;
}
const std::set<AstQualifiedName>& getEDB() const {
return adornmentEdb;
}
const std::set<AstQualifiedName>& getIDB() const {
return adornmentIdb;
}
const std::set<AstQualifiedName>& getNegatedAtoms() const {
return negatedAtoms;
}
const std::set<AstQualifiedName>& getIgnoredAtoms() const {
return ignoredAtoms;
}
const BindingStore& getBindings() const {
return bindings;
}
private:
std::vector<std::vector<AdornedClause>> adornmentClauses;
std::vector<AstQualifiedName> adornmentRelations;
std::set<AstQualifiedName> adornmentEdb;
std::set<AstQualifiedName> adornmentIdb;
std::set<AstQualifiedName> negatedAtoms;
std::set<AstQualifiedName> ignoredAtoms;
BindingStore bindings;
};
} // namespace souffle