Hi,大家好,我是半亩花海。本项目旨在开发一个基于产生式规则的动物识别专家系统。通过构建事实库和规则库,系统能够利用正向推理机制,根据用户输入的动物特征识别出相应的动物种类。采用Python编程语言,我们将实现专家系统的基本结构,并提供扩展功能的可能性,以满足多样化的识别需求。
目录
一、实验目的
(1)了解产生式规则的原理。
(2)了解产生式规则专家系统的构成及其原理。
(3)了解产生式规则专家系统的正向推理机制。
二、实验要求
(1)学会使用Python构造专家系统事实库、产生式规则库。
(2)学会使用Python进行专家系统规则的正向推理。
(3)可以修改程序以扩充相应功能。
三、实验原理
参见4.2节《从规则到系统》的内容,在表4-1的基础上建立动物识别产生式规则库,建立事实库动态数据库和控制系统,并采用正向推理控制策略完成简单的动物识别。
表1如下所示为一个动物识别产生式规则库。
表1 动物识别产生式规则库
规则编号 | 如果 | 前件 | 那么 | 后件 |
R1 | IF | 胎生 | THEN | 哺乳动物 |
R2 | IF | 卵生 AND 会飞 | THEN | 鸟 |
R3 | IF | 有毛发 | THEN | 哺乳动物 |
R4 | IF | 有羽毛 | THEN | 鸟 |
R5 | IF | 有喙 | THEN | 鸟 |
R6 | IF | 食肉 | THEN | 肉食动物 |
R7 | IF | 吃草 | THEN | 草食动物 |
R8 | IF | 有爪 | THEN | 爪类动物 |
R9 | IF | 哺乳动物 AND 有蹄 | THEN | 蹄类动物 |
R10 | IF | 爪类动物 AND 有犬齿 | THEN | 肉食动物 |
R11 | IF | 鸟 AND 肉食动物 AND 高飞 AND 爪类动物 | THEN | 鹰 |
R12 | IF | 哺乳动物 AND 肉食动物 AND 褐色斑纹 | THEN | 猫 |
R13 | IF | 蹄类动物 AND 黑白斑纹 | THEN | 斑马 |
R14 | IF | 哺乳动物 AND 肉食动物 AND 王字纹 | THEN | 虎 |
R15 | IF | 鸟 AND 细腿 AND 红顶 | THEN | 丹顶鹤 |
四、实验步骤
1. 构造动物实体Animal
# (1)构造动物实体Animal
class Animal(object):
def __init__(self):
self.name = ''
self.phylum = '' # 门
self.classes = '' # 纲
self.order = '' # 目
self.family = '' # 科
self.surface = '' # 表面
self.food = '' # 食物
self.mouth = '' # 嘴型
self.foot = '' # 足部
self.head = '' # 头部
self.ornament = '' # 纹饰
self.tooth = '' # 牙齿
self.can_fly = -1 # 会飞:1;不会飞:0;未定义:-1
self.birth = '' # 生产方式
def clone(self, _Animal):
self.name = _Animal.name
self.phylum = _Animal.phylum
self.classes = _Animal.classes
self.order = _Animal.order
self.family = _Animal.family
self.surface = _Animal.surface
self.food = _Animal.food
self.mouth = _Animal.mouth
self.foot = _Animal.foot
self.head = _Animal.head
self.ornament = _Animal.ornament
self.tooth = _Animal.tooth
self.can_fly = _Animal.can_fly
self.birth = _Animal.birth
def output(self):
print('\n')
print('该动物', end='')
if self.birth != '':
print(self.birth, end='')
print(',', end='')
if self.head != '':
print(self.head, end='')
print(',', end='')
if self.mouth != '':
print(self.mouth, end='')
print(',', end='')
if self.tooth != '':
print(self.tooth, end='')
print(',', end='')
if self.food != '':
print(self.food, end='')
print(',', end='')
if self.surface != '':
print(self.surface, end='')
print(',', end='')
if self.ornament != '':
print(self.ornament, end='')
print(',', end='')
if self.foot != '':
print(self.foot, end='')
print(',', end='')
if self.can_fly == 1:
print('会飞,', end='')
elif self.can_fly == 0:
print('不会飞,', end='')
print('应该属于', end='')
if self.phylum != '':
print(self.phylum, end='')
if self.classes != '':
print(self.classes, end='')
if self.order != '':
print(self.order, end='')
if self.family != '':
print(self.family, end='')
if self.name != '':
print(self.name, end='')
print('。\n')
2. 构造规则
# (2)构造规则
class Rule(object):
def __init__(self):
self.antecedent = Animal()
self.consequent = Animal()
def setAntecedent(self, animal):
self.antecedent.clone(animal)
def setConsequent(self, animal):
self.consequent.clone(animal)
# 规则匹配
def match(self, animal):
result = True
if self.antecedent.can_fly != -1:
if self.antecedent.can_fly != animal.can_fly:
result = False
return result
if self.antecedent.classes != '':
if self.antecedent.classes != animal.classes:
result = False
return result
if self.antecedent.family != '':
if self.antecedent.family != animal.family:
result = False
return result
if self.antecedent.food != '':
if self.antecedent.food != animal.food:
result = False
return result
if self.antecedent.foot != '':
if self.antecedent.foot != animal.foot:
result = False
return result
if self.antecedent.head != '':
if self.antecedent.head != animal.head:
result = False
return result
if self.antecedent.mouth != '':
if self.antecedent.mouth != animal.mouth:
result = False
return result
if self.antecedent.name != '':
if self.antecedent.name != animal.name:
result = False
return result
if self.antecedent.order != '':
if self.antecedent.order != animal.order:
result = False
return result
if self.antecedent.ornament != '':
if self.antecedent.ornament != animal.ornament:
result = False
return result
if self.antecedent.surface != '':
if self.antecedent.surface != animal.surface:
result = False
return result
if self.antecedent.tooth != '':
if self.antecedent.tooth != animal.tooth:
result = False
return result
if self.antecedent.birth != '':
if self.antecedent.birth != animal.birth:
result = False
return result
if self.antecedent.phylum != '':
if self.antecedent.phylum != animal.phylum:
result = False
return result
return result
# 规则执行
def execute(self, animal):
if self.consequent.can_fly != -1:
animal.can_fly = self.consequent.can_fly
if self.consequent.classes != '':
animal.classes = self.consequent.classes
if self.consequent.family != '':
animal.family = self.consequent.family
if self.consequent.food != '':
animal.food = self.consequent.food
if self.consequent.foot != '':
animal.foot = self.consequent.foot
if self.consequent.head != '':
animal.head = self.consequent.head
if self.consequent.mouth != '':
animal.mouth = self.consequent.mouth
if self.consequent.name != '':
animal.name = self.consequent.name
if self.consequent.order != '':
animal.order = self.consequent.order
if self.consequent.ornament != '':
animal.ornament = self.consequent.ornament
if self.consequent.surface != '':
animal.surface = self.consequent.surface
if self.consequent.tooth != '':
animal.tooth = self.consequent.tooth
if self.consequent.birth != '':
animal.birth = self.consequent.birth
if self.consequent.phylum != '':
animal.phylum = self.consequent.phylum
3. 构造动物识别专家系统
# (3)构造动物识别专家系统
class ExpertSystem(object):
def __init__(self):
self.facts = [] # 事实库
self.Rules = [] # 规则库
self.dataset = [] # 动态数据库
self.setRules()
4. 构造规则库
# (4)构造规则库
# 建立规则库
def setRules(self):
R1 = Rule()
# 设置前件
R1_antecedent = Animal()
R1_antecedent.birth = '胎生'
R1.setAntecedent(R1_antecedent)
# 设置后件
R1_consequent = Animal()
R1_consequent.classes = '哺乳纲'
R1.setConsequent(R1_consequent)
self.Rules.append(R1)
R2 = Rule()
# 设置前件
R2_antecedent = Animal()
R2_antecedent.birth = '卵生'
R2_antecedent.can_fly = 1
R2.setAntecedent(R2_antecedent)
# 设置后件
R2_consequent = Animal()
R2_consequent.classes = '鸟纲'
R2.setConsequent(R2_consequent)
self.Rules.append(R2)
R3 = Rule()
# 设置前件
R3_antecedent = Animal()
R3_antecedent.surface = '毛发'
R3.setAntecedent(R3_antecedent)
# 设置后件
R3_consequent = Animal()
R3_consequent.classes = '哺乳纲'
R3.setConsequent(R3_consequent)
self.Rules.append(R3)
R4 = Rule()
# 设置前件
R4_antecedent = Animal()
R4_antecedent.birth = '羽毛'
R4.setAntecedent(R4_antecedent)
# 设置后件
R4_consequent = Animal()
R4_consequent.classes = '鸟纲'
R4.setConsequent(R4_consequent)
self.Rules.append(R4)
R5 = Rule()
# 设置前件
R5_antecedent = Animal()
R5_antecedent.mouth = '喙'
R5.setAntecedent(R5_antecedent)
# 设置后件
R5_consequent = Animal()
R5_consequent.classes = '鸟纲'
R5.setConsequent(R5_consequent)
self.Rules.append(R5)
R6 = Rule()
# 设置前件
R6_antecedent = Animal()
R6_antecedent.food = '肉'
R6.setAntecedent(R6_antecedent)
# 设置后件
R6_consequent = Animal()
R6_consequent.order = '食肉目'
R6.setConsequent(R6_consequent)
self.Rules.append(R6)
R7 = Rule()
# 设置前件
R7_antecedent = Animal()
R7_antecedent.food = '草'
R7.setAntecedent(R7_antecedent)
# 设置后件
R7_consequent = Animal()
R7_consequent.order = '食草目'
R7.setConsequent(R7_consequent)
self.Rules.append(R7)
R8 = Rule()
# 设置前件
R8_antecedent = Animal()
R8_antecedent.foot = '爪'
R8.setAntecedent(R8_antecedent)
# 设置后件
R8_consequent = Animal()
R8_consequent.phylum = '有爪门'
R8.setConsequent(R8_consequent)
self.Rules.append(R8)
R9 = Rule()
# 设置前件
R9_antecedent = Animal()
R9_antecedent.foot = '爪'
R9_antecedent.classes = '哺乳纲'
R9.setAntecedent(R9_antecedent)
# 设置后件
R9_consequent = Animal()
R9_consequent.order = '有蹄目'
R9.setConsequent(R9_consequent)
self.Rules.append(R9)
R10 = Rule()
# 设置前件
R10_antecedent = Animal()
R10_antecedent.phylum = '有爪门'
R10_antecedent.tooth = '有犬齿'
R10.setAntecedent(R10_antecedent)
# 设置后件
R10_consequent = Animal()
R10_consequent.order = '食肉目'
R10.setConsequent(R10_consequent)
self.Rules.append(R10)
R11 = Rule()
# 设置前件
R11_antecedent = Animal()
R11_antecedent.phylum = '有爪门'
R11_antecedent.order = '食肉目'
R11_antecedent.classes = '鸟纲'
R11_antecedent.can_fly = 1
R11.setAntecedent(R11_antecedent)
# 设置后件
R11_consequent = Animal()
R11_consequent.family = '鹰'
R11.setConsequent(R11_consequent)
self.Rules.append(R11)
R12 = Rule()
# 设置前件
R12_antecedent = Animal()
R12_antecedent.order = '食肉目'
R12_antecedent.classes = '哺乳纲'
R12_antecedent.ornament = '褐色斑纹'
R12.setAntecedent(R12_antecedent)
# 设置后件
R12_consequent = Animal()
R12_consequent.family = '猫'
R12.setConsequent(R12_consequent)
self.Rules.append(R12)
R13 = Rule()
# 设置前件
R13_antecedent = Animal()
R13_antecedent.order = '有蹄目'
R13_antecedent.ornament = '黑白斑纹'
R13.setAntecedent(R13_antecedent)
# 设置后件
R13_consequent = Animal()
R13_consequent.family = '斑马'
R13.setConsequent(R13_consequent)
self.Rules.append(R13)
R14 = Rule()
# 设置前件
R14_antecedent = Animal()
R14_antecedent.order = '食肉目'
R14_antecedent.classes = '哺乳纲'
R14_antecedent.ornament = '王字纹'
R14.setAntecedent(R14_antecedent)
# 设置后件
R14_consequent = Animal()
R14_consequent.family = '虎'
R14.setConsequent(R14_consequent)
self.Rules.append(R14)
R15 = Rule()
# 设置前件
R15_antecedent = Animal()
R15_antecedent.foot = '细腿'
R15_antecedent.head = '红顶'
R15_antecedent.classes = '鸟纲'
R15.setAntecedent(R15_antecedent)
# 设置后件
R15_consequent = Animal()
R15_consequent.family = '丹顶鹤'
R15.setConsequent(R15_consequent)
self.Rules.append(R15)
5. 设置事实库,初始化动态数据库
# (5)设置事实库,初始化动态数据库
def setFacts(self, _facts):
for ani in _facts:
f = Animal()
f.clone(ani)
self.facts.append(f)
d = Animal()
d.clone(ani)
self.dataset.append(d)
6. 正向推理并输出
# (6)正向推理并输出
def forwardReasoning(self):
for d in self.dataset:
for r in self.Rules:
# 如果规则匹配,则执行规则
if r.match(d):
r.execute(d)
d.output()
7. 测试
# (7)测试
if __name__ == '__main__':
ani_expert = ExpertSystem()
ani1 = Animal()
ani1.birth = '卵生'
ani1.can_fly = 1
ani1.head = '红顶'
ani1.foot = '细腿'
ani2 = Animal()
ani2.birth = '胎生'
ani2.food = '肉'
ani2.ornament = '褐色斑纹'
ani3 = Animal()
ani3.order = '食肉目'
ani3.classes = '哺乳纲'
ani3.ornament = '王字纹'
ani_expert.setFacts([ani1, ani2, ani3])
ani_expert.forwardReasoning()
五、实验结果
由上图结果可知,在输出的六行中,第二、五、六行为最终测试的结果。第一行为第二行的推理结果,第三、四行为第五行的推理结果,最终得出“该动物卵生,红顶,细腿,会飞,应该属于鸟纲丹顶鹤。”和“该动物胎生,肉,褐色斑纹,应该属于哺乳纲食肉目猫。”以及“该动物胎生,肉,褐色斑纹,应该属于哺乳纲食肉目猫。”这三个测试结果。
六、实验总结
该实验项目是一个基于产生式规则的动物识别专家系统,通过定义动物类、规则类和专家系统类,实现了对动物的分类和识别。核心思想是利用一系列规则来匹配动物的属性,从而推断出动物的分类信息。
通过此实验项目,我可以学到以下几个方面的知识和技能:
1. 产生式规则引擎:了解了产生式规则在人工智能领域的应用,通过编写规则来描述问题的解决方案,实现了一种基于规则的推理引擎。
2. 面向对象编程:实验采用了面向对象的编程方法,通过定义动物类和规则类,将系统的功能模块化,使得代码更加清晰和易于扩展。
3. 规则匹配和推理:学会了如何根据规则库中定义的前件条件,对动物的属性进行匹配,从而推断出动物的分类信息。
4. 动态数据库管理:掌握了如何管理动态数据库,即根据用户提供的事实,动态地生成待推理的动物数据集。
5. 系统测试与调试:通过测试部分的编写,学会了如何验证系统的功能是否符合预期,以及如何调试和改进程序。
通过完成这个实验项目,我不仅增加了对人工智能和规则引擎的理解,还提升了面向对象编程的能力和系统设计的能力。同时,也为进一步学习和开发复杂的专家系统奠定了基础。
七、完整代码
# 实验:基于产生式规则的动物识别专家系统
# (1)构造动物实体Animal
class Animal(object):
def __init__(self):
self.name = ''
self.phylum = '' # 门
self.classes = '' # 纲
self.order = '' # 目
self.family = '' # 科
self.surface = '' # 表面
self.food = '' # 食物
self.mouth = '' # 嘴型
self.foot = '' # 足部
self.head = '' # 头部
self.ornament = '' # 纹饰
self.tooth = '' # 牙齿
self.can_fly = -1 # 会飞:1;不会飞:0;未定义:-1
self.birth = '' # 生产方式
def clone(self, _Animal):
self.name = _Animal.name
self.phylum = _Animal.phylum
self.classes = _Animal.classes
self.order = _Animal.order
self.family = _Animal.family
self.surface = _Animal.surface
self.food = _Animal.food
self.mouth = _Animal.mouth
self.foot = _Animal.foot
self.head = _Animal.head
self.ornament = _Animal.ornament
self.tooth = _Animal.tooth
self.can_fly = _Animal.can_fly
self.birth = _Animal.birth
def output(self):
print('\n')
print('该动物', end='')
if self.birth != '':
print(self.birth, end='')
print(',', end='')
if self.head != '':
print(self.head, end='')
print(',', end='')
if self.mouth != '':
print(self.mouth, end='')
print(',', end='')
if self.tooth != '':
print(self.tooth, end='')
print(',', end='')
if self.food != '':
print(self.food, end='')
print(',', end='')
if self.surface != '':
print(self.surface, end='')
print(',', end='')
if self.ornament != '':
print(self.ornament, end='')
print(',', end='')
if self.foot != '':
print(self.foot, end='')
print(',', end='')
if self.can_fly == 1:
print('会飞,', end='')
elif self.can_fly == 0:
print('不会飞,', end='')
print('应该属于', end='')
if self.phylum != '':
print(self.phylum, end='')
if self.classes != '':
print(self.classes, end='')
if self.order != '':
print(self.order, end='')
if self.family != '':
print(self.family, end='')
if self.name != '':
print(self.name, end='')
print('。\n')
# (2)构造规则
class Rule(object):
def __init__(self):
self.antecedent = Animal()
self.consequent = Animal()
def setAntecedent(self, animal):
self.antecedent.clone(animal)
def setConsequent(self, animal):
self.consequent.clone(animal)
# 规则匹配
def match(self, animal):
result = True
if self.antecedent.can_fly != -1:
if self.antecedent.can_fly != animal.can_fly:
result = False
return result
if self.antecedent.classes != '':
if self.antecedent.classes != animal.classes:
result = False
return result
if self.antecedent.family != '':
if self.antecedent.family != animal.family:
result = False
return result
if self.antecedent.food != '':
if self.antecedent.food != animal.food:
result = False
return result
if self.antecedent.foot != '':
if self.antecedent.foot != animal.foot:
result = False
return result
if self.antecedent.head != '':
if self.antecedent.head != animal.head:
result = False
return result
if self.antecedent.mouth != '':
if self.antecedent.mouth != animal.mouth:
result = False
return result
if self.antecedent.name != '':
if self.antecedent.name != animal.name:
result = False
return result
if self.antecedent.order != '':
if self.antecedent.order != animal.order:
result = False
return result
if self.antecedent.ornament != '':
if self.antecedent.ornament != animal.ornament:
result = False
return result
if self.antecedent.surface != '':
if self.antecedent.surface != animal.surface:
result = False
return result
if self.antecedent.tooth != '':
if self.antecedent.tooth != animal.tooth:
result = False
return result
if self.antecedent.birth != '':
if self.antecedent.birth != animal.birth:
result = False
return result
if self.antecedent.phylum != '':
if self.antecedent.phylum != animal.phylum:
result = False
return result
return result
# 规则执行
def execute(self, animal):
if self.consequent.can_fly != -1:
animal.can_fly = self.consequent.can_fly
if self.consequent.classes != '':
animal.classes = self.consequent.classes
if self.consequent.family != '':
animal.family = self.consequent.family
if self.consequent.food != '':
animal.food = self.consequent.food
if self.consequent.foot != '':
animal.foot = self.consequent.foot
if self.consequent.head != '':
animal.head = self.consequent.head
if self.consequent.mouth != '':
animal.mouth = self.consequent.mouth
if self.consequent.name != '':
animal.name = self.consequent.name
if self.consequent.order != '':
animal.order = self.consequent.order
if self.consequent.ornament != '':
animal.ornament = self.consequent.ornament
if self.consequent.surface != '':
animal.surface = self.consequent.surface
if self.consequent.tooth != '':
animal.tooth = self.consequent.tooth
if self.consequent.birth != '':
animal.birth = self.consequent.birth
if self.consequent.phylum != '':
animal.phylum = self.consequent.phylum
# (3)构造动物识别专家系统
class ExpertSystem(object):
def __init__(self):
self.facts = [] # 事实库
self.Rules = [] # 规则库
self.dataset = [] # 动态数据库
self.setRules()
# (4)构造规则库
# 建立规则库
def setRules(self):
R1 = Rule()
# 设置前件
R1_antecedent = Animal()
R1_antecedent.birth = '胎生'
R1.setAntecedent(R1_antecedent)
# 设置后件
R1_consequent = Animal()
R1_consequent.classes = '哺乳纲'
R1.setConsequent(R1_consequent)
self.Rules.append(R1)
R2 = Rule()
# 设置前件
R2_antecedent = Animal()
R2_antecedent.birth = '卵生'
R2_antecedent.can_fly = 1
R2.setAntecedent(R2_antecedent)
# 设置后件
R2_consequent = Animal()
R2_consequent.classes = '鸟纲'
R2.setConsequent(R2_consequent)
self.Rules.append(R2)
R3 = Rule()
# 设置前件
R3_antecedent = Animal()
R3_antecedent.surface = '毛发'
R3.setAntecedent(R3_antecedent)
# 设置后件
R3_consequent = Animal()
R3_consequent.classes = '哺乳纲'
R3.setConsequent(R3_consequent)
self.Rules.append(R3)
R4 = Rule()
# 设置前件
R4_antecedent = Animal()
R4_antecedent.birth = '羽毛'
R4.setAntecedent(R4_antecedent)
# 设置后件
R4_consequent = Animal()
R4_consequent.classes = '鸟纲'
R4.setConsequent(R4_consequent)
self.Rules.append(R4)
R5 = Rule()
# 设置前件
R5_antecedent = Animal()
R5_antecedent.mouth = '喙'
R5.setAntecedent(R5_antecedent)
# 设置后件
R5_consequent = Animal()
R5_consequent.classes = '鸟纲'
R5.setConsequent(R5_consequent)
self.Rules.append(R5)
R6 = Rule()
# 设置前件
R6_antecedent = Animal()
R6_antecedent.food = '肉'
R6.setAntecedent(R6_antecedent)
# 设置后件
R6_consequent = Animal()
R6_consequent.order = '食肉目'
R6.setConsequent(R6_consequent)
self.Rules.append(R6)
R7 = Rule()
# 设置前件
R7_antecedent = Animal()
R7_antecedent.food = '草'
R7.setAntecedent(R7_antecedent)
# 设置后件
R7_consequent = Animal()
R7_consequent.order = '食草目'
R7.setConsequent(R7_consequent)
self.Rules.append(R7)
R8 = Rule()
# 设置前件
R8_antecedent = Animal()
R8_antecedent.foot = '爪'
R8.setAntecedent(R8_antecedent)
# 设置后件
R8_consequent = Animal()
R8_consequent.phylum = '有爪门'
R8.setConsequent(R8_consequent)
self.Rules.append(R8)
R9 = Rule()
# 设置前件
R9_antecedent = Animal()
R9_antecedent.foot = '爪'
R9_antecedent.classes = '哺乳纲'
R9.setAntecedent(R9_antecedent)
# 设置后件
R9_consequent = Animal()
R9_consequent.order = '有蹄目'
R9.setConsequent(R9_consequent)
self.Rules.append(R9)
R10 = Rule()
# 设置前件
R10_antecedent = Animal()
R10_antecedent.phylum = '有爪门'
R10_antecedent.tooth = '有犬齿'
R10.setAntecedent(R10_antecedent)
# 设置后件
R10_consequent = Animal()
R10_consequent.order = '食肉目'
R10.setConsequent(R10_consequent)
self.Rules.append(R10)
R11 = Rule()
# 设置前件
R11_antecedent = Animal()
R11_antecedent.phylum = '有爪门'
R11_antecedent.order = '食肉目'
R11_antecedent.classes = '鸟纲'
R11_antecedent.can_fly = 1
R11.setAntecedent(R11_antecedent)
# 设置后件
R11_consequent = Animal()
R11_consequent.family = '鹰'
R11.setConsequent(R11_consequent)
self.Rules.append(R11)
R12 = Rule()
# 设置前件
R12_antecedent = Animal()
R12_antecedent.order = '食肉目'
R12_antecedent.classes = '哺乳纲'
R12_antecedent.ornament = '褐色斑纹'
R12.setAntecedent(R12_antecedent)
# 设置后件
R12_consequent = Animal()
R12_consequent.family = '猫'
R12.setConsequent(R12_consequent)
self.Rules.append(R12)
R13 = Rule()
# 设置前件
R13_antecedent = Animal()
R13_antecedent.order = '有蹄目'
R13_antecedent.ornament = '黑白斑纹'
R13.setAntecedent(R13_antecedent)
# 设置后件
R13_consequent = Animal()
R13_consequent.family = '斑马'
R13.setConsequent(R13_consequent)
self.Rules.append(R13)
R14 = Rule()
# 设置前件
R14_antecedent = Animal()
R14_antecedent.order = '食肉目'
R14_antecedent.classes = '哺乳纲'
R14_antecedent.ornament = '王字纹'
R14.setAntecedent(R14_antecedent)
# 设置后件
R14_consequent = Animal()
R14_consequent.family = '虎'
R14.setConsequent(R14_consequent)
self.Rules.append(R14)
R15 = Rule()
# 设置前件
R15_antecedent = Animal()
R15_antecedent.foot = '细腿'
R15_antecedent.head = '红顶'
R15_antecedent.classes = '鸟纲'
R15.setAntecedent(R15_antecedent)
# 设置后件
R15_consequent = Animal()
R15_consequent.family = '丹顶鹤'
R15.setConsequent(R15_consequent)
self.Rules.append(R15)
# (5)设置事实库,初始化动态数据库
def setFacts(self, _facts):
for ani in _facts:
f = Animal()
f.clone(ani)
self.facts.append(f)
d = Animal()
d.clone(ani)
self.dataset.append(d)
# (6)正向推理并输出
def forwardReasoning(self):
for d in self.dataset:
for r in self.Rules:
# 如果规则匹配,则执行规则
if r.match(d):
r.execute(d)
d.output()
# (7)测试
if __name__ == '__main__':
ani_expert = ExpertSystem()
ani1 = Animal()
ani1.birth = '卵生'
ani1.can_fly = 1
ani1.head = '红顶'
ani1.foot = '细腿'
ani2 = Animal()
ani2.birth = '胎生'
ani2.food = '肉'
ani2.ornament = '褐色斑纹'
ani3 = Animal()
ani3.order = '食肉目'
ani3.classes = '哺乳纲'
ani3.ornament = '王字纹'
ani_expert.setFacts([ani1, ani2, ani3])
ani_expert.forwardReasoning()