2

这是我的序言数据库代码。

:-
    dynamic myTable/2.

init :-
    removeAll,
    asserta(myTable('avalue', 'another value')),
    asserta(myTable('avalue1', 'another value 1')),
    asserta(myTable('avalue2', 'another value 2')),
    asserta(myTable('avalue3', 'another value 3')),
    asserta(myTable('avalue4', 'another value 4')).

read(Col1, Col2) :-
    myTable(Col1, Col2).

saveQueries(FileName) :-
    tell(FileName).

stopSavingQueries :-
    told.

我想开始将 prolog 输出保存到文件中。对动态数据库进行一些查询,这些查询应该保存到文件中,然后停止保存查询。它看起来像这样

?- init.
true.

?- saveQueries('queries.txt').
true.

?- read(Col1, Col2).
...
?- stopSavingQueries.
true.

当我运行这个代码文件时queries.txt创建。当我运行时,read(Col1, Col2).我在控制台中看到输出并且文件queries.txt保持为空。

4

1 回答 1

2

谷歌搜索了一段时间后,我找到了这个解决方案。

saveQueries(FileName) :-
    protocol(FileName).

stopQueriesSaving :-
    noprotocol.

然后我可以这样做

?- saveQueries('queries.txt').
true.

/* execute some queries here */

?- stopQueriesSaving.
true.

执行这些命令后,我有一个queries.txt包含所有查询及其结果的文件。

于 2016-11-20T16:34:43.670 回答