From 532f44adb8d58cc554d6ea0819959f5faed4778c Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Sun, 20 Oct 2024 09:13:44 +0300 Subject: [PATCH] Reorganized examples --- README.md | 19 +- examples/coins/coins.py | 7 +- examples/coins/coins_new.py | 3 +- examples/create_term.py | 4 +- examples/draughts/puzzle1.py | 5 +- examples/father.py | 10 +- examples/hanoi/hanoi.py | 5 +- examples/hanoi/hanoi_simple.py | 5 +- examples/register_foreign.py | 3 +- examples/register_foreign_simple.py | 7 +- examples/sendmoremoney/money.py | 5 +- examples/sendmoremoney/money_new.py | 3 +- examples/sudoku/sudoku.py | 5 +- examples/sudoku/sudoku_daily.py | 7 +- src/pyswip/examples/sudoku.py | 17 +- src/pyswip/prolog.py | 11 +- tests/examples/test_sudoku.py | 6 +- tests/test_examples.py | 291 +++------------------------- tests/test_foreign.py | 28 +-- tests/test_issues.py | 17 +- tests/test_prolog.py | 5 +- 21 files changed, 100 insertions(+), 363 deletions(-) diff --git a/README.md b/README.md index a1d99f0..1e7ad37 100644 --- a/README.md +++ b/README.md @@ -37,11 +37,10 @@ Thanks to all [contributors](CONTRIBUTORS.txt). ```python from pyswip import Prolog -prolog = Prolog() -prolog.assertz("father(michael,john)") -prolog.assertz("father(michael,gina)") -list(prolog.query("father(michael,X)")) == [{'X': 'john'}, {'X': 'gina'}] -for soln in prolog.query("father(X,Y)"): +Prolog.assertz("father(michael,john)") +Prolog.assertz("father(michael,gina)") +list(Prolog.query("father(michael,X)")) == [{'X': 'john'}, {'X': 'gina'}] +for soln in Prolog.query("father(X,Y)"): print(soln["X"], "is the father of", soln["Y"]) # michael is the father of john # michael is the father of gina @@ -52,8 +51,7 @@ Assuming the filename "knowledge_base.pl" and the Python is being run in the sam ```python from pyswip import Prolog -prolog = Prolog() -prolog.consult("knowledge_base.pl") +Prolog.consult("knowledge_base.pl") ``` ### Foreign Functions @@ -67,10 +65,9 @@ hello.arity = 1 registerForeign(hello) -prolog = Prolog() -prolog.assertz("father(michael,john)") -prolog.assertz("father(michael,gina)") -print(list(prolog.query("father(michael,X), hello(X)"))) +Prolog.assertz("father(michael,john)") +Prolog.assertz("father(michael,gina)") +print(list(Prolog.query("father(michael,X), hello(X)"))) ``` ### Pythonic interface (Experimental) diff --git a/examples/coins/coins.py b/examples/coins/coins.py index a1ae124..3061ff2 100644 --- a/examples/coins/coins.py +++ b/examples/coins/coins.py @@ -25,17 +25,16 @@ def main(): - prolog = Prolog() - prolog.consult("coins.pl", relative_to=__file__) + Prolog.consult("coins.pl", relative_to=__file__) 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))): + for i, soln in enumerate(Prolog.query("coins(S, %d, %d)." % (count, total))): S = zip(soln["S"], [1, 5, 10, 50, 100]) print(i, end=" ") for c, v in S: print(f"{c}x{v}", end=" ") print() - list(prolog.query(f"coins(S, {count}, {total}).")) + list(Prolog.query(f"coins(S, {count}, {total}).")) if __name__ == "__main__": diff --git a/examples/coins/coins_new.py b/examples/coins/coins_new.py index e12f08b..a435c52 100644 --- a/examples/coins/coins_new.py +++ b/examples/coins/coins_new.py @@ -25,8 +25,7 @@ def main(): - prolog = Prolog() - prolog.consult("coins.pl", relative_to=__file__) + Prolog.consult("coins.pl", relative_to=__file__) count = int(input("How many coins (default: 100)? ") or 100) total = int(input("What should be the total (default: 500)? ") or 500) coins = Functor("coins", 3) diff --git a/examples/create_term.py b/examples/create_term.py index 8bb33f0..0ff0135 100644 --- a/examples/create_term.py +++ b/examples/create_term.py @@ -26,8 +26,6 @@ def main(): - prolog = Prolog() - a1 = PL_new_term_refs(2) a2 = a1 + 1 t = PL_new_term_ref() @@ -42,7 +40,7 @@ def main(): PL_cons_functor_v(ta, assertz, t) PL_call(ta, None) - print(list(prolog.query("animal(X,Y)", catcherrors=True))) + print(list(Prolog.query("animal(X,Y)", catcherrors=True))) if __name__ == "__main__": diff --git a/examples/draughts/puzzle1.py b/examples/draughts/puzzle1.py index e44bfcb..e5345b1 100644 --- a/examples/draughts/puzzle1.py +++ b/examples/draughts/puzzle1.py @@ -37,10 +37,9 @@ def main(): - prolog = Prolog() - prolog.consult("puzzle1.pl", relative_to=__file__) + Prolog.consult("puzzle1.pl", relative_to=__file__) - for soln in prolog.query("solve(B)."): + for soln in Prolog.query("solve(B)."): B = soln["B"] # [NW,N,NE,W,E,SW,S,SE] diff --git a/examples/father.py b/examples/father.py index 7ce5a96..6d8ecdb 100644 --- a/examples/father.py +++ b/examples/father.py @@ -25,14 +25,12 @@ def main(): - p = Prolog() - father = Functor("father", 2) mother = Functor("mother", 2) - p.assertz("father(john,mich)") - p.assertz("father(john,gina)") - p.assertz("mother(jane,mich)") + Prolog.assertz("father(john,mich)") + Prolog.assertz("father(john,gina)") + Prolog.assertz("mother(jane,mich)") Y = Variable() Z = Variable() @@ -46,7 +44,7 @@ def main(): q.closeQuery() # Newer versions of SWI-Prolog do not allow nested queries print("\nQuery with strings\n") - for s in p.query("father(john,Y),mother(Z,Y)"): + for s in Prolog.query("father(john,Y),mother(Z,Y)"): print(s["Y"], s["Z"]) diff --git a/examples/hanoi/hanoi.py b/examples/hanoi/hanoi.py index 528a260..8d93563 100644 --- a/examples/hanoi/hanoi.py +++ b/examples/hanoi/hanoi.py @@ -76,12 +76,11 @@ def draw(self): def main(): n = 3 - prolog = Prolog() tower = Tower(n, True) notifier = Notifier(tower.move) registerForeign(notifier.notify) - prolog.consult("hanoi.pl", relative_to=__file__) - list(prolog.query("hanoi(%d)" % n)) + Prolog.consult("hanoi.pl", relative_to=__file__) + list(Prolog.query("hanoi(%d)" % n)) if __name__ == "__main__": diff --git a/examples/hanoi/hanoi_simple.py b/examples/hanoi/hanoi_simple.py index 0fb72ca..b7d9014 100644 --- a/examples/hanoi/hanoi_simple.py +++ b/examples/hanoi/hanoi_simple.py @@ -31,10 +31,9 @@ def notify(t): notify.arity = 1 - prolog = Prolog() registerForeign(notify) - prolog.consult("hanoi.pl", relative_to=__file__) - list(prolog.query(f"hanoi({N})")) + Prolog.consult("hanoi.pl", relative_to=__file__) + list(Prolog.query(f"hanoi({N})")) if __name__ == "__main__": diff --git a/examples/register_foreign.py b/examples/register_foreign.py index cd7ad20..ca69214 100644 --- a/examples/register_foreign.py +++ b/examples/register_foreign.py @@ -33,6 +33,5 @@ def atom_checksum(*a): return False -p = Prolog() registerForeign(atom_checksum, arity=2) -print(list(p.query("X='Python', atom_checksum(X, Y)", catcherrors=False))) +print(list(Prolog.query("X='Python', atom_checksum(X, Y)", catcherrors=False))) diff --git a/examples/register_foreign_simple.py b/examples/register_foreign_simple.py index 31ee425..1c5c8ad 100644 --- a/examples/register_foreign_simple.py +++ b/examples/register_foreign_simple.py @@ -36,10 +36,9 @@ def hello(t): def main(): registerForeign(hello) - prolog = Prolog() - prolog.assertz("father(michael,john)") - prolog.assertz("father(michael,gina)") - list(prolog.query("father(michael,X), hello(X)")) + Prolog.assertz("father(michael,john)") + Prolog.assertz("father(michael,gina)") + list(Prolog.query("father(michael,X), hello(X)")) if __name__ == "__main__": diff --git a/examples/sendmoremoney/money.py b/examples/sendmoremoney/money.py index 21a4d2d..b2edfca 100644 --- a/examples/sendmoremoney/money.py +++ b/examples/sendmoremoney/money.py @@ -30,9 +30,8 @@ from pyswip import Prolog letters = list("SENDMORY") -prolog = Prolog() -prolog.consult("money.pl", relative_to=__file__) -for result in prolog.query("sendmore(X)"): +Prolog.consult("money.pl", relative_to=__file__) +for result in Prolog.query("sendmore(X)"): r = result["X"] for i, letter in enumerate(letters): print(letter, "=", r[i]) diff --git a/examples/sendmoremoney/money_new.py b/examples/sendmoremoney/money_new.py index fcbc7a4..956c3ea 100644 --- a/examples/sendmoremoney/money_new.py +++ b/examples/sendmoremoney/money_new.py @@ -32,9 +32,8 @@ def main(): letters = list("SENDMORY") - prolog = Prolog() sendmore = Functor("sendmore") - prolog.consult("money.pl", relative_to=__file__) + Prolog.consult("money.pl", relative_to=__file__) X = Variable() call(sendmore(X)) diff --git a/examples/sudoku/sudoku.py b/examples/sudoku/sudoku.py index 1efb67f..50cf5eb 100644 --- a/examples/sudoku/sudoku.py +++ b/examples/sudoku/sudoku.py @@ -59,9 +59,9 @@ def pretty_print(table): def solve(problem): - prolog.consult("sudoku.pl", relative_to=__file__) + Prolog.consult("sudoku.pl", relative_to=__file__) p = str(problem).replace("0", "_") - result = list(prolog.query("L=%s,sudoku(L)" % p, maxresult=1)) + result = list(Prolog.query("L=%s,sudoku(L)" % p, maxresult=1)) if result: result = result[0] return result["L"] @@ -83,5 +83,4 @@ def main(): if __name__ == "__main__": - prolog = Prolog() main() diff --git a/examples/sudoku/sudoku_daily.py b/examples/sudoku/sudoku_daily.py index c2908ea..7d11915 100644 --- a/examples/sudoku/sudoku_daily.py +++ b/examples/sudoku/sudoku_daily.py @@ -72,9 +72,9 @@ def get_daily_sudoku(url): def solve(problem): - prolog.consult("sudoku.pl", relative_to=__file__) + Prolog.consult("sudoku.pl", relative_to=__file__) p = str(problem).replace("0", "_") - result = list(prolog.query("Puzzle=%s,sudoku(Puzzle)" % p, maxresult=1)) + result = list(Prolog.query(f"Puzzle={p},sudoku(Puzzle)", maxresult=1)) if result: result = result[0] return result["Puzzle"] @@ -83,9 +83,8 @@ def solve(problem): if __name__ == "__main__": - URL = "http://www.sudoku.org.uk/daily.asp" + URL = "https://www.sudoku.org.uk/daily.asp" - prolog = Prolog() # having this in `solve` bites! because of __del__ print("Getting puzzle from:", URL) puzzle = get_daily_sudoku(URL) print("-- PUZZLE --") diff --git a/src/pyswip/examples/sudoku.py b/src/pyswip/examples/sudoku.py index 050e02a..961aeae 100644 --- a/src/pyswip/examples/sudoku.py +++ b/src/pyswip/examples/sudoku.py @@ -28,12 +28,13 @@ from pyswip.prolog import Prolog -__all__ = "Matrix", "solve" +__all__ = "Matrix", "solve", "prolog_source" -DIMENSION = 9 +_DIMENSION = 9 +_SOURCE_PATH = "sudoku.pl" -Prolog.consult("sudoku.pl", relative_to=__file__) +Prolog.consult(_SOURCE_PATH, relative_to=__file__) class Matrix: @@ -41,7 +42,7 @@ class Matrix: def __init__(self, matrix: List[List[int]]) -> None: if not matrix: raise ValueError("matrix must be given") - if len(matrix) != DIMENSION: + if len(matrix) != _DIMENSION: raise ValueError("Matrix dimension must be 9") self._dimension = len(matrix) self._validate(self._dimension, matrix) @@ -105,6 +106,13 @@ def solve(matrix: Matrix) -> Union[Matrix, Literal[False]]: return Matrix(result) +def prolog_source() -> str: + from pathlib import Path + path = Path(__file__).parent / _SOURCE_PATH + with open(path) as f: + return f.read() + + def main(): puzzle = Matrix.from_text(""" . 6 . 1 . 4 . 5 . @@ -123,7 +131,6 @@ def main(): solution = solve(puzzle) if solution: solution.pretty_print() - print(repr(solution)) else: print("This puzzle has no solutions. Is it valid?") diff --git a/src/pyswip/prolog.py b/src/pyswip/prolog.py index 569edff..192854a 100644 --- a/src/pyswip/prolog.py +++ b/src/pyswip/prolog.py @@ -218,14 +218,13 @@ def query(cls, query, maxresult=-1, catcherrors=True, normalize=True): If the query is a yes/no question, returns {} for yes, and nothing for no. Otherwise returns a generator of dicts with variables as keys. - >>> prolog = Prolog() - >>> prolog.assertz("father(michael,john)") - >>> prolog.assertz("father(michael,gina)") - >>> bool(list(prolog.query("father(michael,john)"))) + >>> Prolog.assertz("father(michael,john)") + >>> Prolog.assertz("father(michael,gina)") + >>> bool(list(Prolog.query("father(michael,john)"))) True - >>> bool(list(prolog.query("father(michael,olivia)"))) + >>> bool(list(Prolog.query("father(michael,olivia)"))) False - >>> print sorted(prolog.query("father(michael,X)")) + >>> print sorted(Prolog.query("father(michael,X)")) [{'X': 'gina'}, {'X': 'john'}] """ return cls._QueryWrapper()(query, maxresult, catcherrors, normalize) diff --git a/tests/examples/test_sudoku.py b/tests/examples/test_sudoku.py index 7f1fdec..a5f6b68 100644 --- a/tests/examples/test_sudoku.py +++ b/tests/examples/test_sudoku.py @@ -1,6 +1,6 @@ import unittest -from pyswip.examples.sudoku import Matrix, solve +from pyswip.examples.sudoku import Matrix, solve, prolog_source class MatrixTestCase(unittest.TestCase): @@ -53,3 +53,7 @@ def test_solve_failure(self): puzzle = Matrix.from_text(fixture) solution = solve(puzzle) self.assertFalse(solution) + + def test_prolog_source(self): + text = prolog_source() + self.assertIn("Prolog Sudoku Solver", text) diff --git a/tests/test_examples.py b/tests/test_examples.py index d48acf3..fe3e151 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -32,274 +32,22 @@ from pyswip import * +examples = [ + "create_term.py", + "father.py", + "register_foreign.py", + "register_foreign_simple.py", + "knowledgebase.py", + "hanoi/hanoi_simple.py", + "sendmoremoney/money.py", + "sendmoremoney/money_new.py", +] -class TestExamples(unittest.TestCase): - """ - Each test method is named after one example in $PYSWIP/examples. - WARNING: Since it is not possible to unload things from the Prolog base, the - examples have to be 'orthogonal'. - - """ - - def test_create_term(self): - """ - Simple example of term creation. - """ - - prolog = Prolog() - - a1 = PL_new_term_refs(2) - a2 = a1 + 1 - t = PL_new_term_ref() - ta = PL_new_term_ref() - - animal2 = PL_new_functor(PL_new_atom("animal"), 2) - assertz = PL_new_functor(PL_new_atom("assertz"), 1) - - PL_put_atom_chars(a1, "gnu") - PL_put_integer(a2, 50) - PL_cons_functor_v(t, animal2, a1) - PL_cons_functor_v(ta, assertz, t) - PL_call(ta, None) - - result = list(prolog.query("animal(X,Y)", catcherrors=True)) - self.assertEqual(len(result), 1) - self.assertEqual(result[0], {"X": "gnu", "Y": 50}) - - def test_knowledgebase(self): - """ - Tests usage of modules. - """ - - _ = Prolog() - - 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("mike", "bob")), module=test2) - call(assertz(parent("gina", "bob")), module=test2) - - # Test knowledgebase module test1 - - result = set() - X = Variable() - q = Query(parent(X, "bob"), module=test1) - while q.nextSolution(): - result.add(X.value.value) # X.value is an Atom - q.closeQuery() - self.assertEqual(result, set(["john", "jane"])) - - # Test knowledgebase module test2 - - result = set() - q = Query(parent(X, "bob"), module=test2) - while q.nextSolution(): - result.add(X.value.value) - q.closeQuery() - self.assertEqual(result, set(["mike", "gina"])) - - def test_father(self): - """ - Tests basic inferences. - """ - - p = Prolog() - - father = Functor("father", 2) - mother = Functor("mother", 2) - - p.assertz("father(john,mich)") - p.assertz("father(john,gina)") - p.assertz("mother(jane,mich)") - - Y = Variable() - Z = Variable() - - result = [] - q = Query(father("john", Y), mother(Z, Y)) - while q.nextSolution(): - y = Y.value.value - z = Z.value.value - result.append({"Y": y, "Z": z}) - q.closeQuery() - - self.assertEqual(len(result), 1) - self.assertEqual(result[0], {"Y": "mich", "Z": "jane"}) - - # Repeat the same query but using strings - result = [] - for s in p.query("father(john,Y),mother(Z,Y)"): - result.append(s) - self.assertEqual(len(result), 1) - self.assertEqual(result[0], {"Y": "mich", "Z": "jane"}) - - def test_coins(self): - """ - Runs the coins example (uses clp library of SWI-Prolog). - """ - - prolog = Prolog() - prolog.consult(example_path("coins/coins.pl")) - count = 100 - total = 500 - coins = Functor("coins", 3) - S = Variable() - q = Query(coins(S, count, total)) - - solutions = [] - while q.nextSolution(): - solutions.append(S.value) - q.closeQuery() - - self.assertEqual(len(solutions), 105) - - # Now do the same test, but using the prolog.query interface - solutions = list(prolog.query("coins(S, %d, %d)." % (count, total))) - self.assertEqual(len(solutions), 105) - - def test_draughts(self): - """ - Runs the draughts example (uses clp library of SWI-Prolog). - """ - - prolog = Prolog() - prolog.consult(example_path("draughts/puzzle1.pl")) - solutions = [] - for soln in prolog.query("solve(B)."): - solutions.append(soln["B"]) - self.assertEqual(len(solutions), 37) - - # Now do the same test, but using the prolog.query interface - solutions = list(prolog.query("solve(B).")) - self.assertEqual(len(solutions), 37) - - def test_hanoi(self): - """ - Runs the hanoi example. - """ - - N = 3 # Number of disks - - result = [] - - def notify(t): - result.append((t[0].value, t[1].value)) - - notify.arity = 1 - - prolog = Prolog() - registerForeign(notify) - prolog.consult(example_path("hanoi/hanoi.pl")) - list(prolog.query("hanoi(%d)" % N)) # Forces the query to run completely - - self.assertEqual(len(result), 7) - self.assertEqual(result[0], ("left", "right")) - self.assertEqual(result[1], ("left", "center")) - self.assertEqual(result[2], ("right", "center")) - - def test_sendmoremoney(self): - """ - Runs the sendmoremoney example:: - - S E N D - M O R E - + ------- - M O N E Y - - So, what should be the values of S, E, N, D, M, O, R, Y - if they are all distinct digits. - """ - - letters = "S E N D M O R Y".split() - prolog = Prolog() - sendmore = Functor("sendmore") - prolog.consult(example_path("sendmoremoney/money.pl")) - - X = Variable() - call(sendmore(X)) - r = X.value - val = {} - for i, letter in enumerate(letters): - val[letter] = r[i] - - self.assertEqual(len(val), 8) - - send = val["S"] * 1e3 + val["E"] * 1e2 + val["N"] * 1e1 + val["D"] * 1e0 - more = val["M"] * 1e3 + val["O"] * 1e2 + val["R"] * 1e1 + val["E"] * 1e0 - money = ( - val["M"] * 1e4 - + val["O"] * 1e3 - + val["N"] * 1e2 - + val["E"] * 1e1 - + val["Y"] * 1e0 - ) - self.assertEqual(money, send + more) - - def test_sudoku(self): - """ - Runs the sudoku example (uses clp library of SWI-Prolog). - """ - - _ = 0 - puzzle1 = [ - [_, 6, _, 1, _, 4, _, 5, _], - [_, _, 8, 3, _, 5, 6, _, _], - [2, _, _, _, _, _, _, _, 1], - [8, _, _, 4, _, 7, _, _, 6], - [_, _, 6, _, _, _, 3, _, _], - [7, _, _, 9, _, 1, _, _, 4], - [5, _, _, _, _, _, _, _, 2], - [_, _, 7, 2, _, 6, 9, _, _], - [_, 4, _, 5, _, 8, _, 7, _], - ] - - puzzle2 = [ - [_, _, 1, _, 8, _, 6, _, 4], - [_, 3, 7, 6, _, _, _, _, _], - [5, _, _, _, _, _, _, _, _], - [_, _, _, _, _, 5, _, _, _], - [_, _, 6, _, 1, _, 8, _, _], - [_, _, _, 4, _, _, _, _, _], - [_, _, _, _, _, _, _, _, 3], - [_, _, _, _, _, 7, 5, 2, _], - [8, _, 2, _, 9, _, 7, _, _], - ] - - prolog = Prolog() - prolog.consult(example_path("sudoku/sudoku.pl")) - - for i, problem in enumerate((puzzle1, puzzle2)): - p = str(problem).replace("0", "_") - result = list(prolog.query("L=%s,sudoku(L)" % p, maxresult=1)) - if result: - # Does a simple check on the result - result = result[0] - for j, line in enumerate(result["L"]): - self.assertEqual( - len(set(line)), 9, "Failure in line %d: %s" % (j, line) - ) - else: - self.fail("Failed while running example number %d" % i) - - @pytest.mark.slow - def test_large_db(self): - """ - Generates a large database, then runs query - """ - - num_facts = 125000 - prolog = Prolog() - for i in range(num_facts): - prolog.assertz("p(%s)" % i) - - results = [r for r in prolog.query("p(I)")] - self.assertEqual(len(results), num_facts) +@pytest.mark.parametrize("example", examples) +def test_example(example): + path = example_path(example) + execfile(path) def example_path(path): @@ -310,3 +58,14 @@ def example_path(path): os.path.split(os.path.abspath(__file__))[0], "..", "examples", path ) ).replace("\\", "\\\\") + + +def execfile(filepath, globals=None, locals=None): + if globals is None: + globals = {} + globals.update({ + "__file__": filepath, + "__name__": "__main__", + }) + with open(filepath, 'rb') as file: + exec(compile(file.read(), filepath, 'exec'), globals, locals) diff --git a/tests/test_foreign.py b/tests/test_foreign.py index 12a654c..d9a7249 100644 --- a/tests/test_foreign.py +++ b/tests/test_foreign.py @@ -23,10 +23,9 @@ def hello(t): registerForeign(hello) - prolog = Prolog() - prolog.assertz("father(michael,john)") - prolog.assertz("father(michael,gina)") - result = list(prolog.query("father(michael,X), hello(X)")) + Prolog.assertz("mother(emily,john)") + Prolog.assertz("mother(emily,gina)") + result = list(Prolog.query("mother(emily,X), hello(X)")) self.assertEqual(len(result), 2, "Query should return two results") for name in ("john", "gina"): self.assertTrue( @@ -34,8 +33,6 @@ def hello(t): ) def test_nondeterministic_foreign(self): - prolog = Prolog() - def nondet(a, context): control = PL_foreign_control(context) context = PL_foreign_context(context) @@ -55,7 +52,7 @@ def nondet(a, context): nondet.arity = 1 registerForeign(nondet, flags=PL_FA_NONDETERMINISTIC) - result = list(prolog.query("nondet(X)")) + result = list(Prolog.query("nondet(X)")) self.assertEqual(len(result), 10, "Query should return 10 results") for i in range(10): @@ -78,9 +75,7 @@ def test_for_string(string, test_result): registerForeign(get_str) registerForeign(test_for_string) - prolog = Prolog() - - result = list(prolog.query("get_str(String), test_for_string(String, Result)")) + result = list(Prolog.query("get_str(String), test_for_string(String, Result)")) self.assertEqual( result[0]["Result"], "true", @@ -100,17 +95,14 @@ def get_list_of_lists(result): registerForeign(get_list_of_lists) - prolog = Prolog() - - result = list(prolog.query("get_list_of_lists(Result)")) + result = list(Prolog.query("get_list_of_lists(Result)")) self.assertTrue( {"Result": [[1], [2]]} in result, "Nested lists should be unified correctly as return value.", ) def test_dictionary(self): - prolog = Prolog() - result = list(prolog.query("X = dict{key1:value1 , key2: value2}")) + result = list(Prolog.query("X = dict{key1:value1 , key2: value2}")) dict = result[0] self.assertTrue( {"key1": "value1", "key2": "value2"} == dict["X"], @@ -118,8 +110,7 @@ def test_dictionary(self): ) def test_empty_dictionary(self): - prolog = Prolog() - result = list(prolog.query("X = dict{}")) + result = list(Prolog.query("X = dict{}")) dict = result[0] self.assertTrue( dict["X"] == {}, @@ -127,8 +118,7 @@ def test_empty_dictionary(self): ) def test_nested_dictionary(self): - prolog = Prolog() - result = list(prolog.query("X = dict{key1:nested{key:value} , key2: value2}")) + result = list(Prolog.query("X = dict{key1:nested{key:value} , key2: value2}")) dict = result[0] self.assertTrue( {"key1": {"key": "value"}, "key2": "value2"} == dict["X"], diff --git a/tests/test_issues.py b/tests/test_issues.py index ba4c437..01a7c6a 100644 --- a/tests/test_issues.py +++ b/tests/test_issues.py @@ -68,8 +68,7 @@ def test_issue_1(self): # issue 13, if it does not work, it will segfault Python. from pyswip import Prolog - prolog = Prolog() - prolog.assertz("randomTerm(michael,john)") + Prolog.assertz("randomTerm(michael,john)") def test_issue_8(self): """ @@ -89,10 +88,9 @@ def hello(t): registerForeign(hello) - prolog = Prolog() - prolog.assertz("parent(michael,john)") - prolog.assertz("parent(michael,gina)") - p = prolog.query("parent(michael,X), hello(X)") + Prolog.assertz("parent(michael,john)") + Prolog.assertz("parent(michael,gina)") + p = Prolog.query("parent(michael,X), hello(X)") result = list(p) # Will run over the iterator self.assertEqual(len(callsToHello), 2) # ['john', 'gina'] @@ -246,13 +244,12 @@ def test_issue_62(self): """ from pyswip import Prolog - prolog = Prolog() - prolog.consult("test_unicode.pl", catcherrors=True, relative_to=__file__) - atoms = list(prolog.query("unicode_atom(B).")) + Prolog.consult("test_unicode.pl", catcherrors=True, relative_to=__file__) + atoms = list(Prolog.query("unicode_atom(B).")) self.assertEqual(len(atoms), 3, "Query should return exactly three atoms") - strings = list(prolog.query("unicode_string(B).")) + strings = list(Prolog.query("unicode_string(B).")) self.assertEqual(len(strings), 1, "Query should return exactly one string") self.assertEqual(strings[0]["B"], b"\xd1\x82\xd0\xb5\xd1\x81\xd1\x82") diff --git a/tests/test_prolog.py b/tests/test_prolog.py index a870789..a84b870 100644 --- a/tests/test_prolog.py +++ b/tests/test_prolog.py @@ -112,7 +112,6 @@ def test_prolog_read_file(self): See: https://github.com/yuce/pyswip/issues/10 """ current_dir = os.path.dirname(os.path.abspath(__file__)) - prolog = pl.Prolog() path = os.path.join(current_dir, "test_read.pl") - prolog.consult(path) - list(prolog.query(f'read_file("{path}", S)')) + pl.Prolog.consult(path) + list(pl.Prolog.query(f'read_file("{path}", S)'))