def main(): # 声明证明目标子句,"happy"是HappyJohn库中声明的谓词对象 # 证明目标:子句"None <- John快乐", # 在子句对象输出时使用None占位,表示某个位置空 # 注:如果更换证明目标后证明失败,先检查库中的规则是否满足证明需求 t = HornClause(None, [HappyJohn.happy.exec(["John"])]) # 事实集 facts_set = [ # 子句"John快乐 <- None",这里的lucky是HappyJohn库中声明的谓词对象 HornClause(HappyJohn.lucky.exec(["John"]), []), # 子句"None <- John学习" HornClause(None, [HappyJohn.study.exec(["John"])]) ] # 实例化一个证明系统 sys = System(target=t) # 向系统中添加规则和事实集 # 将HappyJohn库中设置的规则引入 sys.add_rules(rules=HappyJohn.get_rules_set()) sys.add_facts(facts=facts_set) # 打印推理系统中现有的所有子句 sys.show() # 开始推理,参数debug指定是否将推理过程打印到控制台 # run方法返回一个布尔值,表示推理是否成功 res = sys.run(debug=True) if res: print("证明成功") else: print("证明失败")
def main(): # 证明目标:Tom和Sally是兄妹关系(不区分年龄) # 注:如果更换证明目标后证明失败,先检查库中的规则是否满足证明需求 t = HornClause(None, [Relatives.bro_sister.exec(["Tom", "Sally"])]) # 事实集合 facts_set = [ # 事实:John和Tom是父子关系 HornClause(Relatives.father_son.exec(["John", "Tom"]), []), # 事实:John和Sally是父女关系 HornClause(Relatives.father_dau.exec(["John", "Sally"]), []) ] # 实例化一个证明系统 sys = System(target=t) # 导入上面指定的事实集合 sys.add_facts(facts=facts_set) # 从亲属关系库中导入亲属关系规则 sys.add_rules(rules=Relatives.get_relatives_rules()) # 打印系统中现有的所有子句并开始推理 sys.show() res = sys.run(debug=True) if res: print("证明成功") else: print("证明失败")
class HappyJohn: @classmethod def get_rules_set(cls): """获取Happy John例子中设置的规则""" return cls._rules_set # 定义谓词 ps = Predicate(lambda x: str(x[0]) + "通过" + str(x[1])) win = Predicate(lambda x: str(x[0]) + "中奖") study = Predicate(lambda x: str(x[0]) + "学习") happy = Predicate(lambda x: str(x[0]) + "快乐") lucky = Predicate(lambda x: str(x[0]) + "幸运") # 规则集 _rules_set = [ HornClause(happy.exec([Anyone("A")]), [ps.exec([Anyone("A"), "历史考试"]), win.exec([Anyone("A")])]), HornClause(ps.exec([Anyone("A"), Anyone("B")]), [study.exec([Anyone("A")])]), HornClause(ps.exec([Anyone("A"), Anyone("B")]), [lucky.exec([Anyone("A")])]), HornClause(win.exec([Anyone("A")]), [lucky.exec([Anyone("A")])]), ]
def test_union_case_2(self): """子句消解用例2""" happy = Predicate(lambda x: str(x[0]) + "开心") ps = Predicate(lambda x: str(x[0]) + "通过" + str(x[1])) win = Predicate(lambda x: str(x[0]) + "赢得" + str(x[1])) hc1 = HornClause(None, [happy.exec(["John"])]) hc2 = HornClause(happy.exec([Anyone("X")]), [ ps.exec([Anyone("X"), "history"]), win.exec([Anyone("X"), "lottery"]) ]) result = hc1.union(hc2) self.assertListEqual(result.body[0].constant, ["John", "history"]) self.assertListEqual(result.body[1].constant, ["John", "lottery"])
def create_tmp_horn_clauses(): """ 创建测试用霍恩子句 :return: 二元组,两条霍恩子句 """ ps = Predicate(lambda x: str(x[0]) + "通过" + str(x[1])) win = Predicate(lambda x: str(x[0]) + "中奖") study = Predicate(lambda x: str(x[0]) + "学习") happy = Predicate(lambda x: str(x[0]) + "快乐") # 定义子句 hc1 = HornClause(ps.exec(["John", "历史考试"]), [win.exec(["John"])]) hc2 = HornClause(None, [ps.exec([Anyone("A"), Anyone("B")])]) return hc1, hc2
def test_quick_build(self): """快速建立只含变量的子句""" male = Predicate.create_pred("男性") female = Predicate.create_pred("女性") body = ((male, ["A"]), (female, ["B"])) c = HornClause.create_clause_variables_only((male, ["A"]), body) self.assertEqual(str(c), "(<变量A>)是男性 <- (<变量A>)是男性, (<变量B>)是女性")
def test_system(self): """测试两个子句归结""" # 定义谓词 ps = Predicate(lambda x: str(x[0]) + "通过" + str(x[1])) win = Predicate(lambda x: str(x[0]) + "中奖") study = Predicate(lambda x: str(x[0]) + "学习") happy = Predicate(lambda x: str(x[0]) + "快乐") lucky = Predicate(lambda x: str(x[0]) + "幸运") # 规则集 r1 = HornClause( happy.exec([Anyone("A")]), [ps.exec([Anyone("A"), "历史考试"]), win.exec([Anyone("A")])]) r2 = HornClause(ps.exec([Anyone("A"), Anyone("B")]), [study.exec([Anyone("A")])]) r3 = HornClause(ps.exec([Anyone("A"), Anyone("B")]), [lucky.exec([Anyone("A")])]) r4 = HornClause(win.exec([Anyone("A")]), [lucky.exec([Anyone("A")])]) # 事实集 f1 = HornClause(lucky.exec(["John"]), []) f2 = HornClause(None, [study.exec(["John"])]) # 待证明结论 t = HornClause(None, [happy.exec(["John"])]) # 将上面的霍恩子句加入推理系统 sys = System(t) sys.add_facts([f1, f2]) sys.add_rules([r1, r2, r3, r4]) self.assertTrue(sys.run())
class Relatives: """亲属关系""" @classmethod def get_relatives_rules(cls): """获取亲属关系中设置的规则集""" return cls._rules_set # 谓词定义 male, female, parenting, father_son, father_dau, \ mother_son, mother_dau, bro_sister, cousin \ = Predicate.create_preds(["男性", "女性", "亲子关系", "父子关系", "父女关系", "母子关系", "母女关系", "兄妹关系", "表兄妹"]) # create_preds是简化的批量声明方法,只能声明 # (<para1>, <para2>, ...)是<info> # 形式的谓词 # 规则集,在这个列表中添加亲属关系所需的规则 _rules_set = [ HornClause(female.exec([Anyone("A")]), [father_dau.exec([Anyone("B"), Anyone("A")])]), HornClause(male.exec([Anyone("A")]), [father_son.exec([Anyone("B"), Anyone("A")])]), HornClause(bro_sister.exec([Anyone("A"), Anyone("B")]), [ father_son.exec([Anyone("C"), Anyone("A")]), father_dau.exec([Anyone("C"), Anyone("B")]) ]), HornClause(father_son.exec([Anyone("A"), Anyone("B")]), [ bro_sister.exec([Anyone("B"), Anyone("C")]), father_dau.exec([Anyone("A"), Anyone("C")]) ]), HornClause(father_dau.exec([Anyone("A"), Anyone("C")]), [ bro_sister.exec([Anyone("B"), Anyone("C")]), father_son.exec([Anyone("A"), Anyone("B")]) ]) ]
def test_system_case_2(self): """测试亲戚关系运算""" # 定义谓词 male, female, parenting, father_son, father_dau, \ mother_son, mother_dau, bro_sister, cousin \ = Predicate.create_preds(["男性", "女性", "亲子关系", "父子关系", "父女关系", "母子关系", "母女关系", "兄妹关系", "表兄妹"]) # 事实集合 f1 = HornClause(father_son.exec(["John", "Tom"]), []) f2 = HornClause(father_dau.exec(["John", "Sally"]), []) # 规则集合 r1 = HornClause( female.exec([Anyone("A")]), [father_dau.exec([Anyone("B"), Anyone("A")])]) r2 = HornClause( male.exec([Anyone("A")]), [father_son.exec([Anyone("B"), Anyone("A")])]) r3 = HornClause(bro_sister.exec([Anyone("A"), Anyone("B")]), [ father_son.exec([Anyone("C"), Anyone("A")]), father_dau.exec([Anyone("C"), Anyone("B")]) ]) r4 = HornClause(father_son.exec([Anyone("A"), Anyone("B")]), [ bro_sister.exec([Anyone("B"), Anyone("C")]), father_dau.exec([Anyone("A"), Anyone("C")]) ]) r5 = HornClause(father_dau.exec([Anyone("A"), Anyone("C")]), [ bro_sister.exec([Anyone("B"), Anyone("C")]), father_son.exec([Anyone("A"), Anyone("B")]) ]) # 推理目标 target = HornClause(None, [bro_sister.exec(["Tom", "Sally"])]) sys = System(target) sys.add_facts([f1, f2]) sys.add_rules([r1, r2, r3, r4, r5]) self.assertTrue(sys.run())