2013-03-08 51 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