Пример #1
0
def test_testRulesWithTrue():
    a = "a"
    true = relation("true")
    false = relation("false")

    assert match_relations([false()], true(), [])
    assert match_relations([p(X) <= q(X), q(a)], true(), [])
Пример #2
0
def test_TestUnstratifiable1():
    program = [
        p() <= ~q(),
        q() <= ~p(),
    ]
    with pytest.raises(DatalogValidationException):
        match_relations(program, None, None)
Пример #3
0
def test_testRulesWithNoVariables():
    program = [p() <= q("a", "b"), q("a", "b")]
    ans = match_relations(program)
    assert ans(p(), [p()])

    program = [p() <= [q("a", "b"), r("c", "d")], q("a", "b"), r("c", "d")]
    ans = match_relations(program)
    assert ans(p(), [p()])
Пример #4
0
def test_testRulesWithLongBodies():
    a = relation("a")
    b = relation("b")
    c = relation("c")
    d = relation("d")
    e = relation("e")
    f = relation("f")
    g = relation("g")
    A, B, C, D, E, F, G = variables("A", "B", "C", "D", "E", "F", "G")
    X = variables(*["X" + str(r) for r in range(1, 23)])

    program = [
        p(A, B, C, D, E, F, G) <=
        [a(A), b(B), c(C), d(D), e(E),
         f(F), g(G)],
        a(1),
        b(1),
        c(1),
        d(1),
        d(2),
        e(1),
        f(1),
        g(1),
    ]

    ans = match_relations(program)
    assert ans(p(A, B, C, D, E, F, G),
               [p(1, 1, 1, 1, 1, 1, 1),
                p(1, 1, 1, 2, 1, 1, 1)])

    foo1 = "foo1"
    foo2 = "foo2"
    foo3 = "foo3"
    foo4 = "foo4"
    foo5 = "foo5"
    bar = "bar"
    program = [
        p(A, B, C, D, E) <= [
            a(A, X[1], X[2], X[3], X[5]),
            b(X[6], B, X[7], X[8], X[9]),
            c(X[10], X[11], C, X[12], X[13]),
            d(X[14], X[15], X[16], D, X[17]),
            e(X[18], X[19], X[20], X[21], E),
        ],
        a(foo1, 2, 3, 4, 5),
        b(6, foo2, 8, 9, 10),
        c(11, 12, foo3, 14, 15),
        d(16, 17, 18, foo4, 20),
        e(21, 22, 23, 24, foo5),
        c(foo1, foo2, bar, foo4, foo5),
    ]
    ans = match_relations(program)
    assert ans(
        p(A, B, C, D, E),
        [p(foo1, foo2, foo3, foo4, foo5),
         p(foo1, foo2, bar, foo4, foo5)],
    )
Пример #5
0
def test_queryIDBPredicatesWithConstants():
    program = [p("a", X) <= q(X, X), q("a", "a"), q("b", "b"), q("c", "d")]

    ans = match_relations(program)
    assert ans(p(Z, W), [p("a", "a"), p("a", "b")])
    assert ans(p("a", X), [p("a", "a"), p("a", "b")])

    program = [p(X, X) <= q(X, "a"), q("a", "a"), q("b", "b"), q("c", "d")]
    ans = match_relations(program)
    assert ans(p("a", X), [p("a", "a")])
    assert ans(p(X, "a"), [p("a", "a")])
Пример #6
0
def test_testBinaryUnificationNoAtom():
    program = [
        p(X, b) <= eq(X, a),
        p(b, Y) <= eq(Y, a),
        p(X, Y) <= [eq(X, c), eq(Y, d)],
        p(X, X) <= eq(X, c),
        p(X, Y) <= [eq(X, d), eq(Y, X)],
        p(X, Y) <= [eq(X, Y), eq(X, e)],
    ]

    ans = match_relations(program)
    assert ans(p(X, Y), [p(a, b), p(b, a), p(c, d), p(c, c), p(d, d), p(e, e)])
    program = program + [q(X, Y) <= p(X, Y)]
    ans = match_relations(program)
    assert ans(q(X, Y), [q(a, b), q(b, a), q(c, d), q(c, c), q(d, d), q(e, e)])
