use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class IDConditionImpl method homomorphism.
@Override
public Substitution homomorphism(List<Term> head, List<Term> to) {
if (!checkBody(to)) {
return null;
}
Pair<List<Term>, Substitution> ret = this.generateBody(head);
if (ret == null) {
return null;
}
Substitution s = ret.getRight();
Substitution homo = DefaultSubstitutionFactory.instance().createSubstitution();
List<Term> generatedBody = ret.getLeft();
// check for a simple homomorphism from generated body into 'to'
Iterator<Term> itFrom = generatedBody.iterator();
Iterator<Term> itTo = to.iterator();
while (itFrom.hasNext() && itTo.hasNext()) {
Term termFrom = itFrom.next();
Term termTo = itTo.next();
if (termFrom.isConstant()) {
if (!termFrom.equals(termTo)) {
return null;
}
} else {
if (!homo.put((Variable) termFrom, termTo)) {
return null;
}
}
}
if (itFrom.hasNext() || itTo.hasNext()) {
throw new Error("Wrong term number");
}
// homo
for (Variable t : s.getTerms()) {
homo.put(t, homo.createImageOf(s.createImageOf(t)));
}
return homo;
}
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 IDCompilation method homomorphism.
@Override
public LinkedList<Substitution> homomorphism(Atom father, Atom son) {
LinkedList<Substitution> res = new LinkedList<Substitution>();
Predicate predB = son.getPredicate();
Predicate predH = father.getPredicate();
List<IDCondition> conds = getConditions(predB, predH);
for (IDCondition cond : conds) {
if (cond.checkBody(son.getTerms())) {
Substitution homo = cond.homomorphism(father.getTerms(), son.getTerms());
if (homo != null) {
res.add(new TreeMapSubstitution(homo));
}
}
}
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;
}
use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class UnifierTest method mostGeneralTest1.
@Test
public void mostGeneralTest1() throws IteratorException {
Rule rule = DefaultRuleFactory.instance().create();
rule.getBody().add(TestUtils.pXY);
rule.getHead().add(TestUtils.qXY);
InMemoryAtomSet atomset = DefaultAtomSetFactory.instance().create();
atomset.add(TestUtils.qUV);
CloseableIteratorWithoutException<Substitution> unifiers = DefaultUnifierAlgorithm.instance().computePieceUnifier(rule, atomset);
Assert.assertEquals(1, Iterators.count(unifiers));
}
Aggregations