Skip to content

Commit 019f048

Browse files
authored
Merge pull request #37 from LDflex/feat/custom-engine
feat/custom engine
2 parents fdb5f4b + f1f88e2 commit 019f048

File tree

10 files changed

+487
-16
lines changed

10 files changed

+487
-16
lines changed

README.md

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,86 @@ const ruben = paths.create({
4545
showPerson(ruben);
4646
```
4747

48+
## Features
49+
50+
### Using a customised ComunicaEngine
51+
52+
This example uses the comunica engine for local file queries.
53+
54+
```JavaScript
55+
const { PathFactory } = require('ldflex');
56+
const { default: ComunicaEngine } = require('@ldflex/comunica');
57+
const { namedNode } = require('@rdfjs/data-model');
58+
const { newEngine: localFileEngine } = require('@comunica/actor-init-sparql-file');
59+
60+
// The JSON-LD context for resolving properties
61+
const context = {
62+
"@context": {
63+
"@vocab": "http://xmlns.com/foaf/0.1/",
64+
"friends": "knows",
65+
}
66+
};
67+
// The query engine and its source
68+
const queryEngine = new ComunicaEngine(
69+
path.join(__dirname, 'ruben-verborgh.ttl'),
70+
{ engine: localFileEngine() }
71+
);
72+
// The object that can create new paths
73+
const paths = new PathFactory({ context, queryEngine });
74+
75+
async function showPerson(person) {
76+
console.log(`This person is ${await person.name}`);
77+
78+
console.log(`${await person.givenName} is friends with:`);
79+
for await (const name of person.friends.givenName)
80+
console.log(`- ${name}`);
81+
}
82+
83+
const ruben = paths.create({
84+
subject: namedNode('https://ruben.verborgh.org/profile/#me'),
85+
});
86+
showPerson(ruben);
87+
```
88+
### Adding custom options to the ComunicaEngine
89+
90+
Add [comunica context options](https://comunica.dev/docs/query/advanced/context/) which are passed to the Comunica Engine.
91+
92+
```JavaScript
93+
const { PathFactory } = require('ldflex');
94+
const { default: ComunicaEngine } = require('@ldflex/comunica');
95+
const { namedNode } = require('@rdfjs/data-model');
96+
97+
// The JSON-LD context for resolving properties
98+
const context = {
99+
"@context": {
100+
"@vocab": "http://xmlns.com/foaf/0.1/",
101+
"friends": "knows",
102+
}
103+
};
104+
105+
// The query engine and its source
106+
const queryEngine = new ComunicaEngine(
107+
'https://ruben.verborgh.org/profile/',
108+
{ options: {/* add options here */} },
109+
);
110+
111+
// The object that can create new paths
112+
const paths = new PathFactory({ context, queryEngine });
113+
114+
async function showPerson(person) {
115+
console.log(`This person is ${await person.name}`);
116+
117+
console.log(`${await person.givenName} is friends with:`);
118+
for await (const name of person.friends.givenName)
119+
console.log(`- ${name}`);
120+
}
121+
122+
const ruben = paths.create({
123+
subject: namedNode('https://ruben.verborgh.org/profile/#me'),
124+
});
125+
showPerson(ruben);
126+
```
127+
48128
## License
49129
©2018–present
50-
[Ruben Verborgh](https://ruben.verborgh.org/),
51-
Joachim Van Herwegen.
52-
[MIT License](https://github.com/LDflex/LDflex-Comunica/blob/master/LICENSE.md).
130+
[Ruben Verborgh](https://ruben.verborgh.org/), Joachim Van Herwegen, [Jesse Wright](https://github.com/jeswr/). [MIT License](https://github.com/LDflex/LDflex-Comunica/blob/master/LICENSE.md).

package-lock.json

Lines changed: 175 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@
3030
"@babel/cli": "^7.8.4",
3131
"@babel/core": "^7.9.6",
3232
"@babel/preset-env": "^7.9.6",
33+
"@comunica/actor-http-proxy": "^1.21.1",
34+
"@comunica/actor-init-sparql-file": "^1.21.3",
35+
"@comunica/actor-init-sparql-rdfjs": "^1.21.3",
3336
"@pollyjs/adapter-node-http": "^5.1.1",
3437
"@pollyjs/core": "^4.2.1",
3538
"@pollyjs/persister-fs": "^5.1.1",
3639
"@rdfjs/data-model": "^1.1.2",
3740
"eslint": "^7.0.0",
3841
"eslint-plugin-jest": "^24.3.6",
3942
"husky": "^6.0.0",
40-
"jest": "^26.0.1",
43+
"jest": "^26.6.3",
4144
"mkdirp": "^1.0.4",
4245
"n3": "^1.10.0",
4346
"semantic-release": "^17.4.2",

src/ComunicaEngine.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ export default class ComunicaEngine {
1010
* The default source can be a single URL, an RDF/JS Datasource,
1111
* or an array with any of these.
1212
*/
13-
constructor(defaultSource) {
14-
this._engine = DefaultEngine;
13+
constructor(defaultSource, settings = {}) {
14+
this._engine = settings.engine ? settings.engine : DefaultEngine;
1515
// Preload sources but silence errors; they will be thrown during execution
1616
this._sources = this.parseSources(defaultSource);
1717
this._sources.catch(() => null);
18+
this._options = settings.options ? settings.options : {};
1819
}
1920

2021
/**
@@ -28,7 +29,7 @@ export default class ComunicaEngine {
2829
const sources = await (source ? this.parseSources(source) : this._sources);
2930
if (sources.length !== 0) {
3031
// Execute the query and yield the results
31-
const queryResult = await this._engine.query(sparql, { sources });
32+
const queryResult = await this._engine.query(sparql, { sources, ...this._options });
3233
yield* this.streamToAsyncIterable(queryResult.bindingsStream);
3334
}
3435
}

test/ComunicaEngine-test.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ComunicaEngine from '../src/ComunicaEngine';
22

3-
import { mockHttp } from './util';
3+
import { mockHttp, readAll } from './util';
44
import { namedNode, defaultGraph, quad } from '@rdfjs/data-model';
55
import { Store } from 'n3';
66
import { Readable } from 'stream';
@@ -255,10 +255,3 @@ describe('An ComunicaEngine instance with a default source that errors', () => {
255255
await expect(readAll(result)).rejects.toThrow('my error');
256256
});
257257
});
258-
259-
async function readAll(asyncIterator) {
260-
const items = [];
261-
for await (const item of asyncIterator)
262-
items.push(item);
263-
return items;
264-
}

0 commit comments

Comments
 (0)