예제 #1
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')
예제 #2
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)])
예제 #3
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)
예제 #4
0
    def test_knowledge_database_1(self):
        self.dynamic(Term.from_functor(woman))
        self.dynamic(rock_concert)
        self.assertz(woman(mia))
        self.assertz(woman(jody))
        self.assertz(woman(yolanda))
        self.assertz(plays_air_guitar(jody))
        self.assertz(party)

        assert_true(woman(mia)())
        assert_true(woman(jody)())
        assert_true(woman(yolanda)())
        assert_false(woman(Term.from_atom_name('bob'))())

        once = Functor('once', 1)
        once_pred = Predicate(once)
        assert_true(once_pred(woman(mia)))
        once_pred(woman(mia), check=True)
        assert_false(once_pred(woman(Term.from_atom_name('bob'))))

        assert_true(plays_air_guitar(jody)())
        assert_false(plays_air_guitar(mia)())

        assert_true(party())
        assert_false(Term.from_atom_name('rockConcert')())
예제 #5
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())
예제 #6
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)))
예제 #7
0
def test_term_equality():
    a = Term.from_atom_name('a')
    b = Term.from_atom_name('b')
    eq = Predicate.from_name_arity('=', 2)

    assert_true(eq(a, a))
    assert_false(eq(a, b))
예제 #8
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)
예제 #9
0
def test_term__eq__():
    foo = Term.from_atom_name('foo')
    assert foo == foo

    bar = Term.from_atom_name('bar')
    assert foo != bar

    assert foo != Term()
    assert Term() != Term()
예제 #10
0
def test_term_logic():
    true = Term.from_atom_name('true')
    false = Term.from_atom_name('false')

    assert_true(true())
    assert_false(false())
    assert_true((true | false)())
    assert_false((true & false)())
    assert_false((false & true | false)())
    assert_true((false & false | true)())
    assert_false((false & (false | true))())
예제 #11
0
def test_term__or__():
    a = Term.from_atom_name('a')
    b = Term.from_atom_name('b')
    eq = Functor('=', 2)

    assert_true((eq(a, b) | eq(a, a))())
    assert_false((eq(a, b) | eq(b, a))())
    X = Term()
    assert_true((eq(X, a) | eq(X, b))())
    assert X == a or X == b

    assert_equal(str(eq(a, a) | eq(a, b)), 'a=a;a=b')
예제 #12
0
    def test_knowledge_database_2(self):
        self.dynamic(Term.from_functor(happy))
        self.dynamic(Term.from_functor(listens_2_music))
        self.dynamic(Term.from_functor(plays_air_guitar))

        self.assertz(happy(yolanda))
        self.assertz(listens_2_music(mia))
        self.assertz(self.rule(listens_2_music(yolanda), happy(yolanda)))
        self.assertz(self.rule(plays_air_guitar(mia), listens_2_music(mia)))
        self.assertz(self.rule(plays_air_guitar(yolanda),
                               listens_2_music(yolanda)))

        assert_true(plays_air_guitar(mia)())
예제 #13
0
def test_term_variable_assignment():
    a = Term.from_atom_name('a')
    b = Term.from_atom_name('b')
    eq = Predicate.from_name_arity('=', 2)

    X = Term()
    assert_true(eq(X, a))
    assert_equal(X, a)
    assert_false(eq(X, b))

    assert_true(eq(Term(), b))

    X.put_variable()
    assert_true(eq(X, b))
