use of org.apache.jena.rdf.model.RDFNode in project jena by apache.
the class AbstractTestModel method testRemoveSPO.
/**
Test that remove(s, p, o) works, in the presence of inferencing graphs that
mean emptyness isn't available. This is why we go round the houses and
test that expected ~= initialContent + addedStuff - removed - initialContent.
*/
public void testRemoveSPO() {
ModelCom mc = (ModelCom) ModelFactory.createDefaultModel();
for (String[] aCase : cases) {
for (int j = 0; j < 3; j += 1) {
Model content = getModel();
Model baseContent = copy(content);
modelAdd(content, aCase[0]);
Triple remove = triple(aCase[1]);
Node s = remove.getSubject(), p = remove.getPredicate(), o = remove.getObject();
Resource S = (Resource) (s.equals(Node.ANY) ? null : mc.getRDFNode(s));
Property P = ((p.equals(Node.ANY) ? null : mc.getRDFNode(p).as(Property.class)));
RDFNode O = o.equals(Node.ANY) ? null : mc.getRDFNode(o);
Model expected = modelWithStatements(aCase[2]);
content.removeAll(S, P, O);
Model finalContent = copy(content).remove(baseContent);
assertIsoModels(aCase[1], expected, finalContent);
}
}
}
use of org.apache.jena.rdf.model.RDFNode in project jena by apache.
the class UpdateBuilderExampleTests method example5.
/**
* Example 5:
*
* @see https://www.w3.org/TR/sparql11-update/#example_5
*/
@Test
public void example5() {
Resource p25 = ResourceFactory.createResource("http://example/president25");
Resource p27 = ResourceFactory.createResource("http://example/president27");
Resource p42 = ResourceFactory.createResource("http://example/president42");
m.setNsPrefix("foaf", FOAF.NS);
m.add(p25, FOAF.givenname, "Bill");
m.add(p25, FOAF.family_name, "McKinley");
m.add(p27, FOAF.givenname, "Bill");
m.add(p27, FOAF.family_name, "Taft");
m.add(p42, FOAF.givenname, "Bill");
m.add(p42, FOAF.family_name, "Clinton");
Node graphName = NodeFactory.createURI("http://example/addresses");
Dataset ds = DatasetFactory.create();
ds.addNamedModel(graphName.getURI(), m);
UpdateBuilder builder = new UpdateBuilder().addPrefix("foaf", FOAF.NS).with(graphName).addDelete("?person", FOAF.givenname, "Bill").addInsert("?person", FOAF.givenname, "William").addWhere("?person", FOAF.givenname, "Bill");
UpdateRequest req = builder.buildRequest();
UpdateAction.execute(req, ds);
Model m2 = ds.getNamedModel(graphName.getURI());
List<RDFNode> nodes = m2.listObjectsOfProperty(FOAF.givenname).toList();
assertEquals(1, nodes.size());
assertEquals("William", nodes.get(0).asLiteral().toString());
List<Resource> subjects = m2.listSubjectsWithProperty(FOAF.givenname).toList();
assertEquals(3, subjects.size());
}
use of org.apache.jena.rdf.model.RDFNode in project jena by apache.
the class UpdateBuilderExampleTests method example9.
/**
* Example 9:
*
* @see https://www.w3.org/TR/sparql11-update/#example_9
*/
@Test
public void example9() {
Node graphName1 = NodeFactory.createURI("http://example/addresses");
Node graphName2 = NodeFactory.createURI("http://example/people");
Resource alice = ResourceFactory.createResource();
Resource aliceMail = ResourceFactory.createResource("mailto:alice@example.com");
Resource bob = ResourceFactory.createResource();
Model m1 = ModelFactory.createDefaultModel();
m1.add(alice, RDF.type, FOAF.Person);
m1.add(alice, FOAF.name, "Alice");
m1.add(alice, FOAF.mbox, aliceMail);
m1.add(bob, RDF.type, FOAF.Person);
m1.add(bob, FOAF.name, "Bob");
Model m2 = ModelFactory.createDefaultModel();
Dataset ds = DatasetFactory.create();
ds.addNamedModel(graphName1.getURI(), m1);
ds.addNamedModel(graphName2.getURI(), m2);
SelectBuilder ins = new SelectBuilder().addGraph(graphName2, new SelectBuilder().addWhere("?person", FOAF.name, "?name").addWhere("?person", FOAF.mbox, "?email"));
SelectBuilder whr = new SelectBuilder().addGraph(graphName1, new SelectBuilder().addWhere("?person", FOAF.name, "?name").addOptional("?person", FOAF.mbox, "?email"));
UpdateBuilder builder = new UpdateBuilder().addInsert(ins).addWhere(whr);
UpdateAction.execute(builder.build(), ds);
m1 = ds.getNamedModel(graphName1.getURI());
assertEquals(5, m1.listStatements().toList().size());
assertEquals(3, m1.listStatements(alice, null, (RDFNode) null).toList().size());
assertTrue(m1.contains(alice, RDF.type, FOAF.Person));
assertTrue(m1.contains(alice, FOAF.name, "Alice"));
assertTrue(m1.contains(alice, FOAF.mbox, aliceMail));
assertEquals(2, m1.listStatements(bob, null, (RDFNode) null).toList().size());
assertTrue(m1.contains(bob, RDF.type, FOAF.Person));
assertTrue(m1.contains(bob, FOAF.name, "Bob"));
m2 = ds.getNamedModel(graphName2.getURI());
assertEquals(3, m2.listStatements().toList().size());
assertEquals(2, m2.listStatements(alice, null, (RDFNode) null).toList().size());
assertTrue(m2.contains(alice, FOAF.name, "Alice"));
assertTrue(m2.contains(alice, FOAF.mbox, aliceMail));
assertEquals(1, m2.listStatements(bob, null, (RDFNode) null).toList().size());
assertTrue(m2.contains(bob, FOAF.name, "Bob"));
}
use of org.apache.jena.rdf.model.RDFNode in project jena by apache.
the class ModelHelper method modelAdd.
/**
* add to a model all the statements expressed by a string.
*
* Does not do any transaction manipulation.
*
* @param m
* the model to be updated
* @param facts
* a sequence of semicolon-separated "S P O" facts
* @return the updated model
*/
public static Model modelAdd(Model m, String facts) {
StringTokenizer semis = new StringTokenizer(facts, ";");
while (semis.hasMoreTokens()) {
StringTokenizer st = new StringTokenizer(semis.nextToken());
Resource sub = resource(st.nextToken());
Property pred = property(st.nextToken());
RDFNode obj = rdfNode(st.nextToken());
m.add(sub, pred, obj);
}
return m;
}
use of org.apache.jena.rdf.model.RDFNode in project jena by apache.
the class AbstractTestModel method testCreateBlankFromNode.
public void testCreateBlankFromNode() {
RDFNode S = model.getRDFNode(NodeCreateUtils.create("_Blank"));
assertInstanceOf(Resource.class, S);
assertEquals(new AnonId("_Blank"), ((Resource) S).getId());
}
Aggregations