@linked.archi/kernel 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/assets/archimate3/archimate3.2-principle-shapes.ttl +16 -22
- package/assets/archimate3/archimate3.2-questions.ttl +38 -38
- package/assets/archimate3/archimate3.2-viewpoint-shapes.ttl +1 -1
- package/assets/archimate4/archimate4-principle-shapes.ttl +38 -36
- package/assets/archimate4/archimate4-questions.ttl +38 -38
- package/assets/archimate4/archimate4-viewpoint-shapes.ttl +1 -1
- package/assets/catalog.ttl +209 -105
- package/assets/core/core-onto.ttl +2 -2
- package/assets/core/core-shapes.ttl +1 -1
- package/assets/core/core-vis-shapes.ttl +20 -9
- package/package.json +2 -2
- package/src/model-manager/questions.ts +6 -1
- package/src/rdf-engine/native-load.ts +37 -0
- package/src/rdf-engine/observable-store.ts +98 -120
- package/src/rdf-engine/rdf-store.ts +14 -2
- package/src/rdf-engine/sparql-constraints.ts +15 -0
- package/src/rdf-engine/validation-engine.ts +97 -32
- package/src/rdf-engine/validation-service.ts +3 -1
|
@@ -40,14 +40,17 @@ clean. The two load-bearing rules here are ArchNodeShape (a diagram node that cl
|
|
|
40
40
|
stand for a model element must say which one) and PlacedDiagElementShape (a shape or edge
|
|
41
41
|
must belong to exactly one view); the remainder is cardinality and datatype hygiene.'''@en ;
|
|
42
42
|
dcterms:created "2026-08-13"^^xsd:date ;
|
|
43
|
-
dcterms:modified "2026-
|
|
43
|
+
dcterms:modified "2026-12-02"^^xsd:date ;
|
|
44
44
|
dcterms:publisher "Linked.Archi"@en,
|
|
45
45
|
<https://linked.archi> ;
|
|
46
46
|
bibo:status "draft" ;
|
|
47
|
-
owl:versionIRI <https://meta.linked.archi/core-vis-shapes/0.6.
|
|
48
|
-
owl:versionInfo "0.6.
|
|
49
|
-
owl:priorVersion <https://meta.linked.archi/core-vis-shapes/0.
|
|
50
|
-
skos:historyNote '''0.6.
|
|
47
|
+
owl:versionIRI <https://meta.linked.archi/core-vis-shapes/0.6.1#> ;
|
|
48
|
+
owl:versionInfo "0.6.1"@en ;
|
|
49
|
+
owl:priorVersion <https://meta.linked.archi/core-vis-shapes/0.6.0#> ;
|
|
50
|
+
skos:historyNote '''0.6.1 — NestedNodeShape's query joins arch-vis:inNode inside each branch of its
|
|
51
|
+
union: the same findings, without pairing every node that has a view with every other first (115 s on a model
|
|
52
|
+
of 365 000 triples with 5 213 nested nodes, in Oxigraph; 21 ms after).
|
|
53
|
+
0.6.0 — NodeOrderShape: a node's arch-vis:zOrder, which core-vis 0.10 adds, is
|
|
51
54
|
a single xsd:integer.
|
|
52
55
|
|
|
53
56
|
0.5.0 — ViewReferenceNodeShape: a reference to another view, which
|
|
@@ -315,10 +318,18 @@ edges may be labelled — the domain is DiagElement since core-vis 0.3.'''@en ;
|
|
|
315
318
|
sh:select """
|
|
316
319
|
SELECT $this ?value
|
|
317
320
|
WHERE {
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
{
|
|
321
|
+
# arch-vis:inNode inside each branch rather than joined to the union: an engine that
|
|
322
|
+
# evaluates the union on its own (Oxigraph does) pairs every node with a view with
|
|
323
|
+
# every other first, which took minutes on a model of nine thousand nodes.
|
|
324
|
+
{
|
|
325
|
+
$this arch-vis:inNode ?value .
|
|
326
|
+
$this arch-vis:view ?view .
|
|
327
|
+
?value arch-vis:view ?other .
|
|
328
|
+
FILTER(?view != ?other)
|
|
329
|
+
} UNION {
|
|
330
|
+
$this arch-vis:inNode ?value .
|
|
331
|
+
$this arch-vis:inNode+ $this .
|
|
332
|
+
}
|
|
322
333
|
}
|
|
323
334
|
""" ;
|
|
324
335
|
] ;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@linked.archi/kernel",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "The Linked.Archi model kernel: metamodel loading, permitted relationships, IRIs, model changes, diff and merge, SHACL validation and canonical Turtle, shared by la-gram and the Linked.Archi workspace.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -123,7 +123,7 @@
|
|
|
123
123
|
"dependencies": {
|
|
124
124
|
"@rdfjs/types": "^2.0.1",
|
|
125
125
|
"n3": "2.13.12",
|
|
126
|
-
"oxigraph": "0.5.
|
|
126
|
+
"oxigraph": "0.5.13",
|
|
127
127
|
"rdf-validate-shacl": "^0.5.0",
|
|
128
128
|
"@types/n3": "^1.26.1",
|
|
129
129
|
"@types/rdf-validate-shacl": "^0.5.7"
|
|
@@ -128,8 +128,13 @@ function anyGraph(pattern: string, g: string): string {
|
|
|
128
128
|
|
|
129
129
|
type Row = Record<string, { value: string; language?: string } | undefined>;
|
|
130
130
|
|
|
131
|
+
/**
|
|
132
|
+
* A question set's statements, read where the store keeps the metamodels (`queryMetamodels`): listing the questions
|
|
133
|
+
* must not give the inner store a model just opened, which only SPARQL over the model needs (KD-28).
|
|
134
|
+
*/
|
|
131
135
|
function select(store: RdfStore, sparql: string): Row[] {
|
|
132
|
-
const
|
|
136
|
+
const metamodels = (store as { queryMetamodels?(sparql: string): ReturnType<RdfStore['query']> }).queryMetamodels;
|
|
137
|
+
const result = typeof metamodels === 'function' ? metamodels.call(store, sparql) : store.query(sparql);
|
|
133
138
|
return result.type === 'bindings' ? (result.results as Row[]) : [];
|
|
134
139
|
}
|
|
135
140
|
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Quads given to Oxigraph in one native load of N-Triples rather than quad by quad: on a model of 365 000 triples,
|
|
3
|
+
* 2.1 s against 55 s and 402 against 570 MiB of WebAssembly memory, measured in Node (KD-28, KD-29). For the quads
|
|
4
|
+
* N-Triples carries as they are; the rest go one by one.
|
|
5
|
+
*/
|
|
6
|
+
import { DataFactory, Writer } from 'n3';
|
|
7
|
+
import type { Quad, Term } from '@rdfjs/types';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Whether a term goes into a native load as it is: not a blank node, whose label a bulk load reassigns, nor a
|
|
11
|
+
* directional literal; a triple term of such terms.
|
|
12
|
+
*/
|
|
13
|
+
export function canLoadAsTriples(term: Term): boolean {
|
|
14
|
+
if (term.termType === 'BlankNode') return false;
|
|
15
|
+
if (term.termType === 'Literal') return !(term as { direction?: string }).direction;
|
|
16
|
+
if (term.termType === 'Quad') {
|
|
17
|
+
const triple = term as unknown as Quad;
|
|
18
|
+
return triple.graph.termType === 'DefaultGraph' && canLoadAsTriples(triple.subject) && canLoadAsTriples(triple.object);
|
|
19
|
+
}
|
|
20
|
+
return term.termType === 'NamedNode';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The quads a native load takes as they are, and the rest, for one by one. */
|
|
24
|
+
export function splitForNativeLoad(quads: Iterable<Quad>): { native: Quad[]; oneByOne: Quad[] } {
|
|
25
|
+
const native: Quad[] = [];
|
|
26
|
+
const oneByOne: Quad[] = [];
|
|
27
|
+
for (const q of quads) (canLoadAsTriples(q.subject) && canLoadAsTriples(q.object) ? native : oneByOne).push(q);
|
|
28
|
+
return { native, oneByOne };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The quads as N-Triples, each in the default graph: a load names the graph they go into. */
|
|
32
|
+
export function nTriples(quads: readonly Quad[]): string {
|
|
33
|
+
const triples = quads.map((q) =>
|
|
34
|
+
q.graph.termType === 'DefaultGraph' ? q : (DataFactory.quad(q.subject as never, q.predicate as never, q.object as never) as unknown as Quad),
|
|
35
|
+
);
|
|
36
|
+
return new Writer({ format: 'N-Triples' }).quadsToString(triples as never);
|
|
37
|
+
}
|
|
@@ -23,11 +23,14 @@
|
|
|
23
23
|
* It also keeps a copy of the model graph in JavaScript (Task 22): a read of the
|
|
24
24
|
* model — the model whole, or a pattern in the default graph — is answered from
|
|
25
25
|
* it, without a crossing into Oxigraph's WebAssembly and a conversion of every
|
|
26
|
-
* quad back. The inner store keeps
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* inner store
|
|
26
|
+
* quad back. The inner store keeps the metamodels, and the model for SPARQL
|
|
27
|
+
* alone (KD-28): it is given the model when a SPARQL query, an update, a bulk
|
|
28
|
+
* load into the model or the store's size asks for it, in one native load from
|
|
29
|
+
* the copy. Until then a write of the model reaches the copy only, its terms
|
|
30
|
+
* checked at once as the inner store would check them — but for a small write
|
|
31
|
+
* once the inner store holds the model, which reaches both, so that SPARQL after
|
|
32
|
+
* an edit does not wait for the model again. While the inner store is behind, a
|
|
33
|
+
* read of every graph is the copy's model and the inner store's named graphs.
|
|
31
34
|
*
|
|
32
35
|
* And it lets a local edit be stamped inside itself (`setStamper`, the provenance of edits): once at the end of
|
|
33
36
|
* each outermost write — a {@link record}, a {@link batch}, or a write outside both — what the local edit
|
|
@@ -36,9 +39,10 @@
|
|
|
36
39
|
* not stamped.
|
|
37
40
|
*/
|
|
38
41
|
|
|
39
|
-
import { DataFactory, Store as N3Store
|
|
40
|
-
import type { NamedNode, Quad
|
|
42
|
+
import { DataFactory, Store as N3Store } from 'n3';
|
|
43
|
+
import type { NamedNode, Quad } from '@rdfjs/types';
|
|
41
44
|
import type { RdfStore, SparqlResult } from './interfaces.js';
|
|
45
|
+
import { nTriples, splitForNativeLoad } from './native-load.js';
|
|
42
46
|
|
|
43
47
|
/** Quads an operation added and removed, in the default graph. */
|
|
44
48
|
export interface RdfDiff {
|
|
@@ -64,29 +68,14 @@ export interface StoreChange extends RdfDiff {
|
|
|
64
68
|
export type StoreChangeListener = (change: StoreChange) => void;
|
|
65
69
|
|
|
66
70
|
const DEFAULT_GRAPH = DataFactory.defaultGraph();
|
|
67
|
-
/**
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const STEP = 1000;
|
|
71
|
+
/**
|
|
72
|
+
* A write of the model this large leaves the inner store behind, to be given the model whole when SPARQL asks,
|
|
73
|
+
* rather than being written into it quad by quad (KD-28).
|
|
74
|
+
*/
|
|
75
|
+
const LARGE_WRITE = 5000;
|
|
73
76
|
/** Subject and predicate of the quad a literal pattern is put in its held form through. */
|
|
74
77
|
const SCRATCH = DataFactory.namedNode('urn:la-gram:pattern');
|
|
75
78
|
|
|
76
|
-
function now(): number {
|
|
77
|
-
return typeof performance !== 'undefined' ? performance.now() : Date.now();
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/** Native document loading can reassign blank node labels. Keep those terms on the quad path. */
|
|
81
|
-
function canLoadAsTriples(term: Term): boolean {
|
|
82
|
-
if (term.termType === 'BlankNode') return false;
|
|
83
|
-
if (term.termType === 'Literal') return !term.direction;
|
|
84
|
-
if (term.termType === 'Quad') {
|
|
85
|
-
return term.graph.termType === 'DefaultGraph' && canLoadAsTriples(term.subject) && canLoadAsTriples(term.object);
|
|
86
|
-
}
|
|
87
|
-
return term.termType === 'NamedNode';
|
|
88
|
-
}
|
|
89
|
-
|
|
90
79
|
/** Whether a quad is in the default graph (the model). */
|
|
91
80
|
function inDefaultGraph(q: Quad): boolean {
|
|
92
81
|
return q.graph.termType === 'DefaultGraph';
|
|
@@ -108,10 +97,8 @@ export class ObservableRdfStore implements RdfStore {
|
|
|
108
97
|
private pending: RdfDiff | null = null;
|
|
109
98
|
/** The model graph, as the inner store holds it. */
|
|
110
99
|
private model = new N3Store();
|
|
111
|
-
/**
|
|
112
|
-
private
|
|
113
|
-
private given = 0;
|
|
114
|
-
private backlogTimer: ReturnType<typeof setTimeout> | null = null;
|
|
100
|
+
/** Whether the inner store holds an earlier state of the model, or none of it, and is to be given the copy (KD-28). */
|
|
101
|
+
private stale = false;
|
|
115
102
|
/** What stamps a local edit; none, nothing is stamped. */
|
|
116
103
|
private stamper: ((diff: RdfDiff) => void) | null = null;
|
|
117
104
|
/** The local edit not yet stamped. */
|
|
@@ -128,7 +115,7 @@ export class ObservableRdfStore implements RdfStore {
|
|
|
128
115
|
|
|
129
116
|
/** End a host-owned session, including pending WASM writes (workspace Req 1.5). */
|
|
130
117
|
dispose(): void {
|
|
131
|
-
this.
|
|
118
|
+
this.stale = false; this.listeners.clear(); this.writeListeners.clear(); this.stamper = null;
|
|
132
119
|
this.model = new N3Store();
|
|
133
120
|
(this.inner as RdfStore & { dispose?(): void }).dispose?.();
|
|
134
121
|
}
|
|
@@ -175,6 +162,24 @@ export class ObservableRdfStore implements RdfStore {
|
|
|
175
162
|
this.unstamped = { added: [], removed: [] };
|
|
176
163
|
}
|
|
177
164
|
|
|
165
|
+
/**
|
|
166
|
+
* Run `fn`, writing the model in many small steps — an import — to the copy alone: the inner store is given it
|
|
167
|
+
* when SPARQL next asks, in one native load, instead of quad by quad as it is written (KD-28). Reads, diffs and
|
|
168
|
+
* notifications are as ever.
|
|
169
|
+
*/
|
|
170
|
+
inBulk<T>(fn: () => T): T {
|
|
171
|
+
if (!this.inner.check) return fn();
|
|
172
|
+
this.bulk++;
|
|
173
|
+
try {
|
|
174
|
+
return fn();
|
|
175
|
+
} finally {
|
|
176
|
+
this.bulk--;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Open {@link inBulk} scopes. */
|
|
181
|
+
private bulk = 0;
|
|
182
|
+
|
|
178
183
|
/** Run `fn` without stamping what it changes: opening, merging, restoring — no edit of the user's. */
|
|
179
184
|
withoutStamps<T>(fn: () => T): T {
|
|
180
185
|
this.stampsOff++;
|
|
@@ -262,9 +267,10 @@ export class ObservableRdfStore implements RdfStore {
|
|
|
262
267
|
const added = this.held(quads.filter(inDefaultGraph)).filter((q) => this.model.addQuad(q as never));
|
|
263
268
|
if (added.length === 0) return;
|
|
264
269
|
try {
|
|
265
|
-
if (
|
|
266
|
-
|
|
267
|
-
this.
|
|
270
|
+
if (this.inner.check && (this.stale || this.bulk > 0 || added.length >= LARGE_WRITE)) {
|
|
271
|
+
// Refused now, as adding them would be; given to the inner store with the rest of the model when SPARQL asks.
|
|
272
|
+
this.inner.check(added);
|
|
273
|
+
this.stale = true;
|
|
268
274
|
} else {
|
|
269
275
|
this.inner.addQuads(added);
|
|
270
276
|
}
|
|
@@ -284,17 +290,19 @@ export class ObservableRdfStore implements RdfStore {
|
|
|
284
290
|
}
|
|
285
291
|
const removed = this.held(quads.filter(inDefaultGraph)).filter((q) => this.model.removeQuad(q as never));
|
|
286
292
|
if (removed.length === 0) return;
|
|
287
|
-
// One still waiting in the backlog is passed over when it comes up.
|
|
288
293
|
try {
|
|
289
|
-
|
|
294
|
+
const large = removed.length >= LARGE_WRITE || this.bulk > 0;
|
|
295
|
+
if (this.model.size === 0 && (this.stale || large) && this.inner.update) {
|
|
290
296
|
// The whole model removed — as opening another does: cleared in the
|
|
291
297
|
// inner store in one go, not converted into it quad by quad to be
|
|
292
|
-
// deleted (3.8 s for 347 000)
|
|
293
|
-
this.dropBacklog();
|
|
298
|
+
// deleted (3.8 s for 347 000); the inner store and the copy agree again.
|
|
294
299
|
this.inner.update('CLEAR SILENT DEFAULT');
|
|
295
|
-
|
|
296
|
-
|
|
300
|
+
this.stale = false;
|
|
301
|
+
} else if (!this.stale) {
|
|
302
|
+
if (large && this.inner.check) this.stale = true;
|
|
303
|
+
else this.inner.removeQuads(removed);
|
|
297
304
|
}
|
|
305
|
+
// Behind, the inner store is given the copy, without these, when SPARQL asks.
|
|
298
306
|
} catch (err) {
|
|
299
307
|
for (const q of removed) if (this.innerHas(q)) this.model.addQuad(q as never);
|
|
300
308
|
throw err;
|
|
@@ -377,9 +385,10 @@ export class ObservableRdfStore implements RdfStore {
|
|
|
377
385
|
|
|
378
386
|
/**
|
|
379
387
|
* SPARQL about the metamodels — viewpoints, relationship tables, the class
|
|
380
|
-
* hierarchy — which reads the default graph only for
|
|
381
|
-
* there. It does not
|
|
382
|
-
*
|
|
388
|
+
* hierarchy, the model questions — which reads the default graph only for
|
|
389
|
+
* metamodel data put there. It does not give the inner store the model: until
|
|
390
|
+
* SPARQL over the model asks, it may hold none of it, or an earlier state
|
|
391
|
+
* (Task 22, KD-28).
|
|
383
392
|
*/
|
|
384
393
|
queryMetamodels(sparql: string): SparqlResult {
|
|
385
394
|
return this.inner.query(sparql);
|
|
@@ -425,18 +434,27 @@ export class ObservableRdfStore implements RdfStore {
|
|
|
425
434
|
return this.inner.size ? this.inner.size() : this.inner.getQuads().length;
|
|
426
435
|
}
|
|
427
436
|
|
|
428
|
-
/** Whether the inner store is still to be given
|
|
437
|
+
/** Whether the inner store is still to be given the model (KD-28). */
|
|
429
438
|
get behind(): boolean {
|
|
430
|
-
return this.
|
|
439
|
+
return this.stale;
|
|
431
440
|
}
|
|
432
441
|
|
|
433
|
-
/**
|
|
442
|
+
/**
|
|
443
|
+
* Give the inner store the model now, if it is behind: its default graph
|
|
444
|
+
* cleared and given the copy — in one native load, the quads N-Triples
|
|
445
|
+
* cannot carry as they are (a blank node, which a bulk load renames) one by
|
|
446
|
+
* one (KD-28). Neither the copy nor anyone listening sees a change, unless the
|
|
447
|
+
* inner store refuses a quad, which then leaves the model and is told as removed.
|
|
448
|
+
*/
|
|
434
449
|
giveAll(): void {
|
|
435
|
-
if (this.
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
}
|
|
439
|
-
|
|
450
|
+
if (!this.stale) return;
|
|
451
|
+
if (this.inner.update) this.inner.update('CLEAR SILENT DEFAULT');
|
|
452
|
+
else this.inner.removeQuads(this.defaultGraph());
|
|
453
|
+
const { native, oneByOne } = splitForNativeLoad(this.model.getQuads(null, null, null, DEFAULT_GRAPH) as unknown as Quad[]);
|
|
454
|
+
this.stale = false;
|
|
455
|
+
// Inserting a statement already loaded is safe: a native load that fails part-way is finished one by one.
|
|
456
|
+
if (!this.giveNatively(native)) for (const q of native) oneByOne.push(q);
|
|
457
|
+
this.giveOneByOne(oneByOne);
|
|
440
458
|
}
|
|
441
459
|
|
|
442
460
|
// ── Internals ──────────────────────────────────────────────────────────
|
|
@@ -451,84 +469,44 @@ export class ObservableRdfStore implements RdfStore {
|
|
|
451
469
|
return this.inner.canonical(quads);
|
|
452
470
|
}
|
|
453
471
|
|
|
454
|
-
/**
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
this.
|
|
461
|
-
this.given = 0;
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
/** The inner store is to have these model quads, in the background. */
|
|
465
|
-
private giveLater(quads: Quad[]): void {
|
|
466
|
-
this.backlog = this.behind ? this.backlog.slice(this.given).concat(quads) : quads;
|
|
467
|
-
this.given = 0;
|
|
468
|
-
this.scheduleGiving();
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
private scheduleGiving(): void {
|
|
472
|
-
if (this.backlogTimer || !this.behind) return;
|
|
473
|
-
this.backlogTimer = setTimeout(() => {
|
|
474
|
-
this.backlogTimer = null;
|
|
475
|
-
this.give(SLICE_MS);
|
|
476
|
-
this.scheduleGiving();
|
|
477
|
-
}, 0);
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
/** Give the inner store backlogged quads the model still has, for about `budgetMs`. */
|
|
481
|
-
private give(budgetMs: number): void {
|
|
482
|
-
const started = now();
|
|
483
|
-
while (this.behind) {
|
|
484
|
-
const end = Math.min(this.backlog.length, this.given + STEP);
|
|
485
|
-
const step: Quad[] = [];
|
|
486
|
-
for (let i = this.given; i < end; i++) if (this.model.has(this.backlog[i] as never)) step.push(this.backlog[i]);
|
|
487
|
-
this.given = end;
|
|
488
|
-
this.giveStep(step);
|
|
489
|
-
if (now() - started >= budgetMs) break;
|
|
490
|
-
}
|
|
491
|
-
if (!this.behind) {
|
|
492
|
-
this.backlog = [];
|
|
493
|
-
this.given = 0;
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
/** Finish a large checked write in one native insertion, keeping the model copy and its order. */
|
|
498
|
-
private giveAsTriples(): boolean {
|
|
499
|
-
if (!this.inner.load || this.backlog.length - this.given < IN_BACKGROUND_FROM) return false;
|
|
500
|
-
const quads = this.backlog.slice(this.given).filter(quad => this.model.has(quad as never));
|
|
501
|
-
if (quads.some(({ subject, object }) => !canLoadAsTriples(subject) || !canLoadAsTriples(object))) return false;
|
|
472
|
+
/**
|
|
473
|
+
* Model quads into the inner store in one native load of N-Triples — 26 times faster than quad by quad, and a
|
|
474
|
+
* third less WebAssembly memory (KD-28). Whether it took them: a failure, even part-way, is for the quad path.
|
|
475
|
+
*/
|
|
476
|
+
private giveNatively(quads: Quad[]): boolean {
|
|
477
|
+
if (quads.length === 0) return true;
|
|
478
|
+
if (!this.inner.load || quads.length < LARGE_WRITE) return false;
|
|
502
479
|
try {
|
|
503
|
-
|
|
504
|
-
|
|
480
|
+
this.inner.load(nTriples(quads), { format: 'application/n-triples' });
|
|
481
|
+
return true;
|
|
505
482
|
} catch {
|
|
506
|
-
// Replay through giveStep on a native-load failure. Inserting an already loaded statement is safe.
|
|
507
483
|
return false;
|
|
508
484
|
}
|
|
509
|
-
this.dropBacklog();
|
|
510
|
-
return true;
|
|
511
485
|
}
|
|
512
486
|
|
|
513
487
|
/**
|
|
514
|
-
*
|
|
515
|
-
* added; should it refuse one even so, that quad leaves the
|
|
516
|
-
* told to listeners as a removal — rather than the copy and
|
|
488
|
+
* Model quads into the inner store one step at a time. Their terms were
|
|
489
|
+
* checked when added; should it refuse one even so, that quad leaves the
|
|
490
|
+
* model — said, and told to listeners as a removal — rather than the copy and
|
|
491
|
+
* the store differ.
|
|
517
492
|
*/
|
|
518
|
-
private
|
|
519
|
-
try {
|
|
520
|
-
this.inner.addQuads(step);
|
|
521
|
-
return;
|
|
522
|
-
} catch {
|
|
523
|
-
// One at a time, to find the one refused.
|
|
524
|
-
}
|
|
493
|
+
private giveOneByOne(quads: Quad[]): void {
|
|
525
494
|
const refused: Quad[] = [];
|
|
526
|
-
for (
|
|
495
|
+
for (let at = 0; at < quads.length; at += 1000) {
|
|
496
|
+
const step = quads.slice(at, at + 1000);
|
|
527
497
|
try {
|
|
528
|
-
this.inner.addQuads(
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
498
|
+
this.inner.addQuads(step);
|
|
499
|
+
continue;
|
|
500
|
+
} catch {
|
|
501
|
+
// One at a time, to find the one refused.
|
|
502
|
+
}
|
|
503
|
+
for (const q of step) {
|
|
504
|
+
try {
|
|
505
|
+
this.inner.addQuads([q]);
|
|
506
|
+
} catch (err) {
|
|
507
|
+
console.warn('[la-gram] The store refused a quad of the model', err);
|
|
508
|
+
if (this.model.removeQuad(q as never)) refused.push(q);
|
|
509
|
+
}
|
|
532
510
|
}
|
|
533
511
|
}
|
|
534
512
|
this.track([], refused);
|
|
@@ -257,6 +257,9 @@ export function isOxigraphReady(): boolean {
|
|
|
257
257
|
|
|
258
258
|
// ── OxigraphRdfStore ───────────────────────────────────────────────────
|
|
259
259
|
|
|
260
|
+
/** Past this many terms remembered, `check` forgets them and starts again: a bound on what it keeps. */
|
|
261
|
+
const ACCEPTED_LIMIT = 1_000_000;
|
|
262
|
+
|
|
260
263
|
/**
|
|
261
264
|
* RdfStore implementation backed by Oxigraph WASM.
|
|
262
265
|
*
|
|
@@ -268,6 +271,8 @@ export function isOxigraphReady(): boolean {
|
|
|
268
271
|
*/
|
|
269
272
|
export class OxigraphRdfStore implements RdfStore {
|
|
270
273
|
private store: oxigraph.Store;
|
|
274
|
+
/** The terms `check` made, by IRI, `_:` and a blank node's label, or `@` and a language tag. */
|
|
275
|
+
private readonly accepted = new Set<string>();
|
|
271
276
|
|
|
272
277
|
constructor() {
|
|
273
278
|
this.store = new oxigraph.Store();
|
|
@@ -367,6 +372,9 @@ export class OxigraphRdfStore implements RdfStore {
|
|
|
367
372
|
* Measured on the ArchiMate 3.2 relationship shapes (72 740 quads): 110 ms
|
|
368
373
|
* here against 1.4 s through {@link addQuads}, whose cost is the per-quad term
|
|
369
374
|
* conversion across the WASM boundary rather than the parse.
|
|
375
|
+
*
|
|
376
|
+
* Without a transaction (KD-31): in one, the metamodels took 25 s to load in
|
|
377
|
+
* the conformance test instead of 3 s.
|
|
370
378
|
*/
|
|
371
379
|
load(data: string, options: { format: string; graph?: string; baseIri?: string }): number {
|
|
372
380
|
const before = this.store.size;
|
|
@@ -386,10 +394,14 @@ export class OxigraphRdfStore implements RdfStore {
|
|
|
386
394
|
/**
|
|
387
395
|
* Throw as adding these quads would — an IRI that is not one, a language tag
|
|
388
396
|
* or blank node label Oxigraph refuses, a term out of place — without adding
|
|
389
|
-
* them: each distinct term is made once with Oxigraph's own constructors
|
|
397
|
+
* them: each distinct term is made once with Oxigraph's own constructors, and
|
|
398
|
+
* remembered, since a term Oxigraph accepted once it accepts always (KD-28).
|
|
399
|
+
* An exchange import checks 137 000 writes of a few quads each, mostly of the
|
|
400
|
+
* same terms: made again for each, they took 0.75 s of its time on the UI thread.
|
|
390
401
|
*/
|
|
391
402
|
check(quads: Quad[]): void {
|
|
392
|
-
|
|
403
|
+
if (this.accepted.size > ACCEPTED_LIMIT) this.accepted.clear();
|
|
404
|
+
const made = this.accepted;
|
|
393
405
|
const make = (term: Term): void => {
|
|
394
406
|
switch (term.termType) {
|
|
395
407
|
case 'NamedNode':
|
|
@@ -80,6 +80,10 @@ export interface SparqlSelect {
|
|
|
80
80
|
select(query: string): Array<Map<string, Term>>;
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
+
function now(): number {
|
|
84
|
+
return typeof performance !== 'undefined' ? performance.now() : Date.now();
|
|
85
|
+
}
|
|
86
|
+
|
|
83
87
|
/** The SPARQL-based constraints of a shapes graph, and those that cannot be run. */
|
|
84
88
|
export function sparqlConstraintsOf(shapes: readonly Quad[]): { constraints: SparqlConstraint[]; skipped: SkippedConstraint[] } {
|
|
85
89
|
const bySubject = new Map<string, Quad[]>();
|
|
@@ -178,6 +182,13 @@ export class SparqlRunner {
|
|
|
178
182
|
return this.failed;
|
|
179
183
|
}
|
|
180
184
|
|
|
185
|
+
/** How long each constraint the last {@link run} ran took, in ms, in the order run (KD-30). */
|
|
186
|
+
get lastTimings(): ReadonlyArray<{ shape: string; ms: number }> {
|
|
187
|
+
return this.timings;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
private timings: Array<{ shape: string; ms: number }> = [];
|
|
191
|
+
|
|
181
192
|
/**
|
|
182
193
|
* The results for every target of every constraint — or, given `focus`, for
|
|
183
194
|
* those of them only. `isTarget`, when given, says which of them a
|
|
@@ -186,6 +197,7 @@ export class SparqlRunner {
|
|
|
186
197
|
* a few nodes as for all — a `MINUS` is evaluated over the whole model.
|
|
187
198
|
*/
|
|
188
199
|
run(focus?: readonly Term[], isTarget?: (targets: ShapeTargets, node: Term) => boolean): ValidationResult[] {
|
|
200
|
+
this.timings = [];
|
|
189
201
|
if (focus && focus.length === 0) return [];
|
|
190
202
|
const every = focus ? focus.map(termText).join(' ') : undefined;
|
|
191
203
|
const results: ValidationResult[] = [];
|
|
@@ -200,11 +212,14 @@ export class SparqlRunner {
|
|
|
200
212
|
}
|
|
201
213
|
const query = withTargets(constraint.select, targetPattern(constraint.targets, values));
|
|
202
214
|
let rows: Array<Map<string, Term>>;
|
|
215
|
+
const started = now();
|
|
203
216
|
try {
|
|
204
217
|
rows = this.store.select(query);
|
|
205
218
|
} catch (err: unknown) {
|
|
206
219
|
this.failed.set(key, err instanceof Error ? err.message : String(err));
|
|
207
220
|
continue;
|
|
221
|
+
} finally {
|
|
222
|
+
this.timings.push({ shape: constraint.shape, ms: now() - started });
|
|
208
223
|
}
|
|
209
224
|
for (const row of rows) {
|
|
210
225
|
const focusNode = row.get('this');
|