use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class IDCompilationTest method test.
/**
* Given p(X,Y) -> q(X,Y,X) <br>
* Then rew(q(U,V,W)) <br>
* Return q(U,V,W)-{} AND (p(U,V)-{W->U} || p(W,V)-{U->W})
*/
@Test
public void test() {
Predicate predicateQ = new Predicate("q", 3);
Predicate predicateP = new Predicate("p", 2);
Atom body = new DefaultAtom(predicateP, X, Y);
Atom head = new DefaultAtom(predicateQ, X, Y, X);
Atom query = new DefaultAtom(predicateQ, U, V, W);
RuleSet rules = new LinkedListRuleSet();
rules.add(DefaultRuleFactory.instance().create(body, head));
RulesCompilation comp = new IDCompilation();
comp.compile(rules.iterator());
Collection<Pair<Atom, Substitution>> rewritingOf = comp.getRewritingOf(query);
boolean rew1 = false;
boolean rew2 = false;
for (Pair<Atom, Substitution> p : rewritingOf) {
Atom a = p.getLeft();
Substitution s = p.getRight();
if (a.getPredicate().equals(predicateQ)) {
rew1 = true;
Assert.assertEquals(U, a.getTerm(0));
Assert.assertEquals(V, a.getTerm(1));
Assert.assertEquals(W, a.getTerm(2));
Assert.assertEquals(0, s.getTerms().size());
} else {
rew2 = true;
Assert.assertEquals(predicateP, a.getPredicate());
Assert.assertTrue(a.getTerm(0).equals(U) || a.getTerm(0).equals(W));
Assert.assertEquals(V, a.getTerm(1));
Assert.assertEquals(1, s.getTerms().size());
}
}
Assert.assertTrue(rew1 && rew2);
Assert.assertEquals(2, rewritingOf.size());
}
use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class IDCompilationTest method test3.
/**
* Given p(X,Y) -> q(X,Y,X,Y,X) <br>
* Then rew(q(U,V,A,B,V)) <br>
* Return q(U,V,A,B,V)-{}
*/
@Test
public void test3() {
Predicate predicateQ = new Predicate("q", 5);
Predicate predicateP = new Predicate("p", 2);
Atom body = new DefaultAtom(predicateP, X, Y);
Atom head = new DefaultAtom(predicateQ, X, Y, X, Y, X);
Atom query = new DefaultAtom(predicateQ, U, V, A, B, V);
RuleSet rules = new LinkedListRuleSet();
rules.add(DefaultRuleFactory.instance().create(body, head));
RulesCompilation comp = new IDCompilation();
comp.compile(rules.iterator());
Collection<Pair<Atom, Substitution>> rewritingOf = comp.getRewritingOf(query);
Assert.assertEquals(1, rewritingOf.size());
Pair<Atom, Substitution> p = rewritingOf.iterator().next();
Atom a = p.getLeft();
Substitution s = p.getRight();
Assert.assertEquals(predicateQ, a.getPredicate());
Assert.assertEquals(U, a.getTerm(0));
Assert.assertEquals(V, a.getTerm(1));
Assert.assertEquals(A, a.getTerm(2));
Assert.assertEquals(B, a.getTerm(3));
Assert.assertEquals(V, a.getTerm(4));
Assert.assertEquals(0, s.getTerms().size());
}
use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class IDCompilationTest method test2.
/**
* Given p(X,Y) -> q(X,Y,X) <br>
* Then rew(q(U,U,U)) <br>
* Return q(U,U,U)-{} AND p(U,U)-{})
*/
@Test
public void test2() {
Predicate predicateQ = new Predicate("q", 3);
Predicate predicateP = new Predicate("p", 2);
Atom body = new DefaultAtom(predicateP, X, Y);
Atom head = new DefaultAtom(predicateQ, X, Y, X);
Atom query = new DefaultAtom(predicateQ, U, U, U);
RuleSet rules = new LinkedListRuleSet();
rules.add(DefaultRuleFactory.instance().create(body, head));
RulesCompilation comp = new IDCompilation();
comp.compile(rules.iterator());
Collection<Pair<Atom, Substitution>> rewritingOf = comp.getRewritingOf(query);
boolean rew1 = false;
boolean rew2 = false;
for (Pair<Atom, Substitution> p : rewritingOf) {
Atom a = p.getLeft();
Substitution s = p.getRight();
if (a.getPredicate().equals(predicateQ)) {
rew1 = true;
Assert.assertEquals(U, a.getTerm(0));
Assert.assertEquals(U, a.getTerm(1));
Assert.assertEquals(U, a.getTerm(2));
Assert.assertEquals(0, s.getTerms().size());
} else {
rew2 = true;
Assert.assertEquals(predicateP, a.getPredicate());
Assert.assertEquals(U, a.getTerm(0));
Assert.assertEquals(U, a.getTerm(1));
Assert.assertEquals(0, s.getTerms().size());
}
}
Assert.assertTrue(rew1 && rew2);
Assert.assertEquals(2, rewritingOf.size());
}
use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class HierarchicalCompilation method homomorphism.
@Override
public Collection<Substitution> homomorphism(Atom father, Atom son) {
LinkedList<Substitution> res = new LinkedList<Substitution>();
if (isMappable(father.getPredicate(), son.getPredicate())) {
Substitution sub = DefaultSubstitutionFactory.instance().createSubstitution();
Iterator<Term> fatherTermsIt = father.getTerms().iterator();
Iterator<Term> sonTermsIt = son.getTerms().iterator();
Term fatherTerm, sonTerm;
while (fatherTermsIt.hasNext() && sonTermsIt.hasNext()) {
fatherTerm = fatherTermsIt.next();
sonTerm = sonTermsIt.next();
if (fatherTerm.isConstant()) {
if (!fatherTerm.equals(sonTerm)) {
return res;
}
} else if (!sub.getTerms().contains(fatherTerm))
sub.put((Variable) fatherTerm, sonTerm);
else if (!sub.createImageOf(fatherTerm).equals(sonTerm))
return res;
}
res.add(sub);
}
return res;
}
use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class TermPartitionUtils method getAssociatedSubstitution.
/**
* Compute the substitution associated with the current partition this
* method computes a substitution by choosing one representative term by
* class. Choosing first constant then answer variable (in context) return
* null if the partition contain two constants in the same class
*/
public static Substitution getAssociatedSubstitution(Partition<Term> partition, ConjunctiveQuery context) {
Substitution substitution = DefaultSubstitutionFactory.instance().createSubstitution();
// partition
for (Collection<Term> set : partition) {
Iterator<Term> i = set.iterator();
Term representative = i.next();
while (i.hasNext()) {
Term t = i.next();
// t and the current representative are different
if (representative.equals(t)) {
i.remove();
} else {
if (t.isConstant()) {
// representative is a different constant
if (representative.isConstant()) {
return null;
} else {
representative = t;
}
} else if (representative.isVariable()) {
// t is a variable from the answer
if (context != null && !context.getAnswerVariables().contains(representative) && context.getAtomSet().getTerms().contains(t)) {
representative = t;
}
}
}
}
// representative of the equivalence set
for (Term t : set) {
if (!t.equals(representative)) {
if (t.isVariable()) {
substitution.put((Variable) t, representative);
}
}
}
}
return substitution;
}
Aggregations