示例#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
示例#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')])])
示例#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)
示例#4
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)