Search in sources :

Example 41 with AtomSetException

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

the class NaturalRDBMSStore method termsIterator.

@Override
@Deprecated
public CloseableIterator<Term> termsIterator(Type type) throws AtomSetException {
    Set<Term> terms = new TreeSet<Term>();
    CloseableIterator<Predicate> predIt = this.predicatesIterator();
    try {
        while (predIt.hasNext()) {
            Predicate p = predIt.next();
            for (int i = 0; i < p.getArity(); ++i) {
                CloseableIterator<Term> termIt = this.termsByPredicatePosition(p, i);
                while (termIt.hasNext()) {
                    Term t = termIt.next();
                    if (type.equals(t.getType())) {
                        terms.add(t);
                    }
                }
            }
        }
    } catch (IteratorException e) {
        throw new AtomSetException(e);
    }
    return new CloseableIteratorAdapter<Term>(terms.iterator());
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) TreeSet(java.util.TreeSet) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Term(fr.lirmm.graphik.graal.api.core.Term) CloseableIteratorAdapter(fr.lirmm.graphik.util.stream.CloseableIteratorAdapter) Predicate(fr.lirmm.graphik.graal.api.core.Predicate)

Example 42 with AtomSetException

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

the class NaturalRDBMSStore method termsIterator.

@Override
public CloseableIterator<Term> termsIterator() throws AtomSetException {
    Set<Term> terms = new TreeSet<Term>();
    CloseableIterator<Predicate> predIt = this.predicatesIterator();
    try {
        while (predIt.hasNext()) {
            Predicate p = predIt.next();
            for (int i = 0; i < p.getArity(); ++i) {
                CloseableIterator<Term> termIt = this.termsByPredicatePosition(p, i);
                while (termIt.hasNext()) {
                    terms.add(termIt.next());
                }
            }
        }
    } catch (IteratorException e) {
        throw new AtomSetException(e);
    }
    return new CloseableIteratorAdapter<Term>(terms.iterator());
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) TreeSet(java.util.TreeSet) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Term(fr.lirmm.graphik.graal.api.core.Term) CloseableIteratorAdapter(fr.lirmm.graphik.util.stream.CloseableIteratorAdapter) Predicate(fr.lirmm.graphik.graal.api.core.Predicate)

Example 43 with AtomSetException

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

the class NaturalRDBMSStore method termsByPredicatePosition.

@Override
public CloseableIterator<Term> termsByPredicatePosition(Predicate p, int position) throws AtomSetException {
    if (!this.check(p)) {
        return Iterators.<Term>emptyIterator();
    }
    try {
        SQLQuery query = this.getConjunctiveQueryTranslator().translateTermsByPredicatePositionQuery(p, position);
        if (query.hasSchemaError()) {
            return Iterators.<Term>emptyIterator();
        } else {
            DBTable table = this.getPredicateTable(p);
            if (table == null) {
                throw new AtomSetException("No table for this predicate: " + p);
            }
            DBColumn col = table.getColumns().get(position);
            int sqlType = col.getType();
            return new NaturalResultSetTermIterator(this, query.toString(), sqlType);
        }
    } catch (SQLException e) {
        throw new AtomSetException(e);
    }
}
Also used : DBTable(fr.lirmm.graphik.graal.store.rdbms.util.DBTable) SQLException(java.sql.SQLException) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) DBColumn(fr.lirmm.graphik.graal.store.rdbms.util.DBColumn) Term(fr.lirmm.graphik.graal.api.core.Term) SQLQuery(fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery)

Example 44 with AtomSetException

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

the class ResultSet2SubstitutionConverter method convert.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public Substitution convert(ResultSet result) throws ConversionException {
    try {
        Substitution substitution = new TreeMapSubstitution();
        if (!ans.isEmpty()) {
            for (Term t : ans) {
                if (t.isVariable()) {
                    int i = result.findColumn(t.getLabel());
                    int type = result.getMetaData().getColumnType(i);
                    String value = result.getString(i);
                    Term substitut = this.queryTranslator.createTermFromColumnType(type, value);
                    substitution.put((Variable) t, substitut);
                }
            }
        }
        return substitution;
    } catch (SQLException e) {
        throw new ConversionException(e);
    } catch (AtomSetException e) {
        throw new ConversionException(e);
    }
}
Also used : ConversionException(fr.lirmm.graphik.util.stream.converter.ConversionException) TreeMapSubstitution(fr.lirmm.graphik.graal.core.TreeMapSubstitution) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) SQLException(java.sql.SQLException) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Term(fr.lirmm.graphik.graal.api.core.Term) TreeMapSubstitution(fr.lirmm.graphik.graal.core.TreeMapSubstitution)

Example 45 with AtomSetException

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

the class NaturalConjunctiveQueryTranslator method translate.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public SQLQuery translate(ConjunctiveQuery cquery) throws AtomSetException {
    if (cquery.getAtomSet().isEmpty()) {
        return SQLQuery.emptyInstance();
    }
    AtomSet atomSet = cquery.getAtomSet();
    StringBuilder fields = new StringBuilder();
    StringBuilder tables = new StringBuilder();
    StringBuilder where = new StringBuilder();
    HashMap<Atom, String> tableAsNames = new HashMap<Atom, String>();
    HashMap<Atom, String> tableNames = new HashMap<Atom, String>();
    HashMap<Atom, List<DBColumn>> tableColumns = new HashMap<Atom, List<DBColumn>>();
    HashMap<String, String> lastOccurrence = new HashMap<String, String>();
    ArrayList<String> constants = new ArrayList<String>();
    ArrayList<String> equivalences = new ArrayList<String>();
    TreeMap<Term, String> columns = new TreeMap<Term, String>();
    int count = -1;
    CloseableIterator<Atom> it = atomSet.iterator();
    try {
        while (it.hasNext()) {
            Atom atom = it.next();
            DBTable table = this.store.getPredicateTableIfExist(atom.getPredicate());
            if (table != null) {
                String tableName = "atom" + ++count;
                tableAsNames.put(atom, tableName);
                tableNames.put(atom, table.getName());
                tableColumns.put(atom, table.getColumns());
            } else {
                return SQLQuery.hasSchemaErrorInstance();
            }
        }
        // Create WHERE clause
        it = atomSet.iterator();
        while (it.hasNext()) {
            Atom atom = it.next();
            String currentAtom = tableAsNames.get(atom) + ".";
            List<DBColumn> columnsInfo = tableColumns.get(atom);
            int position = 0;
            for (Term term : atom.getTerms()) {
                String thisTerm = currentAtom + columnsInfo.get(position).getName();
                if (term.isConstant()) {
                    constants.add(thisTerm + " = '" + term.getIdentifier().toString() + "'");
                } else {
                    if (lastOccurrence.containsKey(term.getIdentifier().toString())) {
                        equivalences.add(lastOccurrence.get(term.getIdentifier().toString()) + " = " + thisTerm);
                    }
                    lastOccurrence.put(term.getIdentifier().toString(), thisTerm);
                    if (cquery.getAnswerVariables().contains(term))
                        columns.put(term, thisTerm + " as " + term.getIdentifier().toString());
                }
                ++position;
            }
        }
        for (String equivalence : equivalences) {
            if (where.length() != 0)
                where.append(" AND ");
            where.append(equivalence);
        }
        for (String constant : constants) {
            if (where.length() != 0)
                where.append(" AND ");
            where.append(constant);
        }
        // Create FROM clause
        for (Map.Entry<Atom, String> entries : tableAsNames.entrySet()) {
            if (tables.length() != 0)
                tables.append(", ");
            tables.append(tableNames.get(entries.getKey()));
            tables.append(" as ");
            tables.append(entries.getValue());
        }
        // Create SELECT clause
        for (Term t : cquery.getAnswerVariables()) {
            if (fields.length() != 0)
                fields.append(", ");
            if (t.isConstant()) {
                fields.append("'");
                fields.append(t.getIdentifier());
                fields.append("'");
            } else {
                fields.append(columns.get(t));
            }
        }
        StringBuilder query = new StringBuilder("SELECT DISTINCT ");
        if (fields.length() > 0)
            query.append(fields);
        else
            query.append("1");
        query.append(" FROM ");
        if (tables.length() > 0)
            query.append(tables);
        if (where.length() > 0)
            query.append(" WHERE ").append(where);
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("Generated SQL query :" + cquery + " --> " + query.toString());
        return new SQLQuery(query.toString());
    } catch (IteratorException e) {
        throw new AtomSetException(e);
    }
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) DBTable(fr.lirmm.graphik.graal.store.rdbms.util.DBTable) HashMap(java.util.HashMap) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) ArrayList(java.util.ArrayList) Term(fr.lirmm.graphik.graal.api.core.Term) TreeMap(java.util.TreeMap) SQLQuery(fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery) Atom(fr.lirmm.graphik.graal.api.core.Atom) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) ArrayList(java.util.ArrayList) List(java.util.List) DBColumn(fr.lirmm.graphik.graal.store.rdbms.util.DBColumn) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

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