Search in sources :

Example 41 with RDFNode

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);
        }
    }
}
Also used : Triple(org.apache.jena.graph.Triple) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) ModelCom(org.apache.jena.rdf.model.impl.ModelCom) Property(org.apache.jena.rdf.model.Property) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 42 with RDFNode

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());
}
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) RDFNode(org.apache.jena.rdf.model.RDFNode) Test(org.junit.Test)

Example 43 with RDFNode

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"));
}
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) SelectBuilder(org.apache.jena.arq.querybuilder.SelectBuilder) RDFNode(org.apache.jena.rdf.model.RDFNode) Test(org.junit.Test)

Example 44 with RDFNode

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;
}
Also used : StringTokenizer(java.util.StringTokenizer) Resource(org.apache.jena.rdf.model.Resource) Property(org.apache.jena.rdf.model.Property) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 45 with RDFNode

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());
}
Also used : AnonId(org.apache.jena.rdf.model.AnonId) RDFNode(org.apache.jena.rdf.model.RDFNode)

Aggregations

RDFNode (org.apache.jena.rdf.model.RDFNode)48 Resource (org.apache.jena.rdf.model.Resource)20 QuerySolution (org.apache.jena.query.QuerySolution)9 Model (org.apache.jena.rdf.model.Model)9 Node (org.apache.jena.graph.Node)8 Literal (org.apache.jena.rdf.model.Literal)7 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 Dataset (org.apache.jena.query.Dataset)5 Property (org.apache.jena.rdf.model.Property)5 Date (java.sql.Date)4 UpdateBuilder (org.apache.jena.arq.querybuilder.UpdateBuilder)4 TextIndexException (org.apache.jena.query.text.TextIndexException)4 Extractor (infoeval.main.WikiData.Extractor)3 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)3 ResultSetRewindable (org.apache.jena.query.ResultSetRewindable)3 Statement (org.apache.jena.rdf.model.Statement)3 ARQException (org.apache.jena.sparql.ARQException)3 UpdateRequest (org.apache.jena.update.UpdateRequest)3 File (java.io.File)2