【问题标题】:Write queries results into a file in prolog将查询结果写入 prolog 中的文件
【发布时间】:2016-11-20 14:49:07
【问题描述】:

这是我的 prolog 数据库代码。

:-
    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 仍然为空。

【问题讨论】:

    标签: database logging prolog swi-prolog prolog-toplevel


    【解决方案1】:

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

    saveQueries(FileName) :-
        protocol(FileName).
    
    stopQueriesSaving :-
        noprotocol.
    

    那我就可以了

    ?- saveQueries('queries.txt').
    true.
    
    /* execute some queries here */
    
    ?- stopQueriesSaving.
    true.
    

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

    【讨论】:

      猜你喜欢
      • 2016-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多