-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprolog.cpp
More file actions
293 lines (250 loc) · 7.82 KB
/
prolog.cpp
File metadata and controls
293 lines (250 loc) · 7.82 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <unistd.h>
#include <vector>
#include <SWI-Prolog.h>
#include <SWI-Stream.h>
#include "base.hpp"
#include "config.h"
#include "prolog.hpp"
namespace
{
// RAII class for foreign frames
class Frame
{
fid_t fid;
public:
Frame() : fid(PL_open_foreign_frame()) {}
~Frame() { PL_close_foreign_frame(fid); }
};
inline void put_term(term_t term, std::string const &pred)
{
auto rv = PL_put_term_from_chars(term, 0, pred.size(), pred.data());
if (!rv)
{
std::cerr << "Invalid term: " << pred << std::endl;
abort();
}
}
bool consult_file(std::string filename)
{
Frame fid{}; /* All created terms will go away when
* fid goes out of scope */
/* Create a single term */
term_t term = PL_new_term_ref();
/* Fill it with a predicate */
put_term(term, "consult(\"" + filename + "\")");
/* Run the query */
auto succeed = PL_call(term, nullptr);
if (!succeed)
{
std::cerr << "Could not consult file" << std::endl;
PL_halt(EXIT_FAILURE);
}
return succeed;
}
void initialize_helper()
{
static bool init = false;
if (!init)
{
std::vector<const char *> args = {"dummy", "--quiet=true", "--threads=false"};
auto rv = PL_initialise(args.size(), const_cast<char **>(args.data()));
if (!rv)
{
std::cerr << "Could not start SWIPL" << std::endl;
exit(EXIT_FAILURE);
}
init = true;
}
}
}; // namespace
// Initialize prolog before main
namespace
{
#ifdef PHAROS_RULES_DIR
static auto dummy = (initialize_helper(), consult_file(PHAROS_RULES_DIR "/setup.pl"), true);
#endif
}; // namespace
namespace Prolog
{
bool run_prolog(std::string filename, std::string query_string)
{
initialize_helper();
consult_file(filename);
{
Frame fid{}; /* All created terms will go away when
* fid goes out of scope */
/* Create a single term */
term_t term = PL_new_term_ref();
put_term(term, query_string);
bool succeed = PL_call(term, nullptr);
return succeed;
}
}
bool run_prolog(std::string const &prolog_data, std::string const &streamname,
std::string const &query_string)
{
initialize_helper();
{
Frame fid{}; /* All created terms will go away when
* fid goes out of scope */
term_t term = PL_new_term_ref();
char *buffer = const_cast<char *>(prolog_data.data());
size_t size = prolog_data.size();
{
Frame fid_t{};
term_t terms = PL_new_term_refs(5);
term_t stream = terms + 0;
term_t name = terms + 1;
PL_put_atom(name, PL_new_atom(streamname.c_str()));
// Name -> atom of name
PL_unify_stream(stream, Sopenmem(&buffer, &size, "r"));
// Stream -> pipe stream
term_t stream_opt = terms + 2;
functor_t stream_functor = PL_new_functor(PL_new_atom("stream"), 1);
[[maybe_unused]] int rv;
rv = PL_cons_functor(stream_opt, stream_functor, stream);
// StreamOpt -> stream(Stream)
term_t nil = terms + 3;
PL_put_nil(nil);
// Nil -> []
term_t opts = terms + 4;
rv = PL_cons_list(opts, stream_opt, nil);
// Opts -> [StreamOpt]
functor_t lf_functor = PL_new_functor(PL_new_atom("load_files"), 2);
rv = PL_cons_functor(term, lf_functor, name, opts);
// Term -> load_files(Name, Opts)
}
/* Run the query */
auto succeed = PL_call(term, nullptr);
if (!succeed)
{
std::cerr << "Could not consult file" << std::endl;
PL_halt(EXIT_FAILURE);
}
put_term(term, query_string);
succeed = PL_call(term, nullptr);
return succeed;
}
}
std::vector<coverage_entry> run_prolog_with_coverage(std::string const &prolog_data,
std::string const &streamname,
std::string const &query_string)
{
initialize_helper();
std::vector<coverage_entry> out;
{
Frame fid{}; /* All created terms will go away when
* fid goes out of scope */
term_t term = PL_new_term_ref();
term_t inner_term = PL_new_term_ref();
char *buffer = const_cast<char *>(prolog_data.data());
size_t size = prolog_data.size();
{
Frame fid_t{};
term_t terms = PL_new_term_refs(5);
term_t stream = terms + 0;
term_t name = terms + 1;
PL_put_atom(name, PL_new_atom(streamname.c_str()));
// Name -> atom of name
PL_unify_stream(stream, Sopenmem(&buffer, &size, "r"));
// Stream -> pipe stream
term_t stream_opt = terms + 2;
functor_t stream_functor = PL_new_functor(PL_new_atom("stream"), 1);
[[maybe_unused]] int rv;
rv = PL_cons_functor(stream_opt, stream_functor, stream);
// StreamOpt -> stream(Stream)
term_t nil = terms + 3;
PL_put_nil(nil);
// Nil -> []
term_t opts = terms + 4;
rv = PL_cons_list(opts, stream_opt, nil);
// Opts -> [StreamOpt]
functor_t lf_functor = PL_new_functor(PL_new_atom("load_files"), 2);
rv = PL_cons_functor(term, lf_functor, name, opts);
// Term -> load_files(Name, Opts)
}
/* Run load_files */
auto succeed = PL_call(term, nullptr);
if (!succeed)
{
std::cerr << "Could not consult file" << std::endl;
PL_halt(EXIT_FAILURE);
}
{
Frame fid_t{};
term_t terms = PL_new_term_refs(6);
predicate_t cov_pred = PL_predicate("get_coverage", 6, NULL);
if (!cov_pred)
{
std::cerr << "Could not find get_coverage/6" << std::endl;
PL_halt(EXIT_FAILURE);
}
put_term(terms + 0, query_string);
qid_t qid = PL_open_query(NULL, PL_Q_NORMAL | PL_Q_EXT_STATUS, cov_pred, terms);
if (!qid)
{
std::cerr << "Unable to open query" << std::endl;
PL_halt(EXIT_FAILURE);
}
int rv;
bool continue_loop = true;
while (continue_loop)
{
rv = PL_next_solution(qid);
switch (rv)
{
case PL_S_EXCEPTION:
std::cerr << "Exception" << std::endl;
abort();
PL_halt(EXIT_FAILURE);
break;
case PL_S_FALSE:
std::cerr << "Query failed: " << query_string << std::endl;
PL_halt(EXIT_FAILURE);
break;
case PL_S_TRUE:
case PL_S_LAST:
const auto get_string_from_prolog = [](const term_t &term)
{
size_t len;
char *s;
int rv = PL_get_string_chars(term, &s, &len);
if (rv)
{
return std::string(s, len);
}
else
{
rv = PL_get_atom_chars(term, &s);
assert(rv);
return std::string(s);
}
abort();
};
const auto get_size_from_prolog = [](const term_t &term)
{
uint64_t sz;
assert(PL_get_uint64(term, &sz));
return sz;
};
coverage_entry cov;
cov.file = get_string_from_prolog(terms + 1);
cov.predicate = get_string_from_prolog(terms + 2);
cov.line = get_size_from_prolog(terms + 3);
cov.pos = get_size_from_prolog(terms + 4);
cov.neg = get_size_from_prolog(terms + 5);
out.push_back(cov);
if (rv == PL_S_LAST)
continue_loop = false;
break;
}
}
PL_close_query(qid);
}
return out;
}
}
} // namespace Prolog