我正在使用 python 和 Clips 来解决一个问题,这就是我想要做的:
我正在寻找从 python 加载一个 .clp 文件并运行它。我还需要添加基于数据库的事实。所以 .clp 文件中会有规则,我正在使用
clips.Load("myfile.clp")
加载我的文件。我被困在如何将事实断言到剪辑中。我在剪辑中也有一个可变的最终结果,它将根据事实存储它所产生的内容。我需要把它带回 python 来运行其他代码。
谢谢
我假设您正在使用 PyCLIPS。
import clips
def clips_callable(f):
def wf(*args, **kwargs):
if f(*args, **kwargs):
return clips.Symbol("TRUE")
else:
return clips.Symbol("FALSE")
clips.RegisterPythonFunction(wf, f.__name__)
@clips_callable
def pyprint(s):
print s
print "".join(map(str, s))
clips.Load("test.clp")
clips.Reset()
clips.Run()
# assert a fact.
a = clips.Assert("(order (part-id p1) (quantity 20))")
clips.Run()
test.clp
看起来像:
(deffunction MAIN::print ($?value)
(python-call pyprint ?value)
; (printout t ?value)
)
(deftemplate MAIN::order
(slot part-id)
(slot quantity)
)
我将@clips_callable
装饰器作为奖励包括在内,这使得从剪辑中调用 python 函数变得非常容易。