Search in sources :

Example 1 with CloseableIteratorAggregator

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

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

Example 3 with CloseableIteratorAggregator

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);
    }
}
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)3 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)3 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)3 CloseableIterator (fr.lirmm.graphik.util.stream.CloseableIterator)3 CloseableIteratorAggregator (fr.lirmm.graphik.util.stream.CloseableIteratorAggregator)3 Term (fr.lirmm.graphik.graal.api.core.Term)2 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)2 ConverterCloseableIterator (fr.lirmm.graphik.util.stream.converter.ConverterCloseableIterator)2 LinkedList (java.util.LinkedList)2 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)1 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)1 Variable (fr.lirmm.graphik.graal.api.core.Variable)1 BacktrackException (fr.lirmm.graphik.graal.homomorphism.BacktrackException)1 AbstractCloseableIterator (fr.lirmm.graphik.util.stream.AbstractCloseableIterator)1 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)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 Pair (org.apache.commons.lang3.tuple.Pair)1