def main(): KB = KnowledgeBase([], []) _, fact1 = read.parse_input("fact: (hero A)") _, fact2 = read.parse_input("fact: (person A)") _, rule1 = read.parse_input( "rule: ((hero ?x) (person ?x)) -> (goodman ?x)") _, rule2 = read.parse_input( "rule: ((goodman ?x) (wenttoschool ?x)) -> (doctor ?x)") _, fact3 = read.parse_input("fact: (wenttoschool A)") _, ask1 = read.parse_input("fact: (goodman A)") _, ask2 = read.parse_input("fact: (doctor A)") KB.kb_assert(fact1) KB.kb_assert(fact2) KB.kb_assert(rule1) answer1 = KB.kb_ask(ask1) KB.kb_assert(rule2) KB.kb_assert(fact3) answer2 = KB.kb_ask(ask2) if answer1 and answer2: print "pass test3" exit(0) else: print "fail test3" exit(1)
class KBTest(unittest.TestCase): def setUp(self): # Assert starter facts file = 'statements_kb.txt' data = read.read_tokenize(file) self.KB = KnowledgeBase([], []) for item in data: if isinstance(item, Fact): self.KB.kb_assert(item) ''' def test1(self): ask1 = read.parse_input("fact: (color bigbox red)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(answer[0].bindings, []) #self.assertEqual(answer.list_of_bindings[0][1][0], ask1) def test2(self): ask1 = read.parse_input("fact: (color littlebox red)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertFalse(answer) ''' def test3(self): ask1 = read.parse_input("fact: (color ?X red)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]), "?X : bigbox") self.assertEqual(str(answer[1]), "?X : pyramid3") self.assertEqual(str(answer[2]), "?X : pyramid4") '''
class KB2Tests(unittest.TestCase): def setUp(self): # Assert starter facts file = 'statements_kb2.txt' data = read.read_tokenize(file) self.KB = KnowledgeBase([], []) for item in data: if isinstance(item, Fact): self.KB.kb_assert(item) def test1(self): ask1 = read.parse_input("fact: (attacked Ai Nosliw)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(answer[0].bindings, []) def test2(self): ask1 = read.parse_input("fact: (diamonds ?X)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]), "?X : Loot") def test3(self): ask1 = read.parse_input("fact: (rubies gem)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertFalse(answer)
def setUp(self): # Assert starter facts file = 'statements_kb.txt' data = read.read_tokenize(file) self.KB = KnowledgeBase([], []) for item in data: if isinstance(item, Fact): self.KB.kb_assert(item)
def setUp(self): file = 'statements_kb3.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)
def setUp(self): # Assert starter facts file = 'geography.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)
def setUp(self): # Assert starter facts file = 'statements_kb4.txt' self.data = read.read_tokenize(file) data = read.read_tokenize(file) self.KB = KnowledgeBase([], []) for item in data: print(item.name) if isinstance(item, Fact) or isinstance(item, Rule): self.KB.kb_assert(item)
def main(): KB = KnowledgeBase([], []) _, fact1 = read.parse_input("fact: (rela A B C D E F)") _, fact2 = read.parse_input("fact: (relb D E F G H I)") _, fact3 = read.parse_input("fact: (reld G H I)") _, rule1 = read.parse_input( "rule: ((rela ?a ?b ?c ?d ?e ?f) (relb ?d ?e ?f ?g ?h ?i)) -> (relc ?a ?b ?c ?g ?h ?i)" ) _, rule2 = read.parse_input( "rule: ((relc ?a ?b ?c ?g ?h ?i) (reld ?g ?h ?i)) -> (rele ?a ?b ?c)") _, ask1 = read.parse_input("fact: (rele A B C)") KB.kb_assert(fact1) KB.kb_assert(fact2) KB.kb_assert(fact3) KB.kb_assert(rule1) KB.kb_assert(rule2) answer1 = KB.kb_ask(ask1) if answer1: print "pass test7" exit(0) else: print "fail test7" exit(1)
class KBTest(unittest.TestCase): def setUp(self): # Assert starter facts file = 'statements_kb.txt' data = read.read_tokenize(file) self.KB = KnowledgeBase([], []) for item in data: if isinstance(item, Fact): self.KB.kb_assert(item) def test1(self): ask1 = read.parse_input("fact: (color bigbox red)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(answer[0].bindings, []) #self.assertEqual(answer.list_of_bindings[0][1][0], ask1) def test2(self): ask1 = read.parse_input("fact: (color littlebox red)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertFalse(answer) def test3(self): ask1 = read.parse_input("fact: (color ?X red)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]), "?X : bigbox") self.assertEqual(str(answer[1]), "?X : pyramid3") self.assertEqual(str(answer[2]), "?X : pyramid4") def test4(self): ask1 = read.parse_input("fact: (color bigbox ?Y)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]), "?Y : red") def test5(self): ask1 = read.parse_input("fact: (color ?X ?Y)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]), "?X : bigbox, ?Y : red") self.assertEqual(str(answer[1]), "?X : littlebox, ?Y : blue") self.assertEqual(str(answer[2]), "?X : pyramid1, ?Y : blue") self.assertEqual(str(answer[3]), "?X : pyramid2, ?Y : green") self.assertEqual(str(answer[4]), "?X : pyramid3, ?Y : red") self.assertEqual(str(answer[5]), "?X : pyramid4, ?Y : red") def test6(self): ask1 = read.parse_input("fact: (size pyramid2 ?Y") print('Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]),"?Y : small") def test7(self): ask1 = read.parse_input("fact: (size ?X small") print('Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]),"?X : littlebox")
def main(): KB = KnowledgeBase([], []) _, fact1 = read.parse_input("fact: (rela A X)") _, fact2 = read.parse_input("fact: (relb B X)") _, fact3 = read.parse_input("fact: (relc C X)") _, fact4 = read.parse_input("fact: (reld D X)") _, fact5 = read.parse_input("fact: (rele E X)") _, rule1 = read.parse_input( "rule: ((rela ?a ?x) (relb ?b ?x) (relc ?c ?x) (reld ?d ?x) (rele ?e ?x)) -> (relf ?x)" ) _, ask1 = read.parse_input("fact: (relf X)") KB.kb_assert(fact1) KB.kb_assert(fact2) KB.kb_assert(fact3) KB.kb_assert(fact4) KB.kb_assert(fact5) KB.kb_assert(rule1) answer1 = KB.kb_ask(ask1) if answer1: print "pass test8" exit(0) else: print "fail test8" exit(1)
class CustomTests2(unittest.TestCase): def setUp(self): # Assert starter facts file = 'statements_kb5.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) def test6(self): # makes sure retract does not retract supported fact ask1 = read.parse_input("fact: (grandparent A ?X)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]), "?X : C") # self.assertEqual(str(answer[1]), "?X : chen") r1 = read.parse_input("fact: (parent B C)") print(' Retracting', r1) self.KB.kb_retract(r1) print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]), "?X : C") r1 = read.parse_input("fact: (parent A D)") print(' Retracting', r1) self.KB.kb_retract(r1) print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) print(str(len(answer))) self.assertEqual(len(answer), 0)
def setUp(self): # Assert starter facts file = 'statements_kb2.txt' data = read.read_tokenize(file) self.KB = KnowledgeBase([], []) count = 0 for item in data: count = count + 1 if isinstance(item, Fact): self.KB.kb_assert(item) print(count) print(len(self.KB.facts)) print(self.KB.facts)
def setUp(self): # Assert starter facts 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) # for Piazza-provided tests file = 'statements_kb5.txt' data = read.read_tokenize(file) for item in data: if isinstance(item, Fact) or isinstance(item, Rule): self.KB.kb_assert(item)
class KBTest2(unittest.TestCase): def setUp(self): # Assert starter facts file = 'statements_kb2.txt' data = read.read_tokenize(file) self.KB = KnowledgeBase([], []) for item in data: if isinstance(item, Fact): self.KB.kb_assert(item) def test2_1(self): ask1 = read.parse_input("fact: (attacked Ai Nosliw)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(answer[0].bindings, []) #self.assertEqual(aswer.list_of_bindings[0][1][0], ask1) print("test 2_1 passed!") def test2_2(self): ask1 = read.parse_input("fact: (color littlebox red)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertFalse(answer) print("test 2_2 passed!") def test2_3(self): a1 = read.parse_input("fact: (inst Harry Wizard)") a2 = read.parse_input("fact: (inst Ron Wizard)") a3 = read.parse_input("fact: (inst Hermione Sorceress)") self.KB.kb_assert(a1) self.KB.kb_assert(a2) self.KB.kb_assert(a3) ask1 = read.parse_input("fact: (inst ?X Wizard)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]), "?X : Harry") self.assertEqual(str(answer[1]), "?X : Ron") ask2 = read.parse_input("fact: (inst ?X Sorceress)") print(' Asking if', ask2) answer2 = self.KB.kb_ask(ask2) self.assertEqual(str(answer2[0]), "?X : Sarorah") self.assertEqual(str(answer2[1]), "?X : Hermione") print("test 2_3 passed!")
class KBTest2(unittest.TestCase): def setUp(self): # Assert starter facts file2 = 'statements_kb2.txt' data = read.read_tokenize(file2) self.KB = KnowledgeBase([], []) for item in data: if isinstance(item, Fact): self.KB.kb_assert(item) def test_assert(self): print('test_assert') ask1 = read.parse_input("fact: (color bigbox red)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(answer[0].bindings, [])
def main(): # Assert starter facts file = 'statements_kb3.txt' data = read.read_tokenize(file) KB = KnowledgeBase([], []) for dtype, item in data: if dtype == read.FACT or dtype == read.RULE: KB.kb_assert(item) # KB demonstration # Ask for one of the starter facts print "Starting basic KB demonstration" _, ask1 = read.parse_input("fact: (goodman ?x)") print " Asking if", ask1 answer = KB.kb_ask(ask1) pprint_justification(answer) print KB
class KBTest(unittest.TestCase): def setUp(self): # Assert starter facts file = 'statements_kb.txt' data = read.read_tokenize(file) self.KB = KnowledgeBase([], []) for item in data: if isinstance(item, Fact): self.KB.kb_assert(item) def test2(self): ask1 = read.parse_input("fact: (color littlebox red)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertFalse(answer) print(self.KB.facts) for i in range(len(self.KB.facts)): print(self.KB.facts[i])
class KBTest(unittest.TestCase): def setUp(self): # Assert starter facts 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) def test3(self): r1 = read.parse_input("fact: (motherof ada bing)") print(' Retracting', r1) self.KB.kb_retract(r1) ask1 = read.parse_input("fact: (grandmotherof ada ?X)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]), "?X : felix")
def setUp(self): # Assert starter facts 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) file2 = 'statements_kb5.txt' self.data2 = read.read_tokenize(file2) data2 = read.read_tokenize(file2) for item in data2: if isinstance(item, Fact) or isinstance(item, Rule): self.KB.kb_assert(item) file3 = 'statements_kb6.txt' self.data3 = read.read_tokenize(file3) data3 = read.read_tokenize(file3) for item in data3: if isinstance(item, Fact) or isinstance(item, Rule): self.KB.kb_assert(item)
class KBTest(unittest.TestCase): def setUp(self): # Assert starter facts 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) # def test1(self): # # Did the student code contain syntax errors, AttributeError, etc. # ask1 = read.parse_input("fact: (motherof ada ?X)") # print(' Asking if', ask1) # answer = self.KB.kb_ask(ask1) # self.assertEqual(str(answer[0]), "?X : bing") # # def test2(self): # # Can fc_infer actually infer # ask1 = read.parse_input("fact: (grandmotherof ada ?X)") # print(' Asking if', ask1) # answer = self.KB.kb_ask(ask1) # self.assertEqual(str(answer[0]), "?X : felix") # self.assertEqual(str(answer[1]), "?X : chen") def test3(self): # Does retract actually retract things r1 = read.parse_input("fact: (motherof ada bing)") print(' Retracting', r1) self.KB.kb_retract(r1) ask1 = read.parse_input("fact: (grandmotherof ada ?X)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(len(answer), 1) self.assertEqual(str(answer[0]), "?X : felix")
def main(): KB = KnowledgeBase([], []) _, fact1 = read.parse_input("fact: (hero A)") _, fact2 = read.parse_input("fact: (person A)") _, rule1 = read.parse_input("rule: ((hero ?x) (person ?x)) -> (goodman ?x)") _, rule2 = read.parse_input( "rule: ((goodman ?x) (wenttoschool ?x)) -> (doctor ?x)") _, fact3 = read.parse_input("fact: (wenttoschool A)") _, fact4 = read.parse_input("fact: (goodman A)") _, ask1 = read.parse_input("fact: (goodman A)") _, ask2 = read.parse_input("fact: (doctor A)") _, ask3 = read.parse_input("rule: ((person A)) -> (goodman A)") KB.kb_assert(fact1) KB.kb_assert(fact2) KB.kb_assert(fact4) KB.kb_assert(rule1) KB.kb_assert(rule2) KB.kb_assert(fact3) print KB answer1 = KB.kb_ask(ask1)
class KBTest(unittest.TestCase): def setUp(self): # Assert starter facts file = 'statements_kb6.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) def test1(self): print("--------------------TEST ONE-----------------------") # makes sure retract does not retract supported fact # student test from piazza ask1 = read.parse_input("fact: (grandparent A ?X)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]), "?X : C") r1 = read.parse_input("fact: (parent B C)") print(' Retracting', r1) self.KB.kb_retract(r1) print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]), "?X : C") r1 = read.parse_input("fact: (parent A D)") print(' Retracting', r1) self.KB.kb_retract(r1) print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) print(str(len(answer))) self.assertEqual(0, len(answer))
def main(): KB = KnowledgeBase([], []) _, fact1 = read.parse_input("fact: (hero A)") _, fact2 = read.parse_input("fact: (person A)") _, fact3 = read.parse_input("fact: (goodman A)") _, rule1 = read.parse_input( "rule: ((hero ?x) (person ?x)) -> (goodman ?x)") KB.kb_assert(fact1) KB.kb_assert(fact2) KB.kb_assert(fact3) KB.kb_assert(rule1) answer1 = KB.facts[KB.facts.index(Fact(fact3))].asserted KB.kb_retract(fact3) answer2 = KB.facts[KB.facts.index(Fact(fact3))].asserted if answer1 and not answer2: print "pass test10" exit(0) else: print "fail test10" exit(1)
def main(): KB = KnowledgeBase([], []) _, fact1 = read.parse_input("fact: (hero A)") _, fact2 = read.parse_input("fact: (person A)") _, rule1 = read.parse_input( "rule: ((hero ?x) (person ?x)) -> (goodman ?x)") _, ask1 = read.parse_input("fact: (goodman A)") KB.kb_assert(fact1) KB.kb_assert(fact2) KB.kb_assert(rule1) answer = KB.kb_ask(ask1) if answer: print "pass test1" exit(0) else: print "fail test1" exit(1)
def main(): KB = KnowledgeBase([], []) _, fact1 = read.parse_input("fact: (hero A)") _, fact2 = read.parse_input("fact: (person B)") _, rule1 = read.parse_input( "rule: ((hero ?x) (person ?x)) -> (goodman ?x)") _, ask1 = read.parse_input("rule: ((person A)) -> (goodman A)") _, ask2 = read.parse_input("rule: ((hero B)) -> (goodman B)") KB.kb_assert(fact1) KB.kb_assert(fact2) KB.kb_assert(rule1) ask_rule1 = Rule(ask1) ask_rule2 = Rule(ask2) if ask_rule1 in KB.rules and ask_rule2 not in KB.rules: print "pass test2" exit(0) else: print "fail test2" exit(1)
class KBTest(unittest.TestCase): def setUp(self): # Assert starter facts file = 'statements_kb3.txt' data = read.read_tokenize(file) self.KB = KnowledgeBase([], []) for item in data: if isinstance(item, Fact): self.KB.kb_assert(item) def test1(self): ask1 = read.parse_input("fact: (inst ?X dragon)") print('Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]), "?X : Nosliw") self.assertEqual(str(answer[1]), "?X : Uzna") def test2(self): ask1 = read.parse_input("fact: (friendly ?X)") print('Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]), "?X : Uzna") self.assertEqual(str(answer[1]), "?X : Hershey") self.assertEqual(str(answer[2]), "?X : Ai") self.assertEqual(len(answer), 3) def test3(self): ask1 = read.parse_input("fact: (friendly Nosliw)") print('Askig if', ask1) answer = self.KB.kb_ask(ask1) self.assertFalse(answer) def test4(self): ask1 = read.parse_input("fact: (dead Nosliw)") answer = self.KB.kb_ask(ask1) self.assertFalse(answer)
class CustomTests3(unittest.TestCase): def setUp(self): file = 'statements_kb3.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) def test1(self): ask1 = read.parse_input("fact: (goodman ?X)") answer1 = self.KB.kb_ask(ask1) self.assertEqual(len(answer1), 1) self.assertEqual(str(answer1[0]), "?X : a") def test2(self): fact1 = read.parse_input("fact: (goodman a)") self.KB.kb_retract(fact1) ask1 = read.parse_input("fact: (goodman ?X)") answer1 = self.KB.kb_ask(ask1) self.assertEqual(len(answer1), 1) self.assertEqual(str(answer1[0]), "?X : a") def test3(self): fact1 = read.parse_input("fact: (hero a)") fact2 = read.parse_input("fact: (person a)") self.KB.kb_retract(fact1) self.KB.kb_retract(fact2) ask1 = read.parse_input("fact: (hero ?X)") ask2 = read.parse_input("fact: (person ?X)") answer1 = self.KB.kb_ask(ask1) answer2 = self.KB.kb_ask(ask2) self.assertEqual(len(answer1), 0) self.assertEqual(len(answer2), 0) def test4(self): fact1 = read.parse_input("fact: (hero a)") fact2 = read.parse_input("fact: (person a)") fact3 = read.parse_input("fact: (goodman a)") self.KB.kb_retract(fact1) self.KB.kb_retract(fact2) self.KB.kb_retract(fact3) ask1 = read.parse_input("fact: (goodman ?X") answer1 = self.KB.kb_ask(ask1) self.assertEqual(len(answer1), 0) def test5(self): fact1 = read.parse_input("fact: (hero a)") fact2 = read.parse_input("fact: (person a)") self.KB.kb_retract(fact1) self.KB.kb_retract(fact2) ask1 = read.parse_input("fact: (goodman ?X") answer1 = self.KB.kb_ask(ask1) self.assertEqual(len(answer1), 1) self.assertEqual(str(answer1[0]), "?X : a") fact3 = read.parse_input("fact: (goodman a)") self.KB.kb_retract(fact3) ask1 = read.parse_input("fact: (goodman ?X") answer1 = self.KB.kb_ask(ask1) self.assertEqual(len(answer1), 0) def test6(self): fact1 = read.parse_input("fact: (human a)") rule1 = read.parse_input("rule: ((human ?y)) -> (person ?y)") self.KB.kb_assert(fact1) self.KB.kb_assert(rule1) remove1 = read.parse_input("fact: (person a") self.KB.kb_retract(remove1) ask1 = read.parse_input("fact: (goodman ?X") answer1 = self.KB.kb_ask(ask1) self.assertEqual(len(answer1), 1) self.assertEqual(str(answer1[0]), "?X : a") remove1 = read.parse_input("fact: (human a") self.KB.kb_retract(remove1) ask1 = read.parse_input("fact: (goodman ?X") answer1 = self.KB.kb_ask(ask1) self.assertEqual(len(answer1), 1) self.assertEqual(str(answer1[0]), "?X : a") remove1 = read.parse_input("fact: (person a") self.KB.kb_retract(remove1) ask1 = read.parse_input("fact: (goodman ?X") answer1 = self.KB.kb_ask(ask1) self.assertEqual(len(answer1), 1) self.assertEqual(str(answer1[0]), "?X : a") remove1 = read.parse_input("fact: (goodman a") for x in self.KB.facts: if x == remove1: print("Asserted?") print(x.asserted) self.KB.kb_retract(remove1) ask1 = read.parse_input("fact: (goodman ?X") answer1 = self.KB.kb_ask(ask1) self.assertEqual(len(answer1), 0) def test7(self): remove1 = read.parse_input("fact: (goodman a") self.KB.kb_retract(remove1) ask1 = read.parse_input("fact: (goodman ?X") answer1 = self.KB.kb_ask(ask1) self.assertEqual(len(answer1), 1) self.assertEqual(str(answer1[0]), "?X : a") remove1 = read.parse_input("fact: (person a") self.KB.kb_retract(remove1) ask1 = read.parse_input("fact: (goodman ?X") answer1 = self.KB.kb_ask(ask1) self.assertEqual(len(answer1), 0)
class KBTest(unittest.TestCase): def setUp(self): # Assert starter facts 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) def test1(self): # Did the student code contain syntax errors, AttributeError, etc. ask1 = read.parse_input("fact: (motherof ada ?X)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]), "?X : bing") def test2(self): # Can fc_infer actually infer ask1 = read.parse_input("fact: (grandmotherof ada ?X)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]), "?X : felix") self.assertEqual(str(answer[1]), "?X : chen") def test3(self): # Does retract actually retract things r1 = read.parse_input("fact: (motherof ada bing)") print(' Retracting', r1) self.KB.kb_retract(r1) ask1 = read.parse_input("fact: (grandmotherof ada ?X)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(len(answer), 1) self.assertEqual(str(answer[0]), "?X : felix") def test4(self): # makes sure retract does not retract supported fact ask1 = read.parse_input("fact: (grandmotherof ada ?X)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]), "?X : felix") self.assertEqual(str(answer[1]), "?X : chen") r1 = read.parse_input("fact: (grandmotherof ada chen)") print(' Retracting', r1) self.KB.kb_retract(r1) print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]), "?X : felix") self.assertEqual(str(answer[1]), "?X : chen") def test5(self): # makes sure retract does not deal with rules ask1 = read.parse_input("fact: (parentof ada ?X)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]), "?X : bing") r1 = read.parse_input("rule: ((motherof ?x ?y)) -> (parentof ?x ?y)") print(' Retracting', r1) self.KB.kb_retract(r1) print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]), "?X : bing") def test37(self): r1 = read.parse_input("fact: (motherof ada bing)") print(' Retracting', r1) self.KB.kb_retract(r1) ask1 = read.parse_input("fact: (parentof ada ?X)") answer = self.KB.kb_ask(ask1) print(' Answer is ', answer) self.assertEqual(len(answer), 0)
class CustomTests4(unittest.TestCase): def setUp(self): # Assert starter facts file = 'statements_kb6.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) def test6(self): """this student generated test ensures retract only removes facts and rules that are supported by 1 or less fact-rule pairs """ r1 = read.parse_input("fact: (dresslike profHammond TonyStark)") print(' Retracting', r1) self.KB.kb_retract(r1) ask1 = read.parse_input("fact: (isliterally ?X TonyStark)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]), "?X : profHammond") ask2 = read.parse_input("fact: (resembles profHammond ?Y)") print(' Asking if', ask2) answer = self.KB.kb_ask(ask2) self.assertFalse(answer) def test7(self): """this student generated test ensures retracting the 2nd fact in the lhs of a rule successfully retracts the final inferred fact but re-assertion re-infers the fact """ r1 = read.parse_input("fact: (lookslike profHammond TonyStark)") print(' Retracting', r1) self.KB.kb_retract(r1) ask1 = read.parse_input("fact: (resembles profHammond ?Y)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertFalse(answer) a1 = read.parse_input("fact: (lookslike profHammond TonyStark)") print(' Reasserting', a1) self.KB.kb_assert(a1) ask2 = read.parse_input("fact: (resembles profHammond ?Y)") print(' Asking if', ask2) answer = self.KB.kb_ask(ask2) self.assertEqual(str(answer[0]), "?Y : TonyStark") def test8(self): """this student generated test ensures retracting a fact that supports inferences 2 links away also retracts all facts down that chain """ r1 = read.parse_input("fact: (techgenius profHammond)") print(' Retracting', r1) self.KB.kb_retract(r1) r2 = read.parse_input("fact: (talkslike profHammond)") print(' Retracting', r2) self.KB.kb_retract(r2) ask1 = read.parse_input("fact: (isliterally ?X TonyStark)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertFalse(answer) ask2 = read.parse_input("fact: (IronMan ?X)") print(' Asking if', ask2) answer = self.KB.kb_ask(ask2) self.assertFalse(answer) ask3 = read.parse_input("fact: (Avenger ?X)") print(' Asking if', ask3) answer = self.KB.kb_ask(ask3) self.assertFalse(answer) def test9(self): """this student generated test ensures retracting a fact that supports two or more facts or rules successfully retracts all inferred facts and rules """ r1 = read.parse_input("fact: (techgenius profHammond)") print(' Retracting', r1) self.KB.kb_retract(r1) ask1 = read.parse_input("fact: (employable ?X)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertFalse(answer) ask2 = read.parse_input("fact: (smart ?X)") print(' Asking if', ask2) answer = self.KB.kb_ask(ask2) self.assertFalse(answer) def test10(self): """this student generated test ensures the inference engine is working at a basic level""" ask1 = read.parse_input("fact: (Avenger ?X)") print(' Asking if', ask1) answer = self.KB.kb_ask(ask1) self.assertEqual(str(answer[0]), "?X : profHammond") ask2 = read.parse_input("fact: (smart ?X)") print(' Asking if', ask2) answer = self.KB.kb_ask(ask2) self.assertEqual(str(answer[0]), "?X : profHammond") ask3 = read.parse_input("fact: (employable ?X)") print(' Asking if', ask3) answer = self.KB.kb_ask(ask3) self.assertEqual(str(answer[0]), "?X : profHammond")
def setUp(self): # # Assert starter facts self.KB = KnowledgeBase([], [])