''
@Time:2022/03/29 8:39
@Author: 11863
@File: AIS_main.py
@software:PyCharm
'''
rules = {} #以字典形式存储
#读取文件
def readRules():
rulesFile = open("rules.txt","r",encoding='utf-8')
for line in rulesFile:#按行读取
line = line.replace('IF','').strip()
temp = line.split('THEN')
premise = temp[0] #条件
conclusion = temp[1] #结论
rules[premise] = conclusion
rulesFile.close()
def matchRules(character):
premise = rules.keys()
for pre in premise:
Flag = True
preSplit = pre.split('AND') #将条件里的and去掉
for p in preSplit:
if p not in character: # 如果前提条件在综合数据库里,则将对应的结论添加到综合数据库里
Flag = False
break
if Flag:
temp = rules.get(pre)
character.append(temp)
print(character[-1])
if __name__=='__main__':
character=[]
readRules()
print("规则库如下:")
for line in rules.items():
print(line)
while True:
facts = input("请输入已知条件(输入0终止):")
if facts == "0":
break
character.append(facts)
matchRules(character)
rules.txt(注意不要有空格)
结果