如何在clipspy中获取事实值并将其存储在python变量中 [英] How to get a fact value in clipspy and store it in a python variable
问题描述
说我插入了一条规则:
(defrule matching
(fever ?fever)
(headache ?headache)
(disease (fever ?fever) (disname ?disname1) (headache ?headache))
=>
(assert (dis ?disname1)))
现在,我想将?disname1的值提取到python变量中,以便可以在网页上显示它,
Now I want to fetch the value of ?disname1 into a python variable so that I can display it on a webpage,
烧瓶代码-
import clips
from flask import Flask , render_template ,request
app = Flask(__name__)
env = clips.Environment()
env.load("clips.clp")
env.reset()
@app.route('/')
def home():
return render_template('main.html')
@app.route('/result' , methods = ['POST', 'GET'])
def result():
if request.method == 'POST':
if request.form.get('fever'):
env.assert_string("(fever true) ")
if request.form.get('headache'):
env.assert_string("(headache true)")
rule = """ (defrule matching
(fever ?fever) (headache ?headache)
(disease (fever ?fever) (disname ?disname1) (headache ?headache))
=>
(assert ( dis ?disname1 )) """
env.build(rule)
return render_template('next.html')
env.run()
if __name__ == '__main__':
app.run(debug= True)
因此,在构建规则之后,将(dis?disname)声明为片段中的事实,所以现在,我想检索?disname 的值,该值将给出我将一个人面临的疾病转换为python变量,以便可以将其传递给html模板并显示它.
So after the rule is build ( dis ?disname) is asserted as a fact into clips so now ,I want to retrieve the value of ?disname which will give me the illness a person is facing , into a python variable so that I can pass it to html template and display it.
推荐答案
您可以通过事实迭代器访问环境事实.
You can access to the environment facts via the facts iterator.
根据事实是有序或<您可以通过索引或键访问href ="https://clipspy.readthedocs.io/en/latest/?badge=latest#template-facts" rel ="nofollow noreferrer">无序.
for fact in env.facts():
if fact.template.name == 'dis':
print(fact[0])
关于注释中的问题,取决于您的应用程序.对于您的情况,我建议使用事实来代表决策结果.事实代表了专家的系统知识,因此非常适合.
Regarding your question in the comments, it depends on your application. In your case, I would recommend to use facts to represent the decision outcome. Expert systems knowledge is represented by facts and therefore it's a good fit.
您可以使用生成的知识来进一步推理并激活更多规则,例如,可以对用户生病这一事实做出反应并采取相应的行动.
You can use the generated knowledge to further reason and activate more rules which, for example, could react to the fact the User is ill and take actions accordingly.
这篇关于如何在clipspy中获取事实值并将其存储在python变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!