示例#1
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')]
示例#2
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")]
示例#3
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')]
示例#4
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')]]
示例#5
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')]
示例#6
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'))])
示例#7
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
示例#8
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
示例#9
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
示例#10
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]
示例#11
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
示例#12
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
示例#13
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')]]
示例#14
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']
示例#15
0
def test_match_answer_different_arity():
    yp = YP()
    a = Answer([yp.atom('tom')])
    r = list(a.match([yp.variable(), yp.variable()]))
    assert r == []
示例#16
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 == []
示例#17
0
def test_unify_atoms_different():
    yp = YP()
    a1 = yp.atom('tom')
    a2 = yp.atom('jerry')
    r = list(unify(a1, a2))
    assert r == []
示例#18
0
def test_unify_atoms_equal():
    yp = YP()
    a1 = yp.atom('tom')
    a2 = yp.atom('tom')
    r = list(unify(a1, a2))
    assert r == [False]
示例#19
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')]