def test_TestUnstratifiable1(): program = [ p() <= ~q(), q() <= ~p(), ] with pytest.raises(DatalogValidationException): match_relations(program, None, None)
def test_testMultiLayeredNegation(): program = [ p() <= ~q(), q() <= ~r(), r() <= ~s(), ] assert match_relations(program, p(), [p()])
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_queryNonRecursiveIDBPredicate(): db = facts([p("a", "b"), p("b", "c"), p("c", "d")]) rules = [q(X, Y) <= [p(X, Z), p(Z, Y)]] ans = match3(db, rules) ans(q(X, Y), [(a, c), (b, d)]) ans(q(X, c), [(a, )]) ans(q("x", b), [])
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), [])
def test_queryNonLinearlyRecursiveIDBPredicate(): db = facts([ p("a", "b"), p("b", "c"), p("c", "d"), ]) rules = [ q(X, Y) <= p(X, Y), q(X, Y) <= [q(X, Z), q(Z, Y)], ] ans = match3(db, rules) ans(q(X, Y), [(a, b), (a, c), (a, d), (b, c), (b, d), (c, d)]) ans(q(a, X), [(b, ), (c, ), (d, )])
def test_queryLinearlyRecursiveIDBPredicate(): # // Acyclic transitive closure. db = facts([ p("a", "b"), p("b", "c"), p("c", "d"), ]) rules = [ q(X, Y) <= p(X, Y), q(X, Y) <= [p(X, Z), q(Z, Y)], ] ans = match3(db, rules) ans(q(X, Y), [(a, b), (a, c), (a, d), (b, c), (b, d), (c, d)]) ans(q("a", X), [(b, ), (c, ), (d, )])
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(), [])
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)], )
def test_queryNonLinearlyRecursiveIDBPredicate_withCycle(): # // Transitive closure with a cycle. db = facts([ p("a", "b"), p("b", "c"), p("c", "d"), p("d", "c"), ]) rules = [ q(X, Y) <= p(X, Y), q(X, Y) <= [q(X, Z), q(Z, Y)], ] ans = match3(db, rules) ans( q(X, Y), [(a, b), (a, c), (a, d), (b, c), (b, d), (c, c), (c, d), (d, c), (d, d)], )
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)])
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_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_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")])
def test_transitive_closure_with_a_cycle(): # // Transitive closure with a cycle. db = facts([ p("a", "b"), p("b", "c"), p("c", "d"), p("d", "c"), ]) rules = [q(X, Y) <= p(X, Y), q(X, Y) <= [p(X, Z), q(Z, Y)]] ans = match3(db, rules) ans( q(X, Y), [(a, b), (a, c), (a, d), (b, c), (b, d), (c, c), (c, d), (d, c), (d, d)], )
def test_testNegatedUnboundVariable3(): with pytest.raises(DatalogValidationException): match_relations([p() <= [~q(X, a), ~r(X)]], p(), [])
def test_testImpossibleBinaryUnification2(): assert match_relations([p() <= [eq( Z, b), eq(X, Y), eq(a, X), eq(Z, Y)]], p(), [])
def testImpossibleBinaryUnification1(): assert match_relations([p() <= eq(a, b)], p(), [])
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), [])
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)])
def test_testBinaryDisunificationFail1(): program = [p() <= [~q(a, b), not_eq(X, _)]] with pytest.raises(DatalogValidationException): match_relations(program, p(), [])
def test_testImpossibleBinaryDisunification3(): program = [p() <= [q(X), eq(X, X)]] ans = match_relations(program) assert ans(p(), [])
def test_testExplicitUnification2(): program = [p() <= [~q(X, b), not_eq(X, a)]] with pytest.raises(DatalogValidationException): match_relations(program, p(), [])
def test_testUnboundVariable1(): a = "a" b = "b" with pytest.raises(DatalogValidationException): match_relations([p(X, b)], p(X, Y), []) match_relations([q(X, Y) <= p(X, b), p(a, b)], q(X, Y), [])
def test_testExplicitUnification1(): program = [p() <= [~q(X, b), eq(X, a)]] assert match_relations(program, p(), [p()])
def test_testBinaryDisunificationNoAtom(): program = [p() <= not_eq(a, b), q() <= [not_eq(X, Y), eq(X, a), eq(Y, b)]] ans = match_relations(program) assert ans(p(), [p()]) assert ans(q(), [q()])
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)])
def test_testImpossibleBinaryDisunification1(): assert match_relations([p() <= not_eq(a, a)], p(), [])
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)
def test_testImpossibleBinaryDisunification2(): program = [p() <= [eq(Z, a), not_eq(X, Y), eq(a, X), eq(Z, Y)]] assert match_relations(program, p(), [])