• Keine Ergebnisse gefunden

Computing single-piece unifiers and their aggregation

7 Implementation and Experiments

7.1 Computing single-piece unifiers and their aggregation

We callsingle-piece aggregatorthe rewriting operator that computes the set of one-step rewritings of a queryQby considering all the most general single-piece aggregated unifiers ofQ.

Theorem 7 The single-piece aggregator is sound, complete and prunable.

Proof:Soundness comes from Property 9 and from the fact that for any set of rulesR, let the ruleRbe its aggregation, one hasR |=R. Completeness and prunability rely on the fact that the piece-based rewriting operator fulfills these properties and the fact that for any queriesQandQ0and any ruleR, ifQ0=β(Q, R, µ), whereµis a piece-unifier, then the queryQ00obtained with the single-piece aggregator corresponding to µis more general thanQ0, as expressed by Property 10.

7 Implementation and Experiments

As explained in Section 6, we now restrict our focus to rules with an atomic head.

We first detail algorithms for computing all the most general single-piece unifiers of a query Qwith a rule R and explain how we use them to compute all single-piece aggregators. Then we report first experiments.

7.1 Computing single-piece unifiers and their aggregation

When a ruleRhas an atomic head, it holds that every atom inQparticipates inat most onemost general single-piece unifier ofQwithR(up to bijective variable renaming).

This is is a corollary of the next property.

Property 11 Let Rbe an atomic-head rule andQbe a BCQ. For all atoma ∈ Q, there is at most oneQ0 ⊆Qsuch thata∈Q0andQ0 is a piece for a piece-unifier of QwithR.

Proof: We prove by contradiction that two single-piece unifiers cannot share an atom ofQ. Assume there areQ01 ⊆QandQ02⊆Qsuch thatQ01 6=Q02andQ01∩Q026=∅, andµ1 = (Q01, H, Pu1)andµ2 = (Q02, H, Pu2)two single-piece-unifiers ofQwithR,

withH =head(R). SinceQ016=Q02, one hasQ01\Q026=∅orQ02\Q01 6=∅. Assume Q01\Q02 6= ∅. LetA = Q01∩Q02 andB = Q01\A. There is at least one variable x ∈ vars(A)∩vars(B)such that there is an existential variableeofhead(R)in the class ofPu1containingx(otherwiseµ1has more than one piece). SinceH is atomic, there is a unique way of associating any atom withH, thus the class ofPu2containing xcontains alsoe. It follows thatQ02is not a piece since one atom ofAand one atom of B sharexunified with an existential variable inµ2whileAis included inQ02andB

is not.

To compute most general single-piece unifiers, we first introduce the notion of the unification of a set of atoms with the head of a rule. This notion is an adaptation of the classical logical unification that takes existential variables into account. To define a piece-unifier, the set of atoms has to satisfy an additional constraint on its separating variables.

Definition 17 (Partition by Position) LetAbe a set of atoms with the same predicate p. Thepartition by positionassociated withA, denoted byPp(A), is the partition on terms(A)such that two terms ofAappearing in the same positioni(1≤i≤arity(p)) are in the same class ofPp(A).

Definition 18 (Unifiability) LetRbe an atomic head rule and letAbe a set of atoms with same predicatepashead(R).AisunifiablewithRif no class ofPp(A∪head(R)) contains two constants, or contains two existential variables ofR, or contains a con-stant and an existential variable ofR, or contains an existential variable ofRand a frontier variable ofR.

Definition 19 (Sticky Variables) LetQbe a BCQ,Rbe an atomic head rule andQ0 be a subset of atoms inQwith the same predicatepashead(R). Thesticky variables ofQ0with respect toQandR, denoted bysticky(Q0), are the separating variables of Q0that occur in a class ofPp(Q0∪head(R))containing an existential variable ofR.

The following property follows from the definitions:

Property 12 LetQbe a BCQ,Rbe an atomic head rule, andQ0a subset of atoms in Qwith the same predicatepashead(R). Thenµ= (Q0,head(R), Pp(Q0∪head(R))) is a piece-unifier ofQwithRiffQ0is unifiable withhead(R)andsticky(Q0) =∅.

The fact that an atom fromQparticipates in at most one most general single-piece unifier suggests an incremental method to compute these unifiers. Assume that the head of R has predicate p. We start from each atoma ∈ Qwith predicate pand compute the subset of atoms fromQthat would necessarily belong to the same piece asa; more precisely, at each step, we buildQ0such thatQ0andhead(R)can be unified, then check ifsticky(Q0) = ∅. If there is a piece-unifier ofQ0 built in this way with head(R), all atoms inQ0can be removed fromQfor the search of other single-piece unifiers; otherwise,ais removed fromQfor the search of other single-piece unifiers but the other atoms inQ0 still have to be taken into account. Note that in both cases, the notion of separating variables is still relative to the originalQ.

Example 12 LetR =q(x) →p(x, y)andQ =p(u, v)∧p(v, t). Let us start from p(u, v): this atom is unifiable withhead(R)andp(v, t)necessarily belongs to the same piece-unifier (if any) becausev ∈ sticky({p(u, v)})(v is in the same class that the existential variable y); however,{p(u, v), p(v, t)} is not unifiable withhead(R) be-cause, sincevoccurs at the first and at the second position of apatom,xandyshould

be unified, which is not possible sinceyis an existential variable; thusp(u, v)does not belong to any piece-unifier withR. However,p(v, t)still needs to be considered.

Let us start from it:p(v, t)is unifiable withhead(R)and forms its own piece because sticky({p(v,t)}) is empty (tis in the same class that the existential variableybut is not shared with another atom). There is thus one (most general) piece-unifier ofQwithR, namely({p(v, t)},{p(x, y)},{{v, x},{t, y}}).

More precisely, Algorithm 2 first builds the subsetAof atoms inQwith the same predicate ashead(R). WhileAhas not been emptied, it initializes a setQ0by picking an atomainA, then repeats the following steps:

1. check ifQ0is unifiable withhead(R); else, the attempt withafails;

2. check ifsticky(Q0) =∅; if so, it is a single-piece unifier and all the atoms inQ0 are removed fromA;

3. otherwise, the algorithm tries to extendQ0with all the atoms inQcontaining a variable fromsticky(Q0); if these atoms are inA,Q0 can grow, otherwise the attempt withafails.

Algorithm 2:Computation of all most general single-piece unifiers Data: a CQQand an atomic-head ruleR

Result: the set of most general single-piece unifiers ofQwithR begin

U ← ∅;// resulting set

A← {a∈Q|predicate(a) =predicate(head(R))};

whileA6=∅do

a←choose an atom inA; Q0 ← {a};

whileQ0 ⊆Aandunif iable(Q0,head(R))andsticky(Q0)6=∅do Q0 ←Q0∪ {a0∈Q|a0contains a variable in sticky(Q0)}; ifQ0 ⊆Aandunif iable(Q0,head(R))then

U ←U∪ {(Q0,head(R), Pp(Q0∪head(R)))}; A←A\Q0

else

A←A\ {a}

returnU

Now, to compute the set of single-piece aggregators ofQwithR, we proceed as follows:

1. Compute all (most general) single-piece unifiers ofQwithR:

U1={µ1, . . . , µk};

2. Forifrom2to the greatest possible rank (as long asUiis not empty): letUibe the set of alli-unifiers obtained by aggregating ani−1-unifier fromUi−1and a single-piece unifier fromU1.

3. Return the union of all theUiobtained.