0

我想使用python字典中的clipspy添加事实(字典到事实)。但到目前为止,我无法这样做。当我是 Clips 规则和事实编码的初学者时,我遇到了语法错误。如果有人可以帮助我解决此问题,请提前感谢您。以下是我的代码:

import clips
template_string = """
(deftemplate person
  (slot name (type STRING))
  (slot surname (type STRING)))
"""
Dict = {'name': 'John', 'surname': 'Doe' }

env = clips.Environment()
env.build(template_string)

template = env.find_template('person')
parstr = """(name%(name))(surname%(surname))"""%Dict
fact = template.assert_fact(parstr)
assert_fact = fact
env.run()
for fact in env.facts():
    print(fact)

这是我遇到的错误:

  Traceback (most recent call last):
  File "/home/aqsa/Clips/example2.py", line 13, in <module>
    parstr = """(name%(name))(surname%(surname))"""%Dict
ValueError: unsupported format character ')' (0x29) at index 12
4

1 回答 1

1

您将事实断言为字符串,但模板assert_fact需要根据文档示例的关键字参数列表。

template.assert_fact(name='John', surname='Doe')

或者

template.assert_fact(**Dict)  # kwargs expansion

您也可以将事实断言为字符串,但由于引擎必须解释它们,因此速度会慢一些。

env.assert_string('(person (name "John") (surname "Doe"))')
于 2021-09-13T08:26:06.707 回答