def test_load_scripts_with_dependencies_out_of_order(get_compiled_file): yp = YP() yp.load_script_from_file(get_compiled_file('mainscript.prolog'), overwrite=False) yp.load_script_from_file(get_compiled_file('subscript.prolog'), overwrite=False) v1 = yp.variable() q = yp.query('main', [v1]) r = [v1.get_value() for x in q] assert r == [yp.atom('yes'), yp.atom('no'), yp.atom('maybe')]
def test_load_scripts_overwrite_multiple_definitions(get_compiled_file): yp = YP() yp.load_script_from_file(get_compiled_file('defs1.prolog'), overwrite=True) yp.load_script_from_file(get_compiled_file('defs2.prolog'), overwrite=True) v1 = yp.variable() v2 = yp.variable() v3 = yp.variable() q = yp.query('testlist', [yp.makelist([v1, v2, v3])]) r = [(v1.get_value(), v2.get_value(), v3.get_value()) for x in q] assert set(r) == set([(yp.atom('cyan'), yp.atom('magenta'), yp.atom('yellow'))])
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_load_scripts_concurrently(get_compiled_file): yp1 = YP() yp2 = YP() yp1.load_script_from_file(get_compiled_file('script1a.prolog')) yp2.load_script_from_file(get_compiled_file('script1b.prolog')) X1 = yp1.variable() q1 = yp1.query('fact', [X1]) X2 = yp2.variable() q2 = yp2.query('fact', [X2]) r1 = [X1.get_value() for r in q1] r2 = [X2.get_value() for r in q2] assert r1 != r2
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_run_lists_script(get_compiled_file): yp = YP() yp.load_script_from_file(get_compiled_file('lists.prolog')) l = yp.makelist([ yp.atom(x) for x in [ 'Johnny', 'Dee Dee', 'Joey', 'Tommy', 'Marky', 'Richie', 'Elvis', 'C. J.' ] ]) q = yp.query('member', [yp.atom('Richie'), l]) r = list(q) assert r == [False] v1 = yp.variable() v2 = yp.variable() v3 = yp.variable() q = yp.query('testlist', [yp.makelist([v1, v2, v3])]) r = [[v1.get_value(), v2.get_value(), v3.get_value()] for x in q] assert r == [[yp.atom('red'), yp.atom('green'), yp.atom('blue')]]
def test_sploit_eval(get_compiled_file): yp = YP() yp.load_script_from_file(get_compiled_file('eval.prolog')) q = yp.query('sploit', ['1+1']) r = list(q) assert r == []