Package org.openrdf.sail

Examples of org.openrdf.sail.SailConnection


      // Forcefully close any connections that are still open
      Iterator<Map.Entry<SailConnection, Throwable>> iter = activeConnections.entrySet().iterator();
      while (iter.hasNext()) {
        Map.Entry<SailConnection, Throwable> entry = iter.next();
        SailConnection con = entry.getKey();
        Throwable stackTrace = entry.getValue();

        iter.remove();

        if (stackTrace == null) {
          logger.warn(
              "Closing active connection due to shut down; consider setting the {} system property",
              DEBUG_PROP);
        }
        else {
          logger.warn("Closing active connection due to shut down, connection was acquired in",
              stackTrace);
        }

        try {
          con.close();
        }
        catch (SailException e) {
          logger.error("Failed to close connection", e);
        }
      }
View Full Code Here


   * @throws SailException
   * @throws RepositoryException
   */
  public void addInferredForResource(Resource base) throws SailException, RepositoryException {
    /* Find alias resources */
    SailConnection userConn = this.getConnection();
    CloseableIteration<? extends Statement, SailException> iterA = userConn.getStatements(base, OWL.SAMEAS, null, false, contexts);
      CloseableIteration<? extends Statement, SailException> iterB = userConn.getStatements(null, OWL.SAMEAS, base, false, contexts);

    while (iterA.hasNext() || iterB.hasNext()) {
      boolean isA = iterA.hasNext();
      Statement st = isA ? iterA.next() : iterB.next();
      Value obj = isA ? st.getObject() : st.getSubject();
       
      if (obj instanceof Resource) {
        processSameAs((InferencerConnection) userConn, base, (Resource) obj);
      }
    }
    userConn.commit();
    userConn.close();
  }
View Full Code Here

                // load cache entry from database
                CacheEntry ce = dbcon.getCacheEntry(resource.stringValue());

                // if entry exists, load triples for the resource from the cache context of the repository
                if(ce != null) {
                    SailConnection con = store.getConnection();
                    try {
                        con.begin();

                        Model triples = new TreeModel();
                        ModelCommons.add(triples,con.getStatements(resource,null,null,true,store.getValueFactory().createURI(cacheContext)));
                        ce.setTriples(triples);

                        con.commit();
                    } catch(SailException ex) {
                        con.rollback();
                    } finally {
                        con.close();
                    }
                }
                return ce;

            }
View Full Code Here

                // store cache entry in database
                dbcon.removeCacheEntry(resource.stringValue());

                // update triples in cache
                SailConnection con = store.getConnection();
                try {
                    con.begin();

                    con.removeStatements(resource, null, null, store.getValueFactory().createURI(cacheContext));
                    for(Statement stmt : entry.getTriples()) {
                        con.addStatement(stmt.getSubject(), stmt.getPredicate(), stmt.getObject(), store.getValueFactory().createURI(cacheContext));
                    }

                    con.commit();

                    entry.setResource(store.getValueFactory().createURI(resource.stringValue()));

                    dbcon.storeCacheEntry(entry);
                } catch(SailException ex) {
                    con.rollback();
                } finally {
                    con.close();
                }

            }

        } catch (SailException | SQLException e) {
View Full Code Here

                // store cache entry in database
                dbcon.removeCacheEntry(resource.stringValue());

                // update triples in cache
                SailConnection con = store.getConnection();
                try {
                    con.begin();

                    con.removeStatements(resource, null, null, store.getValueFactory().createURI(cacheContext));

                    con.commit();
                } catch(SailException ex) {
                    con.rollback();
                } finally {
                    con.close();
                }

            }

        } catch (SailException | SQLException e) {
View Full Code Here

                while (entries.hasNext()) {
                    dbcon.removeCacheEntry(entries.next());
                }

                // update triples in cache
                SailConnection con = store.getConnection();
                try {
                    con.begin();

                    con.removeStatements((Resource) null, null, null, store.getValueFactory().createURI(cacheContext));

                    con.commit();
                } catch(SailException ex) {
                    con.rollback();
                } finally {
                    con.close();
                }

            }

        } catch (SailException | SQLException e) {
View Full Code Here

        CloseableIteration<KiWiTriple,SQLException> tripleIterator = connection.listUnsupportedTriples();
        try {
            if(tripleIterator.hasNext()) {

                updateTaskStatus("deleting unsupported triples");
                SailConnection tc = store.getConnection();
                KiWiSailConnection ic = getWrappedConnection(tc);
                try {
                    tc.begin();
                    while(tripleIterator.hasNext()) {
                        ic.removeInferredStatement(tripleIterator.next());
                        count++;
                    }
                    log.debug("removed {} unsupported triples",count);
                    tc.commit();
                } catch(SailException ex) {
                    ic.rollback();
                    throw ex;
                } finally {
                    ic.close();
View Full Code Here

        }

        CloseableIteration<QueryResult, SQLException> bodyResult;

        KiWiReasoningConnection connection = persistence.getConnection();
        SailConnection     sail = store.getConnection();
        KiWiSailConnection isail = getWrappedConnection(sail);
        try {

            // if there are further patterns, evaluate them; if the matched pattern was the only pattern, then
            // simply take the match as binding
            if(body.size() > 0) {
                bodyResult = connection.query(body,match,null,null,true);
            } else if(match != null) {
                bodyResult = new SingletonIteration<QueryResult, SQLException>(match);
            } else {
                bodyResult = new EmptyIteration<QueryResult, SQLException>();
            }

            // construct triples out of the bindings and the rule heads
            long counter = 0;

            // initialise a new set of justifications
            Set<Justification> justifications = new HashSet<Justification>();

            sail.begin();
            while(bodyResult.hasNext()) {
                QueryResult row = bodyResult.next();
                Map<VariableField,KiWiNode> binding = row.getBindings();

                Resource subject = null;
                URI property = null;
                Value object;

                if(rule.getHead().getSubject() != null && rule.getHead().getSubject().isVariableField()) {
                    if(!binding.get(rule.getHead().getSubject()).isUriResource() && !binding.get(rule.getHead().getSubject()).isAnonymousResource()) {
                        log.info("cannot use value {} as subject, because it is not a resource",binding.get(rule.getHead().getSubject()));
                        continue;
                    }
                    subject = (KiWiResource)binding.get(rule.getHead().getSubject());
                } else if(rule.getHead().getSubject() != null && rule.getHead().getSubject().isResourceField()) {
                    subject = ((ResourceField)rule.getHead().getSubject()).getResource();
                } else
                    throw new IllegalArgumentException("Subject of rule head may only be a variable or a resource; rule: "+rule);

                if(rule.getHead().getProperty() != null && rule.getHead().getProperty().isVariableField()) {
                    if(!binding.get(rule.getHead().getProperty()).isUriResource()) {
                        log.info("cannot use value {} as property, because it is not a URI resource",binding.get(rule.getHead().getProperty()));
                        continue;
                    }
                    property = (KiWiUriResource)binding.get(rule.getHead().getProperty());
                } else if(rule.getHead().getProperty() != null && rule.getHead().getProperty().isResourceField()) {
                    property = (KiWiUriResource)((ResourceField)rule.getHead().getProperty()).getResource();
                } else
                    throw new IllegalArgumentException("Property of rule head may only be a variable or a resource; rule: "+rule);

                if(rule.getHead().getObject() != null && rule.getHead().getObject().isVariableField()) {
                    object = binding.get(rule.getHead().getObject());
                } else if(rule.getHead().getObject() != null && rule.getHead().getObject().isResourceField()) {
                    object = ((ResourceField)rule.getHead().getObject()).getResource();
                } else if(rule.getHead().getObject() != null && rule.getHead().getObject().isLiteralField()) {
                    object = ((LiteralField)rule.getHead().getObject()).getLiteral();
                } else
                    throw new IllegalArgumentException("Object of rule head may only be a variable, a literal, or a resource; rule: "+rule);


                KiWiTriple triple = isail.addInferredStatement(subject, property, object);

                Justification justification = new Justification();
                justification.setTriple(triple);
                justification.getSupportingRules().add(rule);
                justification.getSupportingTriples().addAll(row.getJustifications());
                justifications.add(justification);

                // when the batch size is reached, commit the transaction, save the justifications, and start a new
                // transaction and new justification set
                if(++counter % config.getBatchSize() == 0) {
                    persistenceLock.lock();

                    try {

                        sail.commit();

                        log.debug("adding {} justifications",justifications.size());

                        updateTaskStatus("storing justifications ...");
                        Set<Justification> baseJustifications = getBaseJustifications(connection,justifications);

                        if(config.isRemoveDuplicateJustifications()) {
                            removeDuplicateJustifications(connection,baseJustifications);
                        }

                        log.debug("{} justifications added after resolving inferred triples", baseJustifications.size());

                        // persist the justifications that have been created in the rule processing
                        if(baseJustifications.size() > 0) {
                            connection.storeJustifications(baseJustifications);
                        }
                        connection.commit();
                        sail.begin();
                    } finally {
                        persistenceLock.unlock();
                    }
                    justifications.clear();
                }
            }

            persistenceLock.lock();
            try {
                sail.commit();

                log.debug("adding {} justifications",justifications.size());
                updateTaskStatus("storing justifications ...");
                Set<Justification> baseJustifications = getBaseJustifications(connection,justifications);

                if(config.isRemoveDuplicateJustifications()) {
                    removeDuplicateJustifications(connection,baseJustifications);
                }

                // persist the justifications that have been created in the rule processing
                if(baseJustifications.size() > 0) {
                    connection.storeJustifications(baseJustifications);
                }

                log.debug("{} justifications added after resolving inferred triples", baseJustifications.size());

                Iterations.closeCloseable(bodyResult);
                connection.commit();
            } finally {
                persistenceLock.unlock();
            }
        } catch(SailException | SQLException | ReasoningException ex) {
            log.error("REASONING ERROR: could not process rule, database state will be inconsistent! Message: {}",ex.getMessage());
            log.debug("Exception details:",ex);

            connection.rollback();
            sail.rollback();
            throw ex;
        } finally {
            connection.close();
            sail.close();
        }

    }
View Full Code Here

     * connections until it finds the KiWiSailConnection, or otherwise throws a SailException.
     * @param connection
     * @return
     */
    private KiWiSailConnection getWrappedConnection(SailConnection connection) throws SailException {
        SailConnection it = connection;
        while(it instanceof SailConnectionWrapper) {
            it = ((SailConnectionWrapper) it).getWrappedConnection();
            if(it instanceof KiWiSailConnection) {
                return (KiWiSailConnection) it;
            }
View Full Code Here

     *
     * @param version    the version to revert
     * @throws SailException in case reverting the version failed
     */
    public void revertVersion(Version version) throws SailException {
        SailConnection con = getConnection();
        try {
            con.begin();

            for(Statement stmt : version.getAddedTriples()) {
                con.removeStatements(stmt.getSubject(), stmt.getPredicate(), stmt.getObject(), stmt.getContext());
            }

            for(Statement stmt : version.getRemovedTriples()) {
                con.addStatement(stmt.getSubject(), stmt.getPredicate(), stmt.getObject(), stmt.getContext());
            }

            con.commit();
        } finally {
            con.close();
        }
    }
View Full Code Here

TOP

Related Classes of org.openrdf.sail.SailConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.