Package org.openrdf.sail

Examples of org.openrdf.sail.SailConnection


     *
     * @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


    }


    @Test
    public void testGetStatements() throws SailException {
        final SailConnection con = cas.getConnection();
        try {
            con.begin();

            Assert.assertTrue(hasStatement(con, u1, p1, l1));
            Assert.assertFalse(hasStatement(con, u2, p2, l2));

            con.commit();
        } catch (final Throwable t) {
            con.rollback();
            throw t;
        } finally {
            con.close();
        }
    }
View Full Code Here

        }
    }

    @Test
    public void testAddStatement() throws SailException {
        final SailConnection con = cas.getConnection();
        try {
            con.begin();
            con.addStatement(u1, p2, l3);
            con.commit();
        } catch (final Throwable t) {
            con.rollback();
            throw t;
        } finally {
            con.close();
        }
        final SailConnection con2 = sail.getConnection();
        try {
            con2.begin();

            Assert.assertTrue(hasStatement(con2, u1, p2, l3, c1));
            Assert.assertFalse(hasStatement(con2, u1, p2, l3, c2));

            con2.commit();
        } catch (final Throwable t) {
            con2.rollback();
            throw t;
        } finally {
            con2.close();
        }
    }
View Full Code Here

        }
    }

    @Test
    public void testGetContextIDs() throws SailException {
        final SailConnection con = cas.getConnection();
        try {
            con.begin();

            final CloseableIteration<? extends Resource, SailException> cid = con.getContextIDs();
            try {
                assertTrue(cid.hasNext());
                assertThat(cid.next(), CoreMatchers.is(c1));
                assertFalse(cid.hasNext());
            } finally {
                cid.close();
            }

            con.commit();
        } catch (final Throwable t) {
            con.rollback();
            throw t;
        } finally {
            con.close();
        }
    }
View Full Code Here

        }
    }
   
    @Test
    public void testClear() throws SailException {
        final SailConnection con = cas.getConnection();
        try {
            con.begin();

            assertTrue(hasStatement(con, null, null, null));
            con.clear();
            assertFalse(hasStatement(con, null, null, null));

            con.commit();
        } catch (final Throwable t) {
            con.rollback();
            throw t;
        } finally {
            con.close();
        }
        final SailConnection con2 = sail.getConnection();
        try {
            con2.begin();

            assertFalse(hasStatement(con2, null, null, null, c1));
            assertTrue(hasStatement(con2, null, null, null, c2));

            con2.commit();
        } catch (final Throwable t) {
            con2.rollback();
            throw t;
        } finally {
            con2.close();
        }
    }
View Full Code Here

        }
    }
   
    @Test
    public void testRemoveStatements() throws SailException {
        final SailConnection con = cas.getConnection();
        try {
            con.begin();

            assertTrue(hasStatement(con, u1, p1, l1));
            assertTrue(hasStatement(con, u3, p3, l3));
           
            con.removeStatements(u1, p1, l1, c1);
            con.removeStatements(u2, p2, l2);
            con.removeStatements(u4, p4, l4, c1);
           
            assertFalse(hasStatement(con, u1, p1, l1));
            assertTrue(hasStatement(con, u3, p3, l3));

            con.commit();
        } catch (final Throwable t) {
            con.rollback();
            throw t;
        } finally {
            con.close();
        }
        final SailConnection con2 = sail.getConnection();
        try {
            con2.begin();

            assertFalse(hasStatement(con2, u1, p1, l1, c1));
            assertTrue(hasStatement(con2, u2, p2, l2, c2));
            assertTrue(hasStatement(con2, u3, p3, l3, c1));

            con2.commit();
        } catch (final Throwable t) {
            con2.rollback();
            throw t;
        } finally {
            con2.close();
        }
    }
View Full Code Here

        l1 = vf.createLiteral(UUID.randomUUID().toString());
        l2 = vf.createLiteral(UUID.randomUUID().toString());
        l3 = vf.createLiteral(UUID.randomUUID().toString());
        l4 = vf.createLiteral(UUID.randomUUID().toString());
       
        final SailConnection con = sail.getConnection();
        try {
            con.begin();
           
            con.addStatement(u1, p1, l1, c1);
            con.addStatement(u2, p2, l2, c2);
            con.addStatement(u3, p3, l3, c1);
            con.addStatement(u4, p4, l4, c2);
           
            con.commit();
        } catch (final Throwable t) {
            con.rollback();
            throw t;
        } finally {
            con.close();
        }
    }
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

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.