Search in sources :

Example 61 with Resource

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

the class TestOntClass method testDatatypeIsClassOwlFull.

public void testDatatypeIsClassOwlFull() {
    OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    Resource c = m.createResource();
    c.addProperty(RDF.type, RDFS.Datatype);
    assertTrue(c.canAs(OntClass.class));
}
Also used : Resource(org.apache.jena.rdf.model.Resource)

Example 62 with Resource

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

the class UpdateBuilderExampleTests method example2.

/**
	 * Example 2: Adding some triples to a graph
	 * 
	 * @see https://www.w3.org/TR/sparql11-update/#example_2
	 */
@Test
public void example2() {
    Resource r = ResourceFactory.createResource("http://example/book1");
    Property price = ResourceFactory.createProperty(NS_prefix + "price");
    Literal priceV = ResourceFactory.createTypedLiteral(42);
    m.setNsPrefix("dc", DC_11.NS);
    m.add(r, DC_11.title, "Fundamentals of Compiler Design");
    UpdateBuilder builder = new UpdateBuilder().addPrefix("ns", NS_prefix).addInsert(r, "ns:price", priceV.asNode());
    UpdateAction.execute(builder.buildRequest(), m);
    assertTrue(m.contains(r, price, priceV));
    assertTrue(m.contains(r, DC_11.title, "Fundamentals of Compiler Design"));
    assertEquals(2, triples.size());
    // assertEquals( 2, m.getNsPrefixMap().size());
    // assertEquals( NS_prefix, m.getNsPrefixMap().get("ns"));
    assertEquals(DC_11.NS, m.getNsPrefixMap().get("dc"));
}
Also used : Literal(org.apache.jena.rdf.model.Literal) Resource(org.apache.jena.rdf.model.Resource) UpdateBuilder(org.apache.jena.arq.querybuilder.UpdateBuilder) Property(org.apache.jena.rdf.model.Property) Test(org.junit.Test)

Example 63 with Resource

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

the class UpdateBuilderExampleTests method example4.

/**
	 * Example 4:
	 * 
	 * @see https://www.w3.org/TR/sparql11-update/#example_4
	 */
@Test
public void example4() {
    Resource r = ResourceFactory.createResource("http://example/book1");
    Node graphName = NodeFactory.createURI("http://example/bookStore");
    Dataset ds = DatasetFactory.create();
    ds.addNamedModel(graphName.getURI(), m);
    m.setNsPrefix("dc", DC_11.NS);
    m.add(r, DC_11.title, "Fundamentals of Compiler Desing");
    SelectBuilder sb = new SelectBuilder().addWhere(r, DC_11.title, "Fundamentals of Compiler Desing");
    sb = new SelectBuilder().addPrefix("dc", DC_11.NS).addGraph(graphName, sb);
    UpdateBuilder builder = new UpdateBuilder().addPrefix("dc", DC_11.NS).addDelete(sb);
    UpdateRequest req = builder.buildRequest();
    sb = new SelectBuilder().addWhere(r, DC_11.title, "Fundamentals of Compiler Design");
    sb = new SelectBuilder().addPrefix("dc", DC_11.NS).addGraph(graphName, sb);
    builder = new UpdateBuilder().addPrefix("dc", DC_11.NS).addInsert(sb);
    builder.appendTo(req);
    UpdateAction.execute(req, ds);
    Model m2 = ds.getNamedModel(graphName.getURI());
    assertTrue(m2.contains(r, DC_11.title, "Fundamentals of Compiler Design"));
    assertEquals(1, m2.listStatements().toSet().size());
// assertEquals( 1, m2.getNsPrefixMap().size());
// assertEquals( DC.NS, m2.getNsPrefixMap().get("dc"));
}
Also used : UpdateRequest(org.apache.jena.update.UpdateRequest) Dataset(org.apache.jena.query.Dataset) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node) Resource(org.apache.jena.rdf.model.Resource) Model(org.apache.jena.rdf.model.Model) UpdateBuilder(org.apache.jena.arq.querybuilder.UpdateBuilder) SelectBuilder(org.apache.jena.arq.querybuilder.SelectBuilder) Test(org.junit.Test)

Example 64 with Resource

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

the class UpdateBuilderExampleTests method example7.

/**
	 * Example 7:
	 * 
	 * @see https://www.w3.org/TR/sparql11-update/#example_7
	 */
@Test
public void example7() {
    Resource will = ResourceFactory.createResource("http://example/william");
    Resource willMail = ResourceFactory.createResource("mailto:bill@example");
    Resource fred = ResourceFactory.createResource("http://example/fred");
    Resource fredMail = ResourceFactory.createResource("mailto:fred@example");
    Node graphName = NodeFactory.createURI("http://example/addresses");
    m.add(will, RDF.type, FOAF.Person);
    m.add(will, FOAF.givenname, "William");
    m.add(will, FOAF.mbox, willMail);
    m.add(fred, RDF.type, FOAF.Person);
    m.add(fred, FOAF.givenname, "Fred");
    m.add(fred, FOAF.mbox, fredMail);
    Dataset ds = DatasetFactory.create();
    ds.addNamedModel(graphName.getURI(), m);
    UpdateBuilder builder = new UpdateBuilder().addPrefix("foaf", FOAF.NS).with(graphName).addDelete("?person", "?property", "?value").addWhere("?person", "?property", "?value").addWhere("?person", FOAF.givenname, "'Fred'");
    UpdateAction.execute(builder.build(), ds);
    Model m2 = ds.getNamedModel(graphName.getURI());
    assertTrue(m2.contains(will, RDF.type, FOAF.Person));
    assertTrue(m2.contains(will, FOAF.givenname, "William"));
    assertTrue(m2.contains(will, FOAF.mbox, willMail));
    assertEquals(3, m2.listStatements().toList().size());
}
Also used : Dataset(org.apache.jena.query.Dataset) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node) Resource(org.apache.jena.rdf.model.Resource) Model(org.apache.jena.rdf.model.Model) UpdateBuilder(org.apache.jena.arq.querybuilder.UpdateBuilder) Test(org.junit.Test)

