-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain5.py
executable file
·158 lines (137 loc) · 6.16 KB
/
main5.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
#!/usr/bin/env python
import unittest
import read
from student_code import KnowledgeBase
from logical_classes import *
class CustomTests4(unittest.TestCase):
def setUp(self):
file = 'statements_kb4.txt'
self.data = read.read_tokenize(file)
data = read.read_tokenize(file)
self.KB = KnowledgeBase([], [])
for item in data:
if isinstance(item, Fact) or isinstance(item, Rule):
self.KB.kb_assert(item)
# ########################### checkin retract ########################
# def test1(self):
# ask1 = read.parse_input("fact: (motherof ?X ?Y)")
# answer1 = self.KB.kb_ask(ask1)
# self.assertEqual(len(answer1), 4)
# self.assertEqual(str(answer1[0]), "?X : ada, ?Y : bing")
# self.assertEqual(str(answer1[1]), "?X : bing, ?Y : chen")
# self.assertEqual(str(answer1[2]), "?X : dolores, ?Y : chen")
# self.assertEqual(str(answer1[3]), "?X : greta, ?Y : felix")
# ask2 = read.parse_input("fact: (sisters ?X ?Y)")
# answer2 = self.KB.kb_ask(ask2)
# self.assertEqual(len(answer2), 1)
# self.assertEqual(str(answer2[0]), "?X : ada, ?Y : eva")
# def test2(self):
# ask1 = read.parse_input("fact: (parentof ?X ?Y)")
# answer1 = self.KB.kb_ask(ask1)
# self.assertEqual(len(answer1), 4)
# self.assertEqual(str(answer1[0]), "?X : ada, ?Y : bing")
# self.assertEqual(str(answer1[1]), "?X : bing, ?Y : chen")
# self.assertEqual(str(answer1[2]), "?X : dolores, ?Y : chen")
# self.assertEqual(str(answer1[3]), "?X : greta, ?Y : felix")
# def test3(self):
# ask1 = read.parse_input("fact: (auntof ?X ?Y)")
# answer1 = self.KB.kb_ask(ask1)
# self.assertEqual(len(answer1), 1)
# self.assertEqual(str(answer1[0]), "?X : eva, ?Y : bing")
# def test4(self):
# ask1 = read.parse_input("fact: (grandmotherof ?X ?Y)")
# answer1 = self.KB.kb_ask(ask1)
# self.assertEqual(len(answer1), 2)
# self.assertEqual(str(answer1[0]), "?X : ada, ?Y : felix")
# self.assertEqual(str(answer1[1]), "?X : ada, ?Y : chen")
# ########################### checkin retract ########################
def test5(self):
r1 = read.parse_input("fact: (motherof dolores chen")
print(' Retracting', r1)
self.KB.kb_retract(r1)
ask1 = read.parse_input("fact: (parentof dolores ?X)")
print(' Asking if', ask1)
answer = self.KB.kb_ask(ask1)
# print(answer)
self.assertEqual(len(answer), 0)
def test4_6(self):
print('\nTest 6')
r1 = read.parse_input("fact: (sisters ada eva)")
print(' Retracting', r1)
self.KB.kb_retract(r1)
ask1 = read.parse_input("fact: (auntof eva ?X)")
print(' Asking if', ask1)
answer = self.KB.kb_ask(ask1)
# print(answer)
self.assertEqual(len(answer), 0)
def test7(self):
ask1 = read.parse_input("fact: (greatgrandmotherof ada ?X)")
print(' Asking if', ask1)
answer = self.KB.kb_ask(ask1)
self.assertEqual(str(answer[0]), "?X : bob")
r1 = read.parse_input("fact: (motherof felix bob)")
print(' Retracting', r1)
self.KB.kb_retract(r1)
print(' Asking if', ask1)
answer = self.KB.kb_ask(ask1)
self.assertEqual(str(answer), "[]")
def test8(self):
r1 = read.parse_input("fact: (parentof felix bob)")
print(' Retracting', r1)
self.KB.kb_retract(r1)
ask1 = read.parse_input("fact: (greatgrandmotherof ada ?X)")
print(' Asking if', ask1)
answer = self.KB.kb_ask(ask1)
self.assertEqual(str(answer[0]), "?X : bob")
def test9(self):
r1 = read.parse_input("rule: ((parent felix ?y)) -> (greatgrandmother ada ?y)")
print(' Retracting', r1)
self.KB.kb_retract(r1)
ask1 = read.parse_input("fact: (greatgrandmotherof ada ?X)")
print(' Asking if', ask1)
answer = self.KB.kb_ask(ask1)
self.assertEqual(str(answer[0]), "?X : bob")
# def test10(self):
# fact = read.parse_input("fact: (grandmotherof ada felix)")
# self.KB.kb_retract(fact)
# ask1 = read.parse_input("fact: (grandmotherof ?X ?Y)")
# answer1 = self.KB.kb_ask(ask1)
# self.assertEqual(len(answer1), 1)
# self.assertEqual(str(answer1[0]), "?X : ada, ?Y : chen")
# fact = read.parse_input("fact: (grandmotherof ada chen)")
# self.KB.kb_retract(fact)
# ask1 = read.parse_input("fact: (grandmotherof ?X ?Y)")
# answer1 = self.KB.kb_ask(ask1)
# self.assertEqual(len(answer1), 1)
# self.assertEqual(str(answer1[0]), "?X : ada, ?Y : chen")
# fact = read.parse_input("fact: (motherof bing chen)")
# self.KB.kb_retract(fact)
# ask1 = read.parse_input("fact: (grandmotherof ?X ?Y)")
# answer1 = self.KB.kb_ask(ask1)
# self.assertEqual(len(answer1), 0)
# def test6(self):
# fact = read.parse_input("fact: (grandmotherof ada felix)")
# self.KB.kb_retract(fact)
# ask1 = read.parse_input("fact: (grandmotherof ?X ?Y)")
# answer1 = self.KB.kb_ask(ask1)
# self.assertEqual(len(answer1), 1)
# self.assertEqual(str(answer1[0]), "?X : ada, ?Y : chen")
# fact = read.parse_input("fact: (grandmotherof ada chen)")
# self.KB.kb_retract(fact)
# ask1 = read.parse_input("fact: (grandmotherof ?X ?Y)")
# answer1 = self.KB.kb_ask(ask1)
# self.assertEqual(len(answer1), 1)
# self.assertEqual(str(answer1[0]), "?X : ada, ?Y : chen")
# fact = read.parse_input("fact: (motherof ada bing)")
# self.KB.kb_retract(fact)
# ask1 = read.parse_input("fact: (grandmotherof ?X ?Y)")
# answer1 = self.KB.kb_ask(ask1)
# self.assertEqual(len(answer1), 0)
# def test7(self):
# fact = read.parse_input("fact: (sisters ada eva)")
# self.KB.kb_retract(fact)
# ask1 = read.parse_input("fact: (auntof ?X ?Y)")
# answer1 = self.KB.kb_ask(ask1)
# self.assertEqual(len(answer1), 0)
if __name__ == '__main__':
unittest.main()