Search in sources :

Example 16 with Substitution

use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.

the class IDConditionTest method generateBodyTest5.

/**
 * h(X,Y,X,Y) :- b(X,Y).
 *
 * generateBody([a,b,X,Y]) => [a, b].
 */
@Test
public void generateBodyTest5() {
    Term[] body1 = { X, Y };
    Term[] head1 = { X, Y, X, Y };
    IDCondition cond = createCondition(body1, head1);
    Term[] head = { A, B, X, Y };
    Term[] expected = { A, B };
    Pair<List<Term>, Substitution> ret = cond.generateBody(Arrays.asList(head));
    List<Term> computed = ret.getLeft();
    Substitution s = ret.getRight();
    Assert.assertEquals(Arrays.asList(expected), computed);
    Assert.assertEquals(A, s.createImageOf(X));
    Assert.assertEquals(B, s.createImageOf(Y));
    Assert.assertEquals(2, s.getTerms().size());
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) List(java.util.List) Term(fr.lirmm.graphik.graal.api.core.Term) Test(org.junit.Test)

Example 17 with Substitution

use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.

the class DefaultUCQHomomorphism method exist.

@Override
public boolean exist(UnionOfConjunctiveQueries q, AtomSet a) throws HomomorphismException {
    try {
        CloseableIterator<Substitution> execute = this.execute(q, a);
        boolean res = execute.hasNext();
        execute.close();
        return res;
    } catch (IteratorException e) {
        throw new HomomorphismException(e);
    }
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution)

Example 18 with Substitution

use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.

the class FullyInstantiatedQueryHomomorphism method execute.

@Override
public CloseableIterator<Substitution> execute(ConjunctiveQuery q, AtomSet a, RulesCompilation rc) throws HomomorphismException {
    try {
        CloseableIteratorWithoutException<Atom> it = q.getAtomSet().iterator();
        boolean contains = true;
        while (contains && it.hasNext()) {
            Atom atom = it.next();
            boolean containsAtom = false;
            for (Pair<Atom, Substitution> im : rc.getRewritingOf(atom)) {
                containsAtom = a.contains(im.getLeft());
                if (containsAtom) {
                    break;
                }
            }
            contains = containsAtom;
        }
        if (contains) {
            return Iterators.singletonIterator(Substitutions.emptySubstitution());
        } else {
            return Iterators.emptyIterator();
        }
    } catch (AtomSetException e) {
        throw new HomomorphismException(e);
    }
}
Also used : HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 19 with Substitution

use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.

the class PureHomomorphismImpl method backtrack.

protected boolean backtrack() {
    int level = 0;
    boolean foundImage;
    Substitution nextCandidate;
    // can backtrack and all the atom have not been associate
    while (level >= 0 && level < this.source.size()) {
        nextCandidate = this.getNextCandidate(level);
        if (nextCandidate != null) {
            // try next candidate
            foundImage = this.checkCurrentCandidate(level, nextCandidate);
            if (foundImage) {
                // need go to the next atom
                level++;
            }
        } else {
            // need backtrack
            level--;
        }
    }
    return !(level < 0);
}
Also used : TreeMapSubstitution(fr.lirmm.graphik.graal.core.TreeMapSubstitution) Substitution(fr.lirmm.graphik.graal.api.core.Substitution)

Example 20 with Substitution

use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.

the class RecursiveBacktrackHomomorphism method existHomomorphism.

private static boolean existHomomorphism(AtomSet atomSet1, Collection<Atom>[] queryAtomRanked, AtomSet atomSet2, Substitution substitution, List<Variable> orderedVars, int rank) throws Exception {
    if (orderedVars.size() == 0) {
        return true;
    } else {
        Term var;
        Set<Term> domaine = atomSet2.getTerms();
        var = orderedVars.remove(0);
        if (var.isVariable()) {
            for (Term substitut : domaine) {
                Substitution tmpSubstitution = new HashMapSubstitution(substitution);
                tmpSubstitution.put((Variable) var, substitut);
                // Test partial homomorphism
                if (isHomomorphism(queryAtomRanked[rank], atomSet2, tmpSubstitution))
                    if (existHomomorphism(atomSet1, queryAtomRanked, atomSet2, tmpSubstitution, new LinkedList<Variable>(orderedVars), rank + 1)) {
                        return true;
                    }
            }
        }
    }
    return false;
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) Term(fr.lirmm.graphik.graal.api.core.Term) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) LinkedList(java.util.LinkedList)

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