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 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) # Assert facts from statements_kb2.txt file2 = 'statements_kb2.txt' data2 = read.read_tokenize(file2) for item2 in data2: if isinstance(item2, Fact): self.KB.kb_assert(item2)
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) # Assert kb2 facts, added by me file = 'statements_kb2.txt' data = read.read_tokenize(file) for item in data: if isinstance(item, Fact): 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: 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)
def __init__(self): facts, rules = read.read_tokenize("statements.txt") for fact in facts: assert_fact(Statement(fact)) for new_rule in rules: assert_rule(Rule(new_rule[0], new_rule[1]))
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 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
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)
# # RETRACT (here called KB_retract) that takes a statement (not a rule) and removes # it from the knowledge base and then removes all facts and rules that it might support. # # WHY (here called KB_why) that takes a statement, finds facts that match it and then maps # out the facts and rules that support it. # # ASK+ (here called KB_ask_plus) that takes a list of statements and returns the lists # of the various bindings that have to hold for those statements to be true in the data. # # The test file also displays the instantiated list of input statements instantiated with # the each of the bindings that were found print "\033[0;32m\n=================== Loading in the data ===================\x1b[0m" facts, rules = read.read_tokenize("asserts.txt") retracts, retract_rules = read.read_tokenize("retracts.txt") asks, ask_rules = read.read_tokenize("asks.txt") print "\033[0;32m\n=================== Setting up the Knowledge Base ===================\x1b[0m" kb = logic.kb() print print kb print "\033[0;32m\n=================== Testing KB_Assert ===================\x1b[0m" for rule in rules:
def main(): # Assert starter facts file = 'statements_kb2.txt' data = read.read_tokenize(file) KB = KnowledgeBase([], []) for dtype, item in data: if dtype == read.FACT: KB.kb_assert(item) # KB demonstration # Ask for one of the starter facts print "Starting basic KB demonstration" _, ask1 = read.parse_input("fact: (hero ?x)") print " Asking if", ask1 answer = KB.kb_ask(ask1) #pprint_justification(answer) print ("Basic demonstration of KB complete" if answer else "ERROR: KB demonstration failed") + "\n" print "Starting Test 1" _, test1 = read.parse_input("fact: (hero Ai)") print " Retracting", test1 # KB.kb_retract(Statement(test1)) KB.kb_retract(test1) answer = KB.kb_ask(ask1) # pprint_justification(answer) print (("Fail" if answer else "Pass") + " Test 1\n") print "Starting Test 2" fail = True for dtype, item in data: if dtype == read.RULE: KB.kb_assert(item) _, test2_1 = read.parse_input("fact: (strong ?x)") print " Asking", test2_1 answer = KB.kb_ask(test2_1) #pprint_justification(answer) if answer and len(answer)==1 and answer[0]['?x'] == 'Ai': print " Pass Part 1" _, test2_2 = read.parse_input("fact: (inst Sarorah ?x)") print " Asking", test2_2 answer = KB.kb_ask(test2_2) #pprint_justification(answer) if answer and len(answer)==2 and (answer[0]['?x'] == 'Sorceress' or answer[0]['?x'] == 'Wizard'): print " Pass Part 2" _, test2_3 = read.parse_input("fact: (dead ?dragon)") print " Asking", test2_3 answer = KB.kb_ask(test2_3) #pprint_justification(answer) if not answer: print " Pass Part 3" _, assert_hero = read.parse_input("fact: (hero Ai)") print " Asserting", assert_hero KB.kb_assert(assert_hero) _, test2_4 = read.parse_input("fact: (dead ?dragon)") print " Asking", test2_4 answer = KB.kb_ask(test2_4) #pprint_justification(answer) if answer and len(answer)==1 and answer[0]['?dragon'] == 'Nosliw': print " Pass Part 4\nPass Test 2\n" fail = False if fail: print "Fail Test 2\n" print "Starting Test 3" fail = True _, possesses = read.parse_input("fact: (possesses Ai Loot)") print " Retracting", possesses KB.kb_retract(possesses) _, test3_1 = read.parse_input("fact: (dead ?dragon)") print " Asking", test3_1 answer = KB.kb_ask(test3_1) # pprint_justification(answer) if not answer: print " Pass Part 1" print " Asserting", possesses KB.kb_assert(possesses) _, sleeping = read.parse_input("fact: (sleeping Nosliw)") _, safe = read.parse_input("rule: (sleeping Nosliw) -> (safe HappyDale)") print " Asserting", sleeping KB.kb_assert(sleeping) print " Asserting", safe KB.kb_assert(safe) print " Retracting", possesses KB.kb_retract(possesses) _, possesses = read.parse_input("fact: (possesses Ai Loot)") _, test3_2 = read.parse_input("fact: (safe ?town)") print " Asking", test3_2 answer = KB.kb_ask(test3_2) #pprint_justification(answer) if answer and len(answer)==1 and answer[0]['?town'] == 'HappyDale': print " Pass Part 2\nPass Test 3\n" fail = False if fail: print "Fail Test 3\n"
import read import facts_and_rules facts, rules = read.read_tokenize("statements_backup.txt") global KB KB = [] global RB RB = [] def assert_rule(rule): if rule not in RB: RB.append(rule) infer_from_rule(rule) def assert_fact(fact): if fact not in KB: KB.append(fact) infer_from_fact(fact) def infer_from_fact(fact): for r in RB: bindings = facts_and_rules.match(r.lhs[0], fact) if bindings != False: if len(r.lhs) == 1: new_statement = facts_and_rules.statement( facts_and_rules.instantiate(r.rhs.full, bindings))
print 'No matching solutions \n' return result def retract(pattern): fact = Statement(pattern) delete(fact) if __name__ == "__main__": global KB global RB KB = [] RB = [] facts, rules = read.read_tokenize("statements.txt") for fact in facts: assert_fact(Statement(fact)) for new_rule in rules: assert_rule(Rule(new_rule[0], new_rule[1])) print '\n*******************************Knowledge Base*******************************************\n' for kb in KB: print "fact: " + kb.pretty() print '\n*******************************Rules Base*******************************************\n' for rb in RB: print rb.pretty() print '\n******************************* Ask *******************************************\n' result1 = ask(['color', 'pyramid1', '?x'], 1)
def main(): # Assert starter facts file = 'statements_kb2.txt' data = read.read_tokenize(file) KB = KnowledgeBase([], []) for dtype, item in data: if dtype == read.FACT: KB.kb_assert(item) # KB demonstration # Ask for one of the starter facts # print "Starting basic KB demonstration" _, ask1 = read.parse_input("fact: (hero ?x)") # print " Asking if", ask1 # answer = KB.kb_ask(ask1) # # print answer # pprint_justification(answer) # print ("Basic demonstration of KB complete" if answer else "ERROR: KB demonstration failed") + "\n" print "Starting Test 1" _, test1 = read.parse_input("fact: (hero Ai)") # print " Retracting", test1 KB.kb_retract(test1) # print KB.facts answer = KB.kb_ask(ask1) # print answer # pprint_justification(answer) print(("Fail" if answer else "Pass") + " Test 1\n") hai = Fact(test1).statement print "Starting Test 2" fail = True for dtype, item in data: if dtype == read.RULE: # print "the item is", item KB.kb_assert(item) # if not factq(item): # print "SCREAM", Rule(item) # print "LHS first term is", Rule(item).lhs[0] # first_term = Rule(item).lhs[0] # bound = match(hai, first_term) # if bound: # print "bindings are", bound # insta = instantiate(Rule(item).lhs[0], bound) # print "INSTA", insta # insta2 = instantiate(Rule(item).rhs, bound) # print "INSTA2", insta2 # new_lhs = copy.deepcopy(Rule(item).lhs) # new_lhs.remove(first_term) # print "new lhs", new_lhs # supby = [[Rule(item), hai]] # newfact = Fact(insta2, supby) # print "newfact sup by is", newfact.supported_by # print newfact.statement # # print "now it's", Rule(item) # newrulelist = [[insta.predicate], [insta2.predicate, insta2.terms]] # print "list is", newrulelist # # newrule = Rule(newrulelist) # # else: # # print "DIDN'T MATCH" _, test2_1 = read.parse_input("fact: (strong ?x)") print " Asking", test2_1 answer = KB.kb_ask(test2_1) #pprint_justification(answer) if answer and len(answer) == 1 and answer[0]['?x'] == 'Ai': print " Pass Part 1" _, test2_2 = read.parse_input("fact: (inst Sarorah ?x)") print " Asking", test2_2 answer = KB.kb_ask(test2_2) #pprint_justification(answer) if answer and len(answer) == 2 and (answer[0]['?x'] == 'Sorceress' or answer[0]['?x'] == 'Wizard'): print " Pass Part 2" _, test2_3 = read.parse_input("fact: (dead ?dragon)") print " Asking", test2_3 answer = KB.kb_ask(test2_3) #pprint_justification(answer) if not answer: print " Pass Part 3" _, assert_hero = read.parse_input("fact: (hero Ai)") print " Asserting", assert_hero KB.kb_assert(assert_hero) _, test2_4 = read.parse_input("fact: (dead ?dragon)") print " Asking", test2_4 answer = KB.kb_ask(test2_4) # pprint_justification(answer) if answer and len( answer) == 1 and answer[0]['?dragon'] == 'Nosliw': print " Pass Part 4\nPass Test 2\n" fail = False if fail: print "Fail Test 2\n" # DEBUGGING FOR TEST 2 # answer = KB.kb_ask(test2_4) for f in KB.facts: print f.statement, "\n" f = KB.facts[2] print "this is the fact", f print "what", isinstance(f.statement, Statement) dummy = Fact(['possesses', 'Ai', 'code']) print dummy dummy2 = Fact(['a', 'b', 'c', 'd']) print "HELLO", KB._get_fact(dummy) # print "the fact", f.statement # for s in f.supported_by[0]: # print s # print "consider the fact", f.statement # print f.asserted # for sf in f.supported_by: # print sf, "\n" print "Starting Test 3" fail = True _, possesses = read.parse_input("fact: (possesses Ai Loot)") print " Retracting", possesses KB.kb_retract(possesses) _, test3_1 = read.parse_input("fact: (dead ?dragon)") print " Asking", test3_1 answer = KB.kb_ask(test3_1) print "I HATE IT", factq(['a']) #pprint_justification(answer) if not answer: print " Pass Part 1" print " Asserting", possesses KB.kb_assert(possesses) _, sleeping = read.parse_input("fact: (sleeping Nosliw)") _, safe = read.parse_input( "rule: (sleeping Nosliw) -> (safe HappyDale)") print " Asserting", sleeping KB.kb_assert(sleeping) print " Asserting", safe KB.kb_assert(safe) print " Retracting", possesses KB.kb_retract(possesses) _, possesses = read.parse_input("fact: (possesses Ai Loot)") _, test3_2 = read.parse_input("fact: (safe ?town)") print " Asking", test3_2 answer = KB.kb_ask(test3_2) #pprint_justification(answer) if answer and len(answer) == 1 and answer[0]['?town'] == 'HappyDale': print " Pass Part 2\nPass Test 3\n" fail = False if fail: print "Fail Test 3\n"
# Assert statements and rules # Retract statements and rules # Ask our knowledge base what is true # Knowledge base is a list of facts # facts = [] # facts have two parts, the predicate and the argument # to see if a statment is true, just check if the predicate is true, and the argument is true # The only tweak is that we want to be able to check facts against statements with variables # eg We want to be able to check on(?x, ?y), and if on(21, 22) is a fact, then we know that on(?x, ?y) is true for ?x = 21, ?y = 22 # # rules = [] facts, rules = read.read_tokenize('statements.txt') print facts for rule in rules: print rule # Want classes/objects for # Statements # predicates # arguments # methods for matching # supports # other statements that this fact supports (both facts and rules, so a retracted fact can retract rules and facts, which recursively retract rules and facts etc.) # in other words, what else to remove if we retract this statement # Variables # name
fact (Fact or Rule): Fact or Rule we're asserting in the format produced by read.py """ print("Asserting {!r}".format(fact)) self.facts.append(fact) def kb_ask(self, fact): """Ask if a fact is in the KB Args: fact (Fact) - Fact to be asked Returns: ListOfBindings|False - ListOfBindings if result found, False otherwise """ print("Asking {!r}".format(fact)) for storedFact in self.facts: if storedFact == fact: print("fact found!") return storedFact return False if __name__ == "__main__": kb = KnowledgeBase() factList = read.read_tokenize( "/Users/jaspergilley/Documents/NU Classes/EECS 348/assignment-1-kb-basics-jagilley/statements_kb2.txt" ) for i in factList: kb.kb_assert(i) print(kb.kb_ask(kb.facts[3]))