事实库[family.kfb]
son_of(david, bruce) son_of(bruce, thomas) son_of(thomas, frederik) son_of(frederik, hiram)
规则库[fc_familykrb]
direct_father_son
foreach
family.son_of($son, $father)
assert
family.father_son($father, $son, ())
grand_father_son
foreach
family.father_son($father, $grand_son, ())
family.father_son($grand_father, $father, ())
assert
family.father_son($grand_father, $grand_son, (grand))
great_grand_father_son
foreach
family.father_son($father, $gg_son, ())
family.father_son($gg_father, $father, ($prefix1, *$rest_prefixes))
assert
family.father_son($gg_father, $gg_son,(great, $prefix1, *$rest_prefixes))
driver.py
from pyke import knowledge_engine
# 第一步. 编译pyKe资源文件
# 1..kfb files define fact bases, which are compiled into .fbc pickle files.
# 2..krb files define rule bases, which are compiled into 1 to 3 .py Python source files.
# 3..kqb files define question bases, which are compiled into .qbc pickle files.
engine = knowledge_engine.engine(__file__)
# 第二步:删除特殊情况的事实及失效所有的关系规则
engine.reset()
# 第三步,激活指定的规则库
engine.activate('fc_family') # This is where the rules are run!
# 第三步,获取推理生成规则
rs = engine.get_kb('family').dump_specific_facts()
print(rs)
编译结果:
compiled_pyke_files.py是一个文件目录文件。
fc_familyh_fc.py是正向推荐的py源代码。
运行结果:
father_son('bruce', 'david', ())
father_son('thomas', 'bruce', ())
father_son('frederik', 'thomas', ())
father_son('hiram', 'frederik', ())
father_son('thomas', 'david', ('grand',))
father_son('frederik', 'bruce', ('grand',))
father_son('hiram', 'thomas', ('grand',))
father_son('frederik', 'david', ('great', 'grand'))
father_son('hiram', 'bruce', ('great', 'grand'))
father_son('hiram', 'david', ('great', 'great', 'grand'))