Skip to content

Commit

Permalink
Updated examples, setup and Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
yuce committed May 23, 2018
1 parent 8aa332e commit 9ecd651
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 137 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ __pycache__
*.BAC
.pytest_cache
dist/
pyswip.egg-info/
pyswip.egg-info/
build/
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
.PHONY: cover test
.PHONY: build_posix build_win cover test

build_posix:
python setup.py sdist
python setup.py bdist_wheel --universal

build_win:
python setup.py bdist_msi

cover:
py.test --cov=pyswip tests

test:
py.test tests --verbose

4 changes: 0 additions & 4 deletions examples/coins/coins.pl
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,4 @@
VA + VB + VC + VD + VE #= Total,

label(S).

% :- findall(S, coins(S, 100, 500), Ss), halt.
%:- coins(S, 100, 500), writeln(S), fail.


16 changes: 6 additions & 10 deletions examples/coins/coins.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# -*- coding: utf-8 -*-


# pyswip -- Python SWI-Prolog bridge
# Copyright (c) 2007-2012 Yüce Tekol
# Copyright (c) 2007-2018 Yüce Tekol
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,25 +21,22 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


# 100 coins must sum to $5.00


from pyswip.prolog import Prolog


def main():
prolog = Prolog()
prolog.consult("coins.pl")
count = int(raw_input("How many coins (default: 100)? ") or 100)
total = int(raw_input("What should be the total (default: 500)? ") or 500)
count = int(input("How many coins (default: 100)? ") or 100)
total = int(input("What should be the total (default: 500)? ") or 500)
for i, soln in enumerate(prolog.query("coins(S, %d, %d)." % (count,total))):
# [1,5,10,50,100]
S = zip(soln["S"], [1, 5, 10, 50, 100])
print i,
print(i, end=" ")
for c, v in S:
print "%dx%d" % (c,v),
print
print("%dx%d" % (c,v), end=" ")
print()
list(prolog.query("coins(S, %d, %d)." % (count,total)))


Expand Down
8 changes: 2 additions & 6 deletions examples/create_term.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# -*- coding:utf-8 -*-


# pyswip -- Python SWI-Prolog bridge
# Copyright (c) 2007-2012 Yüce Tekol
# Copyright (c) 2007-2018 Yüce Tekol
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,7 +21,6 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


from pyswip.core import *
from pyswip.prolog import Prolog

Expand All @@ -39,13 +37,11 @@ def main():

PL_put_atom_chars(a1, "gnu")
PL_put_integer(a2, 50)
#PL_cons_functor(t, animal2, a1, a2)
PL_cons_functor_v(t, animal2, a1)
PL_cons_functor_v(ta, assertz, t)
PL_call(ta, None)

# prolog.assertz("animal(gnu, 50)")
print list(prolog.query("animal(X,Y)", catcherrors=True))
print(list(prolog.query("animal(X,Y)", catcherrors=True)))


if __name__ == "__main__":
Expand Down
18 changes: 8 additions & 10 deletions examples/draughts/puzzle1.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# -*- coding: utf-8 -*-


# pyswip -- Python SWI-Prolog bridge
# Copyright (c) 2007-2012 Yüce Tekol
# Copyright (c) 2007-2018 Yüce Tekol
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,7 +21,6 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


# This example is adapted from http://eclipse.crosscoreop.com/examples/puzzle1.pl.txt

# "Twelve draught pieces are arranged in a square frame with four on
Expand All @@ -37,22 +35,22 @@
# are four guards watching each wall. How can they be rearranged such
# that there are five watching each wall?"


from pyswip.prolog import Prolog


def main():
prolog = Prolog()
prolog.consult("puzzle1.pl")

for soln in prolog.query("solve(B)."):
#B = eval(soln["B"])
B = soln["B"]

# [NW,N,NE,W,E,SW,S,SE]
print "%d %d %d" % tuple(B[:3])
print "%d %d" % tuple(B[3:5])
print "%d %d %d" % tuple(B[5:])
cont = raw_input("Press 'n' to finish: ")
print("%d %d %d" % tuple(B[:3]))
print("%d %d" % tuple(B[3:5]))
print("%d %d %d" % tuple(B[5:]))

cont = input("Press 'n' to finish: ")
if cont.lower() == "n": break


Expand Down
28 changes: 4 additions & 24 deletions examples/father.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# -*- coding: utf-8 -*-


# pyswip -- Python SWI-Prolog bridge
# Copyright (c) 2007-2012 Yüce Tekol
# Copyright (c) 2007-2018 Yüce Tekol
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,7 +21,6 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


from pyswip import *


Expand All @@ -33,12 +31,6 @@ def main():
mother = Functor("mother", 2)
assertz = Functor("assertz", 1)

#call(assertz(father("john", "mich")))
#call(assertz(father("john", "gina")))
#call(assertz(father("hank", "cloe")))
#call(assertz(mother("jane", "mich")))
#call(assertz(mother("jane", "gina")))

p.assertz("father(john,mich)")
p.assertz("father(john,gina)")
p.assertz("mother(jane,mich)")
Expand All @@ -48,27 +40,15 @@ def main():
listing = Functor("listing", 1)
call(listing(father))

