Search in sources :

Example 1 with CloseableIterator

use of fr.lirmm.graphik.util.stream.CloseableIterator 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 2 with CloseableIterator

use of fr.lirmm.graphik.util.stream.CloseableIterator in project graal by graphik-team.

the class DefaultKnowledgeBase method query.

@Override
public CloseableIterator<Substitution> query(Query query, long timeout) throws KnowledgeBaseException, TimeoutException {
    long remainingTime = timeout;
    if (this.isSaturated) {
        try {
            return SmartHomomorphism.instance().execute(query, this.store);
        } catch (HomomorphismException e) {
            throw new KnowledgeBaseException(e);
        }
    } else if (query instanceof ConjunctiveQuery) {
        ConjunctiveQuery cq = (ConjunctiveQuery) query;
        if (this.approach != Approach.REWRITING_ONLY && this.approach != Approach.SATURATION_ONLY) {
            this.analyse();
        }
        if (this.approach == Approach.REWRITING_ONLY || this.approach == Approach.SATURATION_ONLY || this.analyse.isDecidable()) {
            try {
                long time = System.currentTimeMillis();
                this.fesSaturate(remainingTime);
                if (timeout > 0) {
                    remainingTime -= (System.currentTimeMillis() - time);
                    if (remainingTime <= 0) {
                        throw new TimeoutException(timeout);
                    }
                }
                this.compileRule();
                RuleSet fusRuleSet = this.getFUSRuleSet();
                PureRewriter pure = new PureRewriter(false);
                CloseableIteratorWithoutException<ConjunctiveQuery> it = pure.execute(cq, fusRuleSet, this.ruleCompilation, remainingTime);
                UnionOfConjunctiveQueries ucq = new DefaultUnionOfConjunctiveQueries(cq.getAnswerVariables(), it);
                CloseableIterator<Substitution> resultIt = null;
                try {
                    resultIt = SmartHomomorphism.instance().execute(ucq, this.store, this.ruleCompilation);
                } catch (HomomorphismException e) {
                    if (this.getApproach().equals(Approach.REWRITING_FIRST)) {
                        it = PureRewriter.unfold(ucq, this.ruleCompilation);
                        ucq = new DefaultUnionOfConjunctiveQueries(cq.getAnswerVariables(), it);
                    } else {
                        this.semiSaturate();
                    }
                    resultIt = SmartHomomorphism.instance().execute(ucq, this.store);
                }
                return new FilterIterator<Substitution, Substitution>(resultIt, new UniqFilter<Substitution>());
            } catch (ChaseException e) {
                throw new KnowledgeBaseException(e);
            } catch (HomomorphismException e) {
                throw new KnowledgeBaseException(e);
            }
        } else {
            throw new KnowledgeBaseException("No decidable combinaison found with the defined approach: " + this.getApproach());
        }
    } else {
        throw new KnowledgeBaseException("No implementation found for this kind of query: " + query.getClass());
    }
}
Also used : DefaultUnionOfConjunctiveQueries(fr.lirmm.graphik.graal.core.DefaultUnionOfConjunctiveQueries) AnalyserRuleSet(fr.lirmm.graphik.graal.rulesetanalyser.util.AnalyserRuleSet) RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) CloseableIterator(fr.lirmm.graphik.util.stream.CloseableIterator) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) PureRewriter(fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter) KnowledgeBaseException(fr.lirmm.graphik.graal.api.kb.KnowledgeBaseException) UniqFilter(fr.lirmm.graphik.util.stream.filter.UniqFilter) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) CloseableIteratorWithoutException(fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException) DefaultUnionOfConjunctiveQueries(fr.lirmm.graphik.graal.core.DefaultUnionOfConjunctiveQueries) UnionOfConjunctiveQueries(fr.lirmm.graphik.graal.api.core.UnionOfConjunctiveQueries) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) TimeoutException(fr.lirmm.graphik.graal.api.util.TimeoutException)

Example 3 with CloseableIterator

use of fr.lirmm.graphik.util.stream.CloseableIterator in project graal by graphik-team.

the class DefaultKnowledgeBase method query.

