120 likes | 274 Views
4. Datalog Queries. Datalog query – a finite set of rules of the form: R 0 (x 1 ,…,x k ) : – R 1 (x 1,1 ,…, x 1,k 1 ),..., R n (x n,1 ,…, x n,k n ) where each R i is either an input or a defined relation name. including built-in relations such as +(x,y,z) which means
E N D
4. Datalog Queries Datalog query– a finite set of rules of the form: R0(x1,…,xk) :– R1(x1,1,…, x1,k1),..., Rn(xn,1,…, xn,kn) where each Ri is either an input or a defined relation name. including built-in relations such as +(x,y,z) which means x + y = z. (We normally use the latter syntax.) head of the rule – R0 body of the rule– R1,…,Rn
Example: Find the SSN and the tax. Tax_Due(s, t) :– Taxrecord(s, w, i, c), Taxtable(inc, t), w+i+c = inc. Find the streets that can be reached from (x0,y0). Reach(n) :– Street(n, x0, y0). Reach(n) :– Reach(m), Street(m, x, y), Street(n, x, y). Find the time to travel from x to y. Travel(x, y, t) :– Go(x, 0, y, t). Travel(x, y, t) :– Travel(x, z, t2), Go(z, t2, y, t).
Example: Find town points covered by a radio station Covered(x2, y2) :– Broadcast(n, x, y), Town(t, x2, y2), Parameters(n, s, blat, blong), Parameters(t, s2, tlat, tlong), x2 = x + (tlat – blat), y2 = y + (tlong – blong).
4.2 Datalog with Sets Example: Hamiltonian Cycle Input: • Vertices(S) where S is a set of vertices • Edge ({c1}, {c2}) if there is an edge from c1 to c2 • Start({c}) where c is start city name Output: • Path ({c}, B) if there is a path from c that uses all vertices except those in B. • Hamiltonian ({c}) if there is a Hamiltonian path.
Base case – Path is a single vertex. All vertices except the start vertex is unvisited. Path(X1, B) :– Vertices(A), Start(X1), B = A \ X1. Recursion – a path to X1 with B unvisited exists if there is Path(X1, B) : – Path(X2, A), a path to X2 with A unvisited Edge(X2, X1), and an edge from X2 to X1, X1 A, which is unvisited, and B = A \ X1. B is A minus X1
If there is a path from start to X2 that visits all vertices and an edge from X2 to start, then there is a Hamiltonian cycle. Hamiltonian(X1) :– Path(X2, ), Edge(X2, X1), Start(X1).
4.4 Datalog with Abstract Data Types Example: Streets(Name, Extent) where extent is a set of 2D points. Let (x0, y0) be a start location. Express the reach relation: Reach(n) :– Street(n, Extent), {(x0,y0)} Extent. Reach(n) :– Reach(m), Street(m, S1), Street(n, S2), S1 S2
4.5 Semantics Rule instantiation – substitution of variables by constants ⊢Q,I R(a1,…..ak)– R(a1,….ak) has a proof using query Q and input database I, iff • R represents input relation r and (a1,….ak) r , or • There is some rule and instantiation R(a1,…,ak):–R1(a1,1,…,a1,k1),…, Rn(an,1,…, an, kn). where ⊢Q,I Ri(ai,1,…,ai,ki) for each 1 i n .
Example: Reach(Vine) Reach(Vine) :– Street(Vine, 5, 2). Reach(Bear) Reach(Bear) :– Reach(Vine), Street(Vine, 5, 12), Street(Bear, 5, 12). Reach(Hare) Reach(Hare) :– Reach(Bear), Street(Bear, 8, 13), Street(Hare, 8, 13).
Example: By the input database (Figure 1.2): Go(Omaha, 0, Lincoln, 60) Go(Lincoln, 60, Kansas_City, 210) Go(Kansas_City, 210, Des_Moines, 390) Go(Des_Moines, 390, Chicago, 990) We also have: Travel(Omaha, Lincoln, 60) Travel(Omaha, Lincoln, 60):- Go(Omaha, 0, Lincoln, 60) Travel(Omaha, Kansas_City, 210) Travel(Omaha,Kansas_City,210):- Travel(Omaha,Lincoln,60), Go(Lincoln,60,Kansas_City,210).
Proof-based semantics – derived relations are the set of tuples that can be proven. Fixed point semantics – an interpretation of the derived relations such that nothing new can be proven. Least fixed point semantics – smallest possible FP semantics. Proof-based semantics = Least fixed point semantics