File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -339,7 +339,6 @@ def topological_sort(X, root):
339339 visited shows the state (visited - not visited) of nodes
340340
341341 """
342- nodes = X .variables
343342 neighbors = X .neighbors
344343
345344 visited = defaultdict (lambda : False )
Original file line number Diff line number Diff line change @@ -845,8 +845,24 @@ def subst(s, x):
845845 return Expr (x .op , * [subst (s , arg ) for arg in x .args ])
846846
847847
848- def fol_fc_ask (KB , alpha ): # TODO
849- raise NotImplementedError
848+ def fol_fc_ask (KB , alpha ):
849+ """A simple forward-chaining algorithm. [Figure 9.3]"""
850+ new = []
851+ while new is not None :
852+ for rule in KB .clauses :
853+ p , q = parse_definite_clause (standardize_variables (rule ))
854+ for p_ in KB .clauses :
855+ if p != p_ :
856+ for theta in KB .clauses :
857+ if subst (theta , p ) == subst (theta , p_ ):
858+ q_ = subst (theta , q )
859+ if not unify (q_ , KB .sentence in KB ) or not unify (q_ , new ):
860+ new .append (q_ )
861+ phi = unify (q_ , alpha )
862+ if phi is not None :
863+ return phi
864+ KB .tell (new )
865+ return None
850866
851867
852868def standardize_variables (sentence , dic = None ):
You can’t perform that action at this time.
0 commit comments