Search in sources :

Example 1 with FilterIterator

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

Aggregations

Atom (fr.lirmm.graphik.graal.api.core.Atom)1 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)1 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)1 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)1 Term (fr.lirmm.graphik.graal.api.core.Term)1 Variable (fr.lirmm.graphik.graal.api.core.Variable)1 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)1 CloseableIterator (fr.lirmm.graphik.util.stream.CloseableIterator)1 CloseableIteratorAggregator (fr.lirmm.graphik.util.stream.CloseableIteratorAggregator)1 ConverterCloseableIterator (fr.lirmm.graphik.util.stream.converter.ConverterCloseableIterator)1 AndFilter (fr.lirmm.graphik.util.stream.filter.AndFilter)1 Filter (fr.lirmm.graphik.util.stream.filter.Filter)1 FilterIterator (fr.lirmm.graphik.util.stream.filter.FilterIterator)1 LinkedList (java.util.LinkedList)1