@linked.archi/kernel 0.1.5 → 0.1.7
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 +8 -0
- package/assets/archimate3/archimate3.2-metamodel.ttl +18 -1
- 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-metamodel.ttl +18 -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 +776 -464
- package/assets/core/core-onto.ttl +12 -9
- package/assets/core/core-shapes.ttl +1 -1
- package/assets/core/core-vis-onto.ttl +99 -5
- package/assets/core/core-vis-shapes.ttl +275 -11
- package/package.json +5 -2
- package/src/model-manager/appearance.ts +131 -7
- package/src/model-manager/cascade.ts +5 -3
- package/src/model-manager/clipboard.ts +10 -2
- package/src/model-manager/concept-styles.ts +230 -0
- package/src/model-manager/interfaces.ts +19 -1
- package/src/model-manager/iri-conformance.ts +9 -2
- package/src/model-manager/placement-copy.ts +35 -8
- package/src/model-manager/questions.ts +6 -1
- package/src/model-manager/style-sets.ts +399 -0
- package/src/model-manager/style-values.ts +349 -0
- package/src/model-manager/view-manager.ts +74 -22
- package/src/model-manager/vocabulary.ts +10 -0
- package/src/rdf-engine/iri.ts +6 -3
- 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/turtle-writer.ts +1 -0
- package/src/rdf-engine/validation-engine.ts +97 -32
- package/src/rdf-engine/validation-service.ts +3 -1
|
@@ -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');
|
|
@@ -23,13 +23,16 @@
|
|
|
23
23
|
*
|
|
24
24
|
* The Oxigraph copy is made only when the shapes have SPARQL-based
|
|
25
25
|
* constraints, and only where Oxigraph is ready (its WebAssembly loaded in the
|
|
26
|
-
* worker): about 0.7 KB a quad (78 MB at 100 000 triples, measured in Node).
|
|
26
|
+
* worker): about 0.7 KB a quad (78 MB at 100 000 triples, measured in Node). It
|
|
27
|
+
* is built in one native load, and built again rather than followed for a
|
|
28
|
+
* change too large to validate incrementally — a model opened (KD-29).
|
|
27
29
|
*/
|
|
28
30
|
import { DataFactory, Store as N3Store, termFromId, termToId } from 'n3';
|
|
29
31
|
import * as oxigraph from 'oxigraph';
|
|
30
32
|
import type { Quad, Term } from '@rdfjs/types';
|
|
31
33
|
import { RdfShaclValidator } from './shacl-validator.js';
|
|
32
34
|
import { isOxigraphReady, toOxQuad } from './rdf-store.js';
|
|
35
|
+
import { nTriples, splitForNativeLoad } from './native-load.js';
|
|
33
36
|
import { SparqlRunner, predicatesNamedBy, sparqlConstraintsOf, type ShapeTargets } from './sparql-constraints.js';
|
|
34
37
|
import { REFERENCE_PREDICATES, missingReferences } from '../model-manager/missing-references.js';
|
|
35
38
|
import { predicatesRead, shapesReach, type ShapesReach } from './shape-reach.js';
|
|
@@ -91,9 +94,23 @@ export interface PlainReport {
|
|
|
91
94
|
* the references; and how many nodes were validated again — `all`, or after
|
|
92
95
|
* an edit only those it can change.
|
|
93
96
|
*/
|
|
94
|
-
timing: {
|
|
97
|
+
timing: {
|
|
98
|
+
shacl: number;
|
|
99
|
+
tables: number;
|
|
100
|
+
sparql?: number;
|
|
101
|
+
references?: number;
|
|
102
|
+
nodes: number | 'all';
|
|
103
|
+
/** The SPARQL-based constraints that took {@link SLOW_RULE_MS} or more, by shape, slowest first (KD-30). */
|
|
104
|
+
slowRules?: Array<{ shape: string; ms: number }>;
|
|
105
|
+
};
|
|
95
106
|
}
|
|
96
107
|
|
|
108
|
+
/**
|
|
109
|
+
* A SPARQL-based constraint this slow, in ms, is named in the report's timing (KD-30): two published rules once took
|
|
110
|
+
* 111 of a validation's 120 s, and the report said only "SPARQL-based rules 110 901 ms".
|
|
111
|
+
*/
|
|
112
|
+
export const SLOW_RULE_MS = 500;
|
|
113
|
+
|
|
97
114
|
/** After this many validations of what edits changed, the whole model is validated again. */
|
|
98
115
|
const FULL_EVERY = 100;
|
|
99
116
|
/** When an edit can change the findings of more nodes than one in this many of the model's quads, the whole model is validated. */
|
|
@@ -253,24 +270,47 @@ export class ValidationEngine {
|
|
|
253
270
|
this.changed.all = true;
|
|
254
271
|
}
|
|
255
272
|
|
|
256
|
-
/**
|
|
273
|
+
/**
|
|
274
|
+
* Apply a change to the model. One past the incremental limit — a model opened, or imported, is one — would have
|
|
275
|
+
* the whole model validated anyway, and is applied as a reload (KD-29): the copy and the references as ever, but
|
|
276
|
+
* nothing noted of what changed, and the Oxigraph copy built again at the end in one native load rather than
|
|
277
|
+
* followed quad by quad (55 s and 570 MiB for 365 000 triples, against 2.1 s and 402 MiB, in Node).
|
|
278
|
+
*/
|
|
257
279
|
apply(added: readonly EncodedQuad[], removed: readonly EncodedQuad[]): void {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
this.refer(q, -1);
|
|
263
|
-
this.touch(q);
|
|
280
|
+
const reload = added.length + removed.length > this.limit;
|
|
281
|
+
if (reload) {
|
|
282
|
+
this.changed.all = true;
|
|
283
|
+
this.rebuilding = true;
|
|
264
284
|
}
|
|
265
|
-
|
|
266
|
-
const
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
285
|
+
try {
|
|
286
|
+
for (const q of decodeQuads(removed)) {
|
|
287
|
+
const k = extraKey(q);
|
|
288
|
+
if (k !== undefined && this.extras.has(k)) this.alsoModel.delete(k);
|
|
289
|
+
else this.drop(q);
|
|
290
|
+
this.refer(q, -1);
|
|
291
|
+
this.touch(q);
|
|
292
|
+
}
|
|
293
|
+
for (const q of decodeQuads(added)) {
|
|
294
|
+
const k = extraKey(q);
|
|
295
|
+
if (k !== undefined && this.extras.has(k)) this.alsoModel.add(k);
|
|
296
|
+
else this.put(q);
|
|
297
|
+
this.refer(q, 1);
|
|
298
|
+
this.touch(q);
|
|
299
|
+
}
|
|
300
|
+
} finally {
|
|
301
|
+
if (reload) {
|
|
302
|
+
this.rebuilding = false;
|
|
303
|
+
this.rebuildSparqlData();
|
|
304
|
+
}
|
|
271
305
|
}
|
|
272
306
|
}
|
|
273
307
|
|
|
308
|
+
/** Free the Oxigraph copy: WebAssembly memory is not collected with the engine. */
|
|
309
|
+
dispose(): void {
|
|
310
|
+
(this.sparqlData as { free?(): void } | undefined)?.free?.();
|
|
311
|
+
this.sparqlData = undefined;
|
|
312
|
+
}
|
|
313
|
+
|
|
274
314
|
/** How many quads the model has. */
|
|
275
315
|
get size(): number {
|
|
276
316
|
return this.data.size - this.extras.size + this.alsoModel.size;
|
|
@@ -311,7 +351,16 @@ export class ValidationEngine {
|
|
|
311
351
|
const t4 = now();
|
|
312
352
|
this.findings = { shacl: byFocus(shacl), tables: byFocus(byTables), sparql: byFocus(sparql), references: byFocus(references) };
|
|
313
353
|
this.sinceWhole = 0;
|
|
314
|
-
return { shacl: Math.round(t1 - t0), tables: Math.round(t2 - t1), sparql: Math.round(t3 - t2), references: Math.round(t4 - t3), nodes: 'all' };
|
|
354
|
+
return { shacl: Math.round(t1 - t0), tables: Math.round(t2 - t1), sparql: Math.round(t3 - t2), references: Math.round(t4 - t3), nodes: 'all', ...this.slowRules() };
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/** The SPARQL-based constraints of the last run that took {@link SLOW_RULE_MS} or more, slowest first — if any. */
|
|
358
|
+
private slowRules(): { slowRules?: Array<{ shape: string; ms: number }> } {
|
|
359
|
+
const slow = (this.sparqlRunner?.lastTimings ?? [])
|
|
360
|
+
.filter((t) => t.ms >= SLOW_RULE_MS)
|
|
361
|
+
.sort((a, b) => b.ms - a.ms)
|
|
362
|
+
.map((t) => ({ shape: t.shape, ms: Math.round(t.ms) }));
|
|
363
|
+
return slow.length > 0 ? { slowRules: slow } : {};
|
|
315
364
|
}
|
|
316
365
|
|
|
317
366
|
private validateNodes(nodes: Map<string, Term>, sparqlNodes?: Map<string, Term>): PlainReport['timing'] {
|
|
@@ -344,7 +393,7 @@ export class ValidationEngine {
|
|
|
344
393
|
for (const [key, results] of byFocus(sparql)) findings.sparql.set(key, results);
|
|
345
394
|
for (const [key, results] of byFocus(references)) findings.references.set(key, results);
|
|
346
395
|
this.sinceWhole++;
|
|
347
|
-
return { shacl: Math.round(t1 - t0), tables: Math.round(t2 - t1), sparql: Math.round(t3 - t2), references: Math.round(t4 - t3), nodes: nodes.size };
|
|
396
|
+
return { shacl: Math.round(t1 - t0), tables: Math.round(t2 - t1), sparql: Math.round(t3 - t2), references: Math.round(t4 - t3), nodes: nodes.size, ...this.slowRules() };
|
|
348
397
|
}
|
|
349
398
|
|
|
350
399
|
/** The report from the findings kept — none about a resource the model only refers to. */
|
|
@@ -590,31 +639,47 @@ export class ValidationEngine {
|
|
|
590
639
|
return pick?.object.value;
|
|
591
640
|
}
|
|
592
641
|
|
|
593
|
-
/** A quad into the data — and its Oxigraph copy. */
|
|
642
|
+
/** A quad into the data — and its Oxigraph copy, unless that is to be built again. */
|
|
594
643
|
private put(q: Quad): void {
|
|
595
644
|
this.data.addQuad(q as never);
|
|
596
|
-
this.sparqlData?.add(toOxQuad(q));
|
|
645
|
+
if (!this.rebuilding) this.sparqlData?.add(toOxQuad(q));
|
|
597
646
|
}
|
|
598
647
|
|
|
599
|
-
/** A quad out of the data — and its Oxigraph copy. */
|
|
648
|
+
/** A quad out of the data — and its Oxigraph copy, unless that is to be built again. */
|
|
600
649
|
private drop(q: Quad): void {
|
|
601
650
|
this.data.removeQuad(q as never);
|
|
602
|
-
this.sparqlData?.delete(toOxQuad(q));
|
|
651
|
+
if (!this.rebuilding) this.sparqlData?.delete(toOxQuad(q));
|
|
603
652
|
}
|
|
604
653
|
|
|
605
|
-
/**
|
|
654
|
+
/** A change being applied as a reload: the Oxigraph copy is built again after it. */
|
|
655
|
+
private rebuilding = false;
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* The Oxigraph copy made again from the data and the context — or none, without SPARQL-based constraints to run:
|
|
659
|
+
* in one native load of what N-Triples carries as it is, the rest one by one (KD-29). The copy it replaces is
|
|
660
|
+
* freed, so that its WebAssembly memory is used again rather than grown past.
|
|
661
|
+
*/
|
|
606
662
|
private rebuildSparqlData(): void {
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
return;
|
|
610
|
-
}
|
|
663
|
+
this.dispose();
|
|
664
|
+
if (!this.sparqlRunner) return;
|
|
611
665
|
const store = new oxigraph.Store();
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
666
|
+
const give = (quads: Iterable<Quad>, graph?: oxigraph.NamedNode) => {
|
|
667
|
+
const { native, oneByOne } = splitForNativeLoad(quads);
|
|
668
|
+
try {
|
|
669
|
+
if (native.length > 0) {
|
|
670
|
+
store.load(nTriples(native), { format: 'application/n-triples', no_transaction: true, ...(graph ? { to_graph_name: graph } : {}) });
|
|
671
|
+
}
|
|
672
|
+
} catch {
|
|
673
|
+
// Statements already loaded are added again harmlessly; one at a time, past the argument limit (KReq 1.6).
|
|
674
|
+
for (const q of native) oneByOne.push(q);
|
|
675
|
+
}
|
|
676
|
+
for (const q of oneByOne) {
|
|
677
|
+
const ox = toOxQuad(q);
|
|
678
|
+
store.add(graph ? oxigraph.quad(ox.subject, ox.predicate, ox.object, graph) : ox);
|
|
679
|
+
}
|
|
680
|
+
};
|
|
681
|
+
give(this.data as unknown as Iterable<Quad>);
|
|
682
|
+
give(this.sparqlContext, oxigraph.namedNode(CONTEXT_GRAPH));
|
|
618
683
|
this.sparqlData = store;
|
|
619
684
|
}
|
|
620
685
|
}
|