def test_unify_complex_atoms_not_matching(): yp = YP() v1 = yp.variable() a1 = yp.functor("point", [v1, 2]) a2 = yp.functor("point", [1, 1]) r = [v1.get_value() for x in unify(a1, a2)] assert r == []
def test_load_script_from_string(get_compiled_file): yp = YP() with open(get_compiled_file('geometricobjects.prolog'), 'r') as f: yp.load_script_from_string(f.read(), overwrite=False) Y = yp.variable() q = yp.query('horizontal', [ yp.functor('seg', [yp.functor('point', [1, 1]), yp.functor('point', [2, Y])]) ]) r = [Y.get_value() for x in q] assert r == [1]
def test_load_clauses_with_functors(get_compiled_file): yp = YP() yp.load_script_from_file(get_compiled_file('geometricobjects.prolog'), overwrite=False) Y = yp.variable() q = yp.query('horizontal', [ yp.functor('seg', [yp.functor('point', [1, 1]), yp.functor('point', [2, Y])]) ]) r = [Y.get_value() for x in q] assert r == [1]
def test_unify_complex_atoms(): yp = YP() v1 = yp.variable() v2 = yp.variable() a1 = yp.functor("point", [v1, 2]) a2 = yp.functor("point", [1, v2]) r = [(v1.get_value(), v2.get_value()) for x in unify(a1, a2)] assert r == [(1, 2)]
def test_unify_complex_atoms_free_variable(): yp = YP() v1 = yp.variable() v2 = yp.variable() a1 = yp.functor("point", [v1, 2]) a2 = yp.functor("point", [v2, 2]) r = [(v1.get_value(), v2.get_value()) for x in unify(a1, a2)] assert len(r) == 1 assert r[0][0] == r[0][1]
def test_load_monkey_and_banana_script(get_compiled_file): yp = YP() yp.load_script_from_file(get_compiled_file('monkey.prolog')) # canget(state(atdoor,onfloor,atwindow,hasnot)) q = yp.query('canget', [ yp.functor('state', [ yp.atom('atdoor'), yp.atom('onfloor'), yp.atom('atwindow'), yp.atom('hasnot') ]) ]) # this query has infinitely many solutions, just get the first one assert next(q) == False
def test_run_infinite_script(get_compiled_file): yp = YP() yp.load_script_from_file(get_compiled_file('monkey.prolog')) # canget(state(atdoor,onfloor,atwindow,hasnot)) q = yp.query('canget', [ yp.functor('state', [ yp.atom('atdoor'), yp.atom('onfloor'), yp.atom('atwindow'), yp.atom('hasnot') ]) ]) # this query has infinitely many solutions, just get the first one recursion_limit = sys.getrecursionlimit() r = yp.evaluate_bounded(q, lambda x: x) assert sys.getrecursionlimit() == recursion_limit assert len(r) >= 1
def test_match_example_horizontal_vertical(): yp = YP() X = yp.variable() Y = yp.variable() X1 = yp.variable() Y1 = yp.variable() # vertical(seg(point(X,Y),point(X,Y1))). yp.assert_fact(yp.atom('vertical'), [ yp.functor('seg', [yp.functor('point', [X, Y]), yp.functor('point', [X, Y1])]) ]) # horizontal(seg(point(X,Y),point(X1,Y))). yp.assert_fact(yp.atom('horizontal'), [ yp.functor('seg', [yp.functor('point', [X, Y]), yp.functor('point', [X1, Y])]) ]) # q0 = yp.match_dynamic(yp.atom('vertical'), [ q0 = yp.query('vertical', [ yp.functor('seg', [yp.functor('point', [1, 1]), yp.functor('point', [1, 2])]) ]) # q1 = yp.match_dynamic(yp.atom('vertical'), [ q1 = yp.query('vertical', [ yp.functor('seg', [yp.functor('point', [1, 1]), yp.functor('point', [2, Y])]) ]) # q2 = yp.match_dynamic(yp.atom('horizontal'), [ q2 = yp.query('horizontal', [ yp.functor('seg', [yp.functor('point', [1, 1]), yp.functor('point', [2, Y])]) ]) r0 = list(q0) r1 = list(q1) r2 = [Y.get_value() for r in q2] assert r0 == [False] assert r1 == [] assert r2 == [1]
def test_unify_variable_complex_atom(): yp = YP() a1 = yp.functor("point", [1, 1]) v1 = yp.variable() r = [v1.get_value() for x in unify(v1, a1)] assert r == [a1]