Prolog 如何将跟踪输出重定向到文件
我正在跟踪一个prolog程序Prolog 如何将跟踪输出重定向到文件,prolog,swi-prolog,Prolog,Swi Prolog,我正在跟踪一个prolog程序 1 ?- trace. true. [trace] 1 ?- solve. Call: (7) solve ? 我也试过了 tell('trace_op.txt'). 文件已创建,但为空 现在痕迹真的有很多行了。我想将输出重定向到一个文件 我们可以将其重定向到文件吗?Linux 如果您使用的是Linux,则可以使用tee命令: $ swipl 2>&1 | tee swipl.log ... 1 ?- trace. true. [tr
1 ?- trace.
true.
[trace] 1 ?- solve.
Call: (7) solve ?
我也试过了
tell('trace_op.txt').
文件已创建,但为空
现在痕迹真的有很多行了。我想将输出重定向到一个文件
我们可以将其重定向到文件吗?Linux
如果您使用的是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对象
命令,该命令的工作原理类似:
swipl-win | Tee-Object -file swipl.log
我在这里假设,swipl-win
是Windows中SWI-Prolog的命令行程序,它存在于PowerShell的程序路径中
当您退出
swipl
时,您可以使用您可以使用的SWI Prolog在Windows上查看swipl.log
中发生的所有事情
将进入屏幕的输出复制到文件中。因此,如果您运行,并且输出进入屏幕,则会将副本发送到文件。为了简化为protocol/1编写整个路径的过程,我发现使用protocol/1设置当前工作目录更容易,然后只使用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 top level并用于加载文件
consult("C:/ ... /trace_example.pl").
注意:目录分隔符是/不是\。如有必要,请更换
由于Windows上的SWI Prolog终端将默认使用带有跟踪的颜色,这将向输出文件添加不需要的转义序列,因此需要运行以关闭颜色
?- 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/'.
因为我不想为每个跟踪输出按空格键,所以我使用禁用所有调试端口的用户交互
因为我想看到所有调试端口的所有输出,所以我使用
启用将屏幕复制到文件
?- 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\u 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.
您使用的是什么操作系统?我使用的是Windows 8PS C:\>swipl|Tee对象-文件swipl.log swipl:术语“swipl”无法识别为cmdlet、函数、脚本文件的名称,或者名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。在第1行,char:1+swipl|Tee对象-文件swipl.log+~~~~~~~+CategoryInfo:ObjectNotFound:(swipl:String)[],CommandNotFoundException+FullyQualifiedErrorId:CommandNotFoundException@Star123,对不起,
swipl
是在Linux中调用的SWI-Prolog命令。我只是在手册中查找答案时再次编辑了我的答案,Windows中的命令是swipl win.exe
(我相信)。您需要替换Windows中的内容,并确保它位于路径中,以便PowerShell可以找到它。这就是错误消息告诉您的。ANSI控制序列现在有一些颜色。这就是你想要的吗?-all
是蓝色的,+all
是黑色的。这会提高可读性吗?
?- noprotocol.
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.