为什么撤回的事实模板仍然可用?

时间:2018-12-30 15:35:18

标签: python clips clipspy

我正在尝试使用ClipsPY修改剪辑中事实模板的事实。以下代码撤回并重新声明该事实。为什么事实撤回后,重新声明时插槽值s_2不是nil

run.py

import clips

clips_env = clips.Environment()

def py_pfact():
    for fact in clips_env.facts():
        print(fact)

def py_modify(p):
    print("\nfacts:")
    py_pfact()

    p.retract()
    print("\nfacts after retracted:")
    py_pfact()

    p["s_1"] = clips.Symbol("v_2") 
    p.assertit()
    print("\nfacts after re_assert:")
    py_pfact()

clips_env.define_function(py_pfact)
clips_env.define_function(py_modify)

clips_env.load("KB.clp")
clips_env.reset()
clips_env.run()

这是clp文件

;; KB.clp
(deftemplate t
            (slot s_1 (type SYMBOL))
            (slot s_2 (type SYMBOL))
    )

    (defrule main-intent
            (initial-fact)
            =>
            (assert (t (s_1 v_1) (s_2 v_2)))
    )

    (defrule rule_1
            ?p<-(t (s_1 ?v&~v_2))
            =>
            (py_modify ?p)
    )

输出为:

facts:
(initial-fact)
(t (s_1 v_1) (s_2 v_2))

facts after retracted:
(initial-fact)

facts after re_assert:
(initial-fact)
(t (s_1 v_2) (s_2 v_2))

我期望输出为(t (s_1 v_2) (s_2 nil)),但是s_2不是nil,而是先前的值v_2,它是事实撤消之前设置的。

1 个答案:

答案 0 :(得分:0)

声明和撤回事实不会对其进行修改。它只是从引擎知识库中添加/删除它。

您可以看到deftemplate作为一个类,并且从其中创建的fact就像一个对象。您可以通过从API或通过规则添加和删除事实来扩展引擎知识库。

# define a fact template within the engine
environment.build('(deftemplate foo (slot bar) (slot baz))')
template = environment.find_template('foo')

# create a new fact and set its values
fact = template.new_fact()
fact['bar'] = 1
fact['baz'] = 1

# assert the fact within the engine
fact.assertit()

assert fact in environment.facts()

# retract it
fact.retract()

assert fact not in environment.facts()
assert fact is fact  # fact is the same object as before

fact['baz'] = 2

assert fact is fact  # still the same object

fact.assertit()

assert str(fact) == '(foo (bar 1) (baz 2))'