Search in sources :

Example 1 with AbstractCloseableIterator

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

Atom (fr.lirmm.graphik.graal.api.core.Atom)1 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)1 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)1 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)1 Term (fr.lirmm.graphik.graal.api.core.Term)1 BacktrackException (fr.lirmm.graphik.graal.homomorphism.BacktrackException)1 AbstractCloseableIterator (fr.lirmm.graphik.util.stream.AbstractCloseableIterator)1 CloseableIterator (fr.lirmm.graphik.util.stream.CloseableIterator)1 CloseableIteratorAggregator (fr.lirmm.graphik.util.stream.CloseableIteratorAggregator)1 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)1 Pair (org.apache.commons.lang3.tuple.Pair)1