Search in sources :

Example 56 with Statement

use of org.eclipse.rdf4j.model.Statement 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 57 with Statement

use of org.eclipse.rdf4j.model.Statement 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 58 with Statement

use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.

the class AbstractRepositoryConnection method add.

@Override
public void add(Iterable<? extends Statement> statements, Resource... contexts) throws RepositoryException {
    OpenRDFUtil.verifyContextNotNull(contexts);
    boolean localTransaction = startLocalTransaction();
    try {
        for (Statement st : statements) {
            addWithoutCommit(st, contexts);
        }
        conditionalCommit(localTransaction);
    } catch (RepositoryException e) {
        conditionalRollback(localTransaction);
        throw e;
    } catch (RuntimeException e) {
        conditionalRollback(localTransaction);
        throw e;
    }
}
Also used : Statement(org.eclipse.rdf4j.model.Statement) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException)

Example 59 with Statement

use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.

the class ContextStatementTest method test.

@Test
public void test() {
    Statement st1 = vf.createStatement(s1, p1, o1);
    Statement st2 = vf.createStatement(s1, p1, o1, g1);
    Statement st3 = vf.createStatement(s1, p1, o2);
    Statement st4 = vf.createStatement(s1, p1, o1, g1);
    Statement st5 = vf.createStatement(s1, p1, o1, g2);
    assertNotEquals(st1, st2);
    assertNotEquals(st1, st3);
    assertEquals(st2, st4);
    assertNotEquals(st2, st5);
    Set<Statement> set = new HashSet<Statement>();
    set.add(st1);
    set.add(st2);
    set.add(st3);
    set.add(st4);
    set.add(st5);
    assertEquals(4, set.size());
}
Also used : Statement(org.eclipse.rdf4j.model.Statement) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 60 with Statement

use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.

the class DAWGTestResultSetUtil method toTupleQueryResult.

public static TupleQueryResult toTupleQueryResult(Iterable<? extends Statement> dawgGraph) throws DAWGTestResultSetParseException {
    TupleQueryResultBuilder tqrBuilder = new TupleQueryResultBuilder();
    DAWGTestResultSetParser parser = new DAWGTestResultSetParser(tqrBuilder);
    try {
        parser.startRDF();
        for (Statement st : dawgGraph) {
            parser.handleStatement(st);
        }
        parser.endRDF();
        return tqrBuilder.getQueryResult();
    } catch (RDFHandlerException e) {
        throw new DAWGTestResultSetParseException(e.getMessage(), e);
    }
}
Also used : TupleQueryResultBuilder(org.eclipse.rdf4j.query.impl.TupleQueryResultBuilder) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) Statement(org.eclipse.rdf4j.model.Statement)

Aggregations

Statement (org.eclipse.rdf4j.model.Statement)74 IRI (org.eclipse.rdf4j.model.IRI)17 Test (org.junit.Test)17 Model (org.eclipse.rdf4j.model.Model)16 Literal (org.eclipse.rdf4j.model.Literal)15 Resource (org.eclipse.rdf4j.model.Resource)15 Value (org.eclipse.rdf4j.model.Value)13 RDFWriter (org.eclipse.rdf4j.rio.RDFWriter)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ArrayList (java.util.ArrayList)10 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)8 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)8 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)8 StringWriter (java.io.StringWriter)7 BNode (org.eclipse.rdf4j.model.BNode)6 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)6 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)6 RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)6 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)6 IOException (java.io.IOException)5