Search in sources :

Example 1 with RuleApplicationException

use of fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException in project graal by graphik-team.

the class SQLRuleApplier method apply.

// //////////////////////////////////////////////////////////////////////////
// METHODS
// //////////////////////////////////////////////////////////////////////////
@Override
public boolean apply(Rule rule, RdbmsStore store) throws RuleApplicationException {
    boolean returnValue = false;
    if (rule.getExistentials().isEmpty()) {
        Statement statement = null;
        try {
            statement = store.getDriver().createStatement();
            Iterator<SQLQuery> sqlQueries = store.getConjunctiveQueryTranslator().translate(rule);
            while (sqlQueries.hasNext()) {
                SQLQuery query = sqlQueries.next();
                if (!query.hasSchemaError())
                    statement.addBatch(query.toString());
            }
            int[] res = statement.executeBatch();
            for (int i = 0; i < res.length; ++i) {
                if (res[i] > 0) {
                    returnValue = true;
                    break;
                }
            }
        } catch (AtomSetException e) {
            throw new RuleApplicationException("An error has been occured during rule application.", e);
        } catch (SQLException e) {
            throw new RuleApplicationException("An error has been occured during rule application.", e);
        } finally {
            if (statement != null) {
                try {
                    statement.getConnection().commit();
                    statement.close();
                } catch (SQLException e) {
                }
            }
        }
    } else {
        returnValue = this.fallback.apply(rule, store);
    }
    return returnValue;
}
Also used : RuleApplicationException(fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) SQLQuery(fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery)

Example 2 with RuleApplicationException

use of fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException in project graal by graphik-team.

the class SccChase method next.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public void next() throws ChaseException {
    ++this.level;
    tmpAtom = new LinkedList<Atom>();
    for (Integer scc : layers[level]) {
        Set<Rule> component = this.sccg.getComponent(scc);
        GraphOfRuleDependencies subGraph = this.grd.getSubGraph(component);
        if (component.size() == 1 && !subGraph.hasCircuit()) {
            try {
                CloseableIterator<Atom> it = this.getRuleApplier().delegatedApply(component.iterator().next(), atomSet);
                while (it.hasNext()) {
                    tmpAtom.add(it.next());
                }
                it.close();
            } catch (RuleApplicationException e) {
                throw new ChaseException("", e);
            } catch (IteratorException e) {
                throw new ChaseException("", e);
            }
        } else {
            Chase chase = new ChaseWithGRD<T>(subGraph, atomSet, this.getRuleApplier());
            chase.execute();
        }
    }
    try {
        atomSet.addAll(new CloseableIteratorAdapter<Atom>(tmpAtom.iterator()));
    } catch (AtomSetException e) {
        throw new ChaseException("", e);
    }
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) RuleApplicationException(fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) Atom(fr.lirmm.graphik.graal.api.core.Atom) Chase(fr.lirmm.graphik.graal.api.forward_chaining.Chase) AbstractChase(fr.lirmm.graphik.graal.api.forward_chaining.AbstractChase) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultGraphOfRuleDependencies(fr.lirmm.graphik.graal.core.grd.DefaultGraphOfRuleDependencies) GraphOfRuleDependencies(fr.lirmm.graphik.graal.api.core.GraphOfRuleDependencies)

Example 3 with RuleApplicationException

use of fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException 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 4 with RuleApplicationException

use of fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException in project graal by graphik-team.

the class RestrictedChaseRuleApplier method delegatedApply.

@Override
public CloseableIterator<Atom> delegatedApply(Rule rule, T atomSet) throws RuleApplicationException {
    try {
        ConjunctiveQueryWithNegatedParts query = new RuleWrapper2ConjunctiveQueryWithNegatedParts(rule);
        CloseableIterator<Substitution> results = SmartHomomorphism.instance().execute(query, atomSet);
        return new RuleApplierIterator(results, rule, atomSet);
    } catch (HomomorphismException e) {
        throw new RuleApplicationException("", e);
    }
}
Also used : RuleWrapper2ConjunctiveQueryWithNegatedParts(fr.lirmm.graphik.graal.core.RuleWrapper2ConjunctiveQueryWithNegatedParts) RuleApplicationException(fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException) 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)

Example 5 with RuleApplicationException

use of fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException 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)

Aggregations

RuleApplicationException (fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException)8 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)6 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)5 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)5 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)5 Atom (fr.lirmm.graphik.graal.api.core.Atom)4 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)3 Rule (fr.lirmm.graphik.graal.api.core.Rule)3 HomomorphismFactoryException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismFactoryException)3 ConjunctiveQueryWithNegatedParts (fr.lirmm.graphik.graal.api.core.ConjunctiveQueryWithNegatedParts)2 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)2 ChaseException (fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)2 RuleWrapper2ConjunctiveQueryWithNegatedParts (fr.lirmm.graphik.graal.core.RuleWrapper2ConjunctiveQueryWithNegatedParts)2 SQLException (java.sql.SQLException)2 GraphOfRuleDependencies (fr.lirmm.graphik.graal.api.core.GraphOfRuleDependencies)1 Term (fr.lirmm.graphik.graal.api.core.Term)1 Variable (fr.lirmm.graphik.graal.api.core.Variable)1 AbstractChase (fr.lirmm.graphik.graal.api.forward_chaining.AbstractChase)1 Chase (fr.lirmm.graphik.graal.api.forward_chaining.Chase)1 ParseException (fr.lirmm.graphik.graal.api.io.ParseException)1