我正在寻找一个prolog程序
1 ?- trace.
true.
[trace] 1 ?- solve.
Call: (7) solve ?
我也试过
tell('trace_op.txt').
文件已创建,但为空
现在迹线真的很多。我想将输出重定向到文件 我们可以将其重定向到文件吗?
答案 0 :(得分:3)
在使用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").
N.B。目录分隔符是/不是\。如有必要,请更改它们。
由于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.
答案 1 :(得分:2)
如果您使用的是Linux,则可以使用tee
命令:
$ swipl 2>&1 | tee swipl.log
...
1 ?- trace.
true.
[trace] 1 ?- solve.
Call: (7) solve ?
...
tee
命令将所有标准输入发送到标准输出并并行发送到指定文件。 2>&1
确保您还可以捕获标准输出中的任何标准错误,以便它显示在swipl.log
文件中。
在Windows中,如果您使用PowerShell
,则可以使用类似的Tee-Object
命令:
swipl-win | Tee-Object -file swipl.log
我假设swipl-win
是Windows中SWI Prolog的命令行程序,它存在于PowerShell
的程序路径中。
当您退出swipl
时,您可以看到swipl.log
中发生的所有事情。