Search in sources :

Example 6 with Statement

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

the class ArrangedWriter method getUsedNamespaces.

private void getUsedNamespaces(Set<Statement> stmts, Set<String> used) {
    for (Statement st : stmts) {
        used.add(st.getPredicate().getNamespace());
        if (st.getObject() instanceof IRI) {
            IRI uri = (IRI) st.getObject();
            used.add(uri.getNamespace());
        } else if (st.getObject() instanceof Literal) {
            Literal lit = (Literal) st.getObject();
            if (lit.getDatatype() != null) {
                used.add(lit.getDatatype().getNamespace());
            }
        }
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Statement(org.eclipse.rdf4j.model.Statement) Literal(org.eclipse.rdf4j.model.Literal)

Example 7 with Statement

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

the class ArrangedWriter method flushStatements.

private synchronized void flushStatements() throws RDFHandlerException {
    if (!stmtBySubject.isEmpty() || !blanks.isEmpty()) {
        flushNamespaces();
        Statement st;
        while ((st = nextStatement()) != null) {
            delegate.handleStatement(st);
        }
        assert queueSize == 0;
    }
}
Also used : Statement(org.eclipse.rdf4j.model.Statement)

Example 8 with Statement

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

the class ArrangedWriter method queueBlankStatements.

private synchronized Set<Statement> queueBlankStatements(SubjectInContext key) {
    Model firstMatch = blanks.filter(key.getSubject(), null, null, key.getContext());
    Model matches = firstMatch.isEmpty() ? blankReferences.filter(key.getSubject(), null, null, key.getContext()) : firstMatch;
    if (matches.isEmpty()) {
        return null;
    }
    Set<Statement> set = stmtBySubject.get(key);
    if (set == null) {
        stmtBySubject.put(key, set = new TreeSet<Statement>(comparator));
    }
    set.addAll(matches);
    if (firstMatch.isEmpty()) {
        // repeat blank node values
        queueSize += matches.size();
    } else {
        if (repeatBlankNodes && key.getSubject() instanceof BNode && isStillReferenced(key)) {
            blankReferences.addAll(matches);
        }
        blanks.remove(key.getSubject(), null, null, key.getContext());
    }
    return set;
}
Also used : BNode(org.eclipse.rdf4j.model.BNode) Statement(org.eclipse.rdf4j.model.Statement) TreeSet(java.util.TreeSet) Model(org.eclipse.rdf4j.model.Model) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel)

Example 9 with Statement

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

the class TestTurtleParser method rdfXmlLoadedFromInsideAJarResolvesRelativeUris.

@Test
public void rdfXmlLoadedFromInsideAJarResolvesRelativeUris() throws Exception {
    URL zipfileUrl = TestTurtleParser.class.getResource("sample-with-turtle-data.zip");
    assertNotNull("The sample-with-turtle-data.zip file must be present for this test", zipfileUrl);
    String url = "jar:" + zipfileUrl + "!/index.ttl";
    RDFParser parser = new TurtleParser();
    StatementCollector sc = new StatementCollector();
    parser.setRDFHandler(sc);
    InputStream in = new URL(url).openStream();
    parser.parse(in, url);
    in.close();
    Collection<Statement> stmts = sc.getStatements();
    assertThat(stmts, Matchers.<Statement>iterableWithSize(2));
    Iterator<Statement> iter = stmts.iterator();
    Statement stmt1 = iter.next(), stmt2 = iter.next();
    assertEquals(vf.createIRI("http://www.example.com/#"), stmt1.getSubject());
    assertEquals(vf.createIRI("http://www.example.com/ns/#document-about"), stmt1.getPredicate());
    Resource res = (Resource) stmt1.getObject();
    String resourceUrl = res.stringValue();
    assertThat(resourceUrl, CoreMatchers.startsWith("jar:" + zipfileUrl + "!"));
    URL javaUrl = new URL(resourceUrl);
    assertEquals("jar", javaUrl.getProtocol());
    InputStream uc = javaUrl.openStream();
    assertEquals("The resource stream should be empty", -1, uc.read());
    uc.close();
    assertEquals(res, stmt2.getSubject());
    assertEquals(DC.TITLE, stmt2.getPredicate());
    assertEquals(vf.createLiteral("Empty File"), stmt2.getObject());
}
Also used : StatementCollector(org.eclipse.rdf4j.rio.helpers.StatementCollector) InputStream(java.io.InputStream) Statement(org.eclipse.rdf4j.model.Statement) Resource(org.eclipse.rdf4j.model.Resource) RDFParser(org.eclipse.rdf4j.rio.RDFParser) URL(java.net.URL) Test(org.junit.Test)

Example 10 with Statement

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

the class QueryResults method report.

/**
 * Reports a graph query result to an {@link RDFHandler}. <br>
 * The {@link GraphQueryResult#close()} method will always be called before this method returns.<br>
 * If there is an exception generated by the GraphQueryResult, {@link RDFHandler#endRDF()} will not be
 * called.
 *
 * @param gqr
 *        The query result to report.
 * @param rdfHandler
 *        The handler to report the query result to.
 * @throws RDFHandlerException
 *         If such an exception is thrown by the used RDF writer.
 * @throws QueryEvaluationException
 */
public static void report(GraphQueryResult gqr, RDFHandler rdfHandler) throws RDFHandlerException, QueryEvaluationException {
    try {
        rdfHandler.startRDF();
        for (Map.Entry<String, String> entry : gqr.getNamespaces().entrySet()) {
            String prefix = entry.getKey();
            String namespace = entry.getValue();
            rdfHandler.handleNamespace(prefix, namespace);
        }
        while (gqr.hasNext()) {
            Statement st = gqr.next();
            rdfHandler.handleStatement(st);
        }
    } finally {
        gqr.close();
    }
    rdfHandler.endRDF();
}
Also used : Statement(org.eclipse.rdf4j.model.Statement) HashMap(java.util.HashMap) Map(java.util.Map)

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