Exemple #1
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'))])
Exemple #2
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
Exemple #3
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
Exemple #4
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'),
    ])
Exemple #5
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")]
Exemple #6
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 == []
Exemple #7
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]
Exemple #8
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')]
Exemple #9
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')]
Exemple #10
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
Exemple #11
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]
Exemple #12
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)]
Exemple #13
0
def test_setup_yp():
    yp = YP()
    yp.assert_fact(yp.atom('cat'), [yp.atom('tom')])
    V1 = yp.variable()
    args = [V1]
    q = yp.query('cat', args)
    r = [[v.get_value() for v in args] for r in q]
    assert r == [[yp.atom('tom')]]
Exemple #14
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]
Exemple #15
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')]]
Exemple #16
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')]
Exemple #17
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]
Exemple #18
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]
Exemple #19
0
def test_match_answer_different_arity():
    yp = YP()
    a = Answer([yp.atom('tom')])
    r = list(a.match([yp.variable(), yp.variable()]))
    assert r == []
Exemple #20
0
def test_unify_variable_integer():
    yp = YP()
    v1 = yp.variable()
    r = [v1.get_value() for x in unify(v1, 5)]
    assert r == [5]
Exemple #21
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]
Exemple #22
0
def test_match_answer_matching():
    yp = YP()
    a = Answer([yp.atom('tom')])
    v = yp.variable()
    r = [v.get_value() for x in a.match([v])]
    assert r == [yp.atom('tom')]
Exemple #23
0
def test_query_without_predicates():
    yp = YP()
    v1 = yp.variable()
    q = yp.query('nonexistentpred', [v1])
    r = list(q)
    assert r == []
Exemple #24
0
def test_query_operator_neq():
    yp = YP()
    v1 = yp.variable()
    q = yp.query('/=', [v1, v1])
    r = list(q)
    assert len(r) == 0