def fol_bc_or(KB, query, theta): for fact in KB.facts: fact = fact.get_unit(0) if fact.checkable(query): theta_ = unify(fact.get_args(), query.get_args(), theta) if not theta_ == "Fail": yield theta_ for rule in KB.rules: p, q = rule.standardize_apart().get_ante_cons() if q.checkable(query): theta_ = unify(q.get_args(), query.get_args(), theta) for theta1 in fol_bc_and(KB, p, theta_): yield theta1
def gain_new_knowledge(KB, fact): for rule in KB.rules: for idx, unit in enumerate(rule.get_units()): if unit.potential_conflict(fact): theta = unify(fact.get_args(), unit.get_args(), {}) if theta == "Fail": continue remainder = rule.clone() remainder.remove_unit_at(idx) remainder = remainder.substitute(theta) if remainder.get_number_of_units() == 1: # if not KB.contain_fact(remainder): KB.add_fact(remainder) else: # if not remainder in KB.rules: KB.add_rule(remainder)
def backward_chain_(KB, goals, theta): if not goals.to_list(): return [theta] answers = list() first, rest = goals.getFirstRest() q_ = first.substitute(theta) for rule in KB.rules + KB.facts: p, q = rule.standardize_apart().get_ante_cons() if q.checkable(q_): theta_ = unify(q_.get_args(), q.get_args(), {}) if theta_ == "Fail": continue new_goals = p for unit in rest.get_units(): new_goals.add_unit(unit) compose = compose_theta(theta, theta_) answers += backward_chain_(KB, new_goals, compose) return answers
def gain_new_knowledge(KB, clause, query): for clause_ in KB.rules: if clause == clause_: continue for idx, unit in enumerate(clause.get_units()): for idx_, unit_ in enumerate(clause_.get_units()): if unit.potential_conflict(unit_): theta = unify(unit.get_args(), unit_.get_args(), {}) if theta == "Fail": continue remainder = clause.clone() remainder_ = clause_.clone() remainder.remove_unit_at(idx) remainder_.remove_unit_at(idx_) for unit in remainder_.get_units(): remainder.add_unit(unit) if remainder.get_number_of_units() == 0: yield theta continue remainder = remainder.substitute(theta) KB.add_rule(remainder)
def check_done(new_fact, query): fact = new_fact.get_unit(0) if fact.checkable(query): theta = unify(query.get_args(), fact.get_args(), {}) yield theta yield "Fail"