Skip to content

Commit c058d7e

Browse files
author
Fletcher91
committed
Decouple rdflib dependency
1 parent 7cdf03d commit c058d7e

37 files changed

+1678
-1041
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ a back-end, so it's only a demo for the view rendering mechanism.
2121

2222
## Installation
2323

24-
`yarn add link-lib rdflib@npm:link-rdflib`
24+
`yarn add link-lib`
25+
26+
and some peer dependencies:
27+
28+
`yarn add @ontologies/as @ontologies/core @ontologies/schema @ontologies/shacl @ontologies/xsd http-status-codes n-quads-parser`
2529

2630
The package externalizes the Promise API, so make sure to include your own when targeting platforms without native
2731
support.

package.json

+8-10
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,19 @@
3333
},
3434
"peerDependencies": {
3535
"@ontologies/as": ">=1.0.1",
36-
"@ontologies/core": ">=1.5.0",
36+
"@ontologies/core": ">=1.7.2",
37+
"@ontologies/ld": ">=1.0.0",
3738
"@ontologies/schema": ">=1.0.0",
3839
"@ontologies/shacl": ">=1.0.0",
3940
"@ontologies/xsd": ">=1.0.0",
40-
"http-status-codes": ">= 1.x",
41-
"rdflib": ">= 0.19.x"
41+
"http-status-codes": ">= 1.x"
4242
},
4343
"devDependencies": {
44-
"@ontola/memoized-hash-factory": "^1.0.1",
44+
"@ontola/memoized-hash-factory": "^1.2.1",
4545
"@ontologies/as": "^1.0.1",
46-
"@ontologies/core": "=1.5.2-strict-17",
46+
"@ontologies/core": "^1.7.2",
4747
"@ontologies/dcterms": "^1.0.0",
48+
"@ontologies/ld": "^1.0.0",
4849
"@ontologies/owl": "^1.0.0",
4950
"@ontologies/rdf": "^1.0.0",
5051
"@ontologies/rdfs": "^1.0.0",
@@ -62,18 +63,15 @@
6263
"jest": "^24.9.0",
6364
"jest-fetch-mock": "^2.1.2",
6465
"n-quads-parser": "=2.0.3-ontologies",
65-
"rdflib": "^1.0.4",
6666
"rollup": "^1.25.1",
6767
"rollup-plugin-commonjs": "^10.1.0",
6868
"rollup-plugin-node-resolve": "^5.2.0",
6969
"rollup-plugin-sourcemaps": "^0.4.2",
7070
"rollup-plugin-typescript2": "^0.24.3",
71-
"solid-auth-cli": "^1.0.8",
72-
"solid-auth-client": "^2.3.1",
7371
"ts-jest": "^24.1.0",
7472
"tslint": "^5.20.0",
75-
"typedoc": "^0.15.0",
76-
"typescript": "^3.7.0-beta"
73+
"typedoc": "^0.15.2",
74+
"typescript": "^3.7.2"
7775
},
7876
"jest": {
7977
"automock": false,

rollup.config.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import typescript from "rollup-plugin-typescript2";
66
export default {
77
// Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
88
external: [
9+
"@ontologies/as",
10+
"@ontologies/core",
11+
"@ontologies/schema",
12+
"@ontologies/shacl",
13+
"@ontologies/xsd",
914
"http-status-codes",
10-
"jsonld",
11-
"ml-disjoint-set",
1215
"n-quads-parser",
1316
"node-fetch",
14-
"rdflib",
15-
"solid-auth-client",
1617
],
1718
input: "src/link-lib.ts",
1819
output: [

src/LinkedRenderStore.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export class LinkedRenderStore<T> implements Dispatcher {
7979
public defaultType: NamedNode = schema.Thing;
8080
public deltaProcessors: DeltaProcessor[];
8181
public report: ErrorReporter;
82+
/** @deprecated */
8283
public namespaces: NamespaceMap = {...defaultNS};
8384

8485
public api: LinkedDataAPI;
@@ -349,7 +350,7 @@ export class LinkedRenderStore<T> implements Dispatcher {
349350
preExistingData = this.tryEntity(iri);
350351
}
351352
if (preExistingData !== undefined) {
352-
this.store.removeStatements(preExistingData);
353+
this.store.removeQuads(preExistingData);
353354
}
354355
await this.api.getEntity(iri, apiOpts);
355356
this.broadcast();
@@ -509,7 +510,7 @@ export class LinkedRenderStore<T> implements Dispatcher {
509510
*
510511
* @renderlibrary
511512
* @param {Node} subject The resource to get the renderer for.
512-
* @param {"rdflib".NamedNode} topology The topology to take into account when picking the renderer.
513+
* @param {NamedNode} topology The topology to take into account when picking the renderer.
513514
* @return {T | undefined}
514515
*/
515516
public resourceComponent(subject: Node, topology?: NamedNode): T | undefined {

src/RDFStore.ts

+54-72
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
import { DataFactory } from "@ontologies/core";
1+
import { DataFactory, QuadPosition } from "@ontologies/core";
22
import ld from "@ontologies/ld";
33
import rdf from "@ontologies/rdf";
4-
import {
5-
graph,
6-
Store,
7-
} from "./rdflib";
84

95
import ll from "./ontology/ll";
106
import rdfFactory, {
117
NamedNode,
12-
Node,
8+
OptionalNamedNode,
139
OptionalNode,
10+
OptionalTerm,
1411
Quad,
1512
Quadruple,
1613
Term,
1714
} from "./rdf";
1815
import { deltaProcessor } from "./store/deltaProcessor";
16+
import RDFIndex from "./store/RDFIndex";
1917
import { ChangeBuffer, DeltaProcessor, SomeNode, StoreProcessor } from "./types";
2018
import { allRDFPropertyStatements, getPropBestLang } from "./utilities";
2119
import { patchRDFLibStoreWithOverrides } from "./utilities/monkeys";
@@ -24,7 +22,7 @@ const EMPTY_ST_ARR: ReadonlyArray<Quad> = Object.freeze([]);
2422

2523
export interface RDFStoreOpts {
2624
deltaProcessorOpts?: {[k: string]: Array<NamedNode | undefined>};
27-
innerStore?: Store;
25+
innerStore?: RDFIndex;
2826
}
2927

3028
/**
@@ -46,21 +44,21 @@ export class RDFStore implements ChangeBuffer, DeltaProcessor {
4644
private langPrefs: string[] = Array.from(typeof navigator !== "undefined"
4745
? (navigator.languages || [navigator.language])
4846
: ["en"]);
49-
private store: Store = graph();
47+
private store: RDFIndex = new RDFIndex();
5048

5149
public get rdfFactory(): DataFactory {
52-
return this.store.rdfFactory;
50+
return rdfFactory;
5351
}
54-
public set rdfFactory(value: DataFactory) {
55-
throw this.store.rdfFactory = value;
52+
public set rdfFactory(_: DataFactory) {
53+
throw new Error("Factory is global (see @ontologies/core)");
5654
}
5755

5856
constructor({ deltaProcessorOpts, innerStore }: RDFStoreOpts = {}) {
5957
this.processDelta = this.processDelta.bind(this);
6058

61-
const g = innerStore || graph();
59+
const g = innerStore || new RDFIndex();
60+
g.addDataCallback(this.processTypeStatement.bind(this));
6261
this.store = patchRDFLibStoreWithOverrides(g, this);
63-
this.store.newPropertyAction(rdf.type, this.processTypeStatement.bind(this));
6462

6563
const defaults = {
6664
addGraphIRIS: [ll.add, ld.add],
@@ -88,7 +86,7 @@ export class RDFStore implements ChangeBuffer, DeltaProcessor {
8886
* Add statements to the store.
8987
* @param data Data to parse and add to the store.
9088
*/
91-
public addStatements(data: Quad[]): void {
89+
public addQuads(data: Quad[]): void {
9290
if (!Array.isArray(data)) {
9391
throw new TypeError("An array of statements must be passed to addStatements");
9492
}
@@ -98,7 +96,7 @@ export class RDFStore implements ChangeBuffer, DeltaProcessor {
9896
}
9997
}
10098

101-
public addQuads(data: Quadruple[]): Quad[] {
99+
public addQuadruples(data: Quadruple[]): Quad[] {
102100
const statements = new Array(data.length);
103101
for (let i = 0, len = data.length; i < len; i++) {
104102
statements[i] = this.store.add(data[i][0], data[i][1], data[i][2], data[i][3]);
@@ -107,35 +105,13 @@ export class RDFStore implements ChangeBuffer, DeltaProcessor {
107105
return statements;
108106
}
109107

110-
public any(
111-
subj: OptionalNode,
112-
pred?: OptionalNode,
113-
obj?: OptionalNode,
114-
why?: OptionalNode,
115-
): Term | undefined {
116-
return this.store.any(subj, pred, obj, why);
117-
}
118-
119-
public anyStatementMatching(subj: OptionalNode,
120-
pred?: OptionalNode,
121-
obj?: OptionalNode,
122-
why?: OptionalNode): Quad | undefined {
123-
return this.store.anyStatementMatching(subj, pred, obj, why);
124-
}
125-
126-
public anyValue(subj: OptionalNode,
127-
pred?: OptionalNode,
128-
obj?: OptionalNode,
129-
why?: OptionalNode): string | undefined {
130-
return this.store.anyValue(subj, pred, obj, why);
131-
}
132-
133108
public canon(term: SomeNode): SomeNode {
134-
return this.store.canon(term);
109+
// TODO
110+
return term;
135111
}
136112

137113
public defaultGraph(): SomeNode {
138-
return this.store.rdfFactory.defaultGraph();
114+
return rdfFactory.defaultGraph();
139115
}
140116

141117
/**
@@ -162,21 +138,26 @@ export class RDFStore implements ChangeBuffer, DeltaProcessor {
162138
this.changeTimestamps[rdfFactory.id(s.subject)] = changeStamp;
163139
return rdfFactory.equals(s.predicate, rdf.type);
164140
})
165-
.map((s) => this.processTypeStatement(undefined, s.subject, s.predicate, undefined, undefined));
141+
.map((s) => this.processTypeStatement(s));
166142

167143
return processingBuffer;
168144
}
169145

170146
/** @private */
171-
public getInternalStore(): Store {
147+
public getInternalStore(): RDFIndex {
172148
return this.store;
173149
}
174150

175151
public match(subj: OptionalNode,
176-
pred?: OptionalNode,
177-
obj?: OptionalNode,
178-
why?: OptionalNode): Quad[] {
179-
return this.store.match(subj, pred, obj, why) || [];
152+
pred?: OptionalNamedNode,
153+
obj?: OptionalTerm,
154+
graph?: OptionalNode): Quad[] {
155+
return this.store.match(
156+
subj || null,
157+
pred || null,
158+
obj || null,
159+
graph || null,
160+
) || [];
180161
}
181162

182163
public processDelta(delta: Quadruple[]): Quad[] {
@@ -186,19 +167,19 @@ export class RDFStore implements ChangeBuffer, DeltaProcessor {
186167
removables,
187168
] = this.deltaProcessor(delta);
188169

189-
this.removeStatements(removables);
170+
this.removeQuads(removables);
190171

191-
return this.replaceMatches(replacables).concat(this.addQuads(addables));
172+
return this.replaceMatches(replacables).concat(this.addQuadruples(addables));
192173
}
193174

194175
public removeResource(subject: SomeNode): void {
195176
this.touch(subject);
196177
this.typeCache[rdfFactory.id(subject)] = [];
197-
this.removeStatements(this.statementsFor(subject));
178+
this.removeQuads(this.statementsFor(subject));
198179
}
199180

200-
public removeStatements(statements: Quad[]): void {
201-
this.store.remove(statements.slice());
181+
public removeQuads(statements: Quad[]): void {
182+
this.store.removeQuads(statements);
202183
}
203184

204185
/**
@@ -210,7 +191,7 @@ export class RDFStore implements ChangeBuffer, DeltaProcessor {
210191
* @param original The statements to remove from the store.
211192
* @param replacement The statements to add to the store.
212193
*/
213-
public replaceStatements(original: Quad[], replacement: Quad[]): void {
194+
public replaceQuads(original: Quad[], replacement: Quad[]): void {
214195
const uniqueStatements = new Array(replacement.length).filter(Boolean);
215196
for (let i = 0; i < replacement.length; i++) {
216197
const cond = original.some(
@@ -222,26 +203,31 @@ export class RDFStore implements ChangeBuffer, DeltaProcessor {
222203
}
223204
}
224205

225-
this.removeStatements(original);
206+
this.removeQuads(original);
226207
// Remove statements not in the old object. Useful for replacing data loosely related to the main resource.
227208
for (let i = 0; i < uniqueStatements.length; i++) {
228-
this.store.removeMatches(uniqueStatements[i].subject, uniqueStatements[i].predicate);
209+
this.store.removeMatches(
210+
uniqueStatements[i].subject,
211+
uniqueStatements[i].predicate,
212+
null,
213+
null,
214+
);
229215
}
230216

231-
return this.addStatements(replacement);
217+
return this.addQuads(replacement);
232218
}
233219

234220
public replaceMatches(statements: Quadruple[]): Quad[] {
235221
for (let i = 0; i < statements.length; i++) {
236-
this.removeStatements(this.match(
222+
this.removeQuads(this.match(
237223
statements[i][0],
238224
statements[i][1],
239225
undefined,
240226
undefined,
241227
));
242228
}
243229

244-
return this.addQuads(statements);
230+
return this.addQuadruples(statements);
245231
}
246232

247233
public getResourcePropertyRaw(subject: SomeNode, property: SomeNode | SomeNode[]): Quad[] {
@@ -297,10 +283,11 @@ export class RDFStore implements ChangeBuffer, DeltaProcessor {
297283
* @param subject The identifier of the resource.
298284
*/
299285
public statementsFor(subject: SomeNode): Quad[] {
300-
const canon = rdfFactory.id(this.store.canon(subject));
286+
// TODO: Use the schema to replace canon
287+
const id = rdfFactory.id(subject);
301288

302-
return typeof this.store.subjectIndex[canon] !== "undefined"
303-
? this.store.subjectIndex[canon]
289+
return typeof this.store.indices[QuadPosition.subject][id] !== "undefined"
290+
? this.store.indices[QuadPosition.subject][id]
304291
: EMPTY_ST_ARR as Quad[];
305292
}
306293

@@ -317,22 +304,17 @@ export class RDFStore implements ChangeBuffer, DeltaProcessor {
317304
/**
318305
* Builds a cache of types per resource. Can be omitted when compiled against a well known service.
319306
*/
320-
private processTypeStatement(_formula: Store | undefined,
321-
subj: SomeNode,
322-
_pred: NamedNode,
323-
obj?: Term,
324-
_why?: Node): boolean {
325-
const subjId = rdfFactory.id(subj);
326-
if (!Array.isArray(this.typeCache[subjId])) {
327-
this.typeCache[subjId] = [obj as NamedNode];
307+
private processTypeStatement(quad: Quad): boolean {
308+
if (!rdfFactory.equals(quad.predicate, rdf.type)) {
328309
return false;
329310
}
330-
this.typeCache[subjId] = this.statementsFor((subj as NamedNode))
311+
const subjId = rdfFactory.id(quad.subject);
312+
if (!Array.isArray(this.typeCache[subjId])) {
313+
this.typeCache[subjId] = [];
314+
}
315+
this.typeCache[subjId] = this.statementsFor((quad.subject as NamedNode))
331316
.filter((s) => rdfFactory.equals(s.predicate, rdf.type))
332317
.map((s) => s.object as NamedNode);
333-
if (obj) {
334-
this.typeCache[subjId].push((obj as NamedNode));
335-
}
336318
return false;
337319
}
338320
}

0 commit comments

Comments
 (0)