【问题标题】:Clipspy printout ruleClipspy 打印输出规则
【发布时间】:2021-10-23 09:47:53
【问题描述】:

所以我最近一直在使用 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()

【问题讨论】:

    标签: python clips clipspy


    【解决方案1】:

    将图形用户界面与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()
    

    【讨论】:

      猜你喜欢
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多