CLIPS Python3 CLIPSError
CLIPS Python3 CLIPSError
我 运行 遇到了一些 CLIPSpy 代码的问题。我已将问题缩小到 CLIPS_CONSTRUCTS.encode() 或 environment.load(constructs_file.name) 的编码方法。我试图捕捉的目标是在油温高于 32 且油压高于 0 时触发规则。我附上了我正在使用的 SampleData.csv。这是我论文研究的一部分,我想感谢所有帮助我的人!
- OS 和版本:Windows 10 64 位
- Python版本:3.7.2
- 库和版本(通过 pip 列表)
cffi 1.11.5
剪辑间谍 0.3.0
点 18.1
pycparser 2.19
安装工具 40.6.2
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()
这是 NamedTemporaryFile
对 Windows 的限制 here。
您可以使用 mkstemp
或您在完成后自行删除的常规文件来解决此问题。
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)
使用此代码成功运行:
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)
由于某种原因,代码在文件描述符方面存在问题,因为它没有写入方法。稍微更改了代码,砰!有效!
我 运行 遇到了一些 CLIPSpy 代码的问题。我已将问题缩小到 CLIPS_CONSTRUCTS.encode() 或 environment.load(constructs_file.name) 的编码方法。我试图捕捉的目标是在油温高于 32 且油压高于 0 时触发规则。我附上了我正在使用的 SampleData.csv。这是我论文研究的一部分,我想感谢所有帮助我的人!
- OS 和版本:Windows 10 64 位
- Python版本:3.7.2
- 库和版本(通过 pip 列表)
cffi 1.11.5
剪辑间谍 0.3.0
点 18.1
pycparser 2.19
安装工具 40.6.2
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()
这是 NamedTemporaryFile
对 Windows 的限制 here。
您可以使用 mkstemp
或您在完成后自行删除的常规文件来解决此问题。
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)
使用此代码成功运行:
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)
由于某种原因,代码在文件描述符方面存在问题,因为它没有写入方法。稍微更改了代码,砰!有效!