@Override
public CloseableIterator<Substitution> query(Query query) throws KnowledgeBaseException {
    if (this.isSaturated) {
        try {
            return SmartHomomorphism.instance().execute(query, this.store);
        } catch (HomomorphismException e) {
            throw new KnowledgeBaseException(e);
        }
    } else if (query instanceof ConjunctiveQuery) {
        ConjunctiveQuery cq = (ConjunctiveQuery) query;
        this.analyse();
        if (this.analyse.isDecidable() && (!this.getApproach().equals(Approach.REWRITING_ONLY) || this.getFESRuleSet().isEmpty()) && (!this.getApproach().equals(Approach.SATURATION_ONLY) || this.getFUSRuleSet().isEmpty())) {
            try {
                this.fesSaturate();
                this.compileRule();
                RuleSet fusRuleSet = this.getFUSRuleSet();
                PureRewriter pure = new PureRewriter(false);
                CloseableIteratorWithoutException<ConjunctiveQuery> it = pure.execute(cq, fusRuleSet, this.ruleCompilation);
                UnionOfConjunctiveQueries ucq = new DefaultUnionOfConjunctiveQueries(cq.getAnswerVariables(), it);
                CloseableIterator<Substitution> resultIt = null;
                try {
                    if (this.isSemiSaturated) {
                        resultIt = SmartHomomorphism.instance().execute(query, this.store);
                    } else {
                        resultIt = SmartHomomorphism.instance().execute(ucq, this.store, this.ruleCompilation);
                    }
                } catch (HomomorphismException e) {
                    if (this.getApproach().equals(Approach.REWRITING_FIRST)) {
                        it = PureRewriter.unfold(ucq, this.ruleCompilation);
                        ucq = new DefaultUnionOfConjunctiveQueries(cq.getAnswerVariables(), it);
                    } else {
                        this.semiSaturate();
                    }
                    resultIt = SmartHomomorphism.instance().execute(ucq, this.store);
                }
                return new FilterIterator<Substitution, Substitution>(resultIt, new UniqFilter<Substitution>());
            } catch (ChaseException e) {
                throw new KnowledgeBaseException(e);
            } catch (HomomorphismException e) {
                throw new KnowledgeBaseException(e);
            }
        } else {
            throw new KnowledgeBaseException("No decidable combinaison found with the defined approach: " + this.getApproach());
        }
    } else {
        throw new KnowledgeBaseException("No implementation found for this kind of query: " + query.getClass());
    }
}
Also used : DefaultUnionOfConjunctiveQueries(fr.lirmm.graphik.graal.core.DefaultUnionOfConjunctiveQueries) AnalyserRuleSet(fr.lirmm.graphik.graal.rulesetanalyser.util.AnalyserRuleSet) RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) CloseableIterator(fr.lirmm.graphik.util.stream.CloseableIterator) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) PureRewriter(fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter) KnowledgeBaseException(fr.lirmm.graphik.graal.api.kb.KnowledgeBaseException) UniqFilter(fr.lirmm.graphik.util.stream.filter.UniqFilter) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) CloseableIteratorWithoutException(fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException) DefaultUnionOfConjunctiveQueries(fr.lirmm.graphik.graal.core.DefaultUnionOfConjunctiveQueries) UnionOfConjunctiveQueries(fr.lirmm.graphik.graal.api.core.UnionOfConjunctiveQueries) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)

Example 4 with CloseableIterator

use of fr.lirmm.graphik.util.stream.CloseableIterator in project graal by graphik-team.

the class AtomicQueryHomomorphismWithNegatedParts method execute.

// /////////////////////////////////////////////////////////////////////////
// HOMOMORPHISM METHODS
// /////////////////////////////////////////////////////////////////////////
public CloseableIterator<Substitution> execute(ConjunctiveQueryWithNegatedParts query, AtomSet data, RulesCompilation compilation) throws HomomorphismException {
    try {
        Atom atom = query.getPositivePart().iterator().next();
        List<Term> ans = query.getAnswerVariables();
        List<CloseableIterator<Substitution>> iteratorsList = new LinkedList<CloseableIterator<Substitution>>();
        for (Pair<Atom, Substitution> im : compilation.getRewritingOf(atom)) {
            iteratorsList.add(new ConverterCloseableIterator<Atom, Substitution>(data.match(im.getLeft()), new Atom2SubstitutionConverter(im.getLeft(), ans, im.getRight())));
        }
        CloseableIterator<Substitution> subIt = new CloseableIteratorAggregator<Substitution>(new CloseableIteratorAdapter<CloseableIterator<Substitution>>(iteratorsList.iterator()));
        // manage negative parts
        Set<Variable> variables = query.getPositivePart().getVariables();
        @SuppressWarnings("rawtypes") Filter[] filters = new Filter[query.getNegatedParts().size()];
        int i = 0;
        for (InMemoryAtomSet negPart : query.getNegatedParts()) {
            Set<Variable> frontier = SetUtils.intersection(variables, negPart.getVariables());
            filters[i++] = new NegFilter(negPart, frontier, data, compilation);
        }
        @SuppressWarnings("unchecked") Filter<Substitution> filter = new AndFilter<Substitution>(filters);
        return new FilterIterator<Substitution, Substitution>(subIt, filter);
    } catch (AtomSetException e) {
        throw new HomomorphismException(e);
    }
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) 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) FilterIterator(fr.lirmm.graphik.util.stream.filter.FilterIterator) ConverterCloseableIterator(fr.lirmm.graphik.util.stream.converter.ConverterCloseableIterator) CloseableIterator(fr.lirmm.graphik.util.stream.CloseableIterator) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom) LinkedList(java.util.LinkedList) AndFilter(fr.lirmm.graphik.util.stream.filter.AndFilter) Filter(fr.lirmm.graphik.util.stream.filter.Filter) AndFilter(fr.lirmm.graphik.util.stream.filter.AndFilter) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)

