''' BC1.py -- first of several incremental implementation of backward chaining to illustrate incremental development in CS4100, Artificial Intelligence.This first version works only with a KB of "facts", meaning positive ground literals (with no variables). BC2 will introduce variables and the possibility of having more than one return value. We use a dictionary to store the KB and its keys are predicates symbols: i.e., the goals we are trying to prove. ''' from unify import * KBdict = {} #Here we will store the knowledge base def acquireKB(KBfname): kbfile = open(KBname) for aline in kbfile.readlines(): nextF = FOLexp(aline) # now add the exp to the dictionary with its key if KBdict.hasKey[nextF.op]: KBdict[nextF.op] = += [nextF] else: KBdict[nextF.op] = [nextF] def FOLBCtest(Qfname): queryfile = open(Qfname) for aline in queryfile.readlines(): nextQ = FOLexp(aline) print "Query = " , nextQ #now find everything the query matches in the KB entries = KBdict[nextQ.op] for e in entries: print "RESULT for " e " is " unify(nextQ, e)