Search in sources :

Example 11 with Statement

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

the class QueryResultsTest method testDistinctGraphQueryResults.

@Test
public void testDistinctGraphQueryResults() throws QueryEvaluationException {
    GraphQueryResult filtered = QueryResults.distinctResults(gqr);
    List<Statement> processed = new ArrayList<Statement>();
    while (filtered.hasNext()) {
        Statement result = filtered.next();
        assertFalse(processed.contains(result));
        processed.add(result);
    }
}
Also used : Statement(org.eclipse.rdf4j.model.Statement) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 12 with Statement

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

the class QueryResultsTest method testStreamGraphResult.

@Test
public void testStreamGraphResult() {
    List<Statement> aboutA = QueryResults.stream(gqr).filter(s -> s.getSubject().equals(a)).collect(Collectors.toList());
    assertFalse(aboutA.isEmpty());
    for (Statement st : aboutA) {
        assertTrue(st.getSubject().equals(a));
    }
}
Also used : Arrays(java.util.Arrays) Assert.assertNotNull(org.junit.Assert.assertNotNull) AbstractCloseableIteration(org.eclipse.rdf4j.common.iteration.AbstractCloseableIteration) Literal(org.eclipse.rdf4j.model.Literal) Assert.assertTrue(org.junit.Assert.assertTrue) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) ListBindingSet(org.eclipse.rdf4j.query.impl.ListBindingSet) SimpleValueFactory(org.eclipse.rdf4j.model.impl.SimpleValueFactory) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Model(org.eclipse.rdf4j.model.Model) ArrayList(java.util.ArrayList) List(java.util.List) MutableTupleQueryResult(org.eclipse.rdf4j.query.impl.MutableTupleQueryResult) EmptyBindingSet(org.eclipse.rdf4j.query.impl.EmptyBindingSet) RDF(org.eclipse.rdf4j.model.vocabulary.RDF) Assert.assertFalse(org.junit.Assert.assertFalse) Map(java.util.Map) Statement(org.eclipse.rdf4j.model.Statement) IRI(org.eclipse.rdf4j.model.IRI) BNode(org.eclipse.rdf4j.model.BNode) Before(org.junit.Before) Statement(org.eclipse.rdf4j.model.Statement) Test(org.junit.Test)

Example 13 with Statement

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

the class RepositoryUtil method difference.

/**
 * Compares two models, defined by two statement collections, and returns the difference between the first
 * and the second model (that is, all statements that are present in model1 but not in model2). Blank node
 * IDs are not relevant for model equality, they are mapped from one model to the other by using the
 * attached properties. *
 * <p>
 * <b>NOTE: this algorithm is currently broken; it doesn't actually map blank nodes between the two
 * models.</b>
 *
 * @return The collection of statements that is the difference between model1 and model2.
 */
public static Collection<? extends Statement> difference(Collection<? extends Statement> model1, Collection<? extends Statement> model2) {
    // Create working copies
    LinkedList<Statement> copy1 = new LinkedList<Statement>(model1);
    LinkedList<Statement> copy2 = new LinkedList<Statement>(model2);
    Collection<Statement> result = new ArrayList<Statement>();
    // Compare statements that don't contain bNodes
    Iterator<Statement> iter1 = copy1.iterator();
    while (iter1.hasNext()) {
        Statement st = iter1.next();
        if (st.getSubject() instanceof BNode || st.getObject() instanceof BNode) {
            // these statements are handled later
            continue;
        }
        // Try to remove the statement from model2
        boolean removed = copy2.remove(st);
        if (!removed) {
            // statement was not present in model2 and is part of the difference
            result.add(st);
        }
        iter1.remove();
    }
    // FIXME: this algorithm is broken: bNodeMapping is assumed to contain a
    // bnode mapping while in reallity it is an empty map
    HashMap<BNode, BNode> bNodeMapping = new HashMap<BNode, BNode>();
    for (Statement st1 : copy1) {
        boolean foundMatch = false;
        for (Statement st2 : copy2) {
            if (statementsMatch(st1, st2, bNodeMapping)) {
                // Found a matching statement
                foundMatch = true;
                break;
            }
        }
        if (!foundMatch) {
            // No statement matching st1 was found in model2, st1 is part of
            // the difference.
            result.add(st1);
        }
    }
    return result;
}
Also used : BNode(org.eclipse.rdf4j.model.BNode) HashMap(java.util.HashMap) Statement(org.eclipse.rdf4j.model.Statement) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 14 with Statement

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

the class RepositoryUtil method difference.

/**
 * Compares two models defined by the default context of two repositories and returns the difference
 * between the first and the second model (that is, all statements that are present in rep1 but not in
 * rep2). Blank node 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.
 * <p>
 * <b>NOTE: this algorithm is currently broken; it doesn't actually map blank nodes between the two
 * models.</b>
 *
 * @return The collection of statements that is the difference between rep1 and rep2.
 */
public static Collection<? extends Statement> difference(Repository rep1, Repository rep2) throws RepositoryException {
    Collection<Statement> model1 = new HashSet<Statement>();
    Collection<Statement> model2 = new HashSet<Statement>();
    RepositoryConnection con1 = rep1.getConnection();
    try {
        Iterations.addAll(con1.getStatements(null, null, null, false), model1);
    } finally {
        con1.close();
    }
    RepositoryConnection con2 = rep2.getConnection();
    try {
        Iterations.addAll(con2.getStatements(null, null, null, false), model2);
    } finally {
        con2.close();
    }
    return difference(model1, model2);
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) Statement(org.eclipse.rdf4j.model.Statement) HashSet(java.util.HashSet)

Example 15 with Statement

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

the class RepositoryUtil method isSubset.

/**
 * Compares the models of the default context of two repositories and returns true if rep1 is a subset of
 * rep2. Note that the method pulls the entire default context of both repositories into main memory. Use
 * with caution.
 */
public static boolean isSubset(Repository rep1, Repository rep2) throws RepositoryException {
    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.isSubset(model1, model2);
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) 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