-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes_test.cpp
319 lines (270 loc) · 10.6 KB
/
types_test.cpp
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "catch.hpp"
#include "Datalog.h"
using namespace datalog;
bool test1()
{
// Relations
typedef const char* Name;
enum Kind {person, god};
struct Thing : Relation<Name, Kind>{};
struct Mortal : Relation<Name>{};
// Extensional data
Name socrates{"Socrates"};
Name rhiannon{"Rhiannon"};
Name albert{"Albert"};
Name anna{"Anna"};
Name henry{"Henry"};
Name ian{"Ian"};
Name zeus{"Zeus"};
Name persephone{"Persephone"};
Name thor{"Thor"};
Thing::Set things{
{socrates, person},
{rhiannon, person},
{albert, person},
{anna, person},
{henry, person},
{ian, person},
{zeus, god},
{persephone, god},
{thor, god}};
// Rule
auto x = var<Name>();
auto rule1 = rule(atom<Mortal>(x), atom<Thing>(x, person));
State<Thing, Mortal> state{things, {}};
// Apply rule
auto rules = ruleset(rule1);
cout << "before = " << state << endl;
state = fixPoint(rules, state);
cout << "after = " << state << endl;
deleteVar(x);
return true;
}
bool test2()
{
// Relations
typedef const char* Name;
struct Adviser : Relation<Name, Name>{};
struct AcademicAncestor : Relation<Name, Name>{};
struct QueryResult : Relation<Name>{};
// Extensional data
Name andrew{"Andrew Rice"};
Name mistral{"Mistral Contrastin"};
Name dominic{"Dominic Orchard"};
Name andy{"Andy Hopper"};
Name alan{"Alan Mycroft"};
Name rod{"Rod Burstall"};
Name robin{"Robin Milner"};
Name david{"David Wheeler"};
Adviser::Set advisers{
{andrew, mistral},
{dominic, mistral},
{andy, andrew},
{alan, dominic},
{david, andy},
{rod, alan},
{robin, alan}};
auto x = new Variable<Name>();
auto y = new Variable<Name>();
auto z = new Variable<Name>();
// TODO
//auto inDirectAcademicAncestor = atom<AcademicAncestor>(x, z) <= atom<Adviser>(x, y) && atom<AcademicAncestor>(y, z);
auto directAcademicAncestor = rule(atom<AcademicAncestor>(x, y), atom<Adviser>(x, y));
auto indirectAcademicAncestor = rule(atom<AcademicAncestor>(x, z), atom<Adviser>(x, y), atom<AcademicAncestor>(y, z));
auto query = rule(
atom<QueryResult>(x),
body(
atom<AcademicAncestor>(robin, x),
atom<AcademicAncestor>(x, mistral)
)
);
// Apply rules
auto rules = ruleset(directAcademicAncestor, indirectAcademicAncestor, query);
State<Adviser, AcademicAncestor, QueryResult> state{advisers, {}, {}};
cout << "before = " << state << endl;
state = fixPoint(rules, state);
cout << "after = " << state << endl;
delete x;
delete y;
delete z;
return true;
}
bool po1()
{
typedef unsigned int Number;
struct Check : Relation<Number, Number, Number, Number, Number, Number>{};
struct In : Relation<Number, Number, Number, Number, Number, Number, Number>{};
struct A : Relation<Number, Number>{};
#include "in.txt"
#include "check.txt"
State<Check, In, A> state{check, in, {}};
auto a = new Variable<Number>();
auto b = new Variable<Number>();
auto c = new Variable<Number>();
auto d = new Variable<Number>();
auto e = new Variable<Number>();
auto f = new Variable<Number>();
auto i = new Variable<Number>();
auto anon1 = new Variable<Number>();
auto anon2 = new Variable<Number>();
auto anon3 = new Variable<Number>();
auto anon4 = new Variable<Number>();
auto anon5 = new Variable<Number>();
auto anon6 = new Variable<Number>();
auto anon7 = new Variable<Number>();
auto anon8 = new Variable<Number>();
// A(1,i) :- Check(_, b, c, d, e, f), In(_, b, c, d, e, f, i).
auto rule1 = rule(atom<A>(1u, i), atom<Check>(anon1, b, c, d, e, f), atom<In>(anon2, b, c, d, e, f, i));
// A(2,i) :- Check(a, _, c, d, e, f), In(a, _, c, d, e, f, i).
auto rule2 = rule(atom<A>(2u, i), atom<Check>(a, anon1, c, d, e, f), atom<In>(a, anon2, c, d, e, f, i));
// A(3,i) :- Check(a, b, _, d, e, f), In(a, b, _, d, e, f, i).
auto rule3 = rule(atom<A>(3u, i), atom<Check>(a, b, anon1, d, e, f), atom<In>(a, b, anon2, d, e, f, i));
// A(4,i) :- Check(a, b, c, _, e, f), In(a, b, c, _, e, f, i).
auto rule4 = rule(atom<A>(4u, i), atom<Check>(a, b, c, anon1, e, f), atom<In>(a, b, c, anon2, e, f, i));
// A(5,i) :- Check(a, b, c, d, _, f), In(a, b, c, d, _, f, i).
auto rule5 = rule(atom<A>(5u, i), atom<Check>(a, b, c, d, anon1, f), atom<In>(a, b, c, d, anon2, f, i));
// A(6,i) :- Check(a, b, c, d, e, _), In(a, b, c, d, e, _, i).
auto rule6 = rule(atom<A>(6u, i), atom<Check>(a, b, c, d, e, anon1), atom<In>(a, b, c, d, e, anon2, i));
// A(7, i) :- Check(_, _, c, d, e, f), In(_, _, c, d, e, f, i).
auto rule7 = rule(atom<A>(7u, i), atom<Check>(anon1, anon2, c, d, e, f), atom<In>(anon3, anon4, c, d, e, f, i));
// A(8, i) :- Check(a, _, _, d, e, f), In(a, _, _, d, e, f, i).
auto rule8 = rule(atom<A>(8u, i), atom<Check>(a, anon1, anon2, d, e, f), atom<In>(a, anon3, anon4, d, e, f, i));
// A(9, i) :- Check(a, b, _, _, e, f), In(a, b, _, _, e, f, i).
auto rule9 = rule(atom<A>(9u, i), atom<Check>(a, b, anon1, anon2, e, f), atom<In>(a, b, anon3, anon4, e, f, i));
// A(10, i) :- Check(a, b, c, _, _, f), In(a, b, c, _, _, f, i).
auto rule10 = rule(atom<A>(10u, i), atom<Check>(a, b, c, anon1, anon2, f), atom<In>(a, b, c, anon3, anon4, f, i));
// A(11, i) :- Check(a, b, c, d, _, _), In(a, b, c, d, _, _, i).
auto rule11 = rule(atom<A>(11u, i), atom<Check>(a, b, c, d, anon1, anon2), atom<In>(a, b, c, d, anon3, anon4, i));
// A(12, i) :- Check(_, _, _, d, e, f), In(_, _, _, d, e, f, i).
auto rule12 = rule(atom<A>(12u, i), atom<Check>(anon1, anon2, anon3, d, e, f), atom<In>(anon4, anon5, anon6, d, e, f, i));
// A(13, i) :- Check(a, _, _, _, e, f), In(a, _, _, _, e, f, i).
auto rule13 = rule(atom<A>(13u, i), atom<Check>(a, anon1, anon2, anon3, e, f), atom<In>(a, anon4, anon5, anon6, e, f, i));
// A(14, i) :- Check(a, b, _, _, _, f), In(a, b, _, _, _, f, i).
auto rule14 = rule(atom<A>(14u, i), atom<Check>(a, b, anon1, anon2, anon3, f), atom<In>(a, b, anon4, anon5, anon6, f, i));
// A(15, i) :- Check(a, b, c, _, _, _), In(a, b, c, _, _, _, i).
auto rule15 = rule(atom<A>(15u, i), atom<Check>(a, b, c, anon1, anon2, anon3), atom<In>(a, b, c, anon4, anon5, anon6, i));
// A(16, i) :- Check(_, _, _, _, e, f), In(_, _, _, _, e, f, i).
auto rule16 = rule(atom<A>(16u, i),
atom<Check>(anon1, anon2, anon3, anon4, e, f), atom<In>(anon5, anon6, anon7, anon8, e, f, i)
);
// A(17, i) :- Check(a, _, _, _, _, f), In(a, _, _, _, _, f, i).
auto rule17 = rule(atom<A>(17u, i), atom<Check>(a, anon1, anon2, anon3, anon4, f), atom<In>(a, anon5, anon6, anon7, anon8, f, i));
// A(18, i) :- Check(a, b, _, _, _, _), In(a, b, _, _, _, _, i).
auto rule18 = rule(atom<A>(18u, i), atom<Check>(a, b, anon1, anon2, anon3, anon4), atom<In>(a, b, anon5, anon6, anon7, anon8, i));
// A(19, i) :- Check(a, b, c, d, e, f), In(a, b, c, d, e, f, i).
auto rule19 = rule(atom<A>(19u, i), atom<Check>(a, b, c, d, e, f), atom<In>(a, b, c, d, e, f, i));
auto rules = ruleset(rule1, rule2, rule3, rule4, rule5, rule6, rule7, rule8, rule9, rule10, rule11, rule12, rule13,
rule14, rule15, rule16, rule17, rule18, rule19);
//cout << "before = " << state << endl;
state = fixPoint(rules, state);
#include "a.txt"
// TODO: FIXME
//const auto& computedA = state.getSet<A>();
auto& temp = state.getTrackedSet<A>();
const auto& computedA = convert<A>(temp);
//return convert<RELATION_TYPE>(getTrackedSet<RELATION_TYPE>());
// cout << "result = ";
// operator<< <A>(cout, computedA);
// cout << endl;
delete a;
delete b;
delete c;
delete d;
delete e;
delete f;
delete i;
delete anon1;
delete anon2;
delete anon3;
delete anon4;
delete anon5;
delete anon6;
delete anon7;
delete anon8;
return computedA == aOut;
}
bool test4()
{
// Relations
typedef const char* Name;
typedef unsigned int Age;
enum Gender {male, female, NA};
enum Country {england, scotland, wales, france, germany, netherlands, spain};
struct Person : Relation<Name, Age, Gender, Country>{};
// Extensional data
Name sam{"Sam"};
Name tim{"Tim"};
Name rod{"Rod"};
Name bob{"Bob"};
Name jill{"Jill"};
Name jane{"Jane"};
Name sally{"Sally"};
Person::Set people{
{sam, 48u, male, scotland},
{tim, 25u, male, england},
{rod, 38u, male, germany},
{bob, 18u, male, england},
{jill, 56u, female, wales},
{jane, 32u, female, france},
{sally, 40u, female, netherlands}
};
auto name = new Variable<Name>();
auto age = new Variable<Age>();
auto gender = new Variable<Gender>();
auto country = new Variable<Country>();
struct Female : Relation<Name>{};
auto females = rule(
atom<Female>(name),
atom<Person>(name, age, female, country)
);
typedef float Metres;
struct Height : Relation<Name, Metres>{};
auto height = new Variable<Metres>();
auto heights = rule(
atom<Height>(name, 1.0f),
body(
atom<Person>(name, age, gender, country)
)
);
// Use this pattern to get at values too
auto anyPerson = atom<Person>(name, age, gender, country);
auto externalHeights1 = rule(
atom<Height>(name, height),
body(
atom<Person>(name, age, gender, country)
),
lambda(
height,
[&anyPerson]() {
//cout << "hello world!" << endl;
auto person = ground<Person>(anyPerson);
auto age = get<Age>(person);
return age * 3.0f;
}
)
);
// Use this pattern to get at variables (values can't be got directly)
auto externalHeights = rule(
atom<Height>(name, height),
body(atom<Person>(name, age, female, country)),
lambda(height, [&age]() { return age->value() * 3.0f; } )
);
// Apply rules
auto rules = ruleset(females, heights, externalHeights);
State<Person, Female, Height> state{people, {}, {}};
cout << "before = " << state << endl;
state = fixPoint(rules, state);
cout << "after = " << state << endl;
delete name;
delete age;
delete gender;
delete country;
delete height;
return true;
}
TEST_CASE( "toy-examples", "[types-test]" ) {
REQUIRE( test1() );
REQUIRE( test2() );
REQUIRE( po1() );
REQUIRE( test4() );
}