Example #1
0
def kb():
    kb = Knowledge()

    l1 = lit(pred('y', 1), [var('X')])
    l2 = lit(pred('x', 2), [var('X'), var('Y')])
    l3 = lit(pred('z', 1), [var('Y')])

    c1 = clause(l1, [l2, l3])
    kb.assert_clause(c1)
    assert kb.db

    kb.retract_clause(c1)
    assert not kb.db
Example #2
0
def equals():
    kb = Knowledge()
    prover = Prover(kb)

    l1 = lit(pred('y',1), [var('X')])
    l2 = lit(pred('=',2), [var('X'), var('Y')])
    l3 = lit(pred('z',1), [var('Y')])

    c1 = clause(l1, [l2, l3])
    kb.assert_clause(c1)

    for v in ('foo', 'bar'):
        kb.assert_clause(clause(lit(pred('z',1), [const(v)]), []))

    query = lit(pred('y',1),[var('X')])
    answer = prover.ask(query)
    assert set(answer) == set([lit(pred('y',1), [const('foo')]), lit(pred('y',1), [const('bar')])])
Example #3
0
def ask():
    kb = Knowledge()
    prover = Prover(kb)

    l1 = lit(pred('y', 1), [var('X')])
    l2 = lit(pred('x', 2), [var('X'), var('Y')])
    l3 = lit(pred('z', 1), [var('Y')])

    c1 = clause(l1, [l2, l3])
    kb.assert_clause(c1)

    for z in ('alice', 'mary', 'susan'):
        head = lit(pred('z',1), [const(z)])
        body = []
        ca = clause(head, body)
        kb.assert_clause(ca)

    for x,y in [('alice', 'mary'), ('susan', 'mary')]:
        head = lit(pred('x',2), [const(x), const(y)])
        body = []
        ca = clause(head, body)
        kb.assert_clause(ca)

    query = lit(pred('y',1),[var('X')])
    answer = prover.ask(query)
    
    assert set(answer) == set([lit(pred('y',1), [const('alice')]), lit(pred('y',1), [const('susan')])])

    query = lit(pred('x',2), [var('X'), const('alice')])
    answer = prover.ask(query)
    assert not set(answer)

    query = lit(pred('nothere',1), [var('X')])
    answer = prover.ask(query)
    assert not set(answer)
Example #4
0
def clause_safe():
    kb = Knowledge()
    l1 = lit(pred('y', 1), [var('X')])
    l2 = lit(pred('x', 2), [var('X'), var('Y')])

    c1 = clause(l1, [l2])
    c2 = clause(l2, [l1])
    
    assert kb.is_safe(c1)
    assert not kb.is_safe(c2)

    l3 = lit(pred('z', 3), [var('A'), const('alice'), const('mary')])
    c3 = clause(l3, [l3])
    assert kb.is_safe(c3)

    l4 = lit(pred('z', 1), [var('Z')], False)

    c4 = clause(l1, [l2, l4])
    assert not kb.is_safe(c4)

    l5 = lit(pred('z', 1), [var('X')], False)

    c5 = clause(l1, [l5])
    assert not kb.is_safe(c5)

    c6 = clause(l1, [l2, l5])
    assert kb.is_safe(c6)
Example #5
0
def primitives():
    kb = Knowledge()
    prover = Prover(kb)

    def testpred(literal, prover):
        if literal[0].is_const():
            yield clause(literal)
        else:
            yield clause(lit(literal.pred, [const('foo')]))
            yield clause(lit(literal.pred, [const('bar')]))
            yield clause(lit(literal.pred, [const('baz')]))

    kb.add_primitive(pred('testpred',1), testpred)

    l1 = lit(pred('y', 1), [var('X')])
    l2 = lit(pred('testpred', 1), [var('X')])

    c1 = clause(l1, [l2])
    kb.assert_clause(c1)

    query = lit(pred('y',1),[var('X')])
    answer = prover.ask(query)
    assert set(answer) == set([lit(pred('y',1), [const('foo')]), lit(pred('y',1), [const('bar')]), lit(pred('y',1), [const('baz')])])

    query = lit(pred('y',2), [const('twelve'), var('X')])
    answer = prover.ask(query)
    assert not set(answer)

    kb.assert_clause(clause(lit(pred('testpred', 1), [const('quux')]), []))
    query = lit(pred('y',1),[var('X')])
    answer = prover.ask(query)
    assert set(answer) == set([lit(pred('y',1), [const('foo')]), lit(pred('y',1), [const('bar')]), lit(pred('y',1), [const('baz')]), lit(pred('y',1), [const('quux')])])

    query = lit(pred('y',2), [const('twelve'), var('X')])
    answer = prover.ask(query)
    assert not set(answer)