示例#1
0
def test_query_operator_neq_atom_var():
    yp = YP()
    v1 = yp.variable()
    a1 = yp.atom(1)
    q = yp.query('/=', [a1, v1])
    r = list(q)
    assert len(r) == 0
示例#2
0
def test_unify_variable_with_variable():
    yp = YP()
    v1 = yp.variable()
    v2 = yp.variable()
    r = [(v1.get_value(), v2.get_value()) for x in unify(v1, v2)]
    assert len(r) == 1
    assert r[0][0] == r[0][1]
示例#3
0
def test_unify_lists_with_makelist():
    yp = YP()
    v1 = yp.variable()
    l1 = yp.listpair(yp.atom("a"), yp.listpair(yp.atom("b"), yp.ATOM_NIL))
    l2 = yp.makelist([yp.atom("a"), v1])
    r = [v1.get_value() for x in unify(l1, l2)]
    assert r == [yp.atom("b")]
示例#4
0
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 == []
示例#5
0
def test_unify_variable_unbound():
    yp = YP()
    a1 = yp.atom('tom')
    v1 = yp.variable()
    r = [v1.get_value() for x in unify(v1, a1)]
    # assert r ==[False]
    assert r == [yp.atom('tom')]
示例#6
0
def test_evaluate_bounded_projection_function():
    yp = YP()
    yp.assert_fact(yp.atom('cat'), [yp.atom('tom')])
    V1 = yp.variable()
    q = yp.query('cat', [V1])
    r = yp.evaluate_bounded(q, (lambda x: V1.get_value()))
    assert r == [yp.atom('tom')]
示例#7
0
def test_query_operator_neq_atom_atom():
    yp = YP()
    # v1 = yp.variable()
    a1 = yp.atom(1)
    a2 = yp.atom(2)
    q = yp.query('\\=', [a1, a2])
    r = list(q)
    assert len(r) == 1
示例#8
0
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)]
示例#9
0
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]
示例#10
0
def test_clear():
    yp = YP()
    yp.assert_fact(yp.atom('cat'), [yp.atom('tom')])
    yp.clear()
    V1 = yp.variable()
    args = [V1]
    q = yp.query('cat', args)
    r = [[v.get_value() for v in args] for r in q]
    assert r == []
示例#11
0
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
示例#12
0
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]
示例#13
0
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]
示例#14
0
def test_functor_arities():
    s = compile_prolog_from_string(
        '''
    likes(A,B) :- person(A), person(B), friend(A,B).
    likes(A,B,C) :- person(A),person(B), person(C), friend(A,B), friend(A,C).
    likes(A,B) :- person(A), person(B), person(C), foe(A,C), foe(B,C).
    person(mike).
    person(joe).
    person(pete).
    person(zack).
    friend(mike,pete).
    friend(mike,joe).
    foe(joe,zack).
    foe(pete,zack).
    ''', TestContext)

    yp = YP()
    yp.load_script_from_string(s, overwrite=False)
    X = yp.variable()
    Y = yp.variable()
    q = yp.query('likes', [X, Y])
    r = [(to_python(X.get_value()), to_python(Y.get_value())) for x in q]
    assert set(r) == set([
        ('mike', 'pete'),
        ('mike', 'joe'),
        ('joe', 'pete'),
        ('joe', 'joe'),
        ('pete', 'pete'),
        ('pete', 'joe'),
    ])
示例#15
0
def test_user_defined_function():
    yp = YP()
    side_effects = []

    def func(arg1):
        # check if arg1 instantiated
        if isinstance(arg1, Atom) and arg1.name() != "blue":
            side_effects.append('not blue')
        for l1 in unify(arg1, yp.atom("blue")):
            yield False

    yp.register_function('func', func)
    q = yp.query('func', [yp.atom('red')])
    r = list(q)
    assert side_effects == ['not blue']
示例#16
0
def test_unify_atoms_different():
    yp = YP()
    a1 = yp.atom('tom')
    a2 = yp.atom('jerry')
    r = list(unify(a1, a2))
    assert r == []
示例#17
0
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
示例#18
0
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]
示例#19
0
def test_unify_variable_integer():
    yp = YP()
    v1 = yp.variable()
    r = [v1.get_value() for x in unify(v1, 5)]
    assert r == [5]
示例#20
0
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
示例#21
0
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')]
示例#22
0
def test_query_operator_neq():
    yp = YP()
    v1 = yp.variable()
    q = yp.query('/=', [v1, v1])
    r = list(q)
    assert len(r) == 0
示例#23
0
def test_assert_facts_concurrently():
    yp1 = YP()
    yp2 = YP()
    yp1.assert_fact(yp1.atom('fact'), [yp1.atom('red')])
    yp2.assert_fact(yp2.atom('fact'), [yp2.atom('blue')])
    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
示例#24
0
def test_query_without_predicates():
    yp = YP()
    v1 = yp.variable()
    q = yp.query('nonexistentpred', [v1])
    r = list(q)
    assert r == []
示例#25
0
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'))])
示例#26
0
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 == []
示例#27
0
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')]]
示例#28
0
def test_unify_atoms_equal():
    yp = YP()
    a1 = yp.atom('tom')
    a2 = yp.atom('tom')
    r = list(unify(a1, a2))
    assert r == [False]
示例#29
0
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]
示例#30
0
def test_match_answer_not_matching():
    yp = YP()
    a = Answer([yp.atom('tom'), yp.atom('cat')])
    r = list(a.match([yp.atom('tom'), yp.atom('mouse')]))
    assert r == []