Search in sources :

Example 66 with Resource

use of org.apache.jena.rdf.model.Resource 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());
}
Also used : Triple(org.apache.jena.graph.Triple) Graph(org.apache.jena.graph.Graph) CollectionGraph(org.apache.jena.graph.impl.CollectionGraph) CollectionGraph(org.apache.jena.graph.impl.CollectionGraph) Node(org.apache.jena.graph.Node) Literal(org.apache.jena.rdf.model.Literal) ArrayList(java.util.ArrayList) Model(org.apache.jena.rdf.model.Model) 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 67 with Resource

use of org.apache.jena.rdf.model.Resource 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"));
}
Also used : UpdateRequest(org.apache.jena.update.UpdateRequest) 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) SelectBuilder(org.apache.jena.arq.querybuilder.SelectBuilder) RDFNode(org.apache.jena.rdf.model.RDFNode) Test(org.junit.Test)

Example 68 with Resource

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

the class SelectBuilderTest method testAggregatorsInSelect.

@Test
public void testAggregatorsInSelect() throws ParseException {
    builder.addVar("?x").addVar("count(*)", "?c").addWhere("?x", "?p", "?o").addGroupBy("?x");
    Model m = ModelFactory.createDefaultModel();
    Resource r = m.createResource("urn:one");
    m.add(r, m.getProperty("urn:p:one"), m.createTypedLiteral(1));
    m.add(r, m.getProperty("urn:p:two"), m.createTypedLiteral(3));
    m.add(r, m.getProperty("urn:p:three"), m.createTypedLiteral(5));
    r = m.createResource("urn:two");
    m.add(r, m.getProperty("urn:p:one"), m.createTypedLiteral(1));
    m.add(r, m.getProperty("urn:p:two"), m.createTypedLiteral(3));
    m.add(r, m.getProperty("urn:p:three"), m.createTypedLiteral(5));
    QueryExecution qexec = QueryExecutionFactory.create(builder.build(), m);
    ResultSet results = qexec.execSelect();
    assertTrue(results.hasNext());
    for (; results.hasNext(); ) {
        QuerySolution soln = results.nextSolution();
        assertTrue(soln.contains("c"));
        assertTrue(soln.contains("x"));
        assertEquals(3, soln.get("c").asLiteral().getInt());
    }
    builder.addVar("min(?o)", "?min").addVar("max(?o)", "?max");
    qexec = QueryExecutionFactory.create(builder.build(), m);
    results = qexec.execSelect();
    assertTrue(results.hasNext());
    for (; results.hasNext(); ) {
        QuerySolution soln = results.nextSolution();
        assertTrue(soln.contains("c"));
        assertTrue(soln.contains("x"));
        assertTrue(soln.contains("?min"));
        assertEquals(3, soln.get("c").asLiteral().getInt());
        assertEquals(1, soln.get("min").asLiteral().getInt());
        assertEquals(5, soln.get("max").asLiteral().getInt());
    }
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test) AbstractRegexpBasedTest(org.apache.jena.arq.AbstractRegexpBasedTest)

Example 69 with Resource

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

the class SchemagenOptionsTest method testGetResourceValueOPT1.

@Test
public void testGetResourceValueOPT1() throws SchemagenOptionsConfigurationException {
    SchemagenOptions so0 = new SchemagenOptions();
    Resource r = ResourceFactory.createResource("http://example.org/foo");
    so0.setOption(OPT.ROOT, r);
    assertEquals(r, so0.getResource(OPT.ROOT));
}
Also used : Resource(org.apache.jena.rdf.model.Resource) Test(org.junit.Test)

Example 70 with Resource

use of org.apache.jena.rdf.model.Resource 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();
    }
}
Also used : ResIterator(org.apache.jena.rdf.model.ResIterator) Statement(org.apache.jena.rdf.model.Statement) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) Property(org.apache.jena.rdf.model.Property) RDFNode(org.apache.jena.rdf.model.RDFNode) URL(java.net.URL)

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