Search in sources :

Example 1 with BacktrackException

use of fr.lirmm.graphik.graal.homomorphism.BacktrackException 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 BacktrackException

use of fr.lirmm.graphik.graal.homomorphism.BacktrackException in project graal by graphik-team.

the class RestrictedChaseHaltingCondition method apply.

@Override
public CloseableIterator<Atom> apply(Rule rule, Substitution substitution, AtomSet data) throws HomomorphismFactoryException, HomomorphismException {
    InMemoryAtomSet newFacts = substitution.createImageOf(rule.getHead());
    ConjunctiveQuery query = new ConjunctiveQueryWithFixedVariables(newFacts, substitution.createImageOf(rule.getFrontier()));
    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);
    } catch (BacktrackException e) {
        throw new HomomorphismException("An errors occurs while iterating results", e);
    }
    // replace variables by fresh symbol
    for (Variable t : rule.getExistentials()) {
        substitution.put(t, data.getFreshSymbolGenerator().getFreshSymbol());
    }
    CloseableIteratorWithoutException<Atom> it = substitution.createImageOf(rule.getHead()).iterator();
    return it;
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Variable(fr.lirmm.graphik.graal.api.core.Variable) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) CloseableIteratorAdapter(fr.lirmm.graphik.util.stream.CloseableIteratorAdapter) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) ConjunctiveQueryWithFixedVariables(fr.lirmm.graphik.graal.core.ConjunctiveQueryWithFixedVariables) Atom(fr.lirmm.graphik.graal.api.core.Atom) BacktrackException(fr.lirmm.graphik.graal.homomorphism.BacktrackException)

Example 3 with BacktrackException

use of fr.lirmm.graphik.graal.homomorphism.BacktrackException 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 4 with BacktrackException

use of fr.lirmm.graphik.graal.homomorphism.BacktrackException in project graal by graphik-team.

the class NoForwardChecking method getCandidatsIterator.

@Override
public CloseableIterator<Term> getCandidatsIterator(AtomSet g, Var var, Substitution initialSubstitution, Map<Variable, Integer> map, Var[] varData, RulesCompilation rc) throws BacktrackException {
    HomomorphismIteratorChecker tmp;
    try {
        tmp = new HomomorphismIteratorChecker(var, g.termsIterator(), var.shared.preAtoms, g, initialSubstitution, map, varData, rc);
    } catch (AtomSetException e) {
        throw new BacktrackException(e);
    }
    tmp.setProfiler(this.getProfiler());
    return tmp;
}
Also used : AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) HomomorphismIteratorChecker(fr.lirmm.graphik.graal.homomorphism.utils.HomomorphismIteratorChecker) BacktrackException(fr.lirmm.graphik.graal.homomorphism.BacktrackException)

Example 5 with BacktrackException

use of fr.lirmm.graphik.graal.homomorphism.BacktrackException 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

BacktrackException (fr.lirmm.graphik.graal.homomorphism.BacktrackException)10 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)9 Atom (fr.lirmm.graphik.graal.api.core.Atom)6 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)6 CloseableIteratorAdapter (fr.lirmm.graphik.util.stream.CloseableIteratorAdapter)5 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)4 HomomorphismIteratorChecker (fr.lirmm.graphik.graal.homomorphism.utils.HomomorphismIteratorChecker)4 Term (fr.lirmm.graphik.graal.api.core.Term)3 Constant (fr.lirmm.graphik.graal.api.core.Constant)2 Variable (fr.lirmm.graphik.graal.api.core.Variable)2 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)1 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)1 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)1 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)1 Store (fr.lirmm.graphik.graal.api.store.Store)1 ConjunctiveQueryWithFixedVariables (fr.lirmm.graphik.graal.core.ConjunctiveQueryWithFixedVariables)1 Var (fr.lirmm.graphik.graal.homomorphism.Var)1 VarSharedData (fr.lirmm.graphik.graal.homomorphism.VarSharedData)1 Profiler (fr.lirmm.graphik.util.profiler.Profiler)1 AbstractCloseableIterator (fr.lirmm.graphik.util.stream.AbstractCloseableIterator)1