如何将跟踪输出重定向到文件

Sta*_*123 2 prolog swi-prolog

我正在跟踪序言程序

1 ?- trace.
true.

[trace] 1 ?- solve.
   Call: (7) solve ? 
Run Code Online (Sandbox Code Playgroud)

我也试过

tell('trace_op.txt').
Run Code Online (Sandbox Code Playgroud)

该文件已创建但为空

现在的痕迹真的很多。我想将输出重定向到一个文件 我们可以将它重定向到一个文件吗?

Guy*_*der 7

在使用 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).
Run Code Online (Sandbox Code Playgroud)

打开SWI-Prolog顶层并使用consult/1加载文件

consult("C:/ ... /trace_example.pl").
Run Code Online (Sandbox Code Playgroud)

注意目录分隔符是 / 不是 \。如有必要,请更改它们。

由于 Windows 上的 SWI-Prolog 终端将默认使用带有跟踪的颜色,这会将不需要的转义序列添加到输出文件中,因此需要运行set_prolog_flag/2以关闭颜色。

?- set_prolog_flag(color_term,false).
true.
Run Code Online (Sandbox Code Playgroud)

验证终端没有使用颜色。

?- current_prolog_flag(color_term,X).
X = false.
Run Code Online (Sandbox Code Playgroud)

快速运行以验证谓词和事实是否有效

?- ancestor(ann,henry).
true ;
false.
Run Code Online (Sandbox Code Playgroud)

现在将当前工作目录设置为将创建输出文件的位置。

?- working_directory(_,"C:/Users/Eric/Documents/Prolog").
Run Code Online (Sandbox Code Playgroud)

并验证更改是否发生

?- working_directory(CWD,CWD).
CWD = 'c:/users/eric/documents/prolog/'.
Run Code Online (Sandbox Code Playgroud)

因为我不想为每个跟踪输出按空格键,所以我使用leash/1禁用所有调试端口的用户交互

?- leash(-all).
Run Code Online (Sandbox Code Playgroud)

并且由于我想查看所有调试端口的所有输出,因此我将它们全部启用为visible/1

?- visible(+all).
Run Code Online (Sandbox Code Playgroud)

启用将屏幕复制到文件

?- protocol("./trace_output.txt").
Run Code Online (Sandbox Code Playgroud)

启动追踪器

?- trace.
Run Code Online (Sandbox Code Playgroud)

并运行要跟踪的查询

?- 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.
Run Code Online (Sandbox Code Playgroud)

结束追踪

?- nodebug.
Run Code Online (Sandbox Code Playgroud)

并结束将屏幕复制到文件

?- noprotocol.
Run Code Online (Sandbox Code Playgroud)

现在打开文件 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.  
Run Code Online (Sandbox Code Playgroud)