v6 initial commit, maybe more to follow
This commit is contained in:
parent
7f91be86c0
commit
d6a32870bc
34 changed files with 16875 additions and 0 deletions
BIN
test/.DS_Store
vendored
Normal file
BIN
test/.DS_Store
vendored
Normal file
Binary file not shown.
49
test/N3/index.js
Normal file
49
test/N3/index.js
Normal file
|
@ -0,0 +1,49 @@
|
|||
const N3 = require('n3');
|
||||
const newEngine = require('@comunica/actor-init-sparql-rdfjs').newEngine;
|
||||
const jsonld = require('jsonld');
|
||||
const N3Store = require('n3').Store;
|
||||
const DataFactory = require('n3').DataFactory;
|
||||
|
||||
|
||||
|
||||
const myEngine = newEngine();
|
||||
const parser = new N3.Parser();
|
||||
const store = new N3Store();
|
||||
|
||||
const doc = {
|
||||
"http://schema.org/name": "Manu Sporny",
|
||||
"http://schema.org/url": {"@id": "http://manu.sporny.org/"},
|
||||
"http://schema.org/image": {"@id": "http://manu.sporny.org/images/manu.png"}
|
||||
};
|
||||
|
||||
|
||||
jsonld.toRDF(doc, {format: 'application/n-quads'}, (err, nquads) => {
|
||||
// nquads is a string of N-Quads
|
||||
//console.log(nquads);
|
||||
|
||||
|
||||
parser.parse(
|
||||
nquads,
|
||||
(error, quad, prefixes) => {
|
||||
if (quad)
|
||||
//console.log(quad);
|
||||
store.addQuad(quad);
|
||||
else
|
||||
console.log(store);
|
||||
|
||||
const start = async function (a,b){
|
||||
const result = await myEngine.query('SELECT * { ?s ?p <http://manu.sporny.org/>. ?s ?p ?o} LIMIT 100',
|
||||
{ sources: [ { value: store } ] });
|
||||
//result.bindingsStream.on('data', (data) => {
|
||||
// Each data object contains a mapping from variables to RDFJS terms.
|
||||
console.log(result);
|
||||
// console.log(data.get('?p'));
|
||||
// console.log(data.get('?o'));
|
||||
// });
|
||||
};
|
||||
|
||||
start();
|
||||
});
|
||||
|
||||
|
||||
});
|
68
test/comunicaSPARQL/index.js
Normal file
68
test/comunicaSPARQL/index.js
Normal file
|
@ -0,0 +1,68 @@
|
|||
const newEngine = require('@comunica/actor-init-sparql').newEngine;
|
||||
const N3 = require('n3');
|
||||
const jsonld = require('jsonld')
|
||||
const DataFactory = require('n3').DataFactory;
|
||||
const parser = new N3.Parser({format: 'application/n-quads'});
|
||||
|
||||
const store = new N3.Store();
|
||||
const myEngine = newEngine();
|
||||
|
||||
const doc = {
|
||||
"http://schema.org/name": "Manu Sporny",
|
||||
"http://schema.org/url": {"@id": "http://manu.sporny.org/"},
|
||||
"http://schema.org/image": {"@id": "http://manu.sporny.org/images/manu.png"}
|
||||
};
|
||||
|
||||
jsonld.toRDF(doc, {format: 'application/n-quads'}, (err, nquads) => {
|
||||
// nquads is a string of N-Quads
|
||||
// console.log(nquads);
|
||||
var quad= [];
|
||||
parser.parse(
|
||||
nquads,
|
||||
(error, quadN, prefixes) => {
|
||||
// console.log(quadN)
|
||||
if (quadN)
|
||||
{
|
||||
store.addQuad(DataFactory.quad(
|
||||
DataFactory.namedNode(quadN.subject.id), DataFactory.namedNode(quadN.predicate.id), DataFactory.namedNode(quadN.object.id)));
|
||||
|
||||
}
|
||||
else
|
||||
console.log("finished");
|
||||
// console.log(quadN)
|
||||
});
|
||||
|
||||
const start = async function (a,b){
|
||||
const result = await myEngine.query('SELECT * WHERE {?s ?p ?o } LIMIT 100',
|
||||
{ sources: [{ type: 'rdfjsSource', value: store}] })
|
||||
result.bindingsStream.on('data', (data) => console.log(data.toObject()));
|
||||
};
|
||||
start()
|
||||
console.log(quad)
|
||||
// store.addQuad(DataFactory.quad(
|
||||
// DataFactory.namedNode(quad.subject.id), DataFactory.namedNode(quad.predicate.id), DataFactory.namedNode(quad.object.id)));
|
||||
|
||||
|
||||
// store.addQuad(DataFactory.quad(
|
||||
// DataFactory.namedNode('http://schema.org/image'), DataFactory.namedNode('http://manu.sporny.org/images/manu.png'), DataFactory.namedNode('http://schema.org/name')));
|
||||
// store.addQuad(DataFactory.quad(
|
||||
// DataFactory.namedNode('http://schema.org/url'), DataFactory.namedNode('http://manu.sporny.org/'), DataFactory.namedNode('http://dbpedia.org/resource/Ghent')));
|
||||
//console.log(store)
|
||||
// const start = async function (a,b){
|
||||
// const result = await myEngine.query('SELECT * WHERE {?s ?p <http://manu.sporny.org/images/manu.png>. ?s ?p ?o } LIMIT 100',
|
||||
// { sources: [{ type: 'rdfjsSource', value: store}] })
|
||||
// result.bindingsStream.on('data', (data) => console.log(data.toObject()));
|
||||
// };
|
||||
// start()
|
||||
});
|
||||
|
||||
|
||||
// store.addQuad(DataFactory.quad(
|
||||
// DataFactory.namedNode('http://schema.org/image'), DataFactory.namedNode('http://manu.sporny.org/images/manu.png'), DataFactory.namedNode('http://schema.org/name')));
|
||||
// store.addQuad(DataFactory.quad(
|
||||
// DataFactory.namedNode('http://schema.org/url'), DataFactory.namedNode('http://manu.sporny.org/'), DataFactory.namedNode('http://dbpedia.org/resource/Ghent')));
|
||||
|
||||
// // console.log(store)
|
||||
|
||||
|
||||
|
20
test/jsonld/index.js
Normal file
20
test/jsonld/index.js
Normal file
|
@ -0,0 +1,20 @@
|
|||
const jsonld = require('jsonld');
|
||||
|
||||
const doc = {
|
||||
"http://schema.org/name": "Manu Sporny",
|
||||
"http://schema.org/url": {"@id": "http://manu.sporny.org/"},
|
||||
"http://schema.org/PrsnOwner": "0400985d4fca84fe0e8cff7e8902326a6703ba182cc8d6d8e20866b0acfc79ecb6bfd3d3b5d6ad7f48cd10fadc6d4348cab918f13db2ebb387ba16c57802bf47b1",
|
||||
|
||||
};
|
||||
|
||||
|
||||
jsonld.toRDF(doc, {format: 'application/n-quads'}, (err, nquads) => {
|
||||
// nquads is a string of N-Quads
|
||||
console.log(nquads);
|
||||
|
||||
});
|
||||
// const start = async function (a,b){
|
||||
// const rdf = await jsonld.toRDF(doc, {format: 'application/n-quads'});
|
||||
// console.log(rdf);
|
||||
// };
|
||||
// start();
|
78
test/sparqlEngine/index.js
Normal file
78
test/sparqlEngine/index.js
Normal file
|
@ -0,0 +1,78 @@
|
|||
'use strict'
|
||||
|
||||
const { BindingBase, HashMapDataset, Graph, PlanBuilder } = require('sparql-engine')
|
||||
const level = require('level')
|
||||
const levelgraph = require('levelgraph')
|
||||
const { Transform } = require('stream')
|
||||
|
||||
// An utility class used to convert LevelGraph bindings
|
||||
// into a format undestood by sparql-engine
|
||||
class FormatterStream extends Transform {
|
||||
constructor () {
|
||||
super({objectMode: true})
|
||||
}
|
||||
|
||||
_transform (item, encoding, callback) {
|
||||
// Transform LevelGraph objects into set of mappings
|
||||
// using BindingBase.fromObject
|
||||
this.push(BindingBase.fromObject(item))
|
||||
callback()
|
||||
}
|
||||
}
|
||||
|
||||
class LevelRDFGraph extends Graph {
|
||||
constructor (db) {
|
||||
super()
|
||||
this._db = db
|
||||
}
|
||||
|
||||
evalBGP (bgp) {
|
||||
// rewrite variables using levelgraph API
|
||||
bgp = bgp.map(t => {
|
||||
if (t.subject.startsWith('?')) {
|
||||
t.subject = this._db.v(t.subject.substring(1))
|
||||
}
|
||||
if (t.predicate.startsWith('?')) {
|
||||
t.predicate = this._db.v(t.predicate.substring(1))
|
||||
}
|
||||
if (t.object.startsWith('?')) {
|
||||
t.object = this._db.v(t.object.substring(1))
|
||||
}
|
||||
return t
|
||||
})
|
||||
// Transform the Stream returned by LevelGraph into an Stream of Bindings
|
||||
return new FormatterStream(this._db.searchStream(bgp))
|
||||
}
|
||||
}
|
||||
|
||||
const db = levelgraph(level('testing_db'))
|
||||
|
||||
// insert some triples
|
||||
var triple1 = { subject: 'http://example.org#a1', predicate: 'http://xmlns.com/foaf/0.1/name', object: '"c"' }
|
||||
var triple2 = { subject: 'http://example.org#a2', predicate: 'http://xmlns.com/foaf/0.1/name', object: '"d"' }
|
||||
db.put([triple1, triple2], () => {
|
||||
const graph = new LevelRDFGraph(db)
|
||||
const dataset = new HashMapDataset('http://example.org#default', graph)
|
||||
|
||||
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 => {
|
||||
console.log('Find solutions:', bindings.toObject())
|
||||
}, err => {
|
||||
console.error('error', err)
|
||||
}, () => {
|
||||
console.log('Query evaluation complete!')
|
||||
})
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue