use of org.apache.jena.rdf.model.Property in project jena by apache.
the class StageAltMain method makeData.
private static Model makeData() {
Model model = ModelFactory.createDefaultModel();
Resource r = model.createResource(NS + "r");
Property p1 = model.createProperty(NS + "p1");
Property p2 = model.createProperty(NS + "p2");
model.add(r, p1, "xyz");
model.add(r, p2, "abc");
return model;
}
use of org.apache.jena.rdf.model.Property in project jena by apache.
the class UpdateBuilderTest method example1.
// testsbased on the examples
/*
* Example 1: Adding some triples to a graph
This snippet describes two RDF triples to be inserted into the default graph of the Graph Store.
PREFIX dc: <http://purl.org/dc/elements/1.1/>
INSERT DATA
{
<http://example/book1> dc:title "A new book" ;
dc:creator "A.N.Other" .
}
Data before:
# Default graph
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix ns: <http://example.org/ns#> .
<http://example/book1> ns:price 42 .
Data after:
# Default graph
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix ns: <http://example.org/ns#> .
<http://example/book1> ns:price 42 .
<http://example/book1> dc:title "A new book" .
<http://example/book1> dc:creator "A.N.Other" .
*/
@Test
public void example1() {
Node n = NodeFactory.createURI("http://example/book1");
Node priceN = NodeFactory.createURI("http://example.org/ns#price");
Node priceV = NodeFactory.createLiteral("42");
UpdateBuilder builder = new UpdateBuilder().addPrefix("dc", DC_11.NS).addInsert(n, DC_11.title, "A new book").addInsert(n, DC_11.creator, "A.N.Other");
List<Triple> triples = new ArrayList<Triple>();
triples.add(new Triple(n, priceN, priceV));
Graph g = new CollectionGraph(triples);
Model m = ModelFactory.createModelForGraph(g);
m.setNsPrefix("dc", DC_11.NS);
m.setNsPrefix("ns", "http://example.org/ns#");
UpdateAction.execute(builder.build(), m);
Resource r = ResourceFactory.createResource(n.getURI());
Property rPriceP = ResourceFactory.createProperty(priceN.getURI());
Literal rPriceV = ResourceFactory.createPlainLiteral("42");
assertTrue(m.contains(r, rPriceP, rPriceV));
assertTrue(m.contains(r, DC_11.title, "A new book"));
assertTrue(m.contains(r, DC_11.creator, "A.N.Other"));
assertEquals(3, triples.size());
}
use of org.apache.jena.rdf.model.Property in project jena by apache.
the class UpdateBuilderExampleTests method example2.
/**
* Example 2: Adding some triples to a graph
*
* @see <a href=
* "https://www.w3.org/TR/sparql11-update/#example_2">sparql11-update
* example_2</a>
*/
@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"));
}
use of org.apache.jena.rdf.model.Property in project jena by apache.
the class UpdateBuilderExampleTests method example8.
/**
* Example 8:
*
* @see <a href=
* "https://www.w3.org/TR/sparql11-update/#example_8">sparql11-update
* example_8</a>
*/
@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"));
}
use of org.apache.jena.rdf.model.Property in project jena by apache.
the class GenericPropertyFunction method queryRewrite.
protected final Boolean queryRewrite(Graph graph, Node subject, Node predicate, Node object, QueryRewriteIndex queryRewriteIndex) {
if (graph.contains(subject, predicate, object)) {
// The graph contains the asserted triple, return the current binding.
return true;
}
// If query re-writing is disabled then exit - already checked that graph does not contain the asserted relation.
if (!queryRewriteIndex.isIndexActive()) {
return false;
}
// Begin Query Re-write by finding the literals of the Feature or Geometry.
SpatialObjectGeometryLiteral subjectSpatialLiteral = SpatialObjectGeometryLiteral.retrieve(graph, subject);
if (!subjectSpatialLiteral.isValid()) {
// Subject is not a Feature or a Geometry or there is no GeometryLiteral so exit.
return false;
}
SpatialObjectGeometryLiteral objectSpatialLiteral = SpatialObjectGeometryLiteral.retrieve(graph, object);
if (!objectSpatialLiteral.isValid()) {
// Object is not a Feature or a Geometry or there is no GeometryLiteral so exit.
return false;
}
// Check the QueryRewriteIndex for the result.
Property predicateProp = ResourceFactory.createProperty(predicate.getURI());
Boolean isPositive = queryRewriteIndex.test(subjectSpatialLiteral.getGeometryLiteral(), predicateProp, objectSpatialLiteral.getGeometryLiteral(), this);
return isPositive;
}
Aggregations