Prolog中事实的持久性 [英] Persistence of facts in Prolog
问题描述
我是 Prolog 的新手,我正在使用 SWI-Prolog v6.6 将断言存储在 *.pl
文件中.
I'm kinda new in Prolog and I'm using SWI-Prolog v6.6 to storage asserts in a *.pl
file.
:- dynamic fact/2.
assert(fact(fact1,fact2)).
使用上面的代码,我可以进行断言并且工作正常,但问题是当我关闭 SWI-Prolog 并再次打开 *.pl
文件时,我所做的断言是走了……
With the code above I can make asserts and it works fine, but the problem is when I close SWI-Prolog and I open the *.pl
file again, the asserts I've made are gone...
有没有办法做出断言,即使我退出 Prolog 进程,这些断言也会被存储?
Is there a way to make asserts and those get stored even if I exit the Prolog process?
抱歉我的英语不好,谢谢!(:
Sorry about my bad english and Thanks! (:
推荐答案
保存状态有一定的限制,另见 最近的讨论 在 SWI-Prolog 邮件列表中.
Saving state has certain limitations, also see the recent discussion on the SWI-Prolog mailing list.
我认为在 SWI-Prolog 上持久存储事实的最简单方法是使用 persistency
库.为此,我将按以下方式重写您的代码:
I think the easiest way to persistently store facts on SWI-Prolog is to use the persistency
library. For that I would rewrite your code in the following way:
:- use_module(library(persistency)).
:- persistent fact(fact1:any, fact2:any).
:- initialization(init).
init:-
absolute_file_name('fact.db', File, [access(write)]),
db_attach(File, []).
您现在可以使用 assert_fact/2
、retract_fact/2
和 retractall_fact/2
添加/删除事实.
You can now add/remove facts using assert_fact/2
, retract_fact/2
, and retractall_fact/2
.
退出 Prolog 后,断言的事实会自动保存到 fact.db
.
Upon exiting Prolog the asserted facts are automatically saved to fact.db
.
示例用法:
$ swipl my_facts.pl
?- assert_fact(some(fact), some(other,fact)).
true.
?- halt.
$ swipl my_facts.pl
?- fact(X, Y).
X = some(fact),
Y = some(other, fact).
这篇关于Prolog中事实的持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!