Example 5 with CloseableIterator

use of fr.lirmm.graphik.util.stream.CloseableIterator in project graal by graphik-team.

the class DefaultBootstrapper method exec.

// /////////////////////////////////////////////////////////////////////////
// 
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIterator<Term> exec(final VarSharedData v, Collection<Atom> preAtoms, Collection<Atom> postAtoms, final AtomSet data, RulesCompilation compilation) throws BacktrackException {
    Iterator<Atom> it = postAtoms.iterator();
    if (it.hasNext()) {
        Atom a = it.next();
        final Iterator<Pair<Atom, Substitution>> rewritingOf = compilation.getRewritingOf(a).iterator();
        // TODO refactor the following code using converter Iterator or
        // create a private class?
        CloseableIterator<CloseableIterator<Term>> metaIt = new AbstractCloseableIterator<CloseableIterator<Term>>() {

            CloseableIterator<Term> next = null;

            @Override
            public void close() {
                if (next != null)
                    this.next.close();
            }

            @Override
            public boolean hasNext() throws IteratorException {
                try {
                    if (next == null && rewritingOf.hasNext()) {
                        Pair<Atom, Substitution> rew = rewritingOf.next();
                        Atom im = rew.getLeft();
                        Predicate predicate = im.getPredicate();
                        int pos = im.indexOf(rew.getRight().createImageOf(v.value));
                        next = data.termsByPredicatePosition(predicate, pos);
                    }
                } catch (AtomSetException e) {
                    throw new IteratorException("An errors occurs while getting terms by predicate position", e);
                }
                return next != null;
            }

            @Override
            public CloseableIterator<Term> next() throws IteratorException {
                if (next == null)
                    this.hasNext();
                CloseableIterator<Term> ret = next;
                next = null;
                return ret;
            }
        };
        return new CloseableIteratorAggregator<Term>(metaIt);
    } else {
        try {
            return data.termsIterator();
        } catch (AtomSetException e) {
            throw new BacktrackException(e);
        }
    }
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) AbstractCloseableIterator(fr.lirmm.graphik.util.stream.AbstractCloseableIterator) CloseableIterator(fr.lirmm.graphik.util.stream.CloseableIterator) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) BacktrackException(fr.lirmm.graphik.graal.homomorphism.BacktrackException) AbstractCloseableIterator(fr.lirmm.graphik.util.stream.AbstractCloseableIterator) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) CloseableIteratorAggregator(fr.lirmm.graphik.util.stream.CloseableIteratorAggregator) Pair(org.apache.commons.lang3.tuple.Pair)

Aggregations

Substitution (fr.lirmm.graphik.graal.api.core.Substitution)5 CloseableIterator (fr.lirmm.graphik.util.stream.CloseableIterator)5 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)4 Atom (fr.lirmm.graphik.graal.api.core.Atom)3 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)3 CloseableIteratorAggregator (fr.lirmm.graphik.util.stream.CloseableIteratorAggregator)3 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)2 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)2 Term (fr.lirmm.graphik.graal.api.core.Term)2 UnionOfConjunctiveQueries (fr.lirmm.graphik.graal.api.core.UnionOfConjunctiveQueries)2 ChaseException (fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)2 KnowledgeBaseException (fr.lirmm.graphik.graal.api.kb.KnowledgeBaseException)2 PureRewriter (fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter)2 DefaultUnionOfConjunctiveQueries (fr.lirmm.graphik.graal.core.DefaultUnionOfConjunctiveQueries)2 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)2 AnalyserRuleSet (fr.lirmm.graphik.graal.rulesetanalyser.util.AnalyserRuleSet)2 CloseableIteratorWithoutException (fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException)2 ConverterCloseableIterator (fr.lirmm.graphik.util.stream.converter.ConverterCloseableIterator)2 UniqFilter (fr.lirmm.graphik.util.stream.filter.UniqFilter)2 LinkedList (java.util.LinkedList)2