use of fr.lirmm.graphik.util.stream.CloseableIteratorAggregator 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);
}
}
use of fr.lirmm.graphik.util.stream.CloseableIteratorAggregator 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);
}
}
}
use of fr.lirmm.graphik.util.stream.CloseableIteratorAggregator 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);
}
}
Aggregations