Search in sources :

Example 26 with IteratorException

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

Example 27 with IteratorException

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

the class SimpleFC method checkForward.

@Override
public boolean checkForward(Var v, AtomSet g, Substitution initialSubstitution, Map<Variable, Integer> map, Var[] varData, RulesCompilation rc) throws BacktrackException {
    Profiler profiler = this.getProfiler();
    for (Atom atom : v.shared.postAtoms) {
        boolean contains = false;
        Atom im = BacktrackUtils.createImageOf(atom, initialSubstitution, map, varData);
        if (profiler != null) {
            profiler.incr("#selectOne", 1);
            profiler.start("selectOneTime");
        }
        for (Pair<Atom, Substitution> rew : rc.getRewritingOf(im)) {
            Atom a = rew.getLeft();
            CloseableIterator<Atom> matchIt = null;
            try {
                matchIt = g.match(a);
                if (matchIt.hasNext()) {
                    contains = true;
                    break;
                }
            } catch (IteratorException e) {
                throw new BacktrackException(e);
            } catch (AtomSetException e) {
                throw new BacktrackException(e);
            } finally {
                if (matchIt != null) {
                    matchIt.close();
                }
            }
        }
        if (profiler != null) {
            profiler.stop("selectOneTime");
        }
        if (!contains) {
            return false;
        }
    }
    return true;
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Profiler(fr.lirmm.graphik.util.profiler.Profiler) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Atom(fr.lirmm.graphik.graal.api.core.Atom) BacktrackException(fr.lirmm.graphik.graal.homomorphism.BacktrackException)

Example 28 with IteratorException

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

the class NFC2 method checkForward.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public boolean checkForward(Var v, AtomSet g, Substitution initialSubstitution, Map<Variable, Integer> map, Var[] varData, RulesCompilation rc) throws BacktrackException {
    // clear all computed candidats for post variables
    for (VarSharedData z : v.shared.postVars) {
        this.clear(v.shared, z);
    }
    Var varToAssign = null;
    for (Atom atom : v.shared.postAtoms) {
        boolean runCheck = true;
        if (checkMode) {
            int i = 0;
            for (Variable t : atom.getVariables()) {
                Integer idx = map.get(t);
                if (idx != null) {
                    Var z = varData[idx];
                    if (z.shared.level > v.shared.level) {
                        ++i;
                        varToAssign = z;
                        if (i > 1 || !this.data[z.shared.level].candidats[v.shared.level].init) {
                            runCheck = false;
                            break;
                        }
                    }
                }
            }
        }
        if (checkMode && runCheck) {
            try {
                if (!check(atom, v.shared, varToAssign.shared, g, initialSubstitution, map, varData, rc)) {
                    return false;
                }
            } catch (AtomSetException e) {
                throw new BacktrackException("An error occurs while checking current candidate");
            }
        } else {
            try {
                if (!select(atom, v, g, initialSubstitution, map, varData, rc)) {
                    return false;
                }
            } catch (IteratorException e) {
                throw new BacktrackException("An error occurs while selecting candidates for next steps ");
            } catch (AtomSetException e) {
                throw new BacktrackException("An error occurs while selecting candidates for next steps ");
            }
        }
    }
    return true;
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Variable(fr.lirmm.graphik.graal.api.core.Variable) Var(fr.lirmm.graphik.graal.homomorphism.Var) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) VarSharedData(fr.lirmm.graphik.graal.homomorphism.VarSharedData) Atom(fr.lirmm.graphik.graal.api.core.Atom) BacktrackException(fr.lirmm.graphik.graal.homomorphism.BacktrackException)

Example 29 with IteratorException

use of fr.lirmm.graphik.util.stream.IteratorException 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 30 with IteratorException

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

the class DefaultKnowledgeBase method isConsistent.

@Override
public boolean isConsistent() throws KnowledgeBaseException {
    try {
        CloseableIterator<Substitution> results = this.query(DefaultConjunctiveQueryFactory.instance().BOOLEAN_BOTTOM_QUERY);
        boolean res = !results.hasNext();
        results.close();
        return res;
    } catch (IteratorException e) {
        throw new KnowledgeBaseException(e);
    }
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) KnowledgeBaseException(fr.lirmm.graphik.graal.api.kb.KnowledgeBaseException)

Aggregations

IteratorException (fr.lirmm.graphik.util.stream.IteratorException)37 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)24 Atom (fr.lirmm.graphik.graal.api.core.Atom)16 Term (fr.lirmm.graphik.graal.api.core.Term)14 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)14 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)13 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)9 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)8 TreeSet (java.util.TreeSet)8 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)7 BacktrackException (fr.lirmm.graphik.graal.homomorphism.BacktrackException)6 CloseableIteratorAdapter (fr.lirmm.graphik.util.stream.CloseableIteratorAdapter)6 RuleApplicationException (fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException)5 Variable (fr.lirmm.graphik.graal.api.core.Variable)4 SQLException (java.sql.SQLException)4 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)3 Rule (fr.lirmm.graphik.graal.api.core.Rule)3 HomomorphismFactoryException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismFactoryException)3 DBTable (fr.lirmm.graphik.graal.store.rdbms.util.DBTable)3 SQLQuery (fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery)3