繁体   English   中英

导入.clp并添加事实

[英]Import .clp and add facts

我正在使用python和Clips解决问题,这是我正在尝试做的事情:

我正在寻找从python加载.clp文件并运行它。 我还需要添加基于数据库的事实。 因此,.clp文件中将包含规则,而我正在使用

clips.Load("myfile.clp")

加载我的文件。 我被困在如何将事实断言片段中。 我在剪辑中也有一个可变的final,它将根据事实存储结果。 我需要将其带回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函数非常容易。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM