Search in sources :

Example 16 with IteratorException

use of fr.lirmm.graphik.util.stream.IteratorException in project graal by graphik-team.

the class AbstractHomomorphism method exist.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public boolean exist(T1 q, T2 a) throws HomomorphismException {
    CloseableIterator<Substitution> results = this.execute(q, a);
    boolean val;
    try {
        val = results.hasNext();
    } catch (IteratorException e) {
        throw new HomomorphismException(e);
    }
    results.close();
    return val;
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution)

Example 17 with IteratorException

use of fr.lirmm.graphik.util.stream.IteratorException 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 18 with IteratorException

use of fr.lirmm.graphik.util.stream.IteratorException in project graal by graphik-team.

the class AdHocConjunctiveQueryTranslator 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> tableNames = new HashMap<Atom, String>();
    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();
            String tableName = "atom" + ++count;
            tableNames.put(atom, tableName);
        }
        // Create WHERE clause
        it = atomSet.iterator();
        while (it.hasNext()) {
            Atom atom = it.next();
            String currentAtom = tableNames.get(atom) + ".";
            int position = 0;
            for (Term term : atom.getTerms()) {
                String thisTerm = currentAtom + AbstractRdbmsConjunctiveQueryTranslator.PREFIX_TERM_FIELD + position;
                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;
            }
        }
    } catch (IteratorException e) {
        throw new AtomSetException(e);
    }
    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
    DBTable table = null;
    for (Map.Entry<Atom, String> entries : tableNames.entrySet()) {
        if (tables.length() != 0)
            tables.append(", ");
        table = store.getPredicateTableIfExist(entries.getKey().getPredicate());
        if (table == null)
            return SQLQuery.hasSchemaErrorInstance();
        else
            tables.append(table.getName());
        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);
    else
        query.append(AdHocRdbmsStore.TEST_TABLE_NAME);
    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());
}
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) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 19 with IteratorException

use of fr.lirmm.graphik.util.stream.IteratorException 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 20 with IteratorException

use of fr.lirmm.graphik.util.stream.IteratorException 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)

Aggregations

IteratorException (fr.lirmm.graphik.util.stream.IteratorException)37 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)24 Atom (fr.lirmm.graphik.graal.api.core.Atom)16 Term (fr.lirmm.graphik.graal.api.core.Term)14 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)14 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)13 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)9 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)8 TreeSet (java.util.TreeSet)8 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)7 BacktrackException (fr.lirmm.graphik.graal.homomorphism.BacktrackException)6 CloseableIteratorAdapter (fr.lirmm.graphik.util.stream.CloseableIteratorAdapter)6 RuleApplicationException (fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException)5 Variable (fr.lirmm.graphik.graal.api.core.Variable)4 SQLException (java.sql.SQLException)4 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)3 Rule (fr.lirmm.graphik.graal.api.core.Rule)3 HomomorphismFactoryException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismFactoryException)3 DBTable (fr.lirmm.graphik.graal.store.rdbms.util.DBTable)3 SQLQuery (fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery)3