如何在pyswip中创建子句

时间:2019-01-29 18:40:37

标签: python prolog swi-prolog

这里有人可以帮助我使用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).

这些是函数

child(X, Y) :- father(Y,X).
son(X, Y) :- man(X) , father(Y, X).
daughter(X, Y) :- woman(X), father(Y, X).
brother(X, Y) :- man(X), father(Z, Y), father(Z, X).
sister(X, Y) :- woman(X), father(Z, Y), father(Z, X).

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

1 个答案:

答案 0 :(得分:1)

我现在没有时间做出详细的答复,我将在后面进行更新,但这是我对播放reversi的prolog程序所做的python接口的简单示例。

#!/usr/bin/python

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

#important lines that were missing
prolog = Prolog()
prolog.consult('./reversi_game.pl')

start_board = Functor("startBoard", 1)
b = Variable()
start_board_query = Query(start_board(b))
start_board_query.nextSolution()
board = list(b.get_value())
print()
print_board(board) # an 8*8 board 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()

希望这会有所帮助。

相关问题