【问题标题】:CLIPS Beginner: How to add Python dictionary data into CLIPS facts using clipspyCLIPS 初学者:如何使用 clipspy 将 Python 字典数据添加到 CLIPS 事实中
【发布时间】:2021-09-11 21:32:40
【问题描述】:

我想在 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

【问题讨论】:

    标签: python clips clipspy


    【解决方案1】:

    您将事实声明为字符串,但模板 assert_fact 需要按照 documentationexamples 的关键字参数列表。

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

    template.assert_fact(**Dict)  # kwargs expansion
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-26
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多