如何使用pyswip在Prolog中获取Functor字符串 [英] How to get Functor string in Prolog using pyswip

查看:27
本文介绍了如何使用pyswip在Prolog中获取Functor字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了通过 pyswip 连接到 Prolog 的 python 应用程序.

I develop python app which connect to Prolog via pyswip.

以下是我执行的 Prolog 部分的示例.

The following is an example of Prolog part that I executed.

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])

溶液[0]是

<'B': 'Functor6905740'>

如何获得 b(n,x,n,x,...) 而不是 Functor 引用?

How can I get b(n,x,n,x,...) instead of Functor reference?

附言.我使用所有 64 位:Python 2.7、SWI-Prolog、pyswip、Visual Studio 2013

Ps. I use all 64 bit: Python 2.7, SWI-Prolog, pyswip, Visual Studio 2013

推荐答案

我找到了自己的解决方案.
我导入函子、变量、查询

I found my own solution.
I import Functor, Variable, Query

from pyswip import Prolog, Functor, Variable, Query

为了得到函子的值,我使用

To get the functor value, I use

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)

这篇关于如何使用pyswip在Prolog中获取Functor字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