update only look at previous facts iteration.

main
bog 2024-05-01 22:35:03 +02:00
parent d44b14e159
commit 0bcd25c693
2 changed files with 26 additions and 4 deletions

View File

@ -4,6 +4,10 @@ import fol
class Kb: class Kb:
def __init__(self): def __init__(self):
self.base = [] self.base = []
self.prev = []
self.current = []
self.indexes = {} self.indexes = {}
self.counter = fol.cnf.Counter() self.counter = fol.cnf.Counter()
@ -107,18 +111,22 @@ class Kb:
self.indexes[p.value] = [req] self.indexes[p.value] = [req]
self.check_request(req) self.check_request(req)
self.base.append(req) self.base.append(req)
self.current.append(req)
def tell(self, request): def tell(self, request):
req = self.make_f(request) req = self.make_f(request)
self.add_req(req) self.add_req(req)
def ask(self, request): def generate(self):
while True: while True:
n = len(self.base)
self.update() self.update()
if n == len(self.base): self.prev = [c for c in self.current]
self.current = []
if len(self.prev) == 0:
break break
def ask(self, request):
self.generate()
req = self.make_req(request) req = self.make_req(request)
results = [] results = []
@ -150,7 +158,12 @@ class Kb:
facts = [f[0] for f in facts] facts = [f[0] for f in facts]
self.solve(rule, self.conds(rule), facts, solution, solutions) rules = self.conds(rule)
if not self.is_rule_needed(rules, self.prev):
continue
self.solve(rule, rules, facts, solution, solutions)
for sol in solutions: for sol in solutions:
concl = conclusion.subst(sol) concl = conclusion.subst(sol)
@ -158,6 +171,14 @@ class Kb:
self.add_req([concl]) self.add_req([concl])
return return
def is_rule_needed(self, rule, prev):
for f in prev:
for r in rule:
s = fol.unify(r, f[0])
if s is not None:
return True
return False
def merge_substs(self, substs): def merge_substs(self, substs):
res = {} res = {}
for s in substs: for s in substs:

View File

@ -14,6 +14,7 @@ if __name__ == '__main__':
answer = kb.ask('Est(ALICE, x, y)') answer = kb.ask('Est(ALICE, x, y)')
print(answer) print(answer)
# [print(k) for k in kb.base]
if False: if False:
print(f'{len(kb.base)}----------------') print(f'{len(kb.base)}----------------')