我正在跟踪序言程序
1 ?- trace.
true.
[trace] 1 ?- solve.
Call: (7) solve ?
我也试过
tell('trace_op.txt').
文件已创建但为空
现在痕迹真的很多行。我想将输出重定向到一个文件 我们可以将它重定向到一个文件吗?
最佳答案
在使用 SWI-Prolog 的 Windows 上,您可以使用 protocol/1
protocol/1会将屏幕上的输出复制到文件中。所以如果你运行 trace/0并且输出到屏幕,副本将被发送到文件。为了简化必须为 protocol/1 编写整个路径,我发现使用 working_directory/2 设置当前工作目录更容易。然后仅使用 protocol/1 设置特定文件。
例子
对于这个例子创建一个文件,例如
trace_example.pl
并添加一些事实和谓词来演示跟踪。
parent(ann,helen).
parent(helen,henry).
parent(henry,mary).
ancestor(X,Y) :- parent(X,Y).
ancestor(X,Y) :- parent(X,Z),
ancestor(Z,Y).
打开 SWI-Prolog 顶层并使用 consult/1加载文件
consult("C:/ ... /trace_example.pl").
注意目录分隔符是/而不是\。如有必要,请更改它们。
由于 Windows 上的 SWI-Prolog 终端将默认使用带有跟踪的颜色,这将向输出文件添加不需要的转义序列 set_prolog_flag/2需要运行以关闭颜色。
?- set_prolog_flag(color_term,false).
true.
验证终端未使用颜色。
?- current_prolog_flag(color_term,X).
X = false.
快速运行以验证谓词和事实是否有效
?- ancestor(ann,henry).
true ;
false.
现在将当前工作目录设置为将创建输出文件的位置。
?- working_directory(_,"C:/Users/Eric/Documents/Prolog").
并验证是否发生了变化
?- working_directory(CWD,CWD).
CWD = 'c:/users/eric/documents/prolog/'.
因为我不想为每个跟踪输出按空格键,所以我使用 leash/1 禁用了所有调试端口的用户交互。
?- leash(-all).
因为我想查看所有调试端口的所有输出,所以我使用 visible/1 启用它们
?- visible(+all).
启用屏幕复制到文件
?- protocol("./trace_output.txt").
启动跟踪器
?- trace.
并运行要跟踪的查询
?- ancestor(ann,henry).
Call: (8) ancestor(ann, henry) ? creep
Call: (9) parent(ann, henry) ? creep
Fail: (9) parent(ann, henry) ? creep
Redo: (8) ancestor(ann, henry) ? creep
Call: (9) parent(ann, _1124) ? creep
Exit: (9) parent(ann, helen) ? creep
Call: (9) ancestor(helen, henry) ? creep
Call: (10) parent(helen, henry) ? creep
Exit: (10) parent(helen, henry) ? creep
Exit: (9) ancestor(helen, henry) ? creep
Exit: (8) ancestor(ann, henry) ? creep
true ;
Redo: (9) ancestor(helen, henry) ? creep
Call: (10) parent(helen, _1124) ? creep
Exit: (10) parent(helen, henry) ? creep
Call: (10) ancestor(henry, henry) ? creep
Call: (11) parent(henry, henry) ? creep
Fail: (11) parent(henry, henry) ? creep
Redo: (10) ancestor(henry, henry) ? creep
Call: (11) parent(henry, _1124) ? creep
Exit: (11) parent(henry, mary) ? creep
Call: (11) ancestor(mary, henry) ? creep
Call: (12) parent(mary, henry) ? creep
Fail: (12) parent(mary, henry) ? creep
Redo: (11) ancestor(mary, henry) ? creep
Call: (12) parent(mary, _1124) ? creep
Fail: (12) parent(mary, _1124) ? creep
Fail: (11) ancestor(mary, henry) ? creep
Fail: (10) ancestor(henry, henry) ? creep
Fail: (9) ancestor(helen, henry) ? creep
Fail: (8) ancestor(ann, henry) ? creep
false.
结束追踪
?- nodebug.
并结束屏幕复制到文件
?- noprotocol.
现在打开文件C:\Users\Eric\Documents\Prolog\trace_output.txt
true.
10 ?- trace.
true.
[trace] 10 ?- ancestor(ann,henry).
Call: (8) ancestor(ann, henry)
Unify: (8) ancestor(ann, henry)
Call: (9) parent(ann, henry)
Fail: (9) parent(ann, henry)
Redo: (8) ancestor(ann, henry)
Unify: (8) ancestor(ann, henry)
Call: (9) parent(ann, _6466)
Unify: (9) parent(ann, helen)
Exit: (9) parent(ann, helen)
Call: (9) ancestor(helen, henry)
Unify: (9) ancestor(helen, henry)
Call: (10) parent(helen, henry)
Unify: (10) parent(helen, henry)
Exit: (10) parent(helen, henry)
Exit: (9) ancestor(helen, henry)
Exit: (8) ancestor(ann, henry)
true ;
Redo: (9) ancestor(helen, henry)
Unify: (9) ancestor(helen, henry)
Call: (10) parent(helen, _6466)
Unify: (10) parent(helen, henry)
Exit: (10) parent(helen, henry)
Call: (10) ancestor(henry, henry)
Unify: (10) ancestor(henry, henry)
Call: (11) parent(henry, henry)
Fail: (11) parent(henry, henry)
Redo: (10) ancestor(henry, henry)
Unify: (10) ancestor(henry, henry)
Call: (11) parent(henry, _6466)
Unify: (11) parent(henry, mary)
Exit: (11) parent(henry, mary)
Call: (11) ancestor(mary, henry)
Unify: (11) ancestor(mary, henry)
Call: (12) parent(mary, henry)
Fail: (12) parent(mary, henry)
Redo: (11) ancestor(mary, henry)
Unify: (11) ancestor(mary, henry)
Call: (12) parent(mary, _6466)
Fail: (12) parent(mary, _6466)
Fail: (11) ancestor(mary, henry)
Fail: (10) ancestor(henry, henry)
Fail: (9) ancestor(helen, henry)
Fail: (8) ancestor(ann, henry)
false.
[trace] 11 ?- nodebug.
true.
12 ?- noprotocol.
关于prolog - 如何将跟踪输出重定向到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40086736/