Search in sources :

Example 36 with AtomSetException

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

the class AdHocRdbmsStore method atomsByPredicate.

@Override
public CloseableIterator<Atom> atomsByPredicate(Predicate p) throws AtomSetException {
    List<Term> terms = new LinkedList<Term>();
    for (int i = 0; i < p.getArity(); ++i) {
        terms.add(DefaultTermFactory.instance().createVariable("X" + i));
    }
    Atom atom = DefaultAtomFactory.instance().create(p, terms);
    ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(new LinkedListAtomSet(atom));
    SqlHomomorphism solver = SqlHomomorphism.instance();
    try {
        return new SubstitutionIterator2AtomIterator(atom, solver.execute(query, this));
    } catch (HomomorphismException e) {
        throw new AtomSetException(e);
    }
}
Also used : HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) SqlHomomorphism(fr.lirmm.graphik.graal.store.rdbms.homomorphism.SqlHomomorphism) Term(fr.lirmm.graphik.graal.api.core.Term) SubstitutionIterator2AtomIterator(fr.lirmm.graphik.graal.core.stream.SubstitutionIterator2AtomIterator) LinkedList(java.util.LinkedList) Atom(fr.lirmm.graphik.graal.api.core.Atom) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)

Example 37 with AtomSetException

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

the class AdHocRdbmsStore method testDatabaseSchema.

@Override
protected boolean testDatabaseSchema() throws AtomSetException {
    Statement statement = null;
    try {
        statement = this.createStatement();
        ResultSet rs = statement.executeQuery(TEST_SCHEMA_QUERY);
        rs.close();
    } catch (SQLException e) {
        return false;
    } catch (AtomSetException e) {
        throw new AtomSetException(e.getMessage(), e);
    } finally {
        if (statement != null) {
            try {
                statement.close();
                this.getConnection().rollback();
            } catch (SQLException e) {
                throw new AtomSetException(e);
            }
        }
    }
    return true;
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException)

Example 38 with AtomSetException

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

the class AbstractRdbmsConjunctiveQueryTranslator method translate.

@Override
public Iterator<SQLQuery> translate(Rule rangeRestrictedRule) throws AtomSetException {
    Collection<SQLQuery> queries = new LinkedList<SQLQuery>();
    InMemoryAtomSet body = rangeRestrictedRule.getBody();
    CloseableIterator<Atom> it = rangeRestrictedRule.getHead().iterator();
    try {
        while (it.hasNext()) {
            Atom headAtom = it.next();
            DBTable table = this.store.createPredicateTableIfNotExist(headAtom.getPredicate());
            List<Term> terms = headAtom.getTerms();
            ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(body, terms);
            SQLQuery selectQuery = this.translate(query);
            if (!selectQuery.hasSchemaError()) {
                queries.add(new SQLQuery(store.getDriver().getInsertOrIgnoreQuery(table, selectQuery.toString())));
            }
        }
    } catch (IteratorException e) {
        throw new AtomSetException(e);
    } catch (SQLException e) {
        throw new AtomSetException(e);
    }
    return queries.iterator();
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) DBTable(fr.lirmm.graphik.graal.store.rdbms.util.DBTable) SQLException(java.sql.SQLException) Term(fr.lirmm.graphik.graal.api.core.Term) SQLQuery(fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery) LinkedList(java.util.LinkedList) Atom(fr.lirmm.graphik.graal.api.core.Atom) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)

Example 39 with AtomSetException

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

the class AbstractRdbmsConjunctiveQueryTranslator method formatFromColumnType.

@Override
public String formatFromColumnType(DBColumn col, Term term) throws AtomSetException {
    switch(col.getType()) {
        case Types.BOOLEAN:
        case Types.INTEGER:
        case Types.BIGINT:
        case Types.SMALLINT:
        case Types.TINYINT:
        case Types.DECIMAL:
        case Types.DOUBLE:
            if (term instanceof Literal) {
                Literal l = (Literal) term;
                URI datatype = l.getDatatype();
                switch(col.getType()) {
                    case Types.BOOLEAN:
                        if (URIUtils.XSD_BOOLEAN.equals(l.getDatatype()) || l.getValue() instanceof Boolean) {
                            return term.getLabel();
                        }
                        throw new AtomSetException("No correspondance between the database field (Boolean) and the Term type (" + datatype.toString() + ")");
                    case Types.INTEGER:
                    case Types.BIGINT:
                    case Types.SMALLINT:
                    case Types.TINYINT:
                        if (URIUtils.XSD_INTEGER.equals(l.getDatatype()) || l.getValue() instanceof Integer || l.getValue() instanceof BigInteger) {
                            return term.getLabel();
                        }
                        throw new AtomSetException("No correspondance between the database field (Boolean) and the Term type (" + datatype.toString() + ")");
                    case Types.DECIMAL:
                        if (URIUtils.XSD_DECIMAL.equals(l.getDatatype()) || l.getValue() instanceof Float || l.getValue() instanceof BigDecimal) {
                            return term.getLabel();
                        }
                        throw new AtomSetException("No correspondance between the database field (Boolean) and the Term type (" + datatype.toString() + ")");
                    case Types.DOUBLE:
                        if (URIUtils.XSD_DOUBLE.equals(l.getDatatype()) || l.getValue() instanceof Double) {
                            return term.getLabel();
                        }
                        throw new AtomSetException("No correspondance between the database field (Boolean) and the Term type (" + datatype.toString() + ")");
                }
            } else {
                throw new AtomSetException("No correspondance between the database field (Boolean) and the Term type.");
            }
        case Types.VARCHAR:
        case Types.NVARCHAR:
        case Types.LONGVARCHAR:
        case Types.LONGNVARCHAR:
            return '\'' + term.getIdentifier().toString() + '\'';
        default:
            throw new AtomSetException("Column type not supported (see java.sql.Types): " + col.getType());
    }
}
Also used : BigInteger(java.math.BigInteger) Literal(fr.lirmm.graphik.graal.api.core.Literal) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) BigInteger(java.math.BigInteger) URI(fr.lirmm.graphik.util.URI) BigDecimal(java.math.BigDecimal)

Example 40 with AtomSetException

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

the class NaturalRDBMSStore method atomsByPredicate.

@Override
public CloseableIterator<Atom> atomsByPredicate(Predicate p) throws AtomSetException {
    if (!this.check(p)) {
        return Iterators.<Atom>emptyIterator();
    }
    List<Term> terms = new LinkedList<Term>();
    for (int i = 0; i < p.getArity(); ++i) {
        terms.add(DefaultTermFactory.instance().createVariable("X" + i));
    }
    Atom atom = DefaultAtomFactory.instance().create(p, terms);
    ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(new LinkedListAtomSet(atom));
    SqlHomomorphism solver = SqlHomomorphism.instance();
    try {
        return new SubstitutionIterator2AtomIterator(atom, solver.execute(query, this));
    } catch (HomomorphismException e) {
        throw new AtomSetException(e);
    }
}
Also used : HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) SqlHomomorphism(fr.lirmm.graphik.graal.store.rdbms.homomorphism.SqlHomomorphism) Term(fr.lirmm.graphik.graal.api.core.Term) SubstitutionIterator2AtomIterator(fr.lirmm.graphik.graal.core.stream.SubstitutionIterator2AtomIterator) Atom(fr.lirmm.graphik.graal.api.core.Atom) LinkedList(java.util.LinkedList) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)

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