有一個如何創建交互式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