Search in sources :

Example 21 with AtomSetException

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

the class AbstractRdbmsStore method addAll.

@Override
public boolean addAll(CloseableIterator<? extends Atom> stream) throws AtomSetException {
    try {
        int c = 0;
        Statement statement = this.createStatement();
        while (stream.hasNext()) {
            this.add(statement, stream.next());
            if (++c % MAX_BATCH_SIZE == 0) {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("batch commit, size=" + MAX_BATCH_SIZE);
                }
                statement.executeBatch();
                statement.close();
                statement = this.createStatement();
            }
        }
        // if(statement != null) {// && !statement.isClosed()) {
        statement.executeBatch();
        statement.close();
        // }
        this.getConnection().commit();
    } catch (Exception e) {
        throw new AtomSetException(e);
    }
    return true;
}
Also used : Statement(java.sql.Statement) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) SQLException(java.sql.SQLException) IteratorException(fr.lirmm.graphik.util.stream.IteratorException)

Example 22 with AtomSetException

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

the class AbstractRdbmsStore method contains.

@Override
public boolean contains(Atom atom) throws AtomSetException {
    if (!this.check(atom)) {
        return false;
    }
    Statement statement = this.createStatement();
    SQLQuery query = this.getConjunctiveQueryTranslator().translateContainsQuery(atom);
    boolean res = false;
    if (!query.hasSchemaError()) {
        ResultSet results;
        try {
            statement = this.createStatement();
            results = statement.executeQuery(query.toString());
            if (results.next()) {
                res = true;
            }
            results.close();
        } catch (SQLException e) {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException sqlEx) {
                }
            }
            throw new AtomSetException("Error during check contains atom: " + atom, e);
        }
    }
    return res;
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) SQLQuery(fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery)

Example 23 with AtomSetException

use of fr.lirmm.graphik.graal.api.core.AtomSetException 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 24 with AtomSetException

use of fr.lirmm.graphik.graal.api.core.AtomSetException 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 25 with AtomSetException

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

the class FullyInstantiatedQueryHomomorphism method execute.

@Override
public CloseableIterator<Substitution> execute(ConjunctiveQuery q, AtomSet a) throws HomomorphismException {
    try {
        CloseableIteratorWithoutException<Atom> it = q.getAtomSet().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)

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