def test_testMultiLayeredNegation(): program = [ p() <= ~q(), q() <= ~r(), r() <= ~s(), ] assert match_relations(program, p(), [p()])
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), ], )
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()])
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()])
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), [])
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()])
def test_testNegatedUnboundVariable3(): with pytest.raises(DatalogValidationException): match_relations([p() <= [~q(X, a), ~r(X)]], p(), [])
def test_queryIDBPredicateWithExplicitIDBFact(): # // Note that q is defined by a rule, but we also give an explicit fact. db = facts([r("a", "b"), q("b", "c")]) rules = [q(X, Y) <= r(X, Y)] ans = match3(db, rules) ans(q(X, Y), [(a, b), (b, c)])
def test_queryIDBPredicateWithUndefinedEDB(): db = facts([r("a", "b")]) rules = [q(X, Y) <= p(X, Y)] ans = match3(db, rules) ans(q(X, Y), [])