File tree Expand file tree Collapse file tree 2 files changed +18
-3
lines changed Expand file tree Collapse file tree 2 files changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -339,7 +339,6 @@ def topological_sort(X, root):
339
339
visited shows the state (visited - not visited) of nodes
340
340
341
341
"""
342
- nodes = X .variables
343
342
neighbors = X .neighbors
344
343
345
344
visited = defaultdict (lambda : False )
Original file line number Diff line number Diff line change @@ -845,8 +845,24 @@ def subst(s, x):
845
845
return Expr (x .op , * [subst (s , arg ) for arg in x .args ])
846
846
847
847
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
850
866
851
867
852
868
def standardize_variables (sentence , dic = None ):
You can’t perform that action at this time.
0 commit comments