Search in sources :

Example 66 with AtomSetException

use of fr.lirmm.graphik.graal.api.core.AtomSetException in project graal by graphik-team.

the class BacktrackIterator method computeNext.

// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
/**
 * level < 0 : no more answers <br/>
 * level 0 : not initialized
 */
private Substitution computeNext() throws BacktrackException {
    if (profiler != null) {
        profiler.start("backtrackingTime");
    }
    try {
        if (level == 0) {
            // check if the full instantiated atoms from the query are in data
            if (BacktrackUtils.isHomomorphism(this.data.varsOrder[level].preAtoms, this.data.data, this.initialSubstitution, this.data.index, this.vars, this.data.compilation)) {
                if (this.existNegParts()) {
                    this.data.bj.success();
                    backtrack(false);
                } else {
                    ++level;
                }
            } else {
                --level;
            }
        }
        while (level > 0) {
            profiler.incr("#calls", 1);
            if (level > this.data.levelMax) {
                // Homomorphism found
                Substitution sol = solutionFound(this.data.ans);
                this.data.bj.success();
                backtrack(false);
                if (profiler != null) {
                    profiler.stop("backtrackingTime");
                }
                return sol;
            } else {
                if (goBack) {
                    if (hasMoreValues(currentVar(), this.data.data)) {
                        goBack = false;
                        ++level;
                    } else {
                        backtrack(true);
                    }
                } else {
                    if (getFirstValue(currentVar(), this.data.data)) {
                        ++level;
                    } else {
                        backtrack(true);
                    }
                }
            }
        }
    } catch (AtomSetException e) {
        throw new BacktrackException("Exception during backtracking", e);
    }
    --level;
    if (profiler != null) {
        profiler.stop("backtrackingTime");
    }
    return null;
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException)

Example 67 with AtomSetException

use of fr.lirmm.graphik.graal.api.core.AtomSetException in project graal by graphik-team.

the class FullyInstantiatedQueryHomomorphism method execute.

// /////////////////////////////////////////////////////////////////////////
// HOMOMORPHISM METHODS
// /////////////////////////////////////////////////////////////////////////
public <U2 extends AtomSet> CloseableIterator<Substitution> execute(InMemoryAtomSet q, U2 a) throws HomomorphismException {
    try {
        CloseableIteratorWithoutException<Atom> it = q.iterator();
        boolean contains = true;
        while (contains && it.hasNext()) {
            contains = a.contains(it.next());
        }
        if (contains) {
            return Iterators.singletonIterator(Substitutions.emptySubstitution());
        } else {
            return Iterators.emptyIterator();
        }
    } catch (AtomSetException e) {
        throw new HomomorphismException(e);
    }
}
Also used : HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 68 with AtomSetException

use of fr.lirmm.graphik.graal.api.core.AtomSetException in project graal by graphik-team.

the class RestrictedChaseRuleApplier method apply.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public boolean apply(Rule rule, T atomSet) throws RuleApplicationException {
    try {
        boolean res = false;
        ConjunctiveQueryWithNegatedParts query = new RuleWrapper2ConjunctiveQueryWithNegatedParts(rule);
        CloseableIterator<Substitution> results;
        results = SmartHomomorphism.instance().execute(query, atomSet);
        while (results.hasNext()) {
            res = true;
            Substitution proj = results.next();
            // replace variables by fresh symbol
            for (Variable t : rule.getExistentials()) {
                proj.put(t, atomSet.getFreshSymbolGenerator().getFreshSymbol());
            }
            CloseableIteratorWithoutException<Atom> it = proj.createImageOf(rule.getHead()).iterator();
            while (it.hasNext()) {
                atomSet.add(it.next());
            }
        }
        return res;
    } catch (HomomorphismException e) {
        throw new RuleApplicationException("", e);
    } catch (AtomSetException e) {
        throw new RuleApplicationException("", e);
    } catch (IteratorException e) {
        throw new RuleApplicationException("", e);
    }
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) RuleWrapper2ConjunctiveQueryWithNegatedParts(fr.lirmm.graphik.graal.core.RuleWrapper2ConjunctiveQueryWithNegatedParts) RuleApplicationException(fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException) Variable(fr.lirmm.graphik.graal.api.core.Variable) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) RuleWrapper2ConjunctiveQueryWithNegatedParts(fr.lirmm.graphik.graal.core.RuleWrapper2ConjunctiveQueryWithNegatedParts) ConjunctiveQueryWithNegatedParts(fr.lirmm.graphik.graal.api.core.ConjunctiveQueryWithNegatedParts) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 69 with AtomSetException

use of fr.lirmm.graphik.graal.api.core.AtomSetException in project graal by graphik-team.

the class BreadthFirstChase method dispatchNewData.

// /////////////////////////////////////////////////////////////////////////
// PRIVATE CLASS
// /////////////////////////////////////////////////////////////////////////
protected void dispatchNewData(Collection<Atom> newData) throws ChaseException {
    for (Atom a : newData) {
        Predicate p = a.getPredicate();
        for (Rule r : ruleSet.getRulesByBodyPredicate(p)) {
            if (linearRuleCheck(r)) {
                AtomSet set = nextRulesToCheck.get(r);
                if (set == null) {
                    set = new DefaultInMemoryGraphStore();
                    nextRulesToCheck.put(r, set);
                }
                try {
                    set.add(a);
                } catch (AtomSetException e) {
                    throw new ChaseException("Exception while adding data into a tmp store", e);
                }
            } else {
                nextRulesToCheck.put(r, atomSet);
            }
        }
    }
}
Also used : AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate)

Aggregations

AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)69 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)26 SQLException (java.sql.SQLException)26 Atom (fr.lirmm.graphik.graal.api.core.Atom)25 Term (fr.lirmm.graphik.graal.api.core.Term)23 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)12 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)11 Statement (java.sql.Statement)11 DBTable (fr.lirmm.graphik.graal.store.rdbms.util.DBTable)10 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)9 BacktrackException (fr.lirmm.graphik.graal.homomorphism.BacktrackException)9 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)7 TreeSet (java.util.TreeSet)7 SQLQuery (fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery)6 CloseableIteratorAdapter (fr.lirmm.graphik.util.stream.CloseableIteratorAdapter)6 RuleApplicationException (fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException)5 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)5 TreeMap (java.util.TreeMap)5 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)4 Variable (fr.lirmm.graphik.graal.api.core.Variable)4