Beispiel #1
0
def _evaluate_query_term_next_solution(query, var):
    with query as active_query:
        assert_true(active_query.next_solution())
        assert_equal(var, Term.from_integer(1))
        assert_true(active_query.next_solution())
        assert_equal(var, Term.from_integer(2))
        assert_false(active_query.next_solution())
Beispiel #2
0
def _make_query__init__arglist(var):
    eq = Functor('=', 2)
    or_ = Predicate(Functor(';', 2))
    args = TermList(2)
    args[0].put_cons_functor(eq, var, Term.from_integer(1))
    args[1].put_cons_functor(eq, var, Term.from_integer(2))
    return Query(or_, arglist=args)
Beispiel #3
0
 def __init__(self):
     super().__init__('[-1,2,a,3.4]')
     self.term = Term.from_list_terms([
         Term.from_integer(-1),
         Term.from_integer(2),
         Term.from_atom_name('a'),
         Term.from_float(3.4)])
Beispiel #4
0
    def test_lines_example(self):
        point = Functor('point', 2)
        line = Functor('line', 2)
        vertical = Functor('vectical', 1)
        horizontal = Functor('horizontal', 1)

        with Frame():
            X = Term()
            Y = Term()
            Z = Term()
            self.assertz(vertical(line(point(X, Y), point(X, Z))))
            self.assertz(horizontal(line(point(X, Y), point(Z, Y))))

        _1 = Term.from_integer(1)
        _2 = Term.from_integer(2)
        _3 = Term.from_integer(3)
        assert_true(vertical(line(point(_1, _1), point(_1, _3)))())
        assert_false(vertical(line(point(_1, _1), point(_3, _2)))())

        with Frame():
            Y = Term()
            assert_true(horizontal(line(point(_1, _1), point(_2, Y)))())
            assert_equal(Y, _1)

        with Frame():
            P = Term()
            expr = horizontal(line(point(_2, _3), P))
            res = list(Query(self.call, expr).term_assignments(P, True))
            assert_equal(len(res), 1)
            assert_true(res[0].get().unify(point(Term(), _3)))
Beispiel #5
0
def test_frame_close():
    f1 = Frame()
    t1 = f1.term()
    t1.put_atom_name('t1')
    assert_equal(str(t1), 't1')
    f1.close()
    with assert_raises(AttributeError):
        str(t1)
    with assert_raises(AttributeError):
        f1.close()

    with Frame() as f2:
        t2 = f2.term()
        t2.put_atom_name('t2')
        assert_equal(str(t2), 't2')

    with assert_raises(AttributeError):
        str(t2)
    with assert_raises(AttributeError):
        f2.close()

    X = Term()
    with Frame():
        X.put_integer(1)
        assert_equal(str(X), '1')
    assert_equal(str(X), '1')

    eq = Predicate.from_name_arity('=', 2)
    X = Term()
    with Frame():
        eq(X, Term.from_integer(1))
        assert_equal(str(X), '1')
    assert_equal(str(X), '1')
Beispiel #6
0
 def __init__(self):
     super().__init__("[1,a]")
     tail = Term.from_list()
     tail.unify_arg(0, Term.from_atom_name('a'))
     tail.unify_arg(1, Term.from_nil())
     self.term = Term.from_list()
     self.term.unify_arg(0, Term.from_integer(1))
     self.term.unify_arg(1, tail)
Beispiel #7
0
def test_frame_rewind():
    X = Term()
    with Frame() as f:
        t = f.term()
        t.put_integer(1)
        X.unify_integer(2)
        assert_equal(t, Term.from_integer(1))
        assert_equal(X, Term.from_integer(2))

        f.rewind()
        with assert_raises(AttributeError):
            str(t)

        assert_equal(X.type(), 'variable')
        X.unify_integer(3)
        assert_equal(X, Term.from_integer(3))
        f.rewind()
        assert_equal(X.type(), 'variable')
        X.unify_integer(2)

    assert_equal(X, Term.from_integer(2))
Beispiel #8
0
    def test_basic_unification(self):
        assert_true(mia.unify(mia))

        two = Term.from_integer(2)
        assert_true(two, two)

        assert_false(mia.unify(vincent))

        with Frame():
            assert_true(Term().unify(mia))

        with Frame():
            assert_true(Term().unify(Term()))

        with Frame():
            X = Term()
            assert_true(X.unify(mia))
            assert_false(X.unify(vincent))

        k = Functor('k', 2)
        s = Functor('s', 1)
        t = Functor('t', 1)
        g = Term.from_atom_name('g')
        k_atom = Term.from_atom_name('k')

        with Frame():
            X = Term()
            Y = Term()

            assert_true(k(s(g), Y).unify(k(X, t(k_atom))))
            assert_equal(X, s(g))
            assert_equal(Y, t(k_atom))

        with Frame():
            X = Term()
            Y = Term()

            assert_true(k(s(g), t(k_atom)).unify(k(X, t(Y))))
            assert_equal(X, s(g))
            assert_equal(Y, k_atom)

        self.retractall(loves(Term(), Term()))
        self.assertz(loves(vincent, mia))
        self.assertz(loves(marcellus, mia))
        self.assertz(loves(pumpkin, honey_bunny))
        self.assertz(loves(honey_bunny, pumpkin))

        with Frame():
            X = Term()
            assert_false(loves(X, X).unify(loves(marcellus, mia)))
Beispiel #9
0
def test_frame_discard():
    eq = Predicate.from_name_arity('=', 2)

    X = Term()
    with Frame(discard=True):
        X.put_integer(1)
        assert_equal(str(X), '1')
    assert_equal(str(X), '1')

    X = Term()
    with Frame(discard=True):
        eq(X, Term.from_integer(1))
        assert_equal(str(X), '1')
        assert_equal(X.type(), 'integer')
    assert_not_equal(str(X), '1')
    assert_equal(X.type(), 'variable')
Beispiel #10
0
def _make_query_call_term(var):
    eq = Functor('=', 2)
    or_ = Functor(';', 2)
    return Query.call_term(or_(eq(var, Term.from_integer(1)),
                               eq(var, Term.from_integer(2))))
Beispiel #11
0
def _make_query__init__(var):
    eq = Functor('=', 2)
    or_ = Predicate(Functor(';', 2))
    return Query(or_, eq(var, Term.from_integer(1)),
                 eq(var, Term.from_integer(2)))
Beispiel #12
0
 def __init__(self):
     super().__init__('@(%v,[%v=[5|%v]])', is_acyclic=False)
     self.term = Term.from_list()
     self.term.unify_arg(0, Term.from_integer(5))
     self.term.unify_arg(1, self.term)
Beispiel #13
0
 def __init__(self):
     super().__init__("[1|2]")
     self.term = Term.from_list()
     self.term.unify_arg(0, Term.from_integer(1))
     self.term.unify_arg(1, Term.from_integer(2))
Beispiel #14
0
 def __init__(self):
     super().__init__('[1]')
     self.term = Term.from_list()
     self.term.unify_arg(0, Term.from_integer(1))
     self.term.unify_arg(1, Term.from_nil())
Beispiel #15
0
 def __init__(self, value):
     super().__init__(type_='integer', value=value,
                      prolog_string=str(value))
     self.term = Term.from_integer(value)
Beispiel #16
0
def _evaluate_query_term_assignments_persistent(query, var):
    assignments = list(query.term_assignments(var, persistent=True))
    assert_equal(len(assignments), 2)
    assert_equal(assignments[0].get(), Term.from_integer(1))
    assert_equal(assignments[1].get(), Term.from_integer(2))