Search in sources :

Example 36 with Substitution

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

the class AtomicQueryHomomorphism method execute.

@Override
public CloseableIterator<Substitution> execute(ConjunctiveQuery q, AtomSet a, RulesCompilation rc) throws HomomorphismException {
    try {
        List<CloseableIterator<Substitution>> iteratorsList = new LinkedList<CloseableIterator<Substitution>>();
        Atom atom = q.getAtomSet().iterator().next();
        for (Pair<Atom, Substitution> im : rc.getRewritingOf(atom)) {
            iteratorsList.add(new ConverterCloseableIterator<Atom, Substitution>(a.match(im.getLeft()), new Atom2SubstitutionConverter(im.getLeft(), q.getAnswerVariables(), im.getRight())));
        }
        return new CloseableIteratorAggregator<Substitution>(new CloseableIteratorAdapter<CloseableIterator<Substitution>>(iteratorsList.iterator()));
    } catch (AtomSetException e) {
        throw new HomomorphismException(e);
    }
}
Also used : ConverterCloseableIterator(fr.lirmm.graphik.util.stream.converter.ConverterCloseableIterator) CloseableIterator(fr.lirmm.graphik.util.stream.CloseableIterator) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) CloseableIteratorAggregator(fr.lirmm.graphik.util.stream.CloseableIteratorAggregator) LinkedList(java.util.LinkedList) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 37 with Substitution

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

the class BacktrackIterator method existNegParts.

private boolean existNegParts() throws BacktrackException {
    Substitution s = currentSubstitution(this.vars);
    s.put(initialSubstitution);
    for (PreparedExistentialHomomorphism negPart : this.currentVar().shared.negatedPartsToCheck) {
        try {
            if (negPart.exist(s)) {
                this.data.bj.success();
                return true;
            }
        } catch (HomomorphismException e) {
            throw new BacktrackException("Error while checking anegated part: ", e);
        }
    }
    return false;
}
Also used : HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) PreparedExistentialHomomorphism(fr.lirmm.graphik.graal.api.homomorphism.PreparedExistentialHomomorphism)

Example 38 with Substitution

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

the class StatBootstrapper method exec.

// /////////////////////////////////////////////////////////////////////////
// 
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIterator<Term> exec(final VarSharedData v, Collection<Atom> preAtoms, Collection<Atom> postAtoms, final AtomSet data, RulesCompilation rc) throws BacktrackException {
    if (!(data instanceof Store)) {
        return fallback.exec(v, preAtoms, postAtoms, data, rc);
    }
    Store store = (Store) data;
    Set<Term> terms = null;
    if (this.getProfiler() != null) {
        this.getProfiler().start("BootstrapTime");
        this.getProfiler().start("BootstrapTimeFirstPart");
    }
    Iterator<Atom> it;
    Collection<Constant> constants = null;
    Atom aa = null;
    it = postAtoms.iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        if (constants == null || constants.isEmpty()) {
            constants = a.getConstants();
            aa = a;
        }
    }
    it = preAtoms.iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        if (constants == null || constants.isEmpty()) {
            constants = a.getConstants();
            aa = a;
        }
    }
    try {
        if (constants != null && !constants.isEmpty()) {
            terms = new HashSet<Term>();
            for (Pair<Atom, Substitution> im : rc.getRewritingOf(aa)) {
                int pos = im.getLeft().indexOf(im.getRight().createImageOf(v.value));
                CloseableIterator<Atom> match = data.match(im.getLeft());
                while (match.hasNext()) {
                    terms.add(match.next().getTerm(pos));
                }
            }
        }
        if (this.getProfiler() != null) {
            this.getProfiler().stop("BootstrapTimeFirstPart");
        }
        if (terms == null) {
            Atom a = null, tmp;
            double probaA = 1.1;
            it = postAtoms.iterator();
            while (it.hasNext()) {
                tmp = it.next();
                double p = ProbaUtils.computeProba(tmp, store, rc);
                if (p < probaA) {
                    a = tmp;
                    p = probaA;
                }
            }
            it = preAtoms.iterator();
            while (it.hasNext()) {
                tmp = it.next();
                double p = ProbaUtils.computeProba(tmp, store, rc);
                if (p < probaA) {
                    a = tmp;
                    p = probaA;
                }
            }
            terms = BootstrapperUtils.computeCandidatesOverRewritings(a, v, data, rc);
        }
        if (this.getProfiler() != null) {
            this.getProfiler().stop("BootstrapTime");
        }
        if (terms == null) {
            return data.termsIterator();
        } else {
            return new CloseableIteratorAdapter<Term>(terms.iterator());
        }
    } catch (AtomSetException e) {
        throw new BacktrackException(e);
    } catch (IteratorException e) {
        throw new BacktrackException(e);
    }
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Constant(fr.lirmm.graphik.graal.api.core.Constant) Store(fr.lirmm.graphik.graal.api.store.Store) Term(fr.lirmm.graphik.graal.api.core.Term) CloseableIteratorAdapter(fr.lirmm.graphik.util.stream.CloseableIteratorAdapter) Atom(fr.lirmm.graphik.graal.api.core.Atom) BacktrackException(fr.lirmm.graphik.graal.homomorphism.BacktrackException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException)

Example 39 with Substitution

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

the class DefaultKnowledgeBaseQueryTest method testQueryWithTimeout0.

/**
 * Test method for
 * {@link fr.lirmm.graphik.graal.kb.DefaultKnowledgeBase#query(fr.lirmm.graphik.graal.api.core.Query, long)}.
 * @throws AtomSetException
 * @throws KnowledgeBaseException
 * @throws ParseException
 * @throws IteratorException
 * @throws TimeoutException
 */
@Test
public void testQueryWithTimeout0() throws AtomSetException, ParseException, KnowledgeBaseException, IteratorException, TimeoutException {
    KnowledgeBase kb = new DefaultKnowledgeBase(new DlgpParser("p(X) :- q(X). q(X) :- r(X). r(X) :- s(X).  s(a)."));
    CloseableIterator<Substitution> res = kb.query(DlgpParser.parseQuery("? :- p(a)."), 0);
    Assert.assertTrue(res.hasNext());
    res.close();
    kb.close();
}
Also used : KnowledgeBase(fr.lirmm.graphik.graal.api.kb.KnowledgeBase) DlgpParser(fr.lirmm.graphik.graal.io.dlp.DlgpParser) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Test(org.junit.Test)

Example 40 with Substitution

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

the class DefaultKnowledgeBaseQueryTest method testQuery.

/**
 * Test method for
 * {@link fr.lirmm.graphik.graal.kb.DefaultKnowledgeBase#query(fr.lirmm.graphik.graal.api.core.Query)}.
 * @throws AtomSetException
 * @throws KnowledgeBaseException
 * @throws ParseException
 * @throws IteratorException
 */
@Test
public void testQuery() throws AtomSetException, ParseException, KnowledgeBaseException, IteratorException {
    KnowledgeBase kb = new DefaultKnowledgeBase(new DlgpParser("p(X) :- q(X). q(X) :- r(X). r(X) :- s(X).  s(a)."));
    CloseableIterator<Substitution> res = kb.query(DlgpParser.parseQuery("? :- p(a)."));
    Assert.assertTrue(res.hasNext());
    res.close();
    kb.close();
}
Also used : KnowledgeBase(fr.lirmm.graphik.graal.api.kb.KnowledgeBase) DlgpParser(fr.lirmm.graphik.graal.io.dlp.DlgpParser) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) 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