-
Notifications
You must be signed in to change notification settings - Fork 0
/
prolog.py
57 lines (47 loc) · 1.35 KB
/
prolog.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
from pyswip import Prolog
prolog = Prolog()
prolog.consult("src/familie.pl")
prolog.consult("src/teil1.pl")
prolog.consult("src/test.pl")
# Tests, executed by prolog
for result in prolog.query("test(allfamilie)"):
print(result)
# Example from prolog code
"""
test(nachkomme) :-
%positiv
write('nachkomme +'),
nachkomme(helga,eve), write('.'),!,
nachkomme(susanne,klara),
%negativ
write('. -'),!,
\+ nachkomme(helga,marion), write('.'),!,
\+ nachkomme(kruemmel,klara),!, writeln('ok').
"""
# Tests, example translated to python
# (not 1:1 the same, but tests same behaviour)
# positive
descendants = []
for results in prolog.query("nachkomme(helga,Y)"):
descendants.append(results['Y'])
assert 'eve' in descendants
# positive
descendants = []
for results in prolog.query("nachkomme(susanne,Y)"):
descendants.append(results['Y'])
assert 'klara' in descendants
# positive / relationship twisted
ancestors = []
for results in prolog.query("nachkomme(X,klara)"):
ancestors.append(results['X'])
assert 'susanne' in ancestors
# negative
descendants = []
for results in prolog.query("nachkomme(helga,Y)"):
descendants.append(results['Y'])
assert 'marion' not in descendants
# negative
descendants = []
for results in prolog.query("nachkomme(kruemel,Y)"):
descendants.append(results['Y'])
assert 'klara' not in descendants