JavaScript SPARQL

With rdfstore-js.
... all in the world's most popular programming language.

I finally had a chance to play with rdfstore-js by Antonio Garrote and it was all pretty straightforward. I already had node.js installed, so a simple npm install js installed his library. Then, I was ready to include the library in a JavaScript script that would read some RDF and query it with SPARQL. I just ran my script from the command line, but node.js fans know that they can take advantage of this library's features in much more interesting application architectures. (Before I go on, I wanted to mention that after I tweeted yesterday that this blog entry was coming, Andy Seaborne reminded me about Apache Jena's ability to load and run JavaScript functions. I tried the example from the feature's home page and it worked great right out of the box.)

My sample script starts with a function I wrote for general-purpose output of SPARQL SELECT queries, then creates an rdfstore object and saves a query that will be used twice later in the script. After loading some RDF data about my book Learning SPARQL from the OCLC's Worldcat online library catalog into the rdfstore, it runs the saved query against the loaded data to list ISBN numbers. The script then loads data about another book, runs the same query, and you can see the additional ISBN numbers in the new output.

// Utility function for outputting SELECT results
function outputSPARQLResults(results) {
    for (row in results) {
        printedLine = ''
        for (column in results[row]) {
            printedLine = printedLine + results[row][column].value + ' '
        }
        console.log(printedLine)
    }
}

// Create an rdfstore
var rdfstore = require('rdfstore') 

// Define a query to execute.
var listISBNs = 'PREFIX s: <http://schema.org/> \
PREFIX ls: <http://learningsparql.com/ns/data#> \
PREFIX wco: <http://www.worldcat.org/title/-/oclc/> \
PREFIX wci: <http://worldcat.org/isbn/> \
SELECT ?isbn \
FROM ls:g1 WHERE { ?book s:isbn ?isbn } '

rdfstore.create(function(err, store) {   // no error handling
   
    store.execute(
        // Load data about the book Learning SPARQL into named graph g1 in the rdfstore.
        'LOAD <http://worldcat.org/oclc/890467322.ttl> \
        INTO GRAPH <http://learningsparql.com/ns/data#g1>', function(err) {

            store.setPrefix('s', 'http://schema.org/')
            store.setPrefix('ls', 'http://learningsparql.com/ns/data#')
            store.setPrefix('wco', 'http://www.worldcat.org/title/-/oclc/')
            store.setPrefix('wci', 'http://worldcat.org/isbn/')
           
	    store.execute(listISBNs, function(err, results) {
                console.log("=== ISBN value ===")
                outputSPARQLResults(results)
	    })
        }
    )

    store.execute(
        // Load data about the book "XML: The Annotated Specification" into the same graph
        'LOAD <http://worldcat.org/oclc/40768745.ttl> \
        INTO GRAPH <http://learningsparql.com/ns/data#g1>', function(err) {
	    store.execute(listISBNs, function(err, results) {
                console.log("\n=== ISBN values after adding 2nd book's data ===")
                outputSPARQLResults(results)
	    })
        }
    )
    
})

The script produces this output:

=== ISBN value ===
9781449371432 
1449371434 

=== ISBN values after adding 2nd book's data ===
9781449371432 
1449371434 
9780130826763 
0130826766

I loaded the data into a named graph because the library documentation's sample query for loading remote data did. I briefly tried loading the data into the default graph, but had no luck; I'm all for the use of name graphs, anyway. I also tried deleting triples from and inserting them into the g1 named graph and then querying again to see the results, and I didn't have much luck there either (no error messages--I just didn't see the query results I expected after the deletion and insertion) , but my minimal understanding of node.js asynchronous behavior was probably to blame. The library's github page shows that it does support INSERT and DELETE queries.

I wouldn't use this library's triplestore for ongoing production maintenance of a set of triples, anyway; I see it as a great lightweight way to grab triples from one or more sources and then perform SPARQL queries on those triples to look for subsets and patterns that can contribute to an application, all in the world's most popular programming language.

The rdfstore-js github page also shows that it offers many ways to query and manipulate the loaded data that, for a JavaScript programmer, would be more direct. If Antonio's ultimate goal was to bring RDF to JavaScript developers, I won't complain; I'm just glad that he brought a useful JavaScript library to RDF (and SPARQL) developers.


Please add any comments to this Google+ post.