Search in sources :

Example 31 with RepositoryConnection

use of org.eclipse.rdf4j.repository.RepositoryConnection in project rdf4j by eclipse.

the class RepositoryConfigUtil method getRepositoryConfig.

@Deprecated
public static RepositoryConfig getRepositoryConfig(Repository repository, String repositoryID) throws RepositoryConfigException, RepositoryException {
    RepositoryConnection con = repository.getConnection();
    try {
        Statement idStatement = getIDStatement(con, repositoryID);
        if (idStatement == null) {
            // No such config
            return null;
        }
        Resource repositoryNode = idStatement.getSubject();
        Resource context = idStatement.getContext();
        if (context == null) {
            throw new RepositoryException("No configuration context for repository " + repositoryID);
        }
        Model contextGraph = QueryResults.asModel(con.getStatements(null, null, null, true, context));
        return RepositoryConfig.create(contextGraph, repositoryNode);
    } finally {
        con.close();
    }
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) Statement(org.eclipse.rdf4j.model.Statement) Resource(org.eclipse.rdf4j.model.Resource) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) Model(org.eclipse.rdf4j.model.Model) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException)

Example 32 with RepositoryConnection

use of org.eclipse.rdf4j.repository.RepositoryConnection in project rdf4j by eclipse.

the class RepositoryUtil method equals.

/**
 * Compares the models in the default contexts of the two supplied repositories and returns true if they
 * are equal. Models are equal if they contain the same set of statements. bNodes IDs are not relevant for
 * model equality, they are mapped from one model to the other by using the attached properties. Note that
 * the method pulls the entire default context of both repositories into main memory. Use with caution.
 */
public static boolean equals(Repository rep1, Repository rep2) throws RepositoryException {
    // Fetch statements from rep1 and rep2
    Set<Statement> model1, model2;
    RepositoryConnection con1 = rep1.getConnection();
    try {
        model1 = Iterations.asSet(con1.getStatements(null, null, null, true));
    } finally {
        con1.close();
    }
    RepositoryConnection con2 = rep2.getConnection();
    try {
        model2 = Iterations.asSet(con2.getStatements(null, null, null, true));
    } finally {
        con2.close();
    }
    return Models.isomorphic(model1, model2);
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) Statement(org.eclipse.rdf4j.model.Statement)

Example 33 with RepositoryConnection

use of org.eclipse.rdf4j.repository.RepositoryConnection in project rdf4j by eclipse.

the class Connections method consumeRDFCollection.

/**
 * Retrieve all {@link Statement}s that together form the RDF Collection starting with the supplied start
 * resource and send them to the supplied {@link Consumer}.
 *
 * @param conn
 *        the {@link RepositoryConnection} to use for statement retrieval.
 * @param head
 *        the start resource of the RDF Collection. May not be {@code null}.
 * @param collectionConsumer
 *        a {@link Consumer} function to which all retrieved statements will be reported. May not be
 *        {@code null}.
 * @param contexts
 *        the context(s) from which to read the RDF Collection. This argument is an optional vararg and
 *        can be left out.
 * @throws RepositoryException
 *         if an error occurred while reading the collection statements, for example if a cycle is
 *         detected in the RDF collection, or some other anomaly which makes it non-wellformed.
 * @see RDFCollections
 * @see <a href="http://www.w3.org/TR/rdf-schema/#ch_collectionvocab">RDF Schema 1.1 section on Collection
 *      vocabulary</a>.
 */
public static void consumeRDFCollection(RepositoryConnection conn, Resource head, Consumer<Statement> collectionConsumer, Resource... contexts) throws RepositoryException {
    GetStatementOptional statementSupplier = (s, p, o, c) -> getStatement(conn, s, p, o, c);
    Function<String, Supplier<RepositoryException>> exceptionSupplier = Repositories::repositoryException;
    RDFCollections.extract(statementSupplier, head, collectionConsumer, exceptionSupplier, contexts);
}
Also used : Resource(org.eclipse.rdf4j.model.Resource) Collection(java.util.Collection) RepositoryResult(org.eclipse.rdf4j.repository.RepositoryResult) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Model(org.eclipse.rdf4j.model.Model) Value(org.eclipse.rdf4j.model.Value) Objects(java.util.Objects) Consumer(java.util.function.Consumer) RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) RDFCollections(org.eclipse.rdf4j.model.util.RDFCollections) Statement(org.eclipse.rdf4j.model.Statement) GetStatementOptional(org.eclipse.rdf4j.model.util.GetStatementOptional) Optional(java.util.Optional) IRI(org.eclipse.rdf4j.model.IRI) GetStatementOptional(org.eclipse.rdf4j.model.util.GetStatementOptional) Supplier(java.util.function.Supplier)

Example 34 with RepositoryConnection

use of org.eclipse.rdf4j.repository.RepositoryConnection in project rdf4j by eclipse.

the class LocalRepositoryManagerTest method testRestartManagerWithoutTransaction.

@Test
public void testRestartManagerWithoutTransaction() throws Exception {
    Repository rep = manager.getRepository(TEST_REPO);
    assertNotNull("Expected repository to exist.", rep);
    assertTrue("Expected repository to be initialized.", rep.isInitialized());
    RepositoryConnection conn = rep.getConnection();
    try {
        conn.add(conn.getValueFactory().createIRI("urn:sesame:test:subject"), RDF.TYPE, OWL.ONTOLOGY);
        assertEquals(1, conn.size());
    } finally {
        conn.close();
        rep.shutDown();
        manager.shutDown();
    }
    manager = new LocalRepositoryManager(datadir);
    manager.initialize();
    Repository rep2 = manager.getRepository(TEST_REPO);
    assertNotNull("Expected repository to exist.", rep2);
    assertTrue("Expected repository to be initialized.", rep2.isInitialized());
    RepositoryConnection conn2 = rep2.getConnection();
    try {
        assertEquals(1, conn2.size());
    } finally {
        conn2.close();
        rep2.shutDown();
        manager.shutDown();
    }
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) Repository(org.eclipse.rdf4j.repository.Repository) Test(org.junit.Test)

Aggregations

RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)34 Test (org.junit.Test)21 Repository (org.eclipse.rdf4j.repository.Repository)18 IRI (org.eclipse.rdf4j.model.IRI)6 Resource (org.eclipse.rdf4j.model.Resource)6 Statement (org.eclipse.rdf4j.model.Statement)6 Model (org.eclipse.rdf4j.model.Model)5 QueryLanguage (org.eclipse.rdf4j.query.QueryLanguage)5 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)4 RepositoryConfig (org.eclipse.rdf4j.repository.config.RepositoryConfig)4 Value (org.eclipse.rdf4j.model.Value)3 TreeModel (org.eclipse.rdf4j.model.impl.TreeModel)3 Dataset (org.eclipse.rdf4j.query.Dataset)3 ProxyRepositoryConfig (org.eclipse.rdf4j.repository.sail.config.ProxyRepositoryConfig)3 SailRepositoryConfig (org.eclipse.rdf4j.repository.sail.config.SailRepositoryConfig)3 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)2 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)2 Update (org.eclipse.rdf4j.query.Update)2 AbstractUpdate (org.eclipse.rdf4j.query.impl.AbstractUpdate)2 InterceptingRepositoryConnectionWrapper (org.eclipse.rdf4j.repository.event.base.InterceptingRepositoryConnectionWrapper)2