Clipspy 打印输出规则
Clipspy printout rule
所以我最近一直在使用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))))
)
Python 带有 clipspy 的脚本:
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()
将图形用户界面与 CLIPSPy
集成的最简单方法可能是将 GUI 逻辑包装在临时回调函数中,然后通过 define_function 将它们导入 CLIPS
环境方法。
在下面的示例中,我们使用PySimpleGUI绘制问题框并收集用户的输入。 question/answer 逻辑在 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()
所以我最近一直在使用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))))
)
Python 带有 clipspy 的脚本:
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()
将图形用户界面与 CLIPSPy
集成的最简单方法可能是将 GUI 逻辑包装在临时回调函数中,然后通过 define_function 将它们导入 CLIPS
环境方法。
在下面的示例中,我们使用PySimpleGUI绘制问题框并收集用户的输入。 question/answer 逻辑在 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()