Пример #7
0
def test_queryZeroAryPredicates():
    program = [p() <= q(), r() <= [p(), s()], q(), s()]
    ans = match_relations(program)
    assert ans(q(), [q()])
    assert ans(p(), [p()])
    assert ans(s(), [s()])
    assert ans(r(), [r()])
Пример #8
0
def test_testMultiLayeredNegation():
    program = [
        p() <= ~q(),
        q() <= ~r(),
        r() <= ~s(),
    ]
    assert match_relations(program, p(), [p()])
Пример #9
0
def test_testNegatedAtomFirst():
    n = relation("n")
    e = relation("e")
    unreach = relation("unreach")
    program = [
        n(a),
        n(b),
        n(c),
        e(a, b),
        e(b, c),
        r(X, Y) <= e(X, Y),
        r(X, Y) <= [e(X, Z), r(Z, Y)],
        unreach(X, Y) <= [~r(X, Y), n(X), n(Y)],
    ]
    assert match_relations(
        program,
        unreach(X, Y),
        [
            unreach(b, a),
            unreach(c, b),
            unreach(c, a),
            unreach(a, a),
            unreach(b, b),
            unreach(c, c),
        ],
    )
Пример #10
0
def test_testBinaryDisunificationFail2():
    program = [p(X) <= [q(X), not_eq(Y, _)], q(a)]
    with pytest.raises(DatalogValidationException):
        match_relations(program, p(X), [])


# def test_gt():
#     age = relation("age")
#     senior = relation("senior")
#     program = [
#         age("John", 28),
#         age("Mary", 40),
#         age("Dad", 60),
#         age("Mom", 70),
#         senior(X, Y) <= [age(X, Y), gt(Y, 59)]
#     ]
#     ans = match_relations(program)
#     assert ans(senior(X, Y), [age("Dad", 60), age("Mom", 70)])
Пример #11
0
def test_queryEDBPredicate():
    a_q1 = q("a", "b", "c", "d", "e")
    a_q2 = q("e", "d", "c", "b", "a")
    db = [p(), a_q1, a_q2]
    assert match_relations(db, p(), [p()])

    ans = match3(facts(db), [])
    ans(q(V, W, X, Y, Z), [(a, b, c, d, e), (e, d, c, b, a)])
    ans(q(W, "b", X, Y, Z), [(a, c, d, e)])
    ans(q(W, X, "d", Y, Z), [])
Пример #12
0
def testRulesWithAnonymousVariables():
    or_ = relation("or")
    on = relation("on")
    L, L1 = variables("L", "L1")
    a = "a"
    b = "b"
    c = "c"
    d = "d"
    program = [
        on(L) <= [or_(L, L1, _), on(L1)],
        on(L) <= [or_(L, _, L1), on(L1)],
        or_(a, b, c),
        on(b),
    ]
    ans = match_relations(program)
    assert ans(on(a), [on(a)])
    assert ans(on(_), [on(a), on(b)])
    program = [p() <= [q(_, _), r(_, _)], q(a, b), r(c, d)]
    ans = match_relations(program)
    assert ans(p(), [p()])
Пример #13
0
def test_testRulesWithUnusedVariables1():
    or_ = relation("or")
    on = relation("on")
    L, L1 = variables("L", "L1")
    a = "a"
    b = "b"
    c = "c"
    program = [on(L) <= [or_(L, L1, X), on(L1)], or_(a, b, c), on(b)]
    ans = match_relations(program)
    assert ans(on(a), [on(a)])
    assert ans(on(X), [on(a), on(b)])
Пример #14
0
def test_testRulesWithRepeatedVariablesInAnAtom():
    program = [
        p(X) <= q(X, X),
        q("a", "a"),
        q("a", "b"),
        q("b", "b"),
    ]
    ans = match_relations(program)
    assert ans(p(X), [p("a"), p("b")])
    program = [
        p(X, Y) <= q(Y, X, Y, X),
        q("a", "a", "a", "a"),
        q("a", "a", "a", "b"),
        q("a", "a", "b", "b"),
        q("a", "b", "b", "b"),
        q("b", "b", "b", "b"),
    ]

    ans = match_relations(program)

    assert ans(p(X, Y), [p("a", "a"), p("b", "b")])
