首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CLIPS Python3 CLIPSError

CLIPS Python3 CLIPSError
EN

Stack Overflow用户
提问于 2019-01-12 01:32:28
回答 2查看 252关注 0票数 3

我遇到了一些CLIPSpy代码的问题。我已经将问题缩小到了CLIPS_CONSTRUCTS.encode()的编码方法或environment.load(constructs_file.name)。我试图捕捉的目标是,当油温高于32,油压超过0时,启动一条规则。我已经附上了我正在工作的SampleData.csv。这是我的论文研究的一部分,我想归功于所有的帮助我!

  • 操作系统和版本: Windows 10 64位
  • Python版本: 3.7.2
  • 库和版本(通过pip列表) cffi 1.11.5 剪贴画0.3.0 pip 18.1 2.19解析器2.19 setuptools 40.6.2

代码语言:javascript
复制
import sys
from tempfile import NamedTemporaryFile

import clips


CLIPS_CONSTRUCTS = """
(deftemplate oil-measure
  (slot utc-time (type STRING))
  (slot temperature (type INTEGER))
  (slot pressure (type INTEGER)))

(defrule oil-is-hot
  (oil-measure (temperature ?temp) (utc-time ?time))
  (test (> ?temp 32))
  =>
  (printout t ?time tab "temperature:" tab ?temp crlf))

(defrule pressure-is-high
  (oil-measure (pressure ?press&:(> ?press 0)) (utc-time ?time))
  =>
  (printout t ?time tab "pressure:" tab ?press crlf))
"""


def main():
    environment = clips.Environment()

    # use environment.load() to load constructs from a file
    with NamedTemporaryFile() as constructs_file:
        constructs_file.write(CLIPS_CONSTRUCTS.encode())
        constructs_file.flush()

        environment.load(constructs_file.name)

    # enable fact duplication as data has duplicates
    environment.eval("(set-fact-duplication TRUE)")

    # Template facts can be built from their deftemplate
    oil_measure_template = environment.find_template("oil-measure")

    for time, temp, press in get_data_frames(sys.argv[1]):
        new_fact = oil_measure_template.new_fact()

        # Template facts are represented as dictionaries
        new_fact["utc-time"] = time
        new_fact["temperature"] = int(temp)
        new_fact["pressure"] = int(press)

        # Add the fact into the environment Knowledge Base
        new_fact.assertit()

    # Fire all the rules which got activated
    environment.run()


def get_data_frames(file_path):
    """Parse a CSV file returning the dataframes."""
    with open(file_path) as data_file:
        return [l.strip().split(",") for i, l in enumerate(data_file) if i > 1]


if __name__ == "__main__":
    main()

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-01-13 10:22:35

这是NamedTemporaryFile在Windows文档这里上的一个限制。

您可以使用mkstemp或常规文件来解决这个问题,一旦完成,您就可以删除自己。

代码语言:javascript
复制
constructs_file, constructs_file_name = mkstemp()
constructs_file.write(CLIPS_CONSTRUCTS.encode())
constructs_file.close()

environment.load(constructs_file_name)
os.remove(constructs_file_name)
票数 1
EN

Stack Overflow用户

发布于 2019-01-13 18:50:56

使用以下代码使其工作:

代码语言:javascript
复制
constructs_file, constructs_file_name = mkstemp()
file = open(constructs_file, 'wb')
file.write(CLIPS_CONSTRUCTS.encode())
file.close()

environment.load(constructs_file_name)
os.remove(constructs_file_name)

由于某些原因,代码与文件描述符有问题,因为它没有写方法。把代码改了一点,砰!它起作用了!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54156047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档