python程序中的Prolog查询未返回正确答案

时间:2019-05-17 10:38:23

标签: python swi-prolog

我正在用具有GUI的python开发专家系统。为此,我使用pyswip库在python中集成了swi-prolog。在GUI中,用户从下拉菜单(python中的OptionMenu)中选择几个值。将包含这些选定值的变量传递到序言查询中时,将返回“ []”。 但是,如果将值(在这种情况下为症状)而不是变量硬编码到下面的序言查询中,则可以正常工作。

from tkinter import *
from PIL import ImageTk, Image
from pyswip import *


sympList=['--Select Symptom--','headache','sneezing','runny_nose','sore_throat','fever','chills','bodyache','abdominal_pain','loss_of_appetite','skin_rash','conjunctivitus','sweating','vomitting','diarrhea']
class DPDP:
def __init__(self, master):

    frame = Frame(master)
    frame.grid()



    #---------medical symbol pic------------------------
    path1 = "Capture.png"
    img = ImageTk.PhotoImage(Image.open(path1))
    panel = Label(root, image=img)
    panel.photo = img
    panel.place(x=20,y=20,width=140,height=130)

    #----------page title-----------------------------------
    mainHeading=Label(master,text="Disease Prediction & Drug Prescribtion ", font=('Verdana 20'), bg='#44689E')
    mainHeading.grid(padx=200,pady=60)

    #----------symptoms selection---------------------------------------------------

    #-------Symptom1-------------------------
    symp1=Label(root,text="1st Symptom", font=('Verdana 15'),bg='#44689E')
    symp1.place(x=20,y=200)



    self.selSymp1=StringVar()
    self.selSymp1.set(sympList[0])

    sympDropDown1=OptionMenu(root,self.selSymp1,*sympList)
    sympDropDown1.place(x=180,y=200)


    #-------Symptom2-------------------------

    self.symp2=Label(root,text="2nd Symptom", font=('Verdana 15'),bg='#44689E')
    self.symp2.place(x=20,y=300)


    self.selSymp2=StringVar()
    self.selSymp2.set(sympList[0])

    sympDropDown2=OptionMenu(root,self.selSymp2,*sympList)
    sympDropDown2.place(x=180,y=300)

    #-------Symptom3-------------------------

    self.symp3=Label(root,text="3rd Symptom", font=('Verdana 15'),bg='#44689E')
    self.symp3.place(x=20,y=400)


    self.selSymp3=StringVar()
    self.selSymp3.set(sympList[0])

    sympDropDown3=OptionMenu(root,self.selSymp3,*sympList)
    sympDropDown3.place(x=180,y=400)

    #-------Symptom4-------------------------

    symp4=Label(root,text="4th Symptom", font=('Verdana 15'),bg='#44689E')
    symp4.place(x=20,y=500)


    self.selSymp4=StringVar()
    self.selSymp4.set(sympList[0])

    sympDropDown4=OptionMenu(root,self.selSymp4,*sympList)
    sympDropDown4.place(x=180,y=500)

    bt=Button(frame,text="click",width=5,
    command=lambda:queryGenerator(self.selSy 
    mp1.get(),self.selSymp2.get(),self.selSymp3.get(),
    self.selSymp4.get()))
    bt.grid(row=4,column=5)




def queryGenerator(s1,s2,s3,s4):

    print(s1,s2,s3,s4) #this prints the values that are chosen,correctly
    prolog = Prolog()
    prolog.consult('kb.pl')        

    q=list(prolog.query("telldisease(X,s1,s2,s3,s4).")) #prolog query
   # for e in q[0].values():
   # print("You have " + e)
   # break
    print(q)


root = Tk()
root.geometry("820x600")
root.resizable(0,0)
root.config(bg='#44689E')
app= DPDP(root)
root.title("DPDP")
root.mainloop()

1 个答案:

答案 0 :(得分:0)

  

将包含这些选定值的变量传递到序言查询中时,将返回“ []”。但是,如果将值(在这种情况下为症状)代替变量,而是硬编码到下面的序言查询中,则可以正常工作。

您正在传递文字查询"telldisease(X,s1,s2,s3,s4).",但是您想interpolate将s1,s2,s3和s4的值放入查询字符串;这可以做到e。 G。采用%格式:

    q = list(prolog.query("telldisease(X,%s,%s,%s,%s)." % (s1, s2, s3, s4)))