Clipspy打印输出规则

2024-06-20 15:11:50 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我最近一直在使用clipspy开发一个专家系统。我已经拿出了规则文件,并使用clipspy将其加载回。我的一些问题是,如何使用clipspy库提取规则文件中的打印输出内容,因为我必须为系统制作一个简单的GUI。GUI就像弹出问题并提示用户填写答案,直到系统结束

示例规则文件:

(defrule BR_Service
    (service BR)
    =>
    (printout t crlf "Would you like to book or return a car? ("B" for book / "R" for return)" crlf)
    (assert (br (upcase(read))))
)

(defrule Book_Service
    (br B)
    =>
    (printout t crlf "Are you a first-time user? (Y/N)" crlf)
    (assert (b (upcase(read))))
)

(defrule Premium_Member
    (b N)
    =>
    (printout t crlf "Are you a Premium status member? (Y/N)" crlf)
    (assert (p (upcase(read))))
)

带有clipspy的Python脚本:

import clips

env = clips.Environment()
rule_file = 'rule_file.CLP'
env.load(rule_file)
print("What kind of service needed? ('BR' for book/return car / 'EM' for emergency)")
input = input()
env.assert_string("(service {})".format(input))
env.run()

Tags: 文件brenvyouforreturn规则service
1条回答
网友
1楼 · 发布于 2024-06-20 15:11:50

将图形用户界面与CLIPSPy集成的最简单方法可能是将GUI逻辑包装在临时回调函数中,并通过define_function环境方法将它们导入CLIPS

在下面的示例中,我们使用PySimpleGUI绘制问题框并收集用户的输入。问答逻辑在polar_question函数中定义,并在CLIPS中作为polar-question导入。然后可以在CLIPS代码中使用此函数

import clips
import PySimpleGUI as sg


RULES = [
    """
    (defrule book-service
      =>
      (bind ?answer (polar-question "Are you a first-time user?"))
      (assert (first-time-user ?answer)))
    """,
    """
    (defrule first-timer
      (first-time-user "Yes")
      =>
      (bind ?answer (polar-question "Do you like reading books?"))
      (assert (likes-reading-books ?answer)))
    """
]


def polar_question(text: str) -> str:
    """A simple Yes/No question."""
    layout = [[sg.Text(text)], [sg.Button("Yes"), sg.Button("No")]]
    window = sg.Window("CLIPSPy Demo", layout)
    event, _ = window.read()
    window.close()

    # If the User closes the window, we interpret it as No
    if event == sg.WIN_CLOSED:
        return "No"
    else:
        return event


def main():
    env = clips.Environment()
    env.define_function(polar_question, name='polar-question')
    for rule in RULES:
        env.build(rule)
    env.run()


if __name__ == '__main__':
    main()

相关问题 更多 >