예제 #14
0
    def test_knowledge_database_4(self):
        self.retractall(woman(Term()))
        self.retractall(loves(Term(), Term()))

        self.dynamic(Term.from_functor(woman))
        self.dynamic(Term.from_functor(loves))

        self.assertz(woman(mia))
        self.assertz(woman(jody))
        self.assertz(woman(yolanda))

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

        with Frame() as f:
            X = f.term()
            # Creates a compound term: woman(X)
            # Then evaluates that compound term like once(woman(X))
            # Finds the first solution.
            woman(X)()
            assert_equal(X, mia)

        with Frame() as f:
            # Do the same thing by calling a predicate instead of evaluating
            # a compound term.
            # Creates the predicate `woman` then evaluates it with the argument
            # `X`. Again, finds the first solution.
            X = f.term()
            Predicate(woman)(X)
            assert_equal(X, mia)

            # Use a query to find all solutions.
        with Frame() as f:
            X = f.term()
            with Query(Predicate(woman), X) as q:
                q.next_solution()
                assert_equal(X, mia)
                q.next_solution()
                assert_equal(X, jody)
                q.next_solution()
                assert_equal(X, yolanda)
                assert_false(q.next_solution())

        with Frame() as f:
            X = f.term()
            assert_true((loves(marcellus, X) & woman(X))())
            assert_equal(X, mia)
예제 #15
0
    def test_knowledge_database_3(self):
        self.dynamic(Term.from_functor(happy))
        self.dynamic(Term.from_functor(listens_2_music))
        self.dynamic(Term.from_functor(plays_air_guitar))

        self.assertz(happy(vincent))
        self.assertz(listens_2_music(butch))
        self.assertz(self.rule(plays_air_guitar(vincent),
                               (listens_2_music(vincent) & happy(vincent))))
        self.assertz(self.rule(plays_air_guitar(butch), happy(butch)))
        self.assertz(self.rule(plays_air_guitar(butch),
                               listens_2_music(butch)))

        assert_false(plays_air_guitar(vincent)())
        assert_true(plays_air_guitar(butch)())
예제 #16
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)))
예제 #17
0
 def __init__(self, name, prolog_string=None, value=None, term=None):
     atom = Atom(name)
     super().__init__(
         type_='atom',
         value=(name if value is None else value),
         prolog_string=(name if prolog_string is None else prolog_string),
         atom=atom,
         functor=Functor(atom, 0))
     self.term = Term.from_atom(atom) if term is None else term
예제 #18
0
def test_frame_dynamic_database():
    dynamic = Predicate.from_name_arity('dynamic', 1)
    assertz = Predicate.from_name_arity('assertz', 1)

    foo = Functor('foo', 1)
    a = Term.from_atom_name('a')

    dynamic(foo(Term()))

    with Frame(discard=True):
        assertz(foo(a))
        assert_true(foo(a)())

    # Frames have no effect on the dynamic database
    assert_true(foo(a)())
예제 #19
0
def test_term__and__():
    a = Term.from_atom_name('a')
    b = Term.from_atom_name('b')
    eq = Functor('=', 2)

    assert_true((eq(a, a) & eq(b, b))())
    assert_false((eq(a, b) & eq(a, a))())
    assert_false((eq(a, a) & eq(a, b))())

    X = Term()
    Y = Term()
    assert_true((eq(X, a) & eq(Y, a) & eq(X, Y))())
    assert_equal(X, Y)
    X.put_variable()
    Y.put_variable()
    assert_false((eq(X, a) & eq(X, b))())

    assert_equal(str(eq(a, a) & eq(a, b)), 'a=a,a=b')
예제 #20
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')
예제 #21
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))))
예제 #22
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)))
예제 #23
0
 def __init__(self):
     # Pointer needs to be a valid pointer. Get one using ctypes
     pointer = ctypes.addressof(ctypes.c_int())
     super().__init__(type_='integer', value=pointer,
                      prolog_string=str(pointer), is_pointer=True)
     self.term = Term.from_pointer(pointer)
예제 #24
0
 def __init__(self):
     super().__init__('[bar]')
     self.term = Term.from_cons_list(Term.from_atom_name('bar'),
                                     Term.from_nil())
예제 #25
0
 def __init__(self):
     super().__init__('[-1,2,a,3.4]')
     self.term = Term.from_parsed('[-1,2,a,3.4]')
예제 #26
0
 def __init__(self):
     super().__init__("['F',o,o]")
     self.term = Term.from_list_chars('Foo'.encode('ascii'))
예제 #27
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)
예제 #28
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))
예제 #29
0
 def __init__(self):
     super().__init__('[%v|%v]', is_ground=False)
     self.term = Term.from_list()
예제 #30
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())