Пример #15
0
def test_querymutuallyrecursivepredicates():
    program = [
        p(X, Y, Z) <= q(X, Y, Z),
        q(X, Y, Z) <= p(Z, Y, X),
        p(X, Y, Z) <= r(X, Y, Z),
        r("a", "b", "c"),
    ]

    ans = match_relations(program)
    assert ans(p(X, Y, Z), [p("a", "b", "c"), p("c", "b", "a")])
    assert ans(p(X, "b", Y), [p("a", "b", "c"), p("c", "b", "a")])
    assert ans(p("a", X, "c"), [p("a", "b", "c")])
    assert ans(p(X, "c", Y), [])
Пример #16
0
def test_TestStratifiable_1():
    program = [
        edge(a, b),
        edge(b, c),
        tc(a, c),
        tc(X, Y) <= edge(X, Y),
        tc(X, Y) <= [tc(X, Z), tc(Z, Y)],
        node(X) <= edge(X, _),
        node(X) <= edge(_, X),
        not_tc(X, Y) <= [node(X), node(Y), ~tc(X, Y)],
    ]
    ans = match_relations(program)
    assert ans(tc(X, Y), [tc(a, b), tc(b, c), tc(a, c)])
Пример #17
0
def test_testRuleThatEndsInAGroundAtom():

    edge = relation("edge")
    tc = relation(("tc"))
    trigger = relation("trigger")
    program = [
        edge(0, 1),
        edge(1, 2),
        edge(2, 3),
        tc(X, Y) <= edge(X, Y),
        tc(X, Y) <= [edge(X, Z), tc(Z, Y), trigger()],
    ]

    ans = match_relations(program)
    assert ans(tc(X, Y), [tc(0, 1), tc(1, 2), tc(2, 3)])

    program = program + [trigger()]
    ans = match_relations(program)
    assert ans(
        tc(X, Y),
        [tc(0, 1), tc(0, 2),
         tc(0, 3), tc(1, 2),
         tc(1, 3), tc(2, 3)])
Пример #18
0
def test_queryNestedPredicates():
    one = relation("1")
    two = relation("2")
    three = relation("3")
    four = relation("4")
    five = relation("5")
    program = [
        one(X, Y, Z) <= two(X, Y, Z),
        two(X, Y, Z) <= three(X, Y, Z),
        three(X, Y, Z) <= four(X, Y, Z),
        four(X, Y, Z) <= five(X, Y, Z),
        five("a", "b", "c"),
    ]

    assert match_relations(program, one(X, Y, Z), [one("a", "b", "c")])
Пример #19
0
def test_testRulesWithBinaryUnifiers():

    program = [
        tc(X, Y) <= edge(X, Y),
        tc(X, Y) <= [edge(X, Z), tc(Z, Y)],
        edge(a, b),
        edge(b, c),
        edge(c, c),
        edge(c, d),
        cycle(X) <= [eq(X, Y), tc(X, Y)],
        beginsAtC(X, Y) <= [tc(X, Y), eq(c, X)],
    ]
    ans = match_relations(program)
    assert ans(cycle(X), [cycle(c)])
    assert ans(beginsAtC(X, Y), [beginsAtC(c, c), beginsAtC(c, d)])
Пример #20
0
def test_testRulesWithBinaryDisunifiers():
    program = [
        tc(X, Y) <= edge(X, Y),
        tc(X, Y) <= [edge(X, Z), tc(Z, Y)],
        edge(a, b),
        edge(b, c),
        edge(c, c),
        edge(c, d),
        noncycle(X, Y) <= [not_eq(X, Y), tc(X, Y)],
        beginsNotAtC(X, Y) <= [tc(X, Y), not_eq(c, X)],
        noC(X, Y) <= [edge(X, Y), not_eq(X, c),
                      not_eq(Y, c)],
        noC(X, Y) <= [noC(X, Z), noC(Z, Y)],
    ]

    ans = match_relations(program)
    assert ans(
        noncycle(X, Y),
        [
            noncycle(a, b),
            noncycle(a, c),
            noncycle(a, d),
            noncycle(b, c),
            noncycle(b, d),
            noncycle(c, d),
        ],
    )
    assert ans(
        beginsNotAtC(X, Y),
        [
            beginsNotAtC(a, b),
            beginsNotAtC(a, c),
            beginsNotAtC(a, d),
            beginsNotAtC(b, c),
            beginsNotAtC(b, d),
        ],
    )
    assert ans(noC(X, Y), [noC(a, b)])
