Prolog在分离的数据库文本文件中插入,修改和删除事实 [英] Prolog insert, modify and delete facts on a separated database text file

查看:491
本文介绍了Prolog在分离的数据库文本文件中插入,修改和删除事实的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大量事实的prolog数据库文件 knowledge.pl 。例如:

  father_of(joe,paul)。 
father_of(joe,mary)。
mother_of(jane,paul)。
mother_of(jane,mary)。
男(保罗)。
male(joe)。
女性(玛丽)。
女(jane)。 % 等等。

每次我的程序重新执行时,都会查阅这个文件

我想能够通过直接在数据库文本中写入或删除来插入,修改和删除我想要的事实(其中一些是直接的,一些满足某些特定条件的事情)文件



类似assertz,retract和retractall,但修改此文本文件,以便更改将永久保留。



我可以如何做呢?

解决方案

您可以创建所需的事实并将它们写入或修改数据库,然后将其保存在文件中。



区别是第一种方法将加载旧文件的db,而第二种方法



从你写这个问题的方式来看,我假设你想做第二个;为此,您应该:



1)声明要更改为 dynamic
2)在执行期间断言,撤销等
3)将新数据库写入文件。您可以使用上市/ 1



要写你可以做类似的事情:

  tell(knowledge),
。 ....
告诉。

,或者您可以使用其他 io谓词。也许使用 set_prolog_IO / 3 是最简单的方法。 p>

现在,如果你想要第一个,你应该构造谓词(可能使用 univ操作符)或其他字符串操作谓词,然后将其写入文件



编辑:



有列表/ 0,但会列出所有加载的谓词(你可能不想要的东西)。
一些搜索后,我发现 source_file / 2 ;所以你可以做像

  findall(X,source_file(X,FileName),L)。 

请注意,source_file / 2需要绝对文件名。您可以使用 absolute_file_name / 2 获取它
source_file / 2格式的谓词是有点奇怪(我期待的东西像foo / 1),但它看起来像你可以给它/ 1和它的工作正常,所以你可以做一些像:

  save(FileName): -  
absolute_file_name(FileName,Abs),
findall(X,source_file(X,Abs) ,L),
tell(FileName),
maplist(listing,L),

另一方面,你总是可以有一个列表,文件


I have a prolog database file with lots of facts knowledge.pl. For example:

father_of(joe,paul).
father_of(joe,mary).
mother_of(jane,paul).
mother_of(jane,mary).
male(paul).
male(joe).
female(mary).
female(jane). % and so on.

This file is consulted (consult/1) every time my program is executed again.

I would like to be able to insert, modify and delete the facts I want (some of them directly, some others that meet some specific conditions) by writing or deleting directly within this database text file.

something like assertz, retract and retractall but modifying this text file so that the changes remain permanently there.

how can I do it?

解决方案

you can either create the facts you want and write them to the (same) file or modify the database and then save it in the file.

the difference is that with the first approach you will have the db of the old file loaded while the second approach will change it during execution.

From the way you phrased the question I assume that you want to do the second; to do this you should:

1)declare all the predicates that you want to change as dynamic 2)assert,retract etc during execution 3)write the new database to the file. you can use listing/1

To write you can do something like:

tell(knowledge),
.....
told.

or you can use some other io predicates. maybe using set_prolog_IO/3 would be the simplest way.

Now, if you wanted the first, you should construct the predicates (probably using the univ operator) or other string manipulation predicates and then write them to a file

EDIT:

there is listing/0 but that will list all the predicates loaded (something you might not want). after some searching I found source_file/2; so you can do something like

findall(X,source_file(X,FileName),L).

note that source_file/2 requires the absolute filename. you can use absolute_file_name/2 to get it the way source_file/2 formats the predicate is a bit weird (i was expecting something like foo/1) but it looks like you can give it to listing/1 and it works fine so you can do something like:

save(FileName):-
    absolute_file_name(FileName,Abs),
    findall(X,source_file(X,Abs),L),
    tell(FileName),
    maplist(listing,L),
    told.

on the other hand, you can always have a list with the predicates you want to store somewhere in the file

这篇关于Prolog在分离的数据库文本文件中插入,修改和删除事实的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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