有一个如何创建交互式PyCLIPS会话here的例子。使用,作为一个开始,这里是你如何调用一个python功能,根据用户的输入的例子:
import clips
def py_input_is_y():
print 'Input is y.'
def py_input_not_y(s):
print 'Input is not y: %s' % s
def clips_raw_input(prompt):
return clips.String(raw_input(prompt))
clips.RegisterPythonFunction(py_input_is_y, "input-is-y")
clips.RegisterPythonFunction(py_input_not_y, "input-not-y")
clips.RegisterPythonFunction(clips_raw_input, "raw-input")
clips.Build('''
(defrule get-input
(initial-fact)
=>
(bind ?x (python-call raw-input "Enter y or n:"))
(if (= (str-compare ?x "y") 0)
then (python-call input-is-y)
else (python-call input-not-y ?x)))
''')
假设代码是在getinput.py
,这里是一个输出示例:
>>> from getinput import *
>>> clips.Reset()
>>> clips.Run()
Enter y or n:y
Input is y.
1
>>> clips.Reset()
>>> clips.Run()
Enter y or n:foo
Input is not y: foo
1
太精彩了!干杯! – user2148452 2013-03-08 19:27:29