CLIPS 初学者:如何使用 clipspy 将 Python 字典数据添加到 CLIPS 事实中

CLIPS Beginner: How to add Python dictionary data into CLIPS facts using clipspy

我想使用 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

您将一个事实断言为字符串,但模板 assert_fact 需要一个关键字参数列表,符合 documentation and the examples.

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

template.assert_fact(**Dict)  # kwargs expansion

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

env.assert_string('(person (name "John") (surname "Doe"))')