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_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_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_queryUndefinedPredicate(): db1 = facts([p()]) match3(db1, [], q(), [])
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), [])