如何在pyswip中生成子句

2024-06-09 01:41:24 发布

您现在位置:Python中文网/ 问答频道 /正文

这里有谁能帮我用pyswip在python中生成prolog子句吗

数据库:

man(peter).
woman(adam).
man(jonathan).
man(paul).
woman(cloe).
father(jonathan, peter).
father(pierre, adam).
brother(pierre, paul).
father(pierre, cloe).

这些是函数

^{pr2}$

如何通过pyswip在python中定义这些prolog函数


Tags: 函数数据库prologpeterbrother子句adamjonathan
1条回答
网友
1楼 · 发布于 2024-06-09 01:41:24

我现在没有时间做详细的回答,稍后我会更新它,但是这里有一个简单的例子,我用python语言对一个播放reversi的prolog程序做了一个接口。在

#!/usr/bin/python

import sys
from pyswip import Prolog, Functor, Variable, Query

prolog = Prolog()
prolog.consult('./reversi_game.pl')
prolog.consult('./alphabeta.pl')

start_board = Functor("startBoard", 1)
b = Variable()
start_board_query = Query(start_board(b))
start_board_query.nextSolution()
print()
print_board(list(b.get_value())) # an 8*8 grid filled with 0 except at the 4 center squares that have x's and o's
start_board_query.closeQuery()

set_to_x = Functor("setToX", 1)
xp = Variable()
set_player_query = Query(set_to_x(xp))
set_player_query.nextSolution()
x_player = xp.get_value()
print()
print(x_player) # 'x'
set_player_query.closeQuery()

这是怎么回事?要定义谓词接口,需要创建一个Functor,给它一个字符串,该字符串是Prolog中谓词的名称及其arity,您可以根据需要创建任意多个变量,并将它们传递给Functor,从中创建一个查询。在

然后,您可以继续对Query对象调用nextSolution(),时间长短取决于您需要多少个解决方案,如果我没记错的话,当它失败并且停止提供任何解决方案时,结果将是None。然后使用get_value()函数提取谓词变量的值。在

您也可以查看: https://github.com/yuce/pyswip/tree/master/examples

希望有帮助。在

编辑时间:

我知道这个“承诺更详细的答案”现在有点晚了,但无论如何。在

相关问题 更多 >