Example 65 with Resource

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

the class UpdateBuilderExampleTests method example8.

/**
	 * Example 8:
	 * 
	 * @see https://www.w3.org/TR/sparql11-update/#example_8
	 */
@Test
public void example8() {
    Resource book1 = ResourceFactory.createResource("http://example/book1");
    Resource book2 = ResourceFactory.createResource("http://example/book2");
    Resource book3 = ResourceFactory.createResource("http://example/book3");
    Resource book4 = ResourceFactory.createResource("http://example/book4");
    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");
    Node graphName1 = NodeFactory.createURI("http://example/bookStore");
    Node graphName2 = NodeFactory.createURI("http://example/bookStore2");
    Model m1 = ModelFactory.createDefaultModel();
    m1.add(book1, DC_11.title, "Fundamentals of Compiler Design");
    m1.add(book1, DC_11.date, d1977);
    m1.add(book2, price, priceV);
    m1.add(book2, DC_11.title, "David Copperfield");
    m1.add(book2, DC_11.creator, "Edmund Wells");
    m1.add(book2, DC_11.date, d1948);
    m1.add(book3, DC_11.title, "SPARQL 1.1 Tutorial");
    Model m2 = ModelFactory.createDefaultModel();
    m2.add(book4, DC_11.title, "SPARQL 1.1 Tutorial");
    Dataset ds = DatasetFactory.create();
    ds.addNamedModel(graphName1.getURI(), m1);
    ds.addNamedModel(graphName2.getURI(), m2);
    ExprFactory factory = new ExprFactory();
    SelectBuilder ins = new SelectBuilder().addGraph(graphName2, new SelectBuilder().addWhere("?book", "?p", "?v"));
    SelectBuilder whr = new SelectBuilder().addGraph(graphName1, new SelectBuilder().addWhere("?book", DC_11.date, "?date").addFilter(factory.gt("?date", d1970)).addWhere("?book", "?p", "?v"));
    UpdateBuilder builder = new UpdateBuilder().addPrefix("dc", DC_11.NS).addPrefix("xsd", XSD.NS).addInsert(ins).addWhere(whr);
    UpdateAction.execute(builder.build(), ds);
    m1 = ds.getNamedModel(graphName1.getURI());
    assertEquals(7, m1.listStatements().toList().size());
    assertEquals(2, m1.listStatements(book1, null, (RDFNode) null).toList().size());
    assertTrue(m1.contains(book1, DC_11.title, "Fundamentals of Compiler Design"));
    assertTrue(m1.contains(book1, DC_11.date, d1977));
    assertEquals(4, m1.listStatements(book2, null, (RDFNode) null).toList().size());
    assertTrue(m1.contains(book2, price, priceV));
    assertTrue(m1.contains(book2, DC_11.title, "David Copperfield"));
    assertTrue(m1.contains(book2, DC_11.creator, "Edmund Wells"));
    assertTrue(m1.contains(book2, DC_11.date, d1948));
    assertEquals(1, m1.listStatements(book3, null, (RDFNode) null).toList().size());
    assertTrue(m1.contains(book3, DC_11.title, "SPARQL 1.1 Tutorial"));
    m2 = ds.getNamedModel(graphName2.getURI());
    assertEquals(3, m2.listStatements().toList().size());
    assertEquals(2, m2.listStatements(book1, null, (RDFNode) null).toList().size());
    assertTrue(m2.contains(book1, DC_11.title, "Fundamentals of Compiler Design"));
    assertTrue(m2.contains(book1, DC_11.date, d1977));
    assertEquals(1, m2.listStatements(book4, null, (RDFNode) null).toList().size());
    assertTrue(m2.contains(book4, DC_11.title, "SPARQL 1.1 Tutorial"));
}
Also used : Dataset(org.apache.jena.query.Dataset) Literal(org.apache.jena.rdf.model.Literal) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node) Resource(org.apache.jena.rdf.model.Resource) Model(org.apache.jena.rdf.model.Model) UpdateBuilder(org.apache.jena.arq.querybuilder.UpdateBuilder) Property(org.apache.jena.rdf.model.Property) SelectBuilder(org.apache.jena.arq.querybuilder.SelectBuilder) RDFNode(org.apache.jena.rdf.model.RDFNode) Test(org.junit.Test)

Aggregations

Resource (org.apache.jena.rdf.model.Resource)179 Model (org.apache.jena.rdf.model.Model)87 Test (org.junit.Test)55 Property (org.apache.jena.rdf.model.Property)35 RDFNode (org.apache.jena.rdf.model.RDFNode)25 Dataset (org.apache.jena.query.Dataset)20 Literal (org.apache.jena.rdf.model.Literal)17 BaseTest (org.apache.jena.atlas.junit.BaseTest)16 Node (org.apache.jena.graph.Node)16 Statement (org.apache.jena.rdf.model.Statement)15 UpdateBuilder (org.apache.jena.arq.querybuilder.UpdateBuilder)13 StringReader (java.io.StringReader)9 Triple (org.apache.jena.graph.Triple)9 InfModel (org.apache.jena.rdf.model.InfModel)9 Reader (java.io.Reader)8 ArrayList (java.util.ArrayList)8 JsonString (org.apache.jena.atlas.json.JsonString)8 JsonLDWriteContext (org.apache.jena.riot.JsonLDWriteContext)6 PrefixMapping (org.apache.jena.shared.PrefixMapping)6 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)5