So I have been working on an expert system using clipspy recently. I have already came out with the rule file and load it back in using the clipspy. Some my question is, how do I extract the printout content in the rule file using the clipspy library because I have to make a simple GUI for the system. The GUI will be like pop up the question and prompt the user to fill in the answer until the ends of the system.
Example rule file:
(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 script with 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()
The simplest way to integrate a graphical user interface with CLIPSPy
would probably consist in wrapping the GUI logic in ad-hoc callback functions and importing them in CLIPS
via the define_function Environment method.
In the following example we use PySimpleGUI to draw the question boxes and collect the User's input. The question/answer logic is defined within a polar_question
function and imported in CLIPS
as polar-question
. You can then use such function within your CLIPS
code.
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()