2016-11-20 93 views
1

这是我的序言数据库代码。将查询结果写入序言中的文件

:- 
    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保持为空。

回答

1

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

saveQueries(FileName) :- 
    protocol(FileName). 

stopQueriesSaving :- 
    noprotocol. 

然后当我执行我有一个文件queries.txt它包含所有的查询和结果的命令,我可以做到这一点

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

/* execute some queries here */ 

?- stopQueriesSaving. 
true.