From 1a5edbe05c17ee3b3a6c850ab68026b754bf875b Mon Sep 17 00:00:00 2001 From: Jacob McConomy Date: Thu, 20 Jun 2019 15:32:32 -0400 Subject: [PATCH 1/3] Added Uploading data to POD and Running SPARQL queries in the store --- README.md | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 141 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ddae192..0fcceb8 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ try { ## Using data in the store There are two ways to look at RDF data in a store. You can synchronously use the -`each()` and `any()` methods and `statementsMatching()`, or you can do a query +each()` and `any()` methods and `statementsMatching()`, or you can do a query which returns results asynchronously. The `each()`, `any()` and `statementsMatching()` take a pattern of subject, @@ -121,6 +121,146 @@ var me = $rdf.sym('http://www.w3.org/People/Berners-Lee/card#i'); var friend = store.any(me, FOAF('knows')) ``` +## Running SPARQL queries in the store + +RDFLib uses SPARQL queries in order to query data from your local store. Users can find out more information about writing sparql queries + +[here]: https://www.w3.org/TR/rdf-sparql-query/ + +. + +The first step in querying data storerd in your Solid Pod, is to fetch the data from your Pod into your local store. This is performed by the function "loadFromUrl". This function takes in a url from where your Pod can be reached and the variable that defines your local store. + +The next step is call "prepare" in order to convert a SPARQL query string into a query object that can be used to run the query. After the query is prepared, execute the query by passing in the created query object and the local store you fetched your pod into. This will return an arrray of results based on what query the query returns from your local store. + +```javascript +import $rdf from "rdflib"; +const query = "select distinct ?s ?p ?o where { ?s ?p ?o. }"; +const store = $rdf.graph(); + +// Loads the data from a URL into the local store +const loadFromUrl = (url, store) => { + return new Promise((resolve, reject) => { + let fetcher = new $rdf.Fetcher(store); + try { + fetcher.load(url).then(response => { + resolve(response.responseText); + }); + } catch (err) { + reject(err); + } + }); +}; + +// Prepares a query by converting SPARQL into a Solid query +const prepare = (qryStr, store) => { + return new Promise((resolve, reject) => { + try { + let query = $rdf.SPARQLToQuery(qryStr, false, store); + resolve(query); + } catch (err) { + reject(err); + } + }); +}; + +// Puts query results into an array according to the projection +const rowHandler = (wanted, results) => { + const row = {}; + for (var r in results) { + let found = false; + let got = r.replace(/^\?/, ""); + if (wanted.length) { + for (var w in wanted) { + if (got === wanted[w].label) { + found = true; + continue; + } + } + if (!found) continue; + } + row[got] = results[r].value; + } + return row; +}; + +// Executes a query on the local store +const execute = (qry, store) => { + return new Promise((resolve, reject) => { + const wanted = qry.vars; + const resultAry = []; + store.query( + qry, + results => { + if (typeof results === "undefined") { + reject("No results."); + } else { + let row = rowHandler(wanted, results); + if (row) resultAry.push(row); + } + }, + {}, + () => { + resolve(resultAry); + } + ); + }); +}; + +async function getData() { + loadFromUrl(url, store).then(() => + prepare(query, store).then(qry => + execute(qry, store).then(results => { + // Do somthing with results here + }) + ) + ); +} +``` + + + +## Uploading data to POD + +```javascript +const $rdf = require("rdflib"); +const store = $rdf.graph(); +var updater = new $rdf.UpdateManager(store); + + +// Address of the named node that will store you uploaded data +me = store.sym(uri); +// Creates the graph +profile = me.doc(); + +/* +store.match returns a statement that matches the parameters passed +in (subject, predicate, object, graph). These statement keep track of the current state +of the local store at the specific uri. We set the parameters to null in order to pull back everything in the graph. +*/ +var currentstatements = store.match(null, null, null, profile); +// Add the triple to your local store that you want to send to your pod +store.add(me, store.sym(predicate), object, profile); +// Returns everything in the graph with the addition you made +var insertstatements = store.match(null, null, null, profile); +/* +This promise uses updater to take in the state of your graph before the changes and after you add the triple. It then uses the info to update your Solid Pod. +*/ +new Promise((accept, reject) => updater.update(currentstatements, insertstatements, +(uri, ok, message) => { + if (ok) + accept(); + else + reject(message); +})); + + + + +``` + + + ## Wildcards When one of the terms of the triple is set as *undefined*, it then serves as a From 2737df057adb7a0d8fbc400a6cdd072782beabc2 Mon Sep 17 00:00:00 2001 From: JacobMcConomy Date: Fri, 21 Jun 2019 13:01:38 -0400 Subject: [PATCH 2/3] Update README.md --- README.md | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 0fcceb8..d26ff0e 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ try { ## Using data in the store There are two ways to look at RDF data in a store. You can synchronously use the -each()` and `any()` methods and `statementsMatching()`, or you can do a query +`each()` and `any()` methods and `statementsMatching()`, or you can do a query which returns results asynchronously. The `each()`, `any()` and `statementsMatching()` take a pattern of subject, @@ -123,15 +123,12 @@ var friend = store.any(me, FOAF('knows')) ## Running SPARQL queries in the store -RDFLib uses SPARQL queries in order to query data from your local store. Users can find out more information about writing sparql queries +RDFLib uses SPARQL queries in order to query data from your local store. Users can find out more information +about writing sparql queries [here](https://www.w3.org/TR/rdf-sparql-query/). -[here]: https://www.w3.org/TR/rdf-sparql-query/ +When querying data stored in your Solid Pod, the first step is to fetch the data from your Pod into your local store. This is performed by the function "loadFromUrl". This function takes in a url from where your Pod can be reached and the variable that defines your local store. -. - -The first step in querying data storerd in your Solid Pod, is to fetch the data from your Pod into your local store. This is performed by the function "loadFromUrl". This function takes in a url from where your Pod can be reached and the variable that defines your local store. - -The next step is call "prepare" in order to convert a SPARQL query string into a query object that can be used to run the query. After the query is prepared, execute the query by passing in the created query object and the local store you fetched your pod into. This will return an arrray of results based on what query the query returns from your local store. +The next step is to call `prepare()`, to convert a SPARQL query string into a query object that can be used to run the query. After the query is prepared, execute the query by passing in the created query object and the local store you fetched your pod into. This will return an arrray of results based on what query the query returns from your local store. ```javascript import $rdf from "rdflib"; @@ -227,8 +224,7 @@ const $rdf = require("rdflib"); const store = $rdf.graph(); var updater = new $rdf.UpdateManager(store); - -// Address of the named node that will store you uploaded data +// Address of the named node that will store your uploaded data me = store.sym(uri); // Creates the graph profile = me.doc(); From 99528eee5203c6a5d4df53272702a4938393ece9 Mon Sep 17 00:00:00 2001 From: JacobMcConomy Date: Fri, 21 Jun 2019 16:27:12 -0400 Subject: [PATCH 3/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d26ff0e..4920557 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ var friend = store.any(me, FOAF('knows')) RDFLib uses SPARQL queries in order to query data from your local store. Users can find out more information about writing sparql queries [here](https://www.w3.org/TR/rdf-sparql-query/). -When querying data stored in your Solid Pod, the first step is to fetch the data from your Pod into your local store. This is performed by the function "loadFromUrl". This function takes in a url from where your Pod can be reached and the variable that defines your local store. +When querying data stored in your Solid Pod, the first step is to fetch the data from your Pod into your local store. This is performed by the function `loadFromUrl()`. This function takes in a url from where your Pod can be reached and the variable that defines your local store. The next step is to call `prepare()`, to convert a SPARQL query string into a query object that can be used to run the query. After the query is prepared, execute the query by passing in the created query object and the local store you fetched your pod into. This will return an arrray of results based on what query the query returns from your local store.