-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
282 lines (221 loc) · 5.19 KB
/
tests.py
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
from natlog.natlog import *
from natlog.neural_natlog import *
my_text = """
app () Ys Ys.
app (X Xs) Ys (X Zs) :
app Xs Ys Zs.
nrev () ().
nrev (X Xs) Zs : nrev Xs Ys, app Ys (X ()) Zs.
goal N L :
`genList N Xs,
nrev Xs Ys,
`llen Ys L.
"""
def test_generators():
prog = """
good 'l'.
good 'o'.
goal X : ``iter hello X, good X.
goal X : ``range 1000 1005 X.
"""
n = Natlog(text=prog)
for answer in n.solve("goal R?"):
print(answer[1])
def test_answer_stream():
prog = """
perm () ().
perm (X Xs) Zs : perm Xs Ys, ins X Ys Zs.
ins X Xs (X Xs).
ins X (Y Xs) (Y Ys) : ins X Xs Ys.
"""
n = Natlog(text=prog)
for answer in n.solve("perm (a (b (c ()))) P?"):
print(answer[2])
def yield_test():
prog = """
worm : ^o, worm.
"""
n = Natlog(text=prog)
for i, answer in enumerate(n.solve("worm ?")):
print(answer[0], end='')
if i > 42: break
print('')
# testing with string text
def t1():
n = Natlog(text=my_text)
n.query("nrev (a (b (c (d ())))) R ?")
n.query("goal 10 L?")
# testing with some .nat files
def t2():
n = Natlog(file_name="natprogs/tc.nat")
print(n)
n.query("tc Who is animal ?")
# n.query("tc Who is What ?")
def t4():
n = Natlog(file_name="natprogs/perm.nat")
n.query("perm (1 (2 (3 ()))) Ps?")
def t3():
n = Natlog(file_name="natprogs/arith.nat")
print(n)
n.query("goal R ?")
# longer output: 8 queens
def t5():
n = Natlog(file_name="natprogs/queens.nat")
print(n)
n.query("goal10 Queens?")
def t6():
n = Natlog(file_name="natprogs/family.nat")
print(n)
n.query("grand_parent_of 'Joe' GP ?")
def t7():
n = Natlog(file_name="natprogs/family.nat")
n.query("cousin of X B?")
def t8():
n = Natlog(file_name="natprogs/family.nat")
print('Enter some queries!')
n.repl()
def loop():
n = Natlog(file_name="natprogs/loop.nat")
print(n)
n.query("goal X?")
def db_test():
nd = Natlog(file_name="natprogs/dbtc.nat", db_name="natprogs/Db.nat")
print('RULES')
print(nd)
print('DB FACTS')
print(nd.db)
nd.query("tc Who is_a animal ?")
nd.repl()
def ndb_test():
nd = NeuralNatlog(file_name="natprogs/dbtc.nat", db_name="natprogs/Db.nat")
print('RULES')
print(nd)
print('DB FACTS')
print(nd.db)
nd.query("tc Who is_a animal ?")
def db_chem():
nd = Natlog(
file_name="natprogs/elements.nat",
db_name="natprogs/elements.tsv"
)
print('RULES')
print(nd)
print('DB FACTS')
print(nd.db)
nd.query("an_el Num Element ?")
nd.query("gases Num Element ?")
def ndb_chem():
nd = NeuralNatlog(
file_name="natprogs/elements.nat",
db_name="natprogs/elements.tsv"
)
print('RULES')
print(nd)
print('DB FACTS')
print(nd.db)
nd.query("gases Num Element ?")
def py_test():
nd = Natlog(file_name="natprogs/py_call.nat")
print('RULES')
# print(nd)
nd.query("goal X?")
def py_test1():
nd = Natlog(file_name="natprogs/py_call1.nat")
print('RULES')
# print(nd)
nd.query("goal X?")
def go():
t1()
t2()
t3()
t4()
t5()
t6()
# tests
c1 = ('a', Int(1), 'car', 'a')
c2 = ('a', Int(2), 'horse', 'aa')
c3 = ('b', Int(1), 'horse', 'b')
c4 = ('b', Int(2), 'car', 'bb')
g1 = ('a', 0, 1, 2)
g2 = (0, 1, 'car', 2)
g3 = (0, 1, 2, 0)
def dtest1():
print(c1, '<-const:', list(const_of(c1)))
print(c3, '<-vars:', list(vars_of(c3)))
d = Db()
for cs in [c1, c2, c3, c4]:
d.add_clause(cs)
print('index', d.index)
print('css', d.css)
print('Gmatch', g1, list(d.ground_match_of(g1)))
print('Vmatch', g1, list(d.match_of(g1)))
print('Gmatch', g2, list(d.ground_match_of(g2)))
print('Vmatch', g2, list(d.match_of(g2)))
print('Gmatch', g3, list(d.ground_match_of(g3)))
print('Vmatch', g3, list(d.match_of(g3)))
# Db built form text
def dtest():
text = """
John has (a car).
Mary has (a bike).
Mary is (a student).
John is (a pilot).
"""
print(text)
d = Db()
d.digest(text)
print(d)
print('')
query = "Who has (a What)?"
d.ask(query)
query = "Who is (a pilot)?"
d.ask(query)
query = "'Mary' is What?"
d.ask(query)
query = "'John' is (a What)?"
d.ask(query)
query = "Who is What?"
d.ask(query)
# Db from a .nat file
def dtestf():
fname = 'natprogs/Db.nat'
d = Db()
d.load(fname)
print(d)
print('LOADED:', fname)
d.ask("Who is mammal?")
# Db from a json file
def dtestj():
fname = 'natprogs/Db'
jname = fname + '.json'
nname = fname + '.nat'
d = Db()
d.load(nname)
d.save(jname)
d = Db()
d.load(jname)
# print(d)
print('LOADED:', jname)
print("")
query = "Who is What?"
d.ask(query)
if __name__ == "__main__":
"""
uncomment any
db_test()
py_test()
test_generators()
test_answer_stream()
yield_test()
dtestj()
t5()
t7()
ndb_test() # tests transitive closure with learner
ndb_chem() # tests query about chemical elements
"""
pass
db_test()
#t7()
##t8()
#go()
#py_test()