Skip to content

Commit

Permalink
Merge pull request #4 from e-e-e/sparql
Browse files Browse the repository at this point in the history
Add SPARQL query interface
  • Loading branch information
e-e-e committed Jan 15, 2018
2 parents ba521e2 + 53dda02 commit 204b092
Show file tree
Hide file tree
Showing 18 changed files with 367 additions and 5 deletions.
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const stream = require('readable-stream')
const pump = require('pump')
const inherits = require('inherits')
const events = require('events')
const SparqlIterator = require('sparql-iterator')

const utils = require('./lib/utils')
const Variable = require('./lib/Variable')
Expand Down Expand Up @@ -122,6 +123,14 @@ Graph.prototype.search = function (query, options, callback) {
utils.collect(this.searchStream(query, options), callback)
}

Graph.prototype.queryStream = function (query) {
return new SparqlIterator(query, { hypergraph: this })
}

Graph.prototype.query = function (query, callback) {
utils.collect(this.queryStream(query), callback)
}

Graph.prototype.generateBatch = utils.generateBatch

Graph.prototype.close = function (callback) {
Expand Down
11 changes: 10 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ function collect (stream, cb) {
stream.once('end', cb.bind(null, null, res))
}

function namespaceBlankVariables (triple) {
Object.keys(triple).forEach((key) => {
if (triple[key].startsWith && triple[key].startsWith('_:')) {
triple[key] = triple[key].replace(/^_:/, 'hypergraph:')
}
})
return triple
}

function filterTriple (triple) {
const filtered = {}
defs.spo.forEach((key) => {
Expand Down Expand Up @@ -58,7 +67,7 @@ function genKeys (triple) {
function generateBatch (triple, action) {
if (!action) action = 'put'
var data = (action === 'put')
? JSON.stringify(triple)
? JSON.stringify(namespaceBlankVariables(triple))
: null
return genKeys(triple).map(key => ({
type: 'put', // no delete in hyperdb so just puting nulls
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"hyperdb": "^2.0.0",
"lru": "^3.1.0",
"pump": "^2.0.0",
"readable-stream": "^2.3.3"
"readable-stream": "^2.3.3",
"sparql-iterator": "^2.0.5"
},
"devDependencies": {
"chai": "^4.1.2",
Expand Down
17 changes: 14 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,21 @@ Allowed options:
}
```

#### `db.search(queries, [options], callback)`
#### `db.query(query, callback)`

Allows for Basic Graph Patterns searches where all queries must match.
Expects queries to be an array of triple options of the form:
Allows for querying the graph with [SPARQL](https://www.w3.org/TR/sparql11-protocol/) queries.
Returns all entries that match the query.

SPARQL queries are implemented using [sparql-iterator](https://github.com/e-e-e/sparql-iterator) - a fork of [Linked Data Fragments Client](https://github.com/LinkedDataFragments/Client.js).

#### `var stream = db.queryStream(query)`

Returns a stream of results from the SPARQL query.

#### `db.search(patterns, [options], callback)`

Allows for Basic Graph Patterns searches where all patterns must match.
Expects patterns to be an array of triple options of the form:

```js
{
Expand Down
7 changes: 7 additions & 0 deletions test/data/simplefoaf.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:givenname "Alice" .
_:a foaf:family_name "Hacker" .

_:b foaf:firstname "Bob" .
_:b foaf:surname "Hacker" .
25 changes: 25 additions & 0 deletions test/data/sparqlIn11Minutes.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@prefix vcard: <http://www.w3.org/2006/vcard/ns#> .
@prefix sn: <http://www.snee.com/hr/> .

sn:emp1 vcard:given-name "Heidi" .
sn:emp1 vcard:family-name "Smith" .
sn:emp1 vcard:title "CEO" .
sn:emp1 sn:hireDate "2015-01-13" .
sn:emp1 sn:completedOrientation "2015-01-30" .

sn:emp2 vcard:given-name "John" .
sn:emp2 vcard:family-name "Smith" .
sn:emp2 sn:hireDate "2015-01-28" .
sn:emp2 vcard:title "Engineer" .
sn:emp2 sn:completedOrientation "2015-01-30" .
sn:emp2 sn:completedOrientation "2015-03-15" .

sn:emp3 vcard:given-name "Francis" .
sn:emp3 vcard:family-name "Jones" .
sn:emp3 sn:hireDate "2015-02-13" .
sn:emp3 vcard:title "Vice President" .

sn:emp4 vcard:given-name "Jane" .
sn:emp4 vcard:family-name "Berger" .
sn:emp4 sn:hireDate "1000-03-10" .
sn:emp4 vcard:title "Sales" .
7 changes: 7 additions & 0 deletions test/queries/sparqlIn11Minutes1.rq
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>

SELECT ?person
WHERE
{
?person vcard:family-name "Smith" .
}
16 changes: 16 additions & 0 deletions test/queries/sparqlIn11Minutes11.rq
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
PREFIX sn: <http://www.snee.com/hr/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

CONSTRUCT {
?person rdf:type foaf:Person .
?person foaf:givenName ?givenName .
?person foaf:familyName ?familyName .
?person foaf:name ?fullName .
}
WHERE {
?person vcard:given-name ?givenName .
?person vcard:family-name ?familyName .
BIND(concat(?givenName," ",?familyName) AS ?fullName)
}
8 changes: 8 additions & 0 deletions test/queries/sparqlIn11Minutes2.rq
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>

SELECT ?person ?givenName
WHERE
{
?person vcard:family-name "Smith" .
?person vcard:given-name ?givenName .
}
10 changes: 10 additions & 0 deletions test/queries/sparqlIn11Minutes3.rq
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
PREFIX sn: <http://www.snee.com/hr/>

SELECT ?givenName ?familyName ?hireDate
WHERE
{
?person vcard:given-name ?givenName .
?person vcard:family-name ?familyName .
?person sn:hireDate ?hireDate .
}
11 changes: 11 additions & 0 deletions test/queries/sparqlIn11Minutes4.rq
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
PREFIX sn: <http://www.snee.com/hr/>

SELECT ?givenName ?familyName ?hireDate
WHERE
{
?person vcard:given-name ?givenName .
?person vcard:family-name ?familyName .
?person sn:hireDate ?hireDate .
FILTER(?hireDate < "2015-03-01")
}
10 changes: 10 additions & 0 deletions test/queries/sparqlIn11Minutes5.rq
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
PREFIX sn: <http://www.snee.com/hr/>

SELECT ?givenName ?familyName ?oDate
WHERE
{
?person vcard:given-name ?givenName .
?person vcard:family-name ?familyName .
?person sn:completedOrientation ?oDate .
}
10 changes: 10 additions & 0 deletions test/queries/sparqlIn11Minutes6.rq
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
PREFIX sn: <http://www.snee.com/hr/>

SELECT ?givenName ?familyName ?oDate
WHERE
{
?person vcard:given-name ?givenName .
?person vcard:family-name ?familyName .
OPTIONAL { ?person sn:completedOrientation ?oDate . }
}
10 changes: 10 additions & 0 deletions test/queries/sparqlIn11Minutes7.rq
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
PREFIX sn: <http://www.snee.com/hr/>

SELECT ?givenName ?familyName
WHERE
{
?person vcard:given-name ?givenName .
?person vcard:family-name ?familyName .
NOT EXISTS { ?person sn:completedOrientation ?oDate . }
}
6 changes: 6 additions & 0 deletions test/queries/sparqlIn11Minutes8.rq
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
PREFIX sn: <http://www.snee.com/hr/>

SELECT ?givenName ?familyName ?someVariable WHERE { ?person
vcard:given-name ?givenName . ?person vcard:family-name ?familyName .
BIND("some value" AS ?someVariable) }
9 changes: 9 additions & 0 deletions test/queries/sparqlIn11Minutes9.rq
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
PREFIX sn: <http://www.snee.com/hr/>

SELECT ?givenName ?familyName ?fullName
WHERE {
?person vcard:given-name ?givenName .
?person vcard:family-name ?familyName .
BIND(concat(?givenName," ",?familyName) AS ?fullName)
}
11 changes: 11 additions & 0 deletions test/queries/union.rq
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>

CONSTRUCT { ?x vcard:N _:v .
_:v vcard:givenName ?gname .
_:v vcard:familyName ?fname }
WHERE
{
{ ?x foaf:firstname ?gname } UNION { ?x foaf:givenname ?gname } .
{ ?x foaf:surname ?fname } UNION { ?x foaf:family_name ?fname } .
}
Loading

0 comments on commit 204b092

Please sign in to comment.