#print list(p.query("listing(father))"))

q = Query(father("john",Y), mother(Z,Y))
while q.nextSolution():
print Y.value, Z.value
#print X.value, "is the father of", Y.value
#print Z.value, "is the mother of", Y.value
print(Y.value, Z.value)
q.closeQuery() # Newer versions of SWI-Prolog do not allow nested queries

print "\nQuery with strings\n"
print("\nQuery with strings\n")
for s in p.query("father(john,Y),mother(Z,Y)"):
#print s["X"], "is the father of", s["Y"]
#print s["Z"], "is the mother of", s["Y"]
print s["Y"], s["Z"]

#print "running the query again"
#q = Query(father(X, Y))
#while q.nextSolution():
# print X.value, "is the father of", Y.value
print(s["Y"], s["Z"])


if __name__ == "__main__":
main()

Expand Down
26 changes: 12 additions & 14 deletions examples/hanoi/hanoi.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# -*- coding: utf-8 -*-


# pyswip -- Python SWI-Prolog bridge
# Copyright (c) 2007-2012 Yüce Tekol
# Copyright (c) 2007-2018 Yüce Tekol
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,7 +21,6 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


from collections import deque

from pyswip.prolog import Prolog
Expand All @@ -34,7 +32,6 @@ def __init__(self, fun):
self.fun = fun

def notify(self, t):
#return not self.fun(getList(t))
return not self.fun(t)
notify.arity = 1

Expand All @@ -52,7 +49,8 @@ def __init__(self, N=3, interactive=False):
def move(self, r):
if not self.started:
self.step += 1
self.draw()
if self.draw():
return True
self.started = True
disks = self.disks
disks[str(r[1])].append(disks[str(r[0])].pop())
Expand All @@ -61,22 +59,22 @@ def move(self, r):

def draw(self):
disks = self.disks
print "\n Step", self.step
print("\n Step", self.step)
for i in range(self.N):
n = self.N - i - 1
print " ",
print(" ", end=" ")
for pole in ["left", "center", "right"]:
if len(disks[pole]) - n > 0:
print disks[pole][n],
print(disks[pole][n], end=" ")
else:
print " ",
print
print "-"*9
print " ", "L", "C", "R"
print(" ", end=" ")
print()
print("-" * 9)
print(" ", "L", "C", "R")
if self.interactive:
cont = raw_input("Press 'n' to finish: ")
cont = input("Press 'n' to finish: ")
return cont.lower() == "n"


def main():
N = 3
Expand Down
8 changes: 2 additions & 6 deletions examples/hanoi/hanoi_simple.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# -*- coding: utf-8 -*-


# pyswip -- Python SWI-Prolog bridge
# Copyright (c) 2007-2012 Yüce Tekol
# Copyright (c) 2007-2018 Yüce Tekol
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,17 +21,14 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


from pyswip.prolog import Prolog
from pyswip.easy import getList, registerForeign


N = 3 # Number of disks


def main():
def notify(t):
print "move disk from %s pole to %s pole." % tuple(t)
print("move disk from %s pole to %s pole." % tuple(t))
notify.arity = 1

prolog = Prolog()
Expand Down
47 changes: 24 additions & 23 deletions examples/knowledgebase.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# -*- coding: utf-8 -*-


# pyswip -- Python SWI-Prolog bridge
# Copyright (c) 2007-2012 Yüce Tekol
# Copyright (c) 2007-2018 Yüce Tekol
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,35 +21,37 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


from pyswip import *


p = Prolog()
def main():
p = Prolog()

assertz = Functor("assertz")
parent = Functor("parent", 2)
test1 = newModule("test1")
test2 = newModule("test2")
assertz = Functor("assertz")
parent = Functor("parent", 2)
test1 = newModule("test1")
test2 = newModule("test2")

call(assertz(parent("john", "bob")), module=test1)
call(assertz(parent("jane", "bob")), module=test1)
call(assertz(parent("john", "bob")), module=test1)
call(assertz(parent("jane", "bob")), module=test1)

call(assertz(parent("mike", "bob")), module=test2)
call(assertz(parent("gina", "bob")), module=test2)
call(assertz(parent("mike", "bob")), module=test2)
call(assertz(parent("gina", "bob")), module=test2)

print "knowledgebase test1"
print("knowledgebase test1")

X = Variable()
q = Query(parent(X, "bob"), module=test1)
while q.nextSolution():
print X.value
q.closeQuery()
X = Variable()
q = Query(parent(X, "bob"), module=test1)
while q.nextSolution():
print(X.value)
q.closeQuery()

print "knowledgebase test2"
print("knowledgebase test2")

q = Query(parent(X, "bob"), module=test2)
while q.nextSolution():
print X.value
q.closeQuery()
q = Query(parent(X, "bob"), module=test2)
while q.nextSolution():
print(X.value)
q.closeQuery()

if __name__ == "__main__":
main()
Loading

0 comments on commit 9ecd651

Please sign in to comment.