@sigloch/graph-api-core 5.10.3 → 5.11.1
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/dist/browser.d.ts +2 -0
- package/dist/browser.js +1 -0
- package/dist/graph-delta.d.ts +53 -0
- package/dist/graph-delta.js +155 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/kuzu/graph-cypher-engine.js +8 -1
- package/package.json +1 -1
package/dist/browser.d.ts
CHANGED
|
@@ -17,6 +17,8 @@ export { impactSlice } from './impact-slice.js';
|
|
|
17
17
|
export type { ImpactSlice, ImpactSliceNode, ImpactRole } from './impact-slice.js';
|
|
18
18
|
export { SE_DESCRIPTOR, createSeDescriptor, projectToOntologyGraph, fromOntologyGraph } from './se-descriptor.js';
|
|
19
19
|
export { FormatECodec } from './format-e-codec.js';
|
|
20
|
+
export { applyDelta, deltaToFormatE, edgeToTrace, formatEToDelta, formatEToExportedGraph, graphToFormatE, nodeToElement, toExportedGraph } from './graph-delta.js';
|
|
21
|
+
export type { ExportedElement, ExportedGraph, ExportedTrace, GraphDelta } from './graph-delta.js';
|
|
20
22
|
export { isValidTrace, tracePatternsOf } from './types.js';
|
|
21
23
|
export type { GraphNode, GraphEdge, Graph } from './types.js';
|
|
22
24
|
export type { OntologyDescriptor, NodeTypeDescriptor, EdgeTypeDescriptor, TracePattern } from './types.js';
|
package/dist/browser.js
CHANGED
|
@@ -15,4 +15,5 @@ export { findRoot } from './find-root.js';
|
|
|
15
15
|
export { impactSlice } from './impact-slice.js';
|
|
16
16
|
export { SE_DESCRIPTOR, createSeDescriptor, projectToOntologyGraph, fromOntologyGraph } from './se-descriptor.js';
|
|
17
17
|
export { FormatECodec } from './format-e-codec.js';
|
|
18
|
+
export { applyDelta, deltaToFormatE, edgeToTrace, formatEToDelta, formatEToExportedGraph, graphToFormatE, nodeToElement, toExportedGraph } from './graph-delta.js';
|
|
18
19
|
export { isValidTrace, tracePatternsOf } from './types.js';
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Graph, GraphEdge, GraphNode } from './types.js';
|
|
2
|
+
/** Die Wirkung eines Batches auf den Store (CR-GC-503): Kaskaden und Merges stehen drin. */
|
|
3
|
+
export interface GraphDelta {
|
|
4
|
+
upsertNodes: GraphNode[];
|
|
5
|
+
deleteNodes: string[];
|
|
6
|
+
upsertEdges: GraphEdge[];
|
|
7
|
+
deleteEdges: Array<{
|
|
8
|
+
sourceId: string;
|
|
9
|
+
targetId: string;
|
|
10
|
+
edgeType: string;
|
|
11
|
+
}>;
|
|
12
|
+
}
|
|
13
|
+
export type ExportedElement = Record<string, unknown> & {
|
|
14
|
+
id: string;
|
|
15
|
+
type: string;
|
|
16
|
+
};
|
|
17
|
+
export type ExportedTrace = Record<string, unknown> & {
|
|
18
|
+
source: string;
|
|
19
|
+
target: string;
|
|
20
|
+
type: string;
|
|
21
|
+
};
|
|
22
|
+
/** Die Exportform: `docs/graph/*.graph.json` und der Viewer lesen beide diese Felder. */
|
|
23
|
+
export interface ExportedGraph {
|
|
24
|
+
elements: ExportedElement[];
|
|
25
|
+
traces: ExportedTrace[];
|
|
26
|
+
}
|
|
27
|
+
type ResolveType = (uid: string) => string | undefined;
|
|
28
|
+
/** Element der Exportform: `id` zuerst, Attribute flach in ihrer Einfuegereihenfolge. */
|
|
29
|
+
export declare function nodeToElement(node: GraphNode): ExportedElement;
|
|
30
|
+
export declare function edgeToTrace(edge: GraphEdge): ExportedTrace;
|
|
31
|
+
/**
|
|
32
|
+
* Graph → Exportform, deterministisch: Elemente nach uid, Kanten nach (source, type, target). Doppelte
|
|
33
|
+
* Kanten fallen weg, der Schluessel ist derselbe wie im Store.
|
|
34
|
+
*/
|
|
35
|
+
export declare function toExportedGraph(graph: Graph): ExportedGraph;
|
|
36
|
+
/** Der ganze Graph als Format-E, ohne Verlust von Name, Zeitstempel und leerer Beschreibung. */
|
|
37
|
+
export declare function graphToFormatE(graph: Graph): string;
|
|
38
|
+
/**
|
|
39
|
+
* Die Wirkung als Format-E. `typeOf` liefert den Typ geloeschter Knoten aus dem Stand VOR dem Batch — ein
|
|
40
|
+
* Knoten steht in der Sprache immer unter seinem Abschnitt. Ohne Typ wirft die Funktion, statt zu raten.
|
|
41
|
+
*/
|
|
42
|
+
export declare function deltaToFormatE(delta: GraphDelta, typeOf: ResolveType): string;
|
|
43
|
+
/** Format-E → Wirkung. `resolveType` typisiert Endpunkte, die der Text nicht selbst deklariert. Wirft bei Parse-Fehlern. */
|
|
44
|
+
export declare function formatEToDelta(text: string, resolveType?: ResolveType): GraphDelta;
|
|
45
|
+
/** Format-E des ganzen Graphen → Exportform. Der Text deklariert jeden Endpunkt selbst. */
|
|
46
|
+
export declare function formatEToExportedGraph(text: string): ExportedGraph;
|
|
47
|
+
/**
|
|
48
|
+
* Spielt die Wirkung in die Exportform ein; das Ergebnis gleicht dem Export danach. Reihenfolge wie im
|
|
49
|
+
* Store: erst schreiben, dann loeschen. Unberuehrte Eintraege behalten ihr Objekt und eine unberuehrte
|
|
50
|
+
* Liste ihre Identitaet — darauf baut das gezielte Neuzeichnen im Viewer.
|
|
51
|
+
*/
|
|
52
|
+
export declare function applyDelta(view: ExportedGraph, delta: GraphDelta): ExportedGraph;
|
|
53
|
+
export {};
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* graph-delta.ts — die WIRKUNG eines Mutations-Batches, als Format-E zwischen den Modulen (CR-SM-427).
|
|
3
|
+
*
|
|
4
|
+
* `GraphDelta` nennt, was sich am Store geaendert hat: Kaskaden beim Loeschen und Merges stehen drin, der
|
|
5
|
+
* eingereichte Batch steht nicht drin. Der Host schreibt es mit `deltaToFormatE`, der Viewer liest es mit
|
|
6
|
+
* `formatEToDelta` und spielt es mit `applyDelta` ein. Dieselbe Datei traegt die Exportform
|
|
7
|
+
* `{ elements, traces }`: Datei und Viewer rechnen so denselben Stand, kein Modul baut die Abbildung nach.
|
|
8
|
+
*
|
|
9
|
+
* Browser-tauglich: keine `node:`-Module.
|
|
10
|
+
*
|
|
11
|
+
* @author andreas@siglochconsulting
|
|
12
|
+
*/
|
|
13
|
+
import { SE_FORMAT_E_CODEC } from './format-e-door.js';
|
|
14
|
+
/** Element der Exportform: `id` zuerst, Attribute flach in ihrer Einfuegereihenfolge. */
|
|
15
|
+
export function nodeToElement(node) {
|
|
16
|
+
return { id: node.uid, type: node.type, name: node.name, description: node.description ?? '', ...node.attributes };
|
|
17
|
+
}
|
|
18
|
+
export function edgeToTrace(edge) {
|
|
19
|
+
return { source: edge.sourceId, target: edge.targetId, type: edge.edgeType, ...edge.attributes };
|
|
20
|
+
}
|
|
21
|
+
const byId = (a, b) => a.id.localeCompare(b.id);
|
|
22
|
+
const byTrace = (a, b) => a.source.localeCompare(b.source) || a.type.localeCompare(b.type) || a.target.localeCompare(b.target);
|
|
23
|
+
const traceKey = (t) => `${t.source}|${t.type}|${t.target}`;
|
|
24
|
+
/**
|
|
25
|
+
* Graph → Exportform, deterministisch: Elemente nach uid, Kanten nach (source, type, target). Doppelte
|
|
26
|
+
* Kanten fallen weg, der Schluessel ist derselbe wie im Store.
|
|
27
|
+
*/
|
|
28
|
+
export function toExportedGraph(graph) {
|
|
29
|
+
const seen = new Set();
|
|
30
|
+
const traces = graph.edges.map(edgeToTrace).sort(byTrace).filter((t) => {
|
|
31
|
+
const key = traceKey(t);
|
|
32
|
+
if (seen.has(key))
|
|
33
|
+
return false;
|
|
34
|
+
seen.add(key);
|
|
35
|
+
return true;
|
|
36
|
+
});
|
|
37
|
+
return { elements: graph.nodes.map(nodeToElement).sort(byId), traces };
|
|
38
|
+
}
|
|
39
|
+
/** Der ganze Graph als Format-E, ohne Verlust von Name, Zeitstempel und leerer Beschreibung. */
|
|
40
|
+
export function graphToFormatE(graph) {
|
|
41
|
+
return SE_FORMAT_E_CODEC.serialize(graph, { roundTrip: true });
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Die Wirkung als Format-E. `typeOf` liefert den Typ geloeschter Knoten aus dem Stand VOR dem Batch — ein
|
|
45
|
+
* Knoten steht in der Sprache immer unter seinem Abschnitt. Ohne Typ wirft die Funktion, statt zu raten.
|
|
46
|
+
*/
|
|
47
|
+
export function deltaToFormatE(delta, typeOf) {
|
|
48
|
+
const teile = [];
|
|
49
|
+
const knoten = delta.upsertNodes.length > 0
|
|
50
|
+
? [SE_FORMAT_E_CODEC.serialize({ nodes: delta.upsertNodes, edges: [] }, { roundTrip: true }).trim()]
|
|
51
|
+
: delta.deleteNodes.length > 0 ? ['## Nodes'] : [];
|
|
52
|
+
for (const uid of delta.deleteNodes) {
|
|
53
|
+
const type = typeOf(uid);
|
|
54
|
+
if (!type)
|
|
55
|
+
throw new Error(`deltaToFormatE: Typ von "${uid}" unbekannt`);
|
|
56
|
+
knoten.push(`### ${type}`, `- ${uid}`);
|
|
57
|
+
}
|
|
58
|
+
if (knoten.length > 0)
|
|
59
|
+
teile.push(knoten.join('\n'));
|
|
60
|
+
// OHNE roundTrip: dessen Pruefung verlangt jeden Endpunkt als Knoten im selben Text.
|
|
61
|
+
const anlegen = delta.upsertEdges.length > 0
|
|
62
|
+
? SE_FORMAT_E_CODEC.serialize({ nodes: [], edges: delta.upsertEdges }).trim().split('\n').slice(1)
|
|
63
|
+
: [];
|
|
64
|
+
const loeschen = delta.deleteEdges.map((e) => `- ${e.sourceId} -${e.edgeType}-> ${e.targetId}`);
|
|
65
|
+
if (anlegen.length + loeschen.length > 0)
|
|
66
|
+
teile.push(['## Edges', ...anlegen, ...loeschen].join('\n'));
|
|
67
|
+
return teile.join('\n\n');
|
|
68
|
+
}
|
|
69
|
+
function knotenAus(uid, type, description, raw) {
|
|
70
|
+
const { __name, __createdAt, __updatedAt, ...attributes } = raw;
|
|
71
|
+
return {
|
|
72
|
+
uid,
|
|
73
|
+
type,
|
|
74
|
+
name: __name ?? uid,
|
|
75
|
+
description: description ?? '',
|
|
76
|
+
attributes,
|
|
77
|
+
...(__createdAt !== undefined ? { createdAt: __createdAt } : {}),
|
|
78
|
+
...(__updatedAt !== undefined ? { updatedAt: __updatedAt } : {}),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/** Format-E → Wirkung. `resolveType` typisiert Endpunkte, die der Text nicht selbst deklariert. Wirft bei Parse-Fehlern. */
|
|
82
|
+
export function formatEToDelta(text, resolveType = () => undefined) {
|
|
83
|
+
const { operations, errors } = SE_FORMAT_E_CODEC.parse(text, { resolveType });
|
|
84
|
+
if (errors.length > 0)
|
|
85
|
+
throw new Error(`formatEToDelta: ${errors.join('; ')}`);
|
|
86
|
+
const delta = { upsertNodes: [], deleteNodes: [], upsertEdges: [], deleteEdges: [] };
|
|
87
|
+
for (const op of operations) {
|
|
88
|
+
if (op.type === 'add_node') {
|
|
89
|
+
delta.upsertNodes.push(knotenAus(op.semanticId, op.elementType, op.description, op.attributes ?? {}));
|
|
90
|
+
}
|
|
91
|
+
else if (op.type === 'remove_node') {
|
|
92
|
+
delta.deleteNodes.push(op.semanticId);
|
|
93
|
+
}
|
|
94
|
+
else if (op.type === 'add_edge') {
|
|
95
|
+
delta.upsertEdges.push({ sourceId: op.sourceId, targetId: op.targetId, edgeType: op.edgeType, attributes: op.attributes ?? {} });
|
|
96
|
+
}
|
|
97
|
+
else if (op.type === 'remove_edge') {
|
|
98
|
+
delta.deleteEdges.push({ sourceId: op.sourceId, targetId: op.targetId, edgeType: op.edgeType });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return delta;
|
|
102
|
+
}
|
|
103
|
+
/** Format-E des ganzen Graphen → Exportform. Der Text deklariert jeden Endpunkt selbst. */
|
|
104
|
+
export function formatEToExportedGraph(text) {
|
|
105
|
+
const { upsertNodes: nodes, upsertEdges: edges } = formatEToDelta(text);
|
|
106
|
+
return toExportedGraph({ nodes, edges });
|
|
107
|
+
}
|
|
108
|
+
/** Ersetzt Eintraege am Platz; neue wandern an ihre Stelle in der sortierten Liste. */
|
|
109
|
+
function einspielen(list, items, keyOf, cmp) {
|
|
110
|
+
const at = new Map(list.map((x, i) => [keyOf(x), i]));
|
|
111
|
+
const neu = new Map();
|
|
112
|
+
for (const item of items) {
|
|
113
|
+
const i = at.get(keyOf(item));
|
|
114
|
+
if (i !== undefined)
|
|
115
|
+
list[i] = item;
|
|
116
|
+
else
|
|
117
|
+
neu.set(keyOf(item), item);
|
|
118
|
+
}
|
|
119
|
+
for (const item of neu.values()) {
|
|
120
|
+
let lo = 0;
|
|
121
|
+
let hi = list.length;
|
|
122
|
+
while (lo < hi) {
|
|
123
|
+
const mid = (lo + hi) >>> 1;
|
|
124
|
+
if (cmp(list[mid], item) < 0)
|
|
125
|
+
lo = mid + 1;
|
|
126
|
+
else
|
|
127
|
+
hi = mid;
|
|
128
|
+
}
|
|
129
|
+
list.splice(lo, 0, item);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Spielt die Wirkung in die Exportform ein; das Ergebnis gleicht dem Export danach. Reihenfolge wie im
|
|
134
|
+
* Store: erst schreiben, dann loeschen. Unberuehrte Eintraege behalten ihr Objekt und eine unberuehrte
|
|
135
|
+
* Liste ihre Identitaet — darauf baut das gezielte Neuzeichnen im Viewer.
|
|
136
|
+
*/
|
|
137
|
+
export function applyDelta(view, delta) {
|
|
138
|
+
let elements = view.elements;
|
|
139
|
+
if (delta.upsertNodes.length + delta.deleteNodes.length > 0) {
|
|
140
|
+
elements = [...elements];
|
|
141
|
+
einspielen(elements, delta.upsertNodes.map(nodeToElement), (e) => e.id, byId);
|
|
142
|
+
const gone = new Set(delta.deleteNodes);
|
|
143
|
+
if (gone.size > 0)
|
|
144
|
+
elements = elements.filter((e) => !gone.has(e.id));
|
|
145
|
+
}
|
|
146
|
+
let traces = view.traces;
|
|
147
|
+
if (delta.upsertEdges.length + delta.deleteEdges.length > 0) {
|
|
148
|
+
traces = [...traces];
|
|
149
|
+
einspielen(traces, delta.upsertEdges.map(edgeToTrace), traceKey, byTrace);
|
|
150
|
+
const gone = new Set(delta.deleteEdges.map((e) => traceKey({ source: e.sourceId, type: e.edgeType, target: e.targetId })));
|
|
151
|
+
if (gone.size > 0)
|
|
152
|
+
traces = traces.filter((t) => !gone.has(traceKey(t)));
|
|
153
|
+
}
|
|
154
|
+
return { ...view, elements, traces };
|
|
155
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -15,6 +15,8 @@ export { GraphService } from './graph-service.js';
|
|
|
15
15
|
export { FormatECodec } from './format-e-codec.js';
|
|
16
16
|
export { SE_FORMAT_E_CODEC, FormatEInputSchema } from './format-e-door.js';
|
|
17
17
|
export { commandsToFormatE } from './format-e-commands.js';
|
|
18
|
+
export { applyDelta, deltaToFormatE, edgeToTrace, formatEToDelta, formatEToExportedGraph, graphToFormatE, nodeToElement, toExportedGraph } from './graph-delta.js';
|
|
19
|
+
export type { ExportedElement, ExportedGraph, ExportedTrace, GraphDelta } from './graph-delta.js';
|
|
18
20
|
export type { Rule, RuleViolation, RuleEngine } from './rule-engine.js';
|
|
19
21
|
export { DefaultRuleEngine } from './rule-engine.js';
|
|
20
22
|
export type { AuditEntry, AuditLog, OperationsLog } from './audit.js';
|
package/dist/index.js
CHANGED
|
@@ -16,6 +16,7 @@ export { FormatECodec } from './format-e-codec.js';
|
|
|
16
16
|
// CR-SM-359: die Tuer des Format-E-Vertrags und die eine SE-Codec-Instanz — Konsumenten bauen keine eigene.
|
|
17
17
|
export { SE_FORMAT_E_CODEC, FormatEInputSchema } from './format-e-door.js';
|
|
18
18
|
export { commandsToFormatE } from './format-e-commands.js';
|
|
19
|
+
export { applyDelta, deltaToFormatE, edgeToTrace, formatEToDelta, formatEToExportedGraph, graphToFormatE, nodeToElement, toExportedGraph } from './graph-delta.js';
|
|
19
20
|
export { DefaultRuleEngine } from './rule-engine.js';
|
|
20
21
|
export { InMemoryAuditLog, FileOperationsLog, AUDIT_FILE, AUDIT_BASENAME, DEFAULT_COMPACT_BYTES } from './audit.js';
|
|
21
22
|
export { MemoryAdapter } from './memory-adapter.js';
|
|
@@ -27,6 +27,13 @@ import { generateSchema } from './schema-generator.js';
|
|
|
27
27
|
* initialisieren, warten damit auf denselben Ladevorgang statt zwei zu starten.
|
|
28
28
|
*/
|
|
29
29
|
let kuzuModule = null;
|
|
30
|
+
/**
|
|
31
|
+
* Kuzu startet ohne Vorgabe einen Thread je Kern (ITEM-2026-1174). Der Node-Build ruft synchron,
|
|
32
|
+
* der JS-Thread steht also bis zur Antwort. Ein einzelner Graph braucht keine Parallelitaet:
|
|
33
|
+
* 600 Testlaeufe dauerten mit einem Thread 441 s, mit 18 Threads 726 s. Ob das auch den Haenger
|
|
34
|
+
* aus ITEM-2026-871 beseitigt, ist nicht belegt.
|
|
35
|
+
*/
|
|
36
|
+
const MAX_NUM_THREADS = 1;
|
|
30
37
|
function loadKuzu() {
|
|
31
38
|
if (!kuzuModule)
|
|
32
39
|
kuzuModule = initKuzu();
|
|
@@ -72,7 +79,7 @@ export class GraphCypherEngine {
|
|
|
72
79
|
}
|
|
73
80
|
await this.destroy();
|
|
74
81
|
this.kuzu = await loadKuzu();
|
|
75
|
-
this.db = new this.kuzu.Database(this.options.path ?? ':memory:');
|
|
82
|
+
this.db = new this.kuzu.Database(this.options.path ?? ':memory:', 0, MAX_NUM_THREADS);
|
|
76
83
|
this.connection = new this.kuzu.Connection(this.db);
|
|
77
84
|
this.schema = generateSchema(ontology, this.options);
|
|
78
85
|
this.ontology = ontology;
|