Search in sources :

Example 26 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, RulesCompilation rc) throws HomomorphismException {
    try {
        CloseableIteratorWithoutException<Atom> it = q.getAtomSet().iterator();
        boolean contains = true;
        while (contains && it.hasNext()) {
            Atom atom = it.next();
            boolean containsAtom = false;
            for (Pair<Atom, Substitution> im : rc.getRewritingOf(atom)) {
                containsAtom = a.contains(im.getLeft());
                if (containsAtom) {
                    break;
                }
            }
            contains = containsAtom;
        }
        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) 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 27 with AtomSetException

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

the class AtomicQueryHomomorphism method execute.

@Override
public CloseableIterator<Substitution> execute(ConjunctiveQuery q, AtomSet a, RulesCompilation rc) throws HomomorphismException {
    try {
        List<CloseableIterator<Substitution>> iteratorsList = new LinkedList<CloseableIterator<Substitution>>();
        Atom atom = q.getAtomSet().iterator().next();
        for (Pair<Atom, Substitution> im : rc.getRewritingOf(atom)) {
            iteratorsList.add(new ConverterCloseableIterator<Atom, Substitution>(a.match(im.getLeft()), new Atom2SubstitutionConverter(im.getLeft(), q.getAnswerVariables(), im.getRight())));
        }
        return new CloseableIteratorAggregator<Substitution>(new CloseableIteratorAdapter<CloseableIterator<Substitution>>(iteratorsList.iterator()));
    } catch (AtomSetException e) {
        throw new HomomorphismException(e);
    }
}
Also used : ConverterCloseableIterator(fr.lirmm.graphik.util.stream.converter.ConverterCloseableIterator) CloseableIterator(fr.lirmm.graphik.util.stream.CloseableIterator) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) CloseableIteratorAggregator(fr.lirmm.graphik.util.stream.CloseableIteratorAggregator) LinkedList(java.util.LinkedList) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 28 with AtomSetException

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

the class AdHocRdbmsStore method clear.

@Override
public void clear() throws AtomSetException {
    try {
        Statement stat = null;
        CloseableIterator<Predicate> it = this.predicatesIterator();
        try {
            stat = this.createStatement();
            while (it.hasNext()) {
                Predicate p = it.next();
                this.removePredicate(stat, p);
            }
            stat.execute(String.format(TRUNCATE_TABLE, PREDICATE_TABLE_NAME));
            stat.execute(String.format(TRUNCATE_TABLE, TERMS_TABLE));
            stat.close();
            this.getConnection().commit();
        } catch (IteratorException e) {
            this.getConnection().rollback();
            throw new AtomSetException(e);
        } catch (SQLException e) {
            this.getConnection().rollback();
            throw new AtomSetException(e);
        } finally {
            if (stat != null)
                stat.close();
        }
    } catch (SQLException e) {
        throw new AtomSetException(e);
    }
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Predicate(fr.lirmm.graphik.graal.api.core.Predicate)

Example 29 with AtomSetException

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

the class AdHocRdbmsStore method getPredicateTableIfExist.

@Override
protected DBTable getPredicateTableIfExist(Predicate predicate) throws AtomSetException {
    DBTable table = null;
    try {
        this.getPredicateTableStatement.setString(1, predicate.getIdentifier().toString());
        this.getPredicateTableStatement.setInt(2, predicate.getArity());
        ResultSet results = this.getPredicateTableStatement.executeQuery();
        if (results.next()) {
            String predicateTableName = results.getString("predicate_table_name");
            table = this.getDriver().getTable(predicateTableName);
        }
        results.close();
    } catch (SQLException e) {
        throw new AtomSetException(e);
    }
    return table;
}
Also used : DBTable(fr.lirmm.graphik.graal.store.rdbms.util.DBTable) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException)

Example 30 with AtomSetException

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

the class AdHocRdbmsStore method add.

// /////////////////////////////////////////////////////////////////////////
// PROTECTED METHODS
// /////////////////////////////////////////////////////////////////////////
/**
 * @param statement
 * @param atom
 * @throws AtomSetException
 */
@Override
protected Statement add(Statement statement, Atom atom) throws AtomSetException {
    if (!this.check(atom)) {
        // FIXME say why
        throw new UnsupportedAtomTypeException("");
    }
    try {
        for (Term t : atom.getTerms()) {
            if (this.getTerm(t.getLabel()) == null) {
                // FIXME Quick fix for
                // VARIABLE and
                // CONSTANT with same
                // label conflict
                this.add(statement, t);
            }
        }
        DBTable table = this.createPredicateTableIfNotExist(atom.getPredicate());
        Map<String, String> data = new TreeMap<String, String>();
        int i = -1;
        for (Term t : atom.getTerms()) {
            ++i;
            data.put("term" + i, '\'' + StringUtils.addSlashes(t.getIdentifier().toString()) + '\'');
        }
        String query = this.getDriver().getInsertOrIgnoreQuery(table, data);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(atom.toString() + " : " + query.toString());
        }
        statement.addBatch(query);
    } catch (SQLException e) {
        throw new AtomSetException(e);
    }
    return statement;
}
Also used : UnsupportedAtomTypeException(fr.lirmm.graphik.graal.api.core.UnsupportedAtomTypeException) DBTable(fr.lirmm.graphik.graal.store.rdbms.util.DBTable) SQLException(java.sql.SQLException) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Term(fr.lirmm.graphik.graal.api.core.Term) TreeMap(java.util.TreeMap)

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