Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在clipspy中获取事实值并将其存储在python变量中_Python_Flask_Clips_Clipspy - Fatal编程技术网

如何在clipspy中获取事实值并将其存储在python变量中

如何在clipspy中获取事实值并将其存储在python变量中,python,flask,clips,clipspy,Python,Flask,Clips,Clipspy,假设我插入了一条规则: (defrule matching (fever ?fever) (headache ?headache) (disease (fever ?fever) (disname ?disname1) (headache ?headache)) => (assert (dis ?disname1))) 现在我想将?disname1的值提取到一个python变量中,以便在网页上显示它 烧瓶代码- import clips from

假设我插入了一条规则:

(defrule matching
    (fever ?fever)
    (headache ?headache)
    (disease (fever ?fever) (disname ?disname1) (headache ?headache))
    =>
    (assert (dis ?disname1)))
现在我想将?disname1的值提取到一个python变量中,以便在网页上显示它

烧瓶代码-

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)

因此,在规则被构建后,build(dis?disname)作为事实被断言到片段中,因此现在,我想将?disname的值检索到一个python变量中,该值将给出一个人面临的疾病,以便我可以将其传递到html模板并显示它。

您可以通过事实迭代器访问环境事实

这取决于事实是真实的还是您可以通过索引或键访问其内容

for fact in env.facts():
    if fact.template.name == 'dis':
        print(fact[0])
关于你在评论中的问题,这取决于你的申请。在您的情况下,我建议使用事实来表示决策结果。专家系统知识由事实表示,因此非常适合


您可以使用生成的知识进一步推理并激活更多规则,例如,可以对用户生病的事实做出反应并采取相应的措施。

注意-我已经在clips.clp中定义了模板和事实,我在程序开始时加载了clips.clp。另外,如果有更好的方法向用户显示clips的输出,而不是每次根据用户输入添加新的事实。。。。plzz建议,谢谢:)