I’ve recently encountered a need transform named graphs expressed in RDF, into sparql update requests… with the ultimate goal of autonomously pushing named graphs into rdf stores via a sparql endpoint.
The Jena APIs provide excellent support for describing and transforming RDF graphs, though it doesn’t support named graphs, or sparql requests out of the box. So for this, I needed to combine the expressivity of the NG4J APIs and the Fuseki APIs.
Below, is compilable code that:
- Uses Jena models and NG4J named graphs
- Transforms a named graph into a sparql update request
- Runs the sparql update request against a fuseki sparql endpoint, running locally
API versions used: Jena 2.6.4, ng4j 0.9.3, fuseki 0.2.0
/*
* Author: Rob Stewart
*/
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.sparql.modify.request.QuadDataAcc;
import com.hp.hpl.jena.sparql.modify.request.UpdateDataInsert;
import com.hp.hpl.jena.update.Update;
import com.hp.hpl.jena.update.UpdateRequest;
import de.fuberlin.wiwiss.ng4j.NamedGraph;
import de.fuberlin.wiwiss.ng4j.NamedGraphSet;
import de.fuberlin.wiwiss.ng4j.Quad;
import de.fuberlin.wiwiss.ng4j.impl.NamedGraphSetImpl;
import java.util.Iterator;
import org.openjena.fuseki.http.UpdateRemote;
public class RdfToSparqlUpdate {
public RdfToSparqlUpdate() {
/*
* First, create the Jena model,
* for user "PeterSmith"
*/
Model model = ModelFactory.createDefaultModel();
String foafNS = "http://xmlns.com/foaf/0.1/";
model.setNsPrefix("foaf", foafNS);
Resource user =
model.createResource("http://example.com/users/PeterSmith");
Property givenNameProp = model.createProperty(foafNS, "givenName");
Property familyNameProp = model.createProperty(foafNS, "familyName");
/*
* Next, create the NamedGraph using
* ng4j classes, into the named graph
* "http://example.com/people/PeterSmith"
*/
NamedGraphSet graphset = new NamedGraphSetImpl();
NamedGraph graph =
graphset.createGraph("http://example.com/people/PeterSmith");
// Add the triples about Peter Smith
// into the named graph
Statement s;
s = model.createStatement(user, givenNameProp, "Peter");
graph.add(s.asTriple());
s = model.createStatement(user, familyNameProp, "Smith");
graph.add(s.asTriple());
/*
* Let's prepare the named graph
* to become a sparql update request
*/
QuadDataAcc data = new QuadDataAcc();
Iterator iter =
graphset.findQuads(Node.ANY, Node.ANY, Node.ANY, Node.ANY);
com.hp.hpl.jena.sparql.core.Quad quad = null;
Quad next = null;
while (iter.hasNext()) {
next = iter.next();
quad =
new com.hp.hpl.jena.sparql.core.Quad(next.getGraphName(), next.getTriple());
data.addQuad(quad);
}
// Create an Update, with the quads extracted
// from the named graph
Update updateData = new UpdateDataInsert(data);
// Create the sparql update request,
// and add prefixes
UpdateRequest req = new UpdateRequest();
req.setPrefix("foaf", foafNS);
req.add(updateData);
// Finally, execute the sparql update
// against the sparql endpoint
new UpdateRemote().execute(req, "http://localhost:3030/dataset/update");
graphset.close();
}
public static void main(String[] args){
new RdfToSparqlUpdate();
}
}

Comments
Sterling stuff, muchos gracias.