Search in sources :

Example 86 with Literal

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

the class SPARQLJSONTupleTest method testBindings1.

@Test
public void testBindings1() throws Exception {
    SPARQLResultsJSONParser parser = new SPARQLResultsJSONParser(SimpleValueFactory.getInstance());
    QueryResultCollector handler = new QueryResultCollector();
    parser.setQueryResultHandler(handler);
    InputStream stream = this.getClass().getResourceAsStream("/sparqljson/bindings1.srj");
    assertNotNull("Could not find test resource", stream);
    parser.parseQueryResult(stream);
    // there must be two variables
    assertEquals(2, handler.getBindingNames().size());
    // first must be called "book", the second "title"
    assertEquals("book", handler.getBindingNames().get(0));
    assertEquals("title", handler.getBindingNames().get(1));
    // should be 7 solutions alltogether
    assertEquals(7, handler.getBindingSets().size());
    // Results are ordered, so first should be book6
    assertEquals("http://example.org/book/book6", handler.getBindingSets().get(0).getValue("book").stringValue());
    for (BindingSet b : handler.getBindingSets()) {
        assertNotNull(b.getValue("book"));
        assertNotNull(b.getValue("title"));
        assertTrue(b.getValue("book") instanceof IRI);
        assertTrue(b.getValue("title") instanceof Literal);
        IRI book = (IRI) b.getValue("book");
        if (book.stringValue().equals("http://example.org/book/book6")) {
            assertEquals("Harry Potter and the Half-Blood Prince", b.getValue("title").stringValue());
        } else if (book.stringValue().equals("http://example.org/book/book7")) {
            assertEquals("Harry Potter and the Deathly Hallows", b.getValue("title").stringValue());
        } else if (book.stringValue().equals("http://example.org/book/book5")) {
            assertEquals("Harry Potter and the Order of the Phoenix", b.getValue("title").stringValue());
        } else if (book.stringValue().equals("http://example.org/book/book4")) {
            assertEquals("Harry Potter and the Goblet of Fire", b.getValue("title").stringValue());
        } else if (book.stringValue().equals("http://example.org/book/book2")) {
            assertEquals("Harry Potter and the Chamber of Secrets", b.getValue("title").stringValue());
        } else if (book.stringValue().equals("http://example.org/book/book3")) {
            assertEquals("Harry Potter and the Prisoner Of Azkaban", b.getValue("title").stringValue());
        } else if (book.stringValue().equals("http://example.org/book/book1")) {
            assertEquals("Harry Potter and the Philosopher's Stone", b.getValue("title").stringValue());
        } else {
            fail("Found unexpected binding set in result: " + b.toString());
        }
    }
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) IRI(org.eclipse.rdf4j.model.IRI) SPARQLResultsJSONParser(org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONParser) 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 87 with Literal

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

the class RDFParserHelperTest method testCreateLiteralLabelOnly.

/**
 * Test method for
 * {@link org.eclipse.rdf4j.rio.helpers.RDFParserHelper#createLiteral(java.lang.String, java.lang.String, org.eclipse.rdf4j.model.URI, org.eclipse.rdf4j.rio.ParserConfig, org.eclipse.rdf4j.rio.ParseErrorListener, org.eclipse.rdf4j.model.ValueFactory)}
 * .
 */
@Test
public final void testCreateLiteralLabelOnly() throws Exception {
    Literal literal = RDFParserHelper.createLiteral(LABEL_TESTA, null, null, parserConfig, errListener, valueFactory);
    assertEquals(LABEL_TESTA, literal.getLabel());
    assertFalse(literal.getLanguage().isPresent());
    assertEquals(XMLSchema.STRING, literal.getDatatype());
}
Also used : Literal(org.eclipse.rdf4j.model.Literal) Test(org.junit.Test)

Example 88 with Literal

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

the class RDFParserHelperTest method testCreateLiteralLabelAndLanguageWithRDFLangString.

/**
 * Test method for
 * {@link org.eclipse.rdf4j.rio.helpers.RDFParserHelper#createLiteral(java.lang.String, java.lang.String, org.eclipse.rdf4j.model.URI, org.eclipse.rdf4j.rio.ParserConfig, org.eclipse.rdf4j.rio.ParseErrorListener, org.eclipse.rdf4j.model.ValueFactory)}
 * .
 * <p>
 * SES-1803 : Temporary decision to ensure RDF-1.0 backwards compatibility for Literals created by this
 * method in cases where {@link RDF#LANGSTRING} is given and there is a language.
 */
@Test
public final void testCreateLiteralLabelAndLanguageWithRDFLangString() throws Exception {
    Literal literal = RDFParserHelper.createLiteral(LABEL_TESTA, LANG_EN, RDF.LANGSTRING, parserConfig, errListener, valueFactory);
    assertEquals(LABEL_TESTA, literal.getLabel());
    assertEquals(LANG_EN, literal.getLanguage().orElse(null));
    assertEquals(RDF.LANGSTRING, literal.getDatatype());
}
Also used : Literal(org.eclipse.rdf4j.model.Literal) Test(org.junit.Test)

Example 89 with Literal

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

the class JSONLDWriterTest method testRoundTripNamespaces.

@Test
public void testRoundTripNamespaces() throws Exception {
    String exNs = "http://example.org/";
    IRI uri1 = vf.createIRI(exNs, "uri1");
    IRI uri2 = vf.createIRI(exNs, "uri2");
    Literal plainLit = vf.createLiteral("plain", XMLSchema.STRING);
    Statement st1 = vf.createStatement(uri1, uri2, plainLit);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    RDFWriter rdfWriter = rdfWriterFactory.getWriter(out);
    rdfWriter.getWriterConfig().set(JSONLDSettings.JSONLD_MODE, JSONLDMode.COMPACT);
    rdfWriter.handleNamespace("ex", exNs);
    rdfWriter.startRDF();
    rdfWriter.handleStatement(st1);
    rdfWriter.endRDF();
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    RDFParser rdfParser = rdfParserFactory.getParser();
    ParserConfig config = new ParserConfig();
    config.set(BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES, true);
    config.set(BasicParserSettings.FAIL_ON_UNKNOWN_LANGUAGES, true);
    rdfParser.setParserConfig(config);
    rdfParser.setValueFactory(vf);
    Model model = new LinkedHashModel();
    rdfParser.setRDFHandler(new StatementCollector(model));
    rdfParser.parse(in, "foo:bar");
    assertEquals("Unexpected number of statements, found " + model.size(), 1, model.size());
    assertTrue("missing namespaced statement", model.contains(st1));
    if (rdfParser.getRDFFormat().supportsNamespaces()) {
        assertTrue("Expected at least one namespace, found " + model.getNamespaces().size(), model.getNamespaces().size() >= 1);
        assertEquals(exNs, model.getNamespace("ex").get().getName());
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) ByteArrayInputStream(java.io.ByteArrayInputStream) Statement(org.eclipse.rdf4j.model.Statement) StatementCollector(org.eclipse.rdf4j.rio.helpers.StatementCollector) Literal(org.eclipse.rdf4j.model.Literal) Model(org.eclipse.rdf4j.model.Model) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) RDFWriter(org.eclipse.rdf4j.rio.RDFWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) RDFParser(org.eclipse.rdf4j.rio.RDFParser) ParserConfig(org.eclipse.rdf4j.rio.ParserConfig) RDFWriterTest(org.eclipse.rdf4j.rio.RDFWriterTest) Test(org.junit.Test)

Example 90 with Literal

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

the class SPARQLJSONTupleBackgroundTest 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)

Aggregations

Literal (org.eclipse.rdf4j.model.Literal)96 Test (org.junit.Test)52 IRI (org.eclipse.rdf4j.model.IRI)34 Value (org.eclipse.rdf4j.model.Value)17 BNode (org.eclipse.rdf4j.model.BNode)14 Model (org.eclipse.rdf4j.model.Model)13 Statement (org.eclipse.rdf4j.model.Statement)13 Resource (org.eclipse.rdf4j.model.Resource)9 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)8 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)7 InputStream (java.io.InputStream)6 Date (java.util.Date)6 BindingSet (org.eclipse.rdf4j.query.BindingSet)5 IOException (java.io.IOException)4 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)4 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)4 AbstractQueryResultIOTupleTest (org.eclipse.rdf4j.query.resultio.AbstractQueryResultIOTupleTest)4 QueryResultCollector (org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector)4 SPARQLResultsJSONParser (org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONParser)4 ArrayList (java.util.ArrayList)3