Search in sources :

Example 21 with Substitution

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

the class RecursiveBacktrackHomomorphism method homomorphism.

// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private Collection<Substitution> homomorphism(ConjunctiveQuery query, Collection<Atom>[] queryAtomRanked, AtomSet facts, Substitution substitution, List<Variable> orderedVars, int rank) throws Exception {
    Collection<Substitution> substitutionList = new LinkedList<Substitution>();
    if (orderedVars.size() == 0) {
        Substitution filteredSub = new HashMapSubstitution();
        for (Term var : query.getAnswerVariables()) {
            if (var.isVariable()) {
                filteredSub.put((Variable) var, substitution.createImageOf(var));
            }
        }
        substitutionList.add(filteredSub);
    } else {
        Term var = orderedVars.remove(0);
        if (var.isVariable()) {
            for (Term substitut : domain) {
                Substitution tmpSubstitution = new HashMapSubstitution(substitution);
                tmpSubstitution.put((Variable) var, substitut);
                // Test partial homomorphism
                if (isHomomorphism(queryAtomRanked[rank], facts, tmpSubstitution))
                    substitutionList.addAll(homomorphism(query, queryAtomRanked, facts, tmpSubstitution, new LinkedList<Variable>(orderedVars), rank + 1));
            }
        }
    }
    return substitutionList;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) 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)

Example 22 with Substitution

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

the class RecursiveBacktrackHomomorphism method execute.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIterator<Substitution> execute(ConjunctiveQuery query, AtomSet facts) throws HomomorphismException {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace(query.toString());
    }
    if (profiler != null) {
        profiler.start("preprocessing time");
    }
    Pair<ConjunctiveQuery, Substitution> pair = EqualityUtils.processEquality(query);
    List<Variable> orderedVars = order(pair.getLeft().getAtomSet().getVariables());
    Collection<Atom>[] queryAtomRanked = getAtomRank(pair.getLeft().getAtomSet(), orderedVars);
    if (profiler != null) {
        profiler.stop("preprocessing time");
    }
    try {
        this.domain = facts.getTerms();
        if (profiler != null) {
            profiler.start("backtracking time");
        }
        CloseableIterator<Substitution> results;
        if (isHomomorphism(queryAtomRanked[0], facts, new HashMapSubstitution())) {
            results = new CloseableIteratorAdapter<Substitution>(homomorphism(pair.getLeft(), queryAtomRanked, facts, new HashMapSubstitution(), orderedVars, 1).iterator());
        } else {
            // return false
            results = new CloseableIteratorAdapter<Substitution>(Collections.<Substitution>emptyList().iterator());
        }
        if (profiler != null) {
            profiler.stop("backtracking time");
        }
        if (!pair.getRight().getTerms().isEmpty()) {
            results = new ConverterCloseableIterator<Substitution, Substitution>(results, new EqualityHandlerConverter(pair.getRight()));
        }
        return results;
    } catch (Exception e) {
        throw new HomomorphismException(e.getMessage(), e);
    }
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) CloseableIteratorWithoutException(fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException) CloseableIterableWithoutException(fr.lirmm.graphik.util.stream.CloseableIterableWithoutException) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) Collection(java.util.Collection) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) EqualityHandlerConverter(fr.lirmm.graphik.graal.homomorphism.utils.EqualityHandlerConverter)

Example 23 with Substitution

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

the class AbstractNFC method select.

protected boolean select(Atom atom, Var v, AtomSet g, Substitution initialSubstitution, Map<Variable, Integer> map, Var[] varData, RulesCompilation rc) throws AtomSetException, IteratorException {
    boolean contains = false;
    Set<Var> postVarsFromThisAtom = new HashSet<Var>();
    for (Pair<Atom, Substitution> rew : rc.getRewritingOf(atom)) {
        Atom a = rew.getLeft();
        Var[] postV = this.computePostVariablesPosition(a, v.shared.level, map, varData, postVarsFromThisAtom);
        Atom im = BacktrackUtils.createImageOf(a, initialSubstitution, map, varData);
        Profiler profiler = this.getProfiler();
        if (profiler != null) {
            profiler.incr("#Select", 1);
            profiler.start("SelectTime");
        }
        int nbAns = 0;
        CloseableIterator<? extends Atom> it = g.match(im);
        while (it.hasNext()) {
            ++nbAns;
            int i = -1;
            for (Term t : it.next()) {
                ++i;
                if (postV[i] != null) {
                    this.data[postV[i].shared.level].tmp.add(t);
                }
            }
            contains = true;
        }
        if (profiler != null) {
            profiler.stop("SelectTime");
            profiler.incr("#SelectAns", nbAns);
        }
    }
    boolean isThereAnEmptiedList = false;
    if (contains) {
        // set computed candidats for post variables
        for (Var z : postVarsFromThisAtom) {
            if (!isThereAnEmptiedList) {
                AcceptableCandidats ac = this.data[z.shared.level].candidats[v.shared.level];
                if (ac.init) {
                    ac.candidats.retainAll(this.data[z.shared.level].tmp);
                    isThereAnEmptiedList |= ac.candidats.isEmpty();
                    if (ac.candidats.isEmpty()) {
                        this.bj.addNeighborhoodToBackjumpSet(z.shared, v.shared);
                    }
                } else {
                    ac.candidats.addAll(this.data[z.shared.level].tmp);
                    ac.init = true;
                }
            }
            this.data[z.shared.level].tmp.clear();
        }
    } else {
        Var z = postVarsFromThisAtom.iterator().next();
        this.bj.addNeighborhoodToBackjumpSet(z.shared, v.shared);
    }
    return contains && !isThereAnEmptiedList;
}
Also used : Var(fr.lirmm.graphik.graal.homomorphism.Var) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Profiler(fr.lirmm.graphik.util.profiler.Profiler) HashSet(java.util.HashSet)

