在Prolog中存储事实的问题 [英] Issues with storage of facts in Prolog

查看:686
本文介绍了在Prolog中存储事实的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Prolog中有新意,而且我使用SWI-Prolog v6.6来存储我的* .pl文件中的断言。

 : - 动态事实/ 2。 

assert(fact(fact1,fact2))。

使用上面的代码,我可以做出断言,它的工作正常,但问题是当我关闭SWI --Prolog,我再次打开* .pl文件,我所做的断言已经消失了。



有一种方法可以断言,甚至存储甚至如果我关闭SWI-Prolog?



抱歉我的英文不好,谢谢! (:

解决方案

保存状态具有一定的限制,另请参阅最近的讨论在SWI-Prolog邮件列表中。



我认为在SWI-Prolog上持久存储事实的最简单的方法是使用持久化库,为此,我将以以下方式重写代码:

 : -  use_module(library(persistency))

: - 持久的事实(事实1:任何事实2:任何)

:初始化init)

init: -
absolute_file_name('fact.db',File,[access(write)]),
db_attach(File,[])

您现在可以使用 assert_fact / 2添加/删除事实 retract_fact / 2 retractall_fact / 2



退出Prolog断言例如:


$



b $ b

  $ swipl my_facts.pl 
? - assert_fact(some(fact),some(other,fact))。
true。
? - 停止。
$ swipl my_facts.pl
? - 事实(X,Y)。
X = some(fact),
Y = some(other,fact)。


I'm kinda new in Prolog and I'm using SWI-Prolog v6.6 to storage asserts in my *.pl file.

:- dynamic fact/2.

assert(fact(fact1,fact2)).

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...

There is a way to make asserts and those get stored even if I close the SWI-Prolog?

Sorry about my bad english and Thanks! (:

解决方案

Saving state has certain limitations, also see the recent discussion on the SWI-Prolog mailing list.

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, []).

You can now add/remove facts using assert_fact/2, retract_fact/2, and retractall_fact/2.

Upon exiting Prolog the asserted facts are automatically saved to fact.db.

Example usage:

$ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