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);
}
}
}
Aggregations