use of org.apache.jena.rdf.model.RDFNode in project jena by apache.
the class AbstractTestModel method testPresentAsRDFNode.
private void testPresentAsRDFNode(Node n, Class<? extends RDFNode> nodeClass) {
RDFNode r = model.asRDFNode(n);
assertSame(n, r.asNode());
assertInstanceOf(nodeClass, r);
}
use of org.apache.jena.rdf.model.RDFNode 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"));
}
use of org.apache.jena.rdf.model.RDFNode in project jena by apache.
the class UpdateBuilderExampleTests method example10.
/**
* Example 10:
*
* @see https://www.w3.org/TR/sparql11-update/#example_10
*/
@Test
public void example10() {
Resource book1 = ResourceFactory.createResource("http://example/book1");
Resource book3 = ResourceFactory.createResource("http://example/book3");
Resource book4 = ResourceFactory.createResource("http://example/book4");
Literal d1996 = ResourceFactory.createTypedLiteral("1996-01-01T00:00:00-02:00", XSDDatatype.XSDdateTime);
Literal d2000 = ResourceFactory.createTypedLiteral("2000-01-01T00:00:00-02:00", XSDDatatype.XSDdateTime);
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, d1996);
m1.add(book1, RDF.type, DCTypes.PhysicalObject);
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.lt("?date", d2000)).addWhere("?book", "?p", "?v"));
UpdateBuilder builder = new UpdateBuilder().addPrefix("dc", DC_11.NS).addPrefix("xsd", XSD.NS).addInsert(ins).addWhere(whr);
UpdateRequest req = builder.buildRequest();
builder = new UpdateBuilder().with(graphName1).addDelete("?book", "?p", "?v").addWhere("?book", DC_11.date, "?date").addWhere("?book", RDF.type, DCTypes.PhysicalObject).addFilter(factory.lt("?date", d2000)).addWhere("?book", "?p", "?v");
builder.appendTo(req);
UpdateAction.execute(req, ds);
m1 = ds.getNamedModel(graphName1.getURI());
assertEquals(1, m1.listStatements().toList().size());
assertTrue(m1.contains(book3, DC_11.title, "SPARQL 1.1 Tutorial"));
m2 = ds.getNamedModel(graphName2.getURI());
assertEquals(4, m2.listStatements().toList().size());
assertEquals(3, 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, d1996));
assertTrue(m2.contains(book1, RDF.type, DCTypes.PhysicalObject));
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.RDFNode in project jena by apache.
the class SecurityExample method main.
/**
* @param args
*/
public static void main(String[] args) {
String[] names = { "alice", "bob", "chuck", "darla" };
RDFNode msgType = ResourceFactory.createResource("http://example.com/msg");
Property pTo = ResourceFactory.createProperty("http://example.com/to");
Property pFrom = ResourceFactory.createProperty("http://example.com/from");
Property pSubj = ResourceFactory.createProperty("http://example.com/subj");
Model model = ModelFactory.createDefaultModel();
URL url = SecurityExample.class.getClassLoader().getResource("org/apache/jena/security/example/example.ttl");
model.read(url.toExternalForm());
ResIterator ri = model.listSubjectsWithProperty(RDF.type, msgType);
System.out.println("All the messages");
while (ri.hasNext()) {
Resource msg = ri.next();
Statement to = msg.getProperty(pTo);
Statement from = msg.getProperty(pFrom);
Statement subj = msg.getProperty(pSubj);
System.out.println(String.format("%s to: %s from: %s subj: %s", msg, to.getObject(), from.getObject(), subj.getObject()));
}
System.out.println();
ExampleEvaluator evaluator = new ExampleEvaluator(model);
model = Factory.getInstance(evaluator, "http://example.com/SecuredModel", model);
for (String userName : names) {
evaluator.setPrincipal(userName);
System.out.println("Messages " + userName + " can manipulate");
ri = model.listSubjectsWithProperty(RDF.type, msgType);
while (ri.hasNext()) {
Resource msg = ri.next();
Statement to = msg.getProperty(pTo);
Statement from = msg.getProperty(pFrom);
Statement subj = msg.getProperty(pSubj);
System.out.println(String.format("%s to: %s from: %s subj: %s", msg, to.getObject(), from.getObject(), subj.getObject()));
}
ri.close();
for (String name : names) {
System.out.println(String.format("%s messages to %s", model.listSubjectsWithProperty(pTo, name).toList().size(), name));
}
System.out.println();
}
}
use of org.apache.jena.rdf.model.RDFNode in project jena by apache.
the class BindingUtils method addToBinding.
public static void addToBinding(BindingMap binding, QuerySolution qSolution) {
if (qSolution == null)
return;
for (Iterator<String> iter = qSolution.varNames(); iter.hasNext(); ) {
String n = iter.next();
RDFNode x = qSolution.get(n);
if (Var.isBlankNodeVarName(n))
continue;
try {
binding.add(Var.alloc(n), x.asNode());
} catch (ARQInternalErrorException ex) {
// bad binding attempt.
Log.warn(BindingUtils.class, "Attempt to bind " + n + " when already bound");
}
}
}
Aggregations