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

Prolog query in python program not returning correct answers

我正在 python 制作一个具有 GUI 的专家系统。为此,我使用 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()

When the variables holding these selected values are passed into the prolog query, " [] " is returned. However, if instead of the variables, the values(symptoms in this case) are hard coded into the prolog query below, it works fine.

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

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