2013-03-08 26 views
0

我想知道是否有人可以幫助,我一直在玩在線CLIPS教程,所以我有點基礎知識,我只是想玩一個規則會要求用戶輸入一個布爾型的'y'或'n'響應,它會根據用戶的輸入調用一個python函數。專家系統用戶輸入來調用函數

我知道這已經有點被這裏介紹:How to get a rule activation to call a python function, using PyClips

我只是有點困惑,在哪裏可以找到用戶的輸入,你將如何基於調用一個函數或者是「Y」或' N」。

任何幫助將不勝感激!

回答

0

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

太精彩了!乾杯! – user2148452 2013-03-08 19:27:29