我开发了通过pyswip连接到Prolog的python应用程序。
以下是我执行的Prolog部分的示例。
init( Board) :-
Board = b(n,x,n,x,n,x,n,x,x,n,x,n,x,n,x,n,n,x,n,x,n,x,n,x,e,n,e,n,e,n,e,n,n,e,n,e,n,e,n,e,o,n,o,n,o,n,o,n,n,o,n,o,n,o,n,o,o,n,o,n,o,n,o,n).
执行以下代码时
soln = list(self.prolog.query("init(B)"))
print(soln[0])
soln [0]是
<'B': 'Functor6905740'>
如何获得b(n,x,n,x,...)而不是Functor参考?
聚苯乙烯。我使用所有64位:Python 2.7,SWI-Prolog,pyswip,Visual Studio 2013
答案 0 :(得分:0)
我找到了自己的解决方案 我导入Functor,Variable,Query
from pyswip import Prolog, Functor, Variable, Query
要获得仿函数值,我使用
init = Functor("init", 1)
B = Variable()
soln = Query(init(B))
while soln.nextSolution():
b64 = B.get_value()
self.Board_pl = "b("
for i in range (0, 64):
self.Board_pl += (str(b64.args[i]) + ",")
self.Board_pl += (str(b64.args[63]) + ")")
print(Board)
然后,程序将打印
b(n,x,n,x,n,x,n,x,x,n,x,n,x,n,x,n,n,x,n,x,n,x,n,x,e,n,e,n,e,n,e,n,n,e,n,e,n,e,n,e,o,n,o,n,o,n,o,n,n,o,n,o,n,o,n,o,o,n,o,n,o,n,o,n)