Search in sources :

Example 71 with Property

use of org.apache.jena.rdf.model.Property in project jena by apache.

the class UpdateBuilderExampleTests method example6.

/**
 * Example 6:
 *
 * @see <a href=
 * "https://www.w3.org/TR/sparql11-update/#example_6">sparql11-update
 * example_6</a>
 */
@Test
public void example6() {
    Resource book1 = ResourceFactory.createResource("http://example/book1");
    Resource book2 = ResourceFactory.createResource("http://example/book2");
    Resource book3 = ResourceFactory.createResource("http://example/book3");
    Literal d1977 = ResourceFactory.createTypedLiteral("1977-01-01T00:00:00-02:00", XSDDatatype.XSDdateTime);
    Literal d1970 = ResourceFactory.createTypedLiteral("1970-01-01T00:00:00-02:00", XSDDatatype.XSDdateTime);
    Literal d1948 = ResourceFactory.createTypedLiteral("1948-01-01T00:00:00-02:00", XSDDatatype.XSDdateTime);
    Property price = ResourceFactory.createProperty(NS_prefix + "price");
    Literal priceV = ResourceFactory.createPlainLiteral("42");
    m.setNsPrefix("dc", DC_11.NS);
    m.setNsPrefix("ns", NS_prefix);
    m.add(book1, DC_11.title, "Principles of Compiler Design");
    m.add(book1, DC_11.date, d1977);
    m.add(book2, price, priceV);
    m.add(book2, DC_11.title, "David Copperfield");
    m.add(book2, DC_11.creator, "Edmund Wells");
    m.add(book2, DC_11.date, d1948);
    m.add(book3, DC_11.title, "SPARQL 1.1 Tutorial");
    UpdateBuilder builder = new UpdateBuilder().addPrefix("dc", DC_11.NS).addPrefix("xsd", "http://www.w3.org/2001/XMLSchema#").addDelete("?book", "?p", "?v").addWhere("?book", "dc:date", "?date");
    ExprFactory exprFact = builder.getExprFactory();
    builder.addFilter(exprFact.gt(exprFact.asExpr("?date"), d1970)).addWhere("?book", "?p", "?v");
    UpdateRequest req = builder.buildRequest();
    UpdateAction.execute(req, m);
    assertTrue(m.contains(book2, price, priceV));
    assertTrue(m.contains(book2, DC_11.title, "David Copperfield"));
    assertTrue(m.contains(book2, DC_11.creator, "Edmund Wells"));
    assertTrue(m.contains(book2, DC_11.date, d1948));
    assertTrue(m.contains(book3, DC_11.title, "SPARQL 1.1 Tutorial"));
    assertEquals(5, triples.size());
}
Also used : UpdateRequest(org.apache.jena.update.UpdateRequest) Literal(org.apache.jena.rdf.model.Literal) Resource(org.apache.jena.rdf.model.Resource) Property(org.apache.jena.rdf.model.Property) Test(org.junit.Test)

Example 72 with Property

use of org.apache.jena.rdf.model.Property in project jena by apache.

the class TestJsonIterator method testIterator.

@Test
public void testIterator() {
    Model model = ModelFactory.createDefaultModel();
    {
        Resource r = model.createResource(AnonId.create("first"));
        Property p = model.getProperty("");
        RDFNode node = ResourceFactory.createTypedLiteral("123", XSDDatatype.XSDdecimal);
        model.add(r, p, node);
        r = model.createResource(AnonId.create("second"));
        p = model.getProperty("");
        node = ResourceFactory.createTypedLiteral("abc", XSDDatatype.XSDstring);
        model.add(r, p, node);
        r = model.createResource(AnonId.create("third"));
        p = model.getProperty("");
        node = ResourceFactory.createLangLiteral("def", "en");
        model.add(r, p, node);
    }
    Query query = QueryFactory.create("JSON { \"s\": ?s , \"p\": ?p , \"o\" : ?o } " + "WHERE { ?s ?p ?o }", Syntax.syntaxARQ);
    try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
        JsonIterator execJsonItems = (JsonIterator) qexec.execJsonItems();
        assertTrue(execJsonItems.hasNext());
        assertNotNull(execJsonItems.next());
    }
}
Also used : Query(org.apache.jena.query.Query) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) Property(org.apache.jena.rdf.model.Property) RDFNode(org.apache.jena.rdf.model.RDFNode) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test)

