You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Rob Stewart <ro...@googlemail.com> on 2011/10/23 19:00:38 UTC

Transforming RDF graphs to SPARQL updates

Hi,

Given the following simple rdf graph
---
 <http://example/bookStore> { <http://example/book1>  ns:price  42 }
---

Suppose this one graph was stored in a Jena Model. Someone would
perhaps want to transform this graph into a sparql update query, using
a nice API call on the Jena Model (e.g. model.asSparqlUpdate() ), so
that you are given (omitting the prefix declarations):
---
INSERT DATA { GRAPH <http://example/bookStore> {
<http://example/book1>  ns:price  42 } }
---

Does such an API call exist, maybe somewhere in
com.hp.hpl.jena.sparql.* ? I could imagine that such a requirement
might be fairly common.

--
Rob Stewart

Re: Transforming RDF graphs to SPARQL updates

Posted by Dave Reynolds <da...@gmail.com>.
On Sun, 2011-10-23 at 18:00 +0100, Rob Stewart wrote: 
> Hi,
> 
> Given the following simple rdf graph
> ---
>  <http://example/bookStore> { <http://example/book1>  ns:price  42 }
> ---
> 
> Suppose this one graph was stored in a Jena Model. Someone would
> perhaps want to transform this graph into a sparql update query, using
> a nice API call on the Jena Model (e.g. model.asSparqlUpdate() ), so
> that you are given (omitting the prefix declarations):
> ---
> INSERT DATA { GRAPH <http://example/bookStore> {
> <http://example/book1>  ns:price  42 } }
> ---
> 
> Does such an API call exist, maybe somewhere in
> com.hp.hpl.jena.sparql.* ? I could imagine that such a requirement
> might be fairly common.

I don't think there is, or at least wasn't last time I looked.

FWIW the code I use to do something like this is below. I tend to
package it as a writer so it can stream the data body while leaving the
controlling code able to inject other commands in the stream.

Dave

public class SPARQLUpdateWriter extends N3JenaWriterCommon {

    public static void writeUpdatePrefixes(PrefixMapping prefixes,
Writer writer) throws IOException {
        for (Map.Entry<String, String> pm :
prefixes.getNsPrefixMap().entrySet()) {
            writer.write("PREFIX " + pm.getKey() + ": <" + pm.getValue()
+ ">\n");
        }
    }
    
    public void writeUpdateBody(Model model, Writer _out) throws
IOException {
        // Set up output
        if (!(_out instanceof BufferedWriter)) {
            _out = new BufferedWriter(_out);
        }
        out = new N3IndentedWriter(_out);

        bNodesMap = new HashMap<Resource, String>() ;
        
        // Set up prefix mapping
        prefixMap = model.getNsPrefixMap() ;
        for ( Iterator<Entry<String, String>> iter =
prefixMap.entrySet().iterator() ; iter.hasNext() ; )
        {
            Entry<String, String> e = iter.next() ;
            String prefix = e.getKey() ;
            String uri = e.getValue(); 
            
            // XML namespaces name can include '.'
            // Turtle prefixed names can't.
            if ( ! checkPrefixPart(prefix) ) 
                iter.remove() ;
            else
            {
                if ( checkPrefixPart(prefix) )
                    // Build acceptable reverse mapping  
                    reversePrefixMap.put(uri, prefix) ;
            }
        }

        writeModel( ModelFactory.withHiddenStatements(model));
        out.flush();
        bNodesMap = null ;        
    }
    
}