Search in sources :

Example 1 with Substitution

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;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) MethodNotImplementedError(fr.lirmm.graphik.util.MethodNotImplementedError) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Term(fr.lirmm.graphik.graal.api.core.Term)

Example 2 with Substitution

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;
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Term(fr.lirmm.graphik.graal.api.core.Term) LinkedList(java.util.LinkedList)

Example 3 with Substitution

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;
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) TreeMapSubstitution(fr.lirmm.graphik.graal.core.TreeMapSubstitution) LinkedList(java.util.LinkedList) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) TreeMapSubstitution(fr.lirmm.graphik.graal.core.TreeMapSubstitution)

Example 4 with Substitution

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;
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Term(fr.lirmm.graphik.graal.api.core.Term)

Example 5 with 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));
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) Rule(fr.lirmm.graphik.graal.api.core.Rule) Test(org.junit.Test)

Aggregations

Substitution (fr.lirmm.graphik.graal.api.core.Substitution)158 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)58 Test (org.junit.Test)55 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)46 Theory (org.junit.experimental.theories.Theory)44 Atom (fr.lirmm.graphik.graal.api.core.Atom)41 Term (fr.lirmm.graphik.graal.api.core.Term)36 LinkedList (java.util.LinkedList)27 Variable (fr.lirmm.graphik.graal.api.core.Variable)25 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)24 Rule (fr.lirmm.graphik.graal.api.core.Rule)23 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)19 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)18 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)16 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)15 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)14 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)14 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)13 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)13 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)13