Example 73 with Property

use of org.apache.jena.rdf.model.Property in project jena by apache.

the class TestResultSetFormatter method testIterator.

@Test
public void testIterator() throws IOException {
    Model model = ModelFactory.createDefaultModel();
    {
        Resource r = model.createResource(AnonId.create("first"));
        Property p = model.getProperty("");
        RDFNode node = ResourceFactory.createTypedLiteral("123", XSDDatatype.XSDdecimal);
        model.add(r, p, node);
        r = model.createResource(AnonId.create("second"));
        p = model.getProperty("");
        node = ResourceFactory.createTypedLiteral("abc", XSDDatatype.XSDstring);
        model.add(r, p, node);
        r = model.createResource(AnonId.create("third"));
        p = model.getProperty("");
        node = ResourceFactory.createLangLiteral("def", "en");
        model.add(r, p, node);
        r = model.createResource(AnonId.create("fourth"));
        p = model.getProperty("");
        node = ResourceFactory.createTypedLiteral("true", XSDDatatype.XSDboolean);
        model.add(r, p, node);
    }
    Query query = QueryFactory.create("JSON { \"s\": ?s , \"p\": ?p , \"o\" : ?o } " + "WHERE { ?s ?p ?o }", Syntax.syntaxARQ);
    try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
        JsonIterator execJsonItems = (JsonIterator) qexec.execJsonItems();
        try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            ResultSetFormatter.output(baos, execJsonItems);
            String output = baos.toString(Charset.forName("UTF-8"));
            assertTrue(output.contains("\"_:first\""));
            assertTrue(output.contains("\"_:second\""));
            assertTrue(output.contains("\"_:third\""));
            assertTrue(output.contains("\"_:fourth\""));
            assertFalse(output.contains("\"true\""));
            assertTrue(output.contains("true"));
            assertTrue(output.contains("123"));
            assertFalse(output.contains("\"123\""));
            assertTrue(output.contains("\"abc\""));
        }
    }
}
Also used : Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) JsonIterator(org.apache.jena.sparql.engine.JsonIterator) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Property(org.apache.jena.rdf.model.Property) RDFNode(org.apache.jena.rdf.model.RDFNode) Test(org.junit.Test)

Example 74 with Property

use of org.apache.jena.rdf.model.Property in project jena by apache.

the class TestDatasetTDB method load2.

private static void load2(Model model) {
    Resource r2 = model.createResource(base1 + "r2");
    Property p1 = model.createProperty(baseNS + "p1");
    model.add(r2, p1, "x1");
    model.add(r2, p1, "x2");
}
Also used : Resource(org.apache.jena.rdf.model.Resource) Property(org.apache.jena.rdf.model.Property)

Example 75 with Property

use of org.apache.jena.rdf.model.Property in project jena by apache.

the class TestDatasetTDB method load1.

private static void load1(Model model) {
    model.setNsPrefix("", base1);
    Resource r1 = model.createResource(base1 + "r1");
    Property p1 = model.createProperty(baseNS + "p1");
    model.add(r1, p1, "x1");
    model.add(r1, p1, "x2");
}
Also used : Resource(org.apache.jena.rdf.model.Resource) Property(org.apache.jena.rdf.model.Property)

Aggregations

Property (org.apache.jena.rdf.model.Property)90 Resource (org.apache.jena.rdf.model.Resource)59 Model (org.apache.jena.rdf.model.Model)46 RDFNode (org.apache.jena.rdf.model.RDFNode)19 Test (org.junit.Test)18 Statement (org.apache.jena.rdf.model.Statement)17 ArrayList (java.util.ArrayList)14 StmtIterator (org.apache.jena.rdf.model.StmtIterator)14 Literal (org.apache.jena.rdf.model.Literal)11 Iterator (java.util.Iterator)6 Node (org.apache.jena.graph.Node)6 Triple (org.apache.jena.graph.Triple)6 QueryExecution (org.apache.jena.query.QueryExecution)6 OntModel (org.apache.jena.ontology.OntModel)5 Dataset (org.apache.jena.query.Dataset)5 Query (org.apache.jena.query.Query)4 InfModel (org.apache.jena.rdf.model.InfModel)4 Store (org.apache.jena.sdb.Store)4 IOException (java.io.IOException)3 LEGATO (legato.LEGATO)3