Example 24 with Substitution

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

the class AbstractNFC method check.

protected boolean check(Atom atom, VarSharedData currentVar, VarSharedData varToCompute, AtomSet g, Substitution initialSubstitution, Map<Variable, Integer> map, Var[] varData, RulesCompilation rc) throws AtomSetException {
    Substitution s = BacktrackUtils.createSubstitution(varData);
    s.put(initialSubstitution);
    Atom im = s.createImageOf(atom);
    this.data[varToCompute.level].tmp.clear();
    Set<Term> candidats = this.data[varToCompute.level].candidats[currentVar.level].candidats;
    // FIXME bug with p(X,Y,Z) -> q(X,Y) in the compilation
    for (Pair<Atom, Substitution> rew : rc.getRewritingOf(im)) {
        Atom a = rew.getLeft();
        Iterator<Term> it = candidats.iterator();
        while (it.hasNext()) {
            Term t = it.next();
            Atom fullInstantiatedAtom = Substitutions.createImageOf(a, varToCompute.value, t);
            Profiler profiler = this.getProfiler();
            if (profiler != null) {
                profiler.incr("#check", 1);
                profiler.start("checkTime");
            }
            if (g.contains(fullInstantiatedAtom)) {
                this.data[varToCompute.level].tmp.add(t);
            }
            if (profiler != null) {
                profiler.stop("checkTime");
            }
        }
    }
    candidats.retainAll(this.data[varToCompute.level].tmp);
    this.data[varToCompute.level].tmp.clear();
    if (candidats.isEmpty()) {
        this.bj.addNeighborhoodToBackjumpSet(varToCompute, currentVar);
        return false;
    } else {
        return true;
    }
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Profiler(fr.lirmm.graphik.util.profiler.Profiler) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 25 with Substitution

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

the class FrontierRestrictedChaseHaltingConditionTest method test.

@Test
public void test() throws IteratorException, HomomorphismFactoryException, HomomorphismException {
    InMemoryAtomSet atomset = DefaultAtomSetFactory.instance().create(DlgpParser.parseAtom("p(a,b)."));
    Rule rule = DlgpParser.parseRule("p(X,Z):-p(X,Y).");
    Variable x = DefaultTermFactory.instance().createVariable("X");
    Variable y = DefaultTermFactory.instance().createVariable("Y");
    Constant a = DefaultTermFactory.instance().createConstant("a");
    Constant b = DefaultTermFactory.instance().createConstant("b");
    Substitution s = DefaultSubstitutionFactory.instance().createSubstitution();
    s.put(x, a);
    s.put(y, b);
    FrontierRestrictedChaseHaltingCondition condition = new FrontierRestrictedChaseHaltingCondition();
    CloseableIterator<Atom> toAdd = condition.apply(rule, s, atomset);
    Assert.assertTrue(toAdd.hasNext());
    Atom atom1 = toAdd.next();
    atomset.add(atom1);
    Assert.assertFalse(toAdd.hasNext());
    toAdd.close();
    s = DefaultSubstitutionFactory.instance().createSubstitution();
    s.put(x, a);
    s.put(y, atom1.getTerm(1));
    toAdd = condition.apply(rule, s, atomset);
    Assert.assertFalse(toAdd.hasNext());
    toAdd.close();
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Constant(fr.lirmm.graphik.graal.api.core.Constant) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) Rule(fr.lirmm.graphik.graal.api.core.Rule) Atom(fr.lirmm.graphik.graal.api.core.Atom) 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