Search in sources :

Example 31 with IteratorException

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

the class ChaseWithGRDAndUnfiers method next.

// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public void next() throws ChaseException {
    Rule rule, unifiedRule;
    Substitution unificator;
    Queue<Triple<Rule, Substitution, InMemoryAtomSet>> newQueue = new LinkedList<Triple<Rule, Substitution, InMemoryAtomSet>>();
    InMemoryAtomSet newAtomSet = new DefaultInMemoryGraphStore();
    try {
        while (!queue.isEmpty()) {
            Triple<Rule, Substitution, InMemoryAtomSet> pair = queue.poll();
            if (pair != null) {
                unificator = pair.getMiddle();
                InMemoryAtomSet part = pair.getRight();
                rule = pair.getLeft();
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("\nExecute rule: {} with unificator {}", rule, unificator);
                }
                unifiedRule = DefaultUnifierAlgorithm.getTargetVariablesSubstitution().createImageOf(rule);
                unifiedRule = unificator.createImageOf(unifiedRule);
                unifiedRule.getBody().removeAll(part);
                unificator = targetToSource(unificator);
                ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(unifiedRule.getBody(), new LinkedList<Term>(unifiedRule.getFrontier()));
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Rule to execute: {}", unifiedRule.toString());
                    LOGGER.debug("       -- Query: {}", query.toString());
                }
                // Get projections
                List<Substitution> projections = Iterators.toList(SmartHomomorphism.instance().execute(query, atomSet));
                try {
                    for (Substitution proj : projections) {
                        InMemoryAtomSet newFacts = proj.createImageOf(unifiedRule.getHead());
                        ConjunctiveQuery q = new DefaultConjunctiveQuery(newFacts);
                        if (!SmartHomomorphism.instance().execute(q, newAtomSet).hasNext()) {
                            // Existential variables instantiation added to proj
                            CloseableIterator<Atom> it = hc.apply(unifiedRule, proj, atomSet);
                            if (it.hasNext()) {
                                LinkedListAtomSet foundPart = new LinkedListAtomSet();
                                foundPart.addAll(it);
                                newAtomSet.addAll(foundPart);
                                // Makes the projection compatible with triggered rules unifiers
                                Substitution compatibleProj = targetToSource(proj);
                                for (Pair<Rule, Substitution> p : this.grd.getTriggeredRulesWithUnifiers(rule)) {
                                    Rule triggeredRule = p.getKey();
                                    Substitution u = p.getValue();
                                    if (u != null) {
                                        Substitution comp = unificator.compose(u);
                                        Substitution aggreg = compatibleProj.aggregate(comp);
                                        aggreg = forgetSource(aggreg);
                                        if (LOGGER.isDebugEnabled()) {
                                            LOGGER.debug("-- -- Dependency: {}", triggeredRule);
                                            LOGGER.debug("-- -- Substitution:{} ", compatibleProj);
                                            LOGGER.debug("-- -- Unificator: {}", u);
                                            LOGGER.debug("-- -- Aggregation: {}\n", aggreg);
                                        }
                                        if (aggreg != null) {
                                            newQueue.add(new ImmutableTriple<Rule, Substitution, InMemoryAtomSet>(triggeredRule, aggreg, foundPart));
                                        }
                                    }
                                }
                            }
                        }
                    }
                } catch (HomomorphismFactoryException e) {
                    throw new RuleApplicationException("Error during rule application", e);
                } catch (HomomorphismException e) {
                    throw new RuleApplicationException("Error during rule application", e);
                } catch (IteratorException e) {
                    throw new RuleApplicationException("Error during rule application", e);
                }
            }
        }
        queue = newQueue;
        atomSet.addAll(newAtomSet);
    } catch (Exception e) {
        e.printStackTrace();
        throw new ChaseException("An error occur pending saturation step.", e);
    }
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) RuleApplicationException(fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException) HomomorphismFactoryException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismFactoryException) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) Term(fr.lirmm.graphik.graal.api.core.Term) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) LinkedList(java.util.LinkedList) Atom(fr.lirmm.graphik.graal.api.core.Atom) HomomorphismFactoryException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismFactoryException) RuleApplicationException(fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Triple(org.apache.commons.lang3.tuple.Triple) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery)

Example 32 with IteratorException

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

the class AbstractRuleApplier method apply.

@Override
public boolean apply(Rule rule, T atomSet) throws RuleApplicationException {
    boolean isChanged = false;
    ConjunctiveQuery query = this.generateQuery(rule);
    try {
        CloseableIterator<Substitution> subIt = this.executeQuery(query, atomSet);
        while (subIt.hasNext()) {
            Substitution substitution = subIt.next();
            CloseableIterator<Atom> it = this.getHaltingCondition().apply(rule, substitution, atomSet);
            if (it.hasNext()) {
                atomSet.addAll(it);
                isChanged = true;
            }
        }
        subIt.close();
    } catch (HomomorphismFactoryException e) {
        throw new RuleApplicationException("Error during rule application", e);
    } catch (HomomorphismException e) {
        throw new RuleApplicationException("Error during rule application", e);
    } catch (AtomSetException e) {
        throw new RuleApplicationException("Error during rule application", e);
    } catch (IteratorException e) {
        throw new RuleApplicationException("Error during rule application", e);
    }
    return isChanged;
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) RuleApplicationException(fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException) HomomorphismFactoryException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismFactoryException) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 33 with IteratorException

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

the class PreparedBacktrackHomomorphism method exist.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public boolean exist(Substitution s) throws HomomorphismException {
    CloseableIterator<Substitution> results = this.execute(s);
    boolean val;
    try {
        val = results.hasNext();
    } catch (IteratorException e) {
        throw new HomomorphismException(e);
    }
    results.close();
    return val;
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution)

Example 34 with IteratorException

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

the class DefaultUCQHomomorphismWithCompilation method exist.

@Override
public boolean exist(UnionOfConjunctiveQueries q, AtomSet a, RulesCompilation compilation) throws HomomorphismException {
    try {
        CloseableIterator<Substitution> execute = this.execute(q, a, compilation);
        boolean res = execute.hasNext();
        execute.close();
        return res;
    } catch (IteratorException e) {
        throw new HomomorphismException(e);
    }
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution)

Example 35 with IteratorException

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

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