Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ An open-source framework for building SPARQL query engines in Javascript/Typescr
npm install --save sparql-engine
```

We also provide a browser release under [browser/sparqlEngine.js](https://raw.githubusercontent.com/rdfjs/N3.js/main/browser/sparqlEngine.js). Check the browser folder for an example usage. Please note that it's still an experimental feature.

# Getting started

The `sparql-engine` framework allow you to build a custom SPARQL query engine on top of any data storage system.
Expand Down
7,919 changes: 7,919 additions & 0 deletions browser/N3.js

Large diffs are not rendered by default.

109 changes: 109 additions & 0 deletions browser/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@

<p id="out">
Found solutions: <br><br>
</p>

<script src="./N3.js"></script>
<script src="./sparqlEngine.js"></script>
<script>

'use strict'

const { Parser, Store } = N3
const { HashMapDataset, Graph, PlanBuilder } = sparqlEngine

// Format a triple pattern according to N3 API:
// SPARQL variables must be replaced by `null` values
function formatTriplePattern (triple) {
let subject = null
let predicate = null
let object = null
if (!triple.subject.startsWith('?')) {
subject = triple.subject
}
if (!triple.predicate.startsWith('?')) {
predicate = triple.predicate
}
if (!triple.object.startsWith('?')) {
object = triple.object
}
return { subject, predicate, object }
}

class N3Graph extends Graph {
constructor () {
super()
this._store = Store()
}

insert (triple) {
return new Promise((resolve, reject) => {
try {
this._store.addTriple(triple.subject, triple.predicate, triple.object)
resolve()
} catch (e) {
reject(e)
}
})
}

delete (triple) {
return new Promise((resolve, reject) => {
try {
this._store.removeTriple(triple.subject, triple.predicate, triple.object)
resolve()
} catch (e) {
reject(e)
}
})
}

find (triple) {
const { subject, predicate, object } = formatTriplePattern(triple)
return this._store.getTriples(subject, predicate, object)
}

estimateCardinality (triple) {
const { subject, predicate, object } = formatTriplePattern(triple)
return Promise.resolve(this._store.countTriples(subject, predicate, object))
}
}

const graph = new N3Graph()
const dataset = new HashMapDataset('http://example.org#default', graph)

// Load some RDF data into the graph
const parser = new Parser()
parser.parse(`
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix : <http://example.org#> .
:a foaf:name "a" .
:b foaf:name "b" .
`).forEach(t => {
graph._store.addTriple(t)
})

const query = `
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE {
?s foaf:name ?name .
}`

// Creates a plan builder for the RDF dataset
const builder = new PlanBuilder(dataset)

// Get an iterator to evaluate the query
const iterator = builder.build(query)

// Read results
iterator.subscribe(bindings => {
document.getElementById('out').innerText += JSON.stringify(bindings.toObject(), null, 2)
}, err => {
console.error('error', err)
}, () => {
console.log('Query evaluation complete!')
})

</script>

Loading