Пример #21
0
def testImpossibleBinaryUnification1():
    assert match_relations([p() <= eq(a, b)], p(), [])
Пример #22
0
def test_TestUnstratifiable2():
    edb = relation("edb")
    program = [p(X) <= [~q(X), edb(X)], q(X) <= [~p(X), edb(X)], edb(a)]
    with pytest.raises(DatalogValidationException):
        match_relations(program, None, None)
Пример #23
0
def test_testRelated2():
    from tests.data.abcdatalog.related2 import related_data as program, _exp_ancestors

    ans = match_relations(program)
    assert ans(check(X, Y), [])
    assert ans(
        siblings(X, Y),
        [
            siblings(carson, deidra),
            siblings(deidra, carson),
            siblings(harley, gonzalo),
            siblings(gonzalo, harley),
            siblings(eldon, fern),
            siblings(fern, eldon),
            siblings(noe, odell),
            siblings(odell, noe),
            siblings(reanna, sona),
            siblings(sona, reanna),
            siblings(terra, ursula),
            siblings(ursula, terra),
        ],
    )

    assert ans(ancestor(X, Y), _exp_ancestors)
    assert ans(
        same_generation(gonzalo, Y),
        [
            same_generation(gonzalo, harley),
            same_generation(gonzalo, eldon),
            same_generation(gonzalo, fern),
        ],
    )
    assert ans(
        same_generation(X, reanna),
        [
            same_generation(sona, reanna),
            same_generation(terra, reanna),
            same_generation(ursula, reanna),
        ],
    )
    assert ans(
        unrelated(adria, Y),
        [
            unrelated(adria, barrett),
            unrelated(adria, ignacia),
            unrelated(adria, kati),
            unrelated(adria, lauretta),
            unrelated(adria, mayra),
            unrelated(adria, noe),
            unrelated(adria, odell),
            unrelated(adria, reanna),
            unrelated(adria, sona),
            unrelated(adria, terra),
            unrelated(adria, ursula),
            unrelated(adria, virgilio),
        ],
    )
    assert ans(
        unrelated(X, odell),
        [
            unrelated(adria, odell),
            unrelated(barrett, odell),
            unrelated(carson, odell),
            unrelated(harley, odell),
            unrelated(gonzalo, odell),
            unrelated(deidra, odell),
            unrelated(eldon, odell),
            unrelated(fern, odell),
        ],
    )
Пример #24
0
def test_testNegatedNoPositiveAtom():
    program = [p(a) <= ~q(a), p(b) <= [~q(X), eq(X, b)]]
    assert match_relations(program, p(X), [p(a), p(b)])
Пример #25
0
def test_testExplicitUnification2():
    program = [p() <= [~q(X, b), not_eq(X, a)]]
    with pytest.raises(DatalogValidationException):
        match_relations(program, p(), [])
Пример #26
0
def test_testExplicitUnification1():
    program = [p() <= [~q(X, b), eq(X, a)]]
    assert match_relations(program, p(), [p()])
Пример #27
0
def test_testBinaryDisunificationFail1():
    program = [p() <= [~q(a, b), not_eq(X, _)]]
    with pytest.raises(DatalogValidationException):
        match_relations(program, p(), [])
Пример #28
0
def test_testNegatedUnboundVariable3():
    with pytest.raises(DatalogValidationException):
        match_relations([p() <= [~q(X, a), ~r(X)]], p(), [])
Пример #29
0
def test_testImpossibleBinaryUnification2():
    assert match_relations([p() <= [eq(
        Z, b), eq(X, Y), eq(a, X), eq(Z, Y)]], p(), [])
Пример #30
0
def testUselessBinaryUnification():
    program = [p(X) <= [q(X), eq(X, Y)], q(a), p(b) <= eq(X, _)]
    ans = match_relations(program)
    with pytest.raises(DatalogValidationException):
        ans(p(X), [])