簡體   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