因此,我最近一直在使用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()发布于 2021-10-24 15:57:11
将图形用户界面与CLIPSPy集成的最简单方法可能在于将图形用户界面逻辑包装在即席回调函数中,并通过define_function环境方法将其导入CLIPS。
在下面的示例中,我们使用PySimpleGUI绘制问题框并收集用户的输入。问题/答案逻辑在polar_question函数中定义,并作为polar-question导入到CLIPS中。然后,您可以在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()https://stackoverflow.com/questions/69686972
复制相似问题