Question

I'm building a small Node app that queries an local n3 triple file, and using rdfstore-js. I have everything working OK with the example that the documentation uses, but thats a remote triple store. Documentation is confusing as to what parameters to pass in to rdfstore.create() for a local file. Perhaps something like this?

    rdfstore.create(function(store) {
    store.execute('LOAD /Users/Ben/Desktop/MET/triple_SPARQL/triples.n3 text/n3 ',       function() {

    });
})

Anyone used rdfstore-js and has loaded in local files?

Thanks!

Was it helpful?

Solution

From glancing at the source code, rdfstore-js doesn't seem to support loading local files referenced in SPARQL Updates (eg: LOAD <file:///myfile.ttl> ). However, you can read the file yourself and pass the data in directly:

var rdfstore = require('rdfstore')
, fs = require('fs');

rdfstore.create(function(store){
  var rdf = fs.readFileSync('/var/foo/bar.ttl').toString();
  store.load('text/turtle', rdf, function(s,d){
    console.log(s,d);
    store.execute("SELECT * WHERE { ?s ?p ?o } LIMIT 10", function(success, results){
      console.log(success, results);
    });
  });
});

OTHER TIPS

Here is my code sample that allows you to load a file in Node.js using the a path to a file (here options.path could be /uploads/123.owl)

rdfstore.create(function(err, store) {    
      if (err) 
          console.log("There was an error creating the store", err);                    
      else    
      {
          //replace / with \ 
          var syncPath = __dirname + options.path;      //local but not enough 
          var re = new RegExp('/', 'g');
          syncPath = syncPath.replace(re, '\\'); //update path        

          //set full path from filesystem of the ontology        
          var ontology = fs.readFileSync(__dirname + options.path).toString();

                      //LOCAL
                     store.load("application/ld+json" , ontology, "graph", function(err, results) {           

                        if (err)
                          console.log("There was an error loading the store", err);
                        else
                        {
                          store.graph("graph", function(err, graph) {

                             if (err)
                             {
                                console.log("There was an error creating the graph", err);  
                             }
                             else
                             {
                                var triples = graph.toArray();
                                console.log("Constructing triples sync... ");
                                console.log("There are ", triples.length, "triples");   

                                if (triples.length !== 0) 
                                {                  
                                  cb(triples);                                 }    
                            }

                          }); //graph                            
                       }
                    });

                    store.close(); //done
      }//err
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top