Search in sources :

Example 1 with CollectionGraph

use of org.apache.jena.graph.impl.CollectionGraph in project jena by apache.

the class SecuredModelImpl method add.

@Override
public SecuredModel add(final StmtIterator iter) throws UpdateDeniedException, AddDeniedException, AuthenticationRequiredException {
    checkUpdate();
    if (!canCreate(Triple.ANY)) {
        final List<Triple> lst = new ArrayList<>();
        try {
            while (iter.hasNext()) {
                final Statement s = iter.next();
                checkCreate(s);
                lst.add(s.asTriple());
            }
            final Model m = ModelFactory.createModelForGraph(new CollectionGraph(lst));
            holder.getBaseItem().add(m.listStatements());
        } finally {
            iter.close();
        }
    } else {
        holder.getBaseItem().add(iter);
    }
    return holder.getSecuredItem();
}
Also used : Triple(org.apache.jena.graph.Triple) CollectionGraph(org.apache.jena.graph.impl.CollectionGraph)

Example 2 with CollectionGraph

use of org.apache.jena.graph.impl.CollectionGraph 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 3 with CollectionGraph

use of org.apache.jena.graph.impl.CollectionGraph in project jena by apache.

the class SecuredModelImpl method remove.

@Override
public SecuredModel remove(final StmtIterator iter) throws UpdateDeniedException, DeleteDeniedException, AuthenticationRequiredException {
    checkUpdate();
    if (!canDelete(Triple.ANY)) {
        final List<Triple> lst = new ArrayList<>();
        try {
            while (iter.hasNext()) {
                final Statement s = iter.next();
                checkDelete(s);
                lst.add(s.asTriple());
            }
            final Model m = ModelFactory.createModelForGraph(new CollectionGraph(lst));
            holder.getBaseItem().remove(m.listStatements());
        } finally {
            iter.close();
        }
    } else {
        holder.getBaseItem().remove(iter);
    }
    return holder.getSecuredItem();
}
Also used : Triple(org.apache.jena.graph.Triple) CollectionGraph(org.apache.jena.graph.impl.CollectionGraph)

Aggregations

Triple (org.apache.jena.graph.Triple)3 CollectionGraph (org.apache.jena.graph.impl.CollectionGraph)3 ArrayList (java.util.ArrayList)1 UpdateBuilder (org.apache.jena.arq.querybuilder.UpdateBuilder)1 Graph (org.apache.jena.graph.Graph)1 Node (org.apache.jena.graph.Node)1 Literal (org.apache.jena.rdf.model.Literal)1 Model (org.apache.jena.rdf.model.Model)1 Property (org.apache.jena.rdf.model.Property)1 Resource (org.apache.jena.rdf.model.Resource)1 Test (org.junit.Test)1