Search in sources :

Example 1 with BNode

use of org.eclipse.rdf4j.model.BNode in project opentheso by miledrousset.

the class ReadRdfFileTest method test3.

public static void test3() {
    // To create a blank node for the address, we need a ValueFactory
    ValueFactory vf = SimpleValueFactory.getInstance();
    BNode address = vf.createBNode();
    // Identically to example 03, we create a model with some data
    ModelBuilder builder = new ModelBuilder();
    builder.setNamespace("ex", "http://example.org/").subject("ex:Picasso").add(RDF.TYPE, "ex:Artist").add(FOAF.FIRST_NAME, "Pablo").add("ex:homeAddress", // link the blank node
    address).subject(// switch the subject
    address).add("ex:street", "31 Art Gallery").add("ex:city", "Madrid").add("ex:country", "Spain");
    Model model = builder.build();
    // Instead of simply printing the statements to the screen, we use a Rio writer to
    // write the model in RDF/XML syntax:
    Rio.write(model, System.out, RDFFormat.RDFXML);
    File file = new File("test.rdf");
    FileWriter writer = null;
    try {
        file.createNewFile();
        writer = new FileWriter(file);
    } catch (IOException ex) {
        Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
    }
    Rio.write(model, writer, RDFFormat.RDFXML);
}
Also used : ModelBuilder(org.eclipse.rdf4j.model.util.ModelBuilder) BNode(org.eclipse.rdf4j.model.BNode) FileWriter(java.io.FileWriter) Model(org.eclipse.rdf4j.model.Model) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) SimpleValueFactory(org.eclipse.rdf4j.model.impl.SimpleValueFactory) IOException(java.io.IOException) File(java.io.File)

Example 2 with BNode

use of org.eclipse.rdf4j.model.BNode in project opentheso by miledrousset.

the class WriteRdfFileTest method write3.

@Test
public void write3() {
    ValueFactory vf = SimpleValueFactory.getInstance();
    BNode address = vf.createBNode();
    // First we do the same thing we did in example 02: create a new ModelBuilder, and add
    // two statements about Picasso.
    ModelBuilder builder = new ModelBuilder();
    builder.setNamespace("ex", "http://example.org/").subject("ex:Picasso").add(RDF.TYPE, "ex:Artist").add(FOAF.FIRST_NAME, "Pablo").add("ex:homeAddress", // link the blank node
    address).subject(// switch the subject
    address).add("ex:street", "31 Art Gallery").add("ex:city", "Madrid").add("ex:country", "Spain");
    Model model = builder.build();
    // To see what's in our model, let's just print it to the screen
    for (Statement st : model) {
        System.out.println(st);
    }
    Rio.write(model, System.out, RDFFormat.RDFXML);
}
Also used : ModelBuilder(org.eclipse.rdf4j.model.util.ModelBuilder) BNode(org.eclipse.rdf4j.model.BNode) Statement(org.eclipse.rdf4j.model.Statement) Model(org.eclipse.rdf4j.model.Model) TreeModel(org.eclipse.rdf4j.model.impl.TreeModel) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) SimpleValueFactory(org.eclipse.rdf4j.model.impl.SimpleValueFactory) Test(org.junit.Test)

Example 3 with BNode

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

the class SPARQLJSONTupleTest method testBindings2.

@Test
public void testBindings2() throws Exception {
    SPARQLResultsJSONParser parser = new SPARQLResultsJSONParser(SimpleValueFactory.getInstance());
    QueryResultCollector handler = new QueryResultCollector();
    parser.setQueryResultHandler(handler);
    InputStream stream = this.getClass().getResourceAsStream("/sparqljson/bindings2.srj");
    assertNotNull("Could not find test resource", stream);
    parser.parseQueryResult(stream);
    // there must be 7 variables
    assertEquals(7, handler.getBindingNames().size());
    // first must be called "x", etc.,
    assertEquals("x", handler.getBindingNames().get(0));
    assertEquals("hpage", handler.getBindingNames().get(1));
    assertEquals("name", handler.getBindingNames().get(2));
    assertEquals("mbox", handler.getBindingNames().get(3));
    assertEquals("age", handler.getBindingNames().get(4));
    assertEquals("blurb", handler.getBindingNames().get(5));
    assertEquals("friend", handler.getBindingNames().get(6));
    // 2 results
    assertEquals(2, handler.getBindingSets().size());
    // Results are ordered, so first should be alice
    assertEquals("http://work.example.org/alice/", handler.getBindingSets().get(0).getValue("hpage").stringValue());
    for (BindingSet b : handler.getBindingSets()) {
        assertNotNull(b.getValue("x"));
        assertNotNull(b.getValue("hpage"));
        assertNotNull(b.getValue("name"));
        assertNotNull(b.getValue("mbox"));
        assertNotNull(b.getValue("friend"));
        assertTrue(b.getValue("x") instanceof BNode);
        assertTrue(b.getValue("hpage") instanceof IRI);
        assertTrue(b.getValue("name") instanceof Literal);
        assertTrue(b.getValue("friend") instanceof BNode);
        BNode value = (BNode) b.getValue("x");
        if (value.getID().equals("r1")) {
            assertNotNull(b.getValue("blurb"));
            assertTrue(b.getValue("mbox") instanceof Literal);
            assertTrue(b.getValue("blurb") instanceof Literal);
            assertEquals("http://work.example.org/alice/", b.getValue("hpage").stringValue());
            Literal name = (Literal) b.getValue("name");
            assertEquals("Alice", name.stringValue());
            assertFalse(name.getLanguage().isPresent());
            assertEquals(XMLSchema.STRING, name.getDatatype());
            Literal mbox = (Literal) b.getValue("mbox");
            assertEquals("", mbox.stringValue());
            assertFalse(mbox.getLanguage().isPresent());
            assertEquals(XMLSchema.STRING, mbox.getDatatype());
            Literal blurb = (Literal) b.getValue("blurb");
            assertEquals("<p xmlns=\"http://www.w3.org/1999/xhtml\">My name is <b>alice</b></p>", blurb.stringValue());
            assertFalse(blurb.getLanguage().isPresent());
            assertEquals(RDF.XMLLITERAL, blurb.getDatatype());
        } else if (value.getID().equals("r2")) {
            assertNull(b.getValue("blurb"));
            assertTrue(b.getValue("mbox") instanceof IRI);
            assertEquals("http://work.example.org/bob/", b.getValue("hpage").stringValue());
            Literal name = (Literal) b.getValue("name");
            assertEquals("Bob", name.stringValue());
            assertEquals("en", name.getLanguage().orElse(null));
            assertEquals(RDF.LANGSTRING, name.getDatatype());
            assertEquals("mailto:bob@work.example.org", b.getValue("mbox").stringValue());
        } else {
            fail("Found unexpected binding set in result: " + b.toString());
        }
    }
    assertEquals(1, handler.getLinks().size());
    assertEquals("http://www.w3.org/TR/2013/REC-sparql11-results-json-20130321/#example", handler.getLinks().get(0));
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) IRI(org.eclipse.rdf4j.model.IRI) SPARQLResultsJSONParser(org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONParser) BNode(org.eclipse.rdf4j.model.BNode) InputStream(java.io.InputStream) Literal(org.eclipse.rdf4j.model.Literal) QueryResultCollector(org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector) AbstractQueryResultIOTupleTest(org.eclipse.rdf4j.query.resultio.AbstractQueryResultIOTupleTest) Test(org.junit.Test)

Example 4 with BNode

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

the class RenderUtils method toSeRQL.

/**
 * Return the query string rendering of the {@link Value}
 *
 * @param theValue
 *        the value to render
 * @return the value rendered in its query string representation
 */
public static String toSeRQL(Value theValue) {
    StringBuilder aBuffer = new StringBuilder();
    if (theValue instanceof IRI) {
        IRI aURI = (IRI) theValue;
        aBuffer.append("<").append(aURI.toString()).append(">");
    } else if (theValue instanceof BNode) {
        aBuffer.append("_:").append(((BNode) theValue).getID());
    } else if (theValue instanceof Literal) {
        Literal aLit = (Literal) theValue;
        aBuffer.append("\"").append(escape(aLit.getLabel())).append("\"");
        if (Literals.isLanguageLiteral(aLit)) {
            aBuffer.append("@").append(aLit.getLanguage());
        } else {
            aBuffer.append("^^<").append(aLit.getDatatype().toString()).append(">");
        }
    }
    return aBuffer.toString();
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) BNode(org.eclipse.rdf4j.model.BNode) Literal(org.eclipse.rdf4j.model.Literal)

Example 5 with BNode

use of org.eclipse.rdf4j.model.BNode 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)

Aggregations

BNode (org.eclipse.rdf4j.model.BNode)40 IRI (org.eclipse.rdf4j.model.IRI)16 Literal (org.eclipse.rdf4j.model.Literal)14 Test (org.junit.Test)12 Resource (org.eclipse.rdf4j.model.Resource)10 Model (org.eclipse.rdf4j.model.Model)9 Value (org.eclipse.rdf4j.model.Value)7 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)7 StringReader (java.io.StringReader)6 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)6 Statement (org.eclipse.rdf4j.model.Statement)5 Binding (org.eclipse.rdf4j.query.Binding)4 BindingSet (org.eclipse.rdf4j.query.BindingSet)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 ParsedIRI (org.eclipse.rdf4j.common.net.ParsedIRI)3 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)3 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)3 InputStream (java.io.InputStream)2 ModelBuilder (org.eclipse.rdf4j.model.util.ModelBuilder)2