Search in sources :

Example 1 with CloseableIteratorAdapter

use of fr.lirmm.graphik.util.stream.CloseableIteratorAdapter in project graal by graphik-team.

the class StatBootstrapper method exec.

// /////////////////////////////////////////////////////////////////////////
// 
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIterator<Term> exec(final VarSharedData v, Collection<Atom> preAtoms, Collection<Atom> postAtoms, final AtomSet data, RulesCompilation rc) throws BacktrackException {
    if (!(data instanceof Store)) {
        return fallback.exec(v, preAtoms, postAtoms, data, rc);
    }
    Store store = (Store) data;
    Set<Term> terms = null;
    if (this.getProfiler() != null) {
        this.getProfiler().start("BootstrapTime");
        this.getProfiler().start("BootstrapTimeFirstPart");
    }
    Iterator<Atom> it;
    Collection<Constant> constants = null;
    Atom aa = null;
    it = postAtoms.iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        if (constants == null || constants.isEmpty()) {
            constants = a.getConstants();
            aa = a;
        }
    }
    it = preAtoms.iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        if (constants == null || constants.isEmpty()) {
            constants = a.getConstants();
            aa = a;
        }
    }
    try {
        if (constants != null && !constants.isEmpty()) {
            terms = new HashSet<Term>();
            for (Pair<Atom, Substitution> im : rc.getRewritingOf(aa)) {
                int pos = im.getLeft().indexOf(im.getRight().createImageOf(v.value));
                CloseableIterator<Atom> match = data.match(im.getLeft());
                while (match.hasNext()) {
                    terms.add(match.next().getTerm(pos));
                }
            }
        }
        if (this.getProfiler() != null) {
            this.getProfiler().stop("BootstrapTimeFirstPart");
        }
        if (terms == null) {
            Atom a = null, tmp;
            double probaA = 1.1;
            it = postAtoms.iterator();
            while (it.hasNext()) {
                tmp = it.next();
                double p = ProbaUtils.computeProba(tmp, store, rc);
                if (p < probaA) {
                    a = tmp;
                    p = probaA;
                }
            }
            it = preAtoms.iterator();
            while (it.hasNext()) {
                tmp = it.next();
                double p = ProbaUtils.computeProba(tmp, store, rc);
                if (p < probaA) {
                    a = tmp;
                    p = probaA;
                }
            }
            terms = BootstrapperUtils.computeCandidatesOverRewritings(a, v, data, rc);
        }
        if (this.getProfiler() != null) {
            this.getProfiler().stop("BootstrapTime");
        }
        if (terms == null) {
            return data.termsIterator();
        } else {
            return new CloseableIteratorAdapter<Term>(terms.iterator());
        }
    } catch (AtomSetException e) {
        throw new BacktrackException(e);
    } catch (IteratorException e) {
        throw new BacktrackException(e);
    }
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Constant(fr.lirmm.graphik.graal.api.core.Constant) Store(fr.lirmm.graphik.graal.api.store.Store) Term(fr.lirmm.graphik.graal.api.core.Term) CloseableIteratorAdapter(fr.lirmm.graphik.util.stream.CloseableIteratorAdapter) Atom(fr.lirmm.graphik.graal.api.core.Atom) BacktrackException(fr.lirmm.graphik.graal.homomorphism.BacktrackException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException)

Example 2 with CloseableIteratorAdapter

use of fr.lirmm.graphik.util.stream.CloseableIteratorAdapter in project graal by graphik-team.

the class FrontierRestrictedChaseHaltingCondition method apply.

@Override
public CloseableIterator<Atom> apply(Rule rule, Substitution substitution, AtomSet data) throws HomomorphismFactoryException, HomomorphismException {
    Set<Term> fixedVars = substitution.getValues();
    if (ruleIndex.get(rule) == null) {
        ruleIndex.put(rule, _currentRuleIndex++);
    }
    final int index = ruleIndex.get(rule).intValue();
    StringBuilder frontierSb = new StringBuilder();
    SortedSet<Variable> frontierSet = new TreeSet<Variable>(rule.getFrontier());
    for (Term t : frontierSet) {
        frontierSb.append("_");
        frontierSb.append(t.getLabel());
        frontierSb.append(substitution.createImageOf(t).getLabel());
    }
    String frontier = frontierSb.toString();
    for (Variable t : rule.getExistentials()) {
        substitution.put(t, DefaultTermFactory.instance().createConstant("f_" + index + "_" + t.getIdentifier() + frontier));
    }
    InMemoryAtomSet newFacts = substitution.createImageOf(rule.getHead());
    ConjunctiveQuery query = new ConjunctiveQueryWithFixedVariables(newFacts, fixedVars);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Fixed Query:" + query);
    }
    try {
        if (SmartHomomorphism.instance().execute(query, data).hasNext()) {
            return new CloseableIteratorAdapter<Atom>(Collections.<Atom>emptyList().iterator());
        }
    } catch (IteratorException e) {
        throw new HomomorphismException("An errors occurs while iterating results", e);
    }
    return newFacts.iterator();
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Variable(fr.lirmm.graphik.graal.api.core.Variable) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Term(fr.lirmm.graphik.graal.api.core.Term) CloseableIteratorAdapter(fr.lirmm.graphik.util.stream.CloseableIteratorAdapter) ConjunctiveQueryWithFixedVariables(fr.lirmm.graphik.graal.core.ConjunctiveQueryWithFixedVariables) Atom(fr.lirmm.graphik.graal.api.core.Atom) TreeSet(java.util.TreeSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)

Example 3 with CloseableIteratorAdapter

use of fr.lirmm.graphik.util.stream.CloseableIteratorAdapter in project graal by graphik-team.

the class NaturalRDBMSStore method termsIterator.

@Override
@Deprecated
public CloseableIterator<Term> termsIterator(Type type) throws AtomSetException {
    Set<Term> terms = new TreeSet<Term>();
    CloseableIterator<Predicate> predIt = this.predicatesIterator();
    try {
        while (predIt.hasNext()) {
            Predicate p = predIt.next();
            for (int i = 0; i < p.getArity(); ++i) {
                CloseableIterator<Term> termIt = this.termsByPredicatePosition(p, i);
                while (termIt.hasNext()) {
                    Term t = termIt.next();
                    if (type.equals(t.getType())) {
                        terms.add(t);
                    }
                }
            }
        }
    } catch (IteratorException e) {
        throw new AtomSetException(e);
    }
    return new CloseableIteratorAdapter<Term>(terms.iterator());
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) TreeSet(java.util.TreeSet) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Term(fr.lirmm.graphik.graal.api.core.Term) CloseableIteratorAdapter(fr.lirmm.graphik.util.stream.CloseableIteratorAdapter) Predicate(fr.lirmm.graphik.graal.api.core.Predicate)

Example 4 with CloseableIteratorAdapter

use of fr.lirmm.graphik.util.stream.CloseableIteratorAdapter in project graal by graphik-team.

the class NaturalRDBMSStore method termsIterator.

@Override
public CloseableIterator<Term> termsIterator() throws AtomSetException {
    Set<Term> terms = new TreeSet<Term>();
    CloseableIterator<Predicate> predIt = this.predicatesIterator();
    try {
        while (predIt.hasNext()) {
            Predicate p = predIt.next();
            for (int i = 0; i < p.getArity(); ++i) {
                CloseableIterator<Term> termIt = this.termsByPredicatePosition(p, i);
                while (termIt.hasNext()) {
                    terms.add(termIt.next());
                }
            }
        }
    } catch (IteratorException e) {
        throw new AtomSetException(e);
    }
    return new CloseableIteratorAdapter<Term>(terms.iterator());
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) TreeSet(java.util.TreeSet) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Term(fr.lirmm.graphik.graal.api.core.Term) CloseableIteratorAdapter(fr.lirmm.graphik.util.stream.CloseableIteratorAdapter) Predicate(fr.lirmm.graphik.graal.api.core.Predicate)

Example 5 with CloseableIteratorAdapter

use of fr.lirmm.graphik.util.stream.CloseableIteratorAdapter in project graal by graphik-team.

the class StarBootstrapper method exec.

// /////////////////////////////////////////////////////////////////////////
// 
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIterator<Term> exec(final VarSharedData v, Collection<Atom> preAtoms, Collection<Atom> postAtoms, final AtomSet data, RulesCompilation compilation) throws BacktrackException {
    Set<Term> terms = null;
    if (this.getProfiler() != null) {
        this.getProfiler().start("BootstrapTime");
        this.getProfiler().start("BootstrapTimeFirstPart");
    }
    Iterator<Atom> it;
    Collection<Constant> constants = null;
    Atom aa = null;
    it = postAtoms.iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        if (constants == null || constants.isEmpty()) {
            constants = a.getConstants();
            aa = a;
        }
    }
    it = preAtoms.iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        if (constants == null || constants.isEmpty()) {
            constants = a.getConstants();
            aa = a;
        }
    }
    try {
        if (constants != null && !constants.isEmpty()) {
            terms = new TreeSet<Term>(TermValueComparator.instance());
            for (Pair<Atom, Substitution> im : compilation.getRewritingOf(aa)) {
                int pos = im.getLeft().indexOf(im.getRight().createImageOf(v.value));
                CloseableIterator<Atom> match = data.match(im.getLeft());
                while (match.hasNext()) {
                    terms.add(match.next().getTerm(pos));
                }
            }
        }
        if (this.getProfiler() != null) {
            this.getProfiler().stop("BootstrapTimeFirstPart");
        }
        if (terms == null) {
            it = postAtoms.iterator();
            while (it.hasNext()) {
                if (terms == null) {
                    terms = BootstrapperUtils.computeCandidatesOverRewritings(it.next(), v, data, compilation);
                } else {
                    terms.retainAll(BootstrapperUtils.computeCandidatesOverRewritings(it.next(), v, data, compilation));
                }
            }
            it = preAtoms.iterator();
            while (it.hasNext()) {
                if (terms == null) {
                    terms = BootstrapperUtils.computeCandidatesOverRewritings(it.next(), v, data, compilation);
                } else {
                    terms.retainAll(BootstrapperUtils.computeCandidatesOverRewritings(it.next(), v, data, compilation));
                }
            }
        }
        if (this.getProfiler() != null) {
            this.getProfiler().stop("BootstrapTime");
        }
        if (terms == null) {
            return data.termsIterator();
        } else {
            return new CloseableIteratorAdapter<Term>(terms.iterator());
        }
    } catch (AtomSetException e) {
        throw new BacktrackException(e);
    } catch (IteratorException e) {
        throw new BacktrackException(e);
    }
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Constant(fr.lirmm.graphik.graal.api.core.Constant) Term(fr.lirmm.graphik.graal.api.core.Term) CloseableIteratorAdapter(fr.lirmm.graphik.util.stream.CloseableIteratorAdapter) Atom(fr.lirmm.graphik.graal.api.core.Atom) BacktrackException(fr.lirmm.graphik.graal.homomorphism.BacktrackException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException)

Aggregations

CloseableIteratorAdapter (fr.lirmm.graphik.util.stream.CloseableIteratorAdapter)8 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)6 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)6 Term (fr.lirmm.graphik.graal.api.core.Term)5 BacktrackException (fr.lirmm.graphik.graal.homomorphism.BacktrackException)5 Atom (fr.lirmm.graphik.graal.api.core.Atom)4 TreeSet (java.util.TreeSet)3 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)2 Constant (fr.lirmm.graphik.graal.api.core.Constant)2 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)2 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)2 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)2 Variable (fr.lirmm.graphik.graal.api.core.Variable)2 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)2 ConjunctiveQueryWithFixedVariables (fr.lirmm.graphik.graal.core.ConjunctiveQueryWithFixedVariables)2 HomomorphismIteratorChecker (fr.lirmm.graphik.graal.homomorphism.utils.HomomorphismIteratorChecker)2 Store (fr.lirmm.graphik.graal.api.store.Store)1