Beruflich Dokumente
Kultur Dokumente
A
0
= {(bert, alice), (bert, george), (alice, derek), (alice,
part), (derek, frank)}
Step 2: D
2
= {(bert, derek), (bert, pat), (alice, frank)}
A
2
= D
2
A
1
= {(bert, alice), (bert, george), (alice, derek), (alice,
part), (derek, frank), {(bert, derek), (bert, pat),
(alice, frank)}
Deductive Databases
Jan. 2012 Yangjun Chen ACS-3902 52
Datalog programs and their evaluation
- evaluation of recursive queries
- semi-naive strategy
Example:
Step 3: D
3
= {(bert, frank)
A
3
= D
3
A
2
= {(bert, alice), (bert, george), (alice, derek), (alice,
part), (derek, frank), {(bert, derek), (bert, pat),
(alice, frank), (bert, frank)}
Step 3: D
4
= C.
The advantage of the semi-naive method is that at each step a differential term
D
i
is used in each equation instead of the whole R
i
. In this way, the time
complexity of a computation is decreased drastically.
Deductive Databases
Jan. 2012 Yangjun Chen ACS-3902 53
Datalog programs and their evaluation
- evaluation of recursive queries
- The magic-set rule rewriting technique
motivation:
1. During a bottom-up evaluation, too many irrelevant tuples are
evaluated.
For example, to evaluate the query sg(john, Z)? using the
following rules:
sg(X, Y) flat(X, Y),
sg(X, Y) up(X, Z), sg(Z, W), down(W, Y),
a bottom-up method will generate all sg-tuples and then makes
a selection operation to the anwsers.
2. Using the constants appearing in the query to restrict
computation.
Deductive Databases
Jan. 2012 Yangjun Chen ACS-3902 54
Datalog programs and their evaluation
- evaluation of recursive queries
- The magic-set rule rewriting technique
sg(X, Y) magic_sg(X) ,flat(X, Y),
sg(X, Y) magic_sg(X), up(X, Z), sg(Z, W), down(W, Y),
magic_sg(Z) magic_sg(X), up(X, Z),
magic_sg(john).
Two-phase evaluation:
1st phase: evaluate magic rules to generate a magic set.
2nd phase: evaluate modified rules, by which that magic
set is used to restrict the computation.
Deductive Databases
Jan. 2012 Yangjun Chen ACS-3902 55
Datalog programs and their evaluation
- evaluation of recursive queries
- Stratified databases
A stratified database is a Datalog program containing negated
predicates.
Example: Suppose that a supplier might wish to backorder items
that are not in the warehouse. It would be convenient to write:
backorder(X) item(X), warehouse(X).
Its logically equivalent form is
backorder(X), warehouse item(X).
But this rule has a different meaning : if X is an item, then
backorder it or it is stored in the warehouse. This is not what we
want.
Deductive Databases
Jan. 2012 Yangjun Chen ACS-3902 56
Datalog programs and their evaluation
- evaluation of recursive queries
- Stratified databases
- Prolblem: recursion via negation
p(X) q(X),
q(X) p(X).
- To avoid the recursion via negation, we introduce the concept
of stratification, which is defined by the use of a level l mapping.
level l mapping: assign each literal in the program an integer
such that if
B A
1
, , A
n
and A
i
is positive, then l(A
i
) s l(B) for all i, 1 s i s n. If A
i
is
negative, then l(B) < l(A
i
) for all i, 1 s i s n.
Deductive Databases
Jan. 2012 Yangjun Chen ACS-3902 57
Datalog programs and their evaluation
- evaluation of recursive queries
- Stratified databases
- Prolblem: recursion via negation
p(X) q(X),
q(X) p(X).
- To avoid the recursion via negation, we introduce the concept
of stratification, which is defined by the use of a level l mapping.
level l mapping: assign each literal in the program an integer
such that if
B A
1
, , A
n
and A
i
is positive, then l(A
i
) s l(B) for all i, 1 s i s n. If A
i
is
negative, then l(B) < l(A
i
) for all i, 1 s i s n.
Deductive Databases
Jan. 2012 Yangjun Chen ACS-3902 58
Datalog programs and their evaluation
- evaluation of recursive queries
- Stratified databases
- If you can assign integers to all the literals in a programusing a
level mapping, then this program is stratifiable.
p(X) q(X),
q(X) p(X).
In fact, we cannot find a level mapping for any program which
contains recursion via negation.
- Evaluation of a stratified database.
Evaluate the literals in the program from low level to the high
level.
Deductive Databases
Jan. 2012 Yangjun Chen ACS-3902 59
Datalog programs and their evaluation
- evaluation of recursive queries
- Stratified databases
- However, you cannot find any level mapping for the following
program:
Example:
path(X, Y) edge(X, Y),
path(X,Y) edge(X, Z), path(Z, Y),
acyclic_path(X, Y) path(X,Y), path(Y, X).
We can many label mappings for this program. The following are
just two of them: