@unrdf/knowledge-engine 5.0.1 → 26.4.3
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/package.json +13 -7
- package/src/ai-enhanced-search.mjs +371 -0
- package/src/anomaly-detector.mjs +226 -0
- package/src/artifact-generator.mjs +251 -0
- package/src/browser.mjs +1 -1
- package/src/chatman/disruption-arithmetic.mjs +140 -0
- package/src/chatman/market-dynamics.mjs +140 -0
- package/src/chatman/organizational-dynamics.mjs +140 -0
- package/src/chatman/strategic-dynamics.mjs +140 -0
- package/src/chatman-config-loader.mjs +282 -0
- package/src/chatman-engine.mjs +431 -0
- package/src/chatman-operator.mjs +342 -0
- package/src/dark-field-detector.mjs +312 -0
- package/src/formation-theorems.mjs +345 -0
- package/src/index.mjs +20 -2
- package/src/knowledge-hook-manager.mjs +1 -1
- package/src/lockchain-writer-browser.mjs +2 -2
- package/src/observability.mjs +40 -4
- package/src/query-optimizer.mjs +1 -1
- package/src/resolution-layer.mjs +1 -1
- package/src/transaction.mjs +11 -9
- package/README.md +0 -84
- package/src/browser-shims.mjs +0 -343
- package/src/canonicalize.mjs +0 -414
- package/src/condition-cache.mjs +0 -109
- package/src/condition-evaluator.mjs +0 -722
- package/src/dark-matter-core.mjs +0 -742
- package/src/define-hook.mjs +0 -213
- package/src/effect-sandbox-browser.mjs +0 -283
- package/src/effect-sandbox-worker.mjs +0 -170
- package/src/effect-sandbox.mjs +0 -517
- package/src/engines/index.mjs +0 -11
- package/src/engines/rdf-engine.mjs +0 -299
- package/src/file-resolver.mjs +0 -387
- package/src/hook-executor-batching.mjs +0 -277
- package/src/hook-executor.mjs +0 -870
- package/src/hook-management.mjs +0 -150
- package/src/ken-parliment.mjs +0 -119
- package/src/ken.mjs +0 -149
- package/src/knowledge-engine/builtin-rules.mjs +0 -190
- package/src/knowledge-engine/inference-engine.mjs +0 -418
- package/src/knowledge-engine/knowledge-engine.mjs +0 -317
- package/src/knowledge-engine/pattern-dsl.mjs +0 -142
- package/src/knowledge-engine/pattern-matcher.mjs +0 -215
- package/src/knowledge-engine/rules.mjs +0 -184
- package/src/knowledge-engine.mjs +0 -319
- package/src/knowledge-hook-engine.mjs +0 -360
- package/src/knowledge-substrate-core.mjs +0 -927
- package/src/lite.mjs +0 -222
- package/src/lockchain-writer.mjs +0 -602
- package/src/monitoring/andon-signals.mjs +0 -775
- package/src/parse.mjs +0 -290
- package/src/performance-optimizer.mjs +0 -678
- package/src/policy-pack.mjs +0 -572
- package/src/query-cache.mjs +0 -116
- package/src/query.mjs +0 -306
- package/src/reason.mjs +0 -350
- package/src/schemas.mjs +0 -1063
- package/src/security/error-sanitizer.mjs +0 -257
- package/src/security/path-validator.mjs +0 -194
- package/src/security/sandbox-restrictions.mjs +0 -331
- package/src/security-validator.mjs +0 -389
- package/src/store-cache.mjs +0 -137
- package/src/telemetry.mjs +0 -167
- package/src/utils/adaptive-monitor.mjs +0 -746
- package/src/utils/circuit-breaker.mjs +0 -513
- package/src/utils/edge-case-handler.mjs +0 -503
- package/src/utils/memory-manager.mjs +0 -498
- package/src/utils/ring-buffer.mjs +0 -282
- package/src/validate.mjs +0 -319
- package/src/validators/index.mjs +0 -338
package/src/parse.mjs
DELETED
|
@@ -1,290 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Parsing and serialization utilities for RDF data.
|
|
3
|
-
* @module parse
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { createStore, dataFactory } from '@unrdf/oxigraph';
|
|
7
|
-
import { trace, SpanStatusCode } from '@opentelemetry/api';
|
|
8
|
-
|
|
9
|
-
const tracer = trace.getTracer('unrdf');
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Parse a Turtle string into a Store.
|
|
13
|
-
* @param {string} ttl - The Turtle string to parse
|
|
14
|
-
* @param {string} [baseIRI] - Base IRI for resolving relative URIs
|
|
15
|
-
* @returns {Promise<Store>} Promise resolving to a Store containing the parsed quads
|
|
16
|
-
*
|
|
17
|
-
* @throws {Error} If parsing fails
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* const ttl = `
|
|
21
|
-
* @prefix ex: <http://example.org/> .
|
|
22
|
-
* ex:alice ex:knows ex:bob .
|
|
23
|
-
* `;
|
|
24
|
-
* const store = await parseTurtle(ttl, 'http://example.org/');
|
|
25
|
-
*/
|
|
26
|
-
export async function parseTurtle(ttl, baseIRI = 'http://example.org/') {
|
|
27
|
-
if (typeof ttl !== 'string') {
|
|
28
|
-
throw new TypeError('parseTurtle: ttl must be a string');
|
|
29
|
-
}
|
|
30
|
-
if (typeof baseIRI !== 'string') {
|
|
31
|
-
throw new TypeError('parseTurtle: baseIRI must be a string');
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return tracer.startActiveSpan('parse.turtle', async span => {
|
|
35
|
-
try {
|
|
36
|
-
span.setAttributes({
|
|
37
|
-
'parse.format': 'turtle',
|
|
38
|
-
'parse.base_iri': baseIRI,
|
|
39
|
-
'parse.input_length': ttl.length,
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
// Use Oxigraph for parsing Turtle
|
|
43
|
-
const store = createStore();
|
|
44
|
-
store.load(ttl, { format: 'text/turtle', base: baseIRI });
|
|
45
|
-
|
|
46
|
-
span.setAttribute('parse.quads_count', store.size);
|
|
47
|
-
span.setStatus({ code: SpanStatusCode.OK });
|
|
48
|
-
|
|
49
|
-
return store;
|
|
50
|
-
} catch (error) {
|
|
51
|
-
span.recordException(error);
|
|
52
|
-
span.setStatus({
|
|
53
|
-
code: SpanStatusCode.ERROR,
|
|
54
|
-
message: error.message,
|
|
55
|
-
});
|
|
56
|
-
throw new Error(`Failed to parse Turtle: ${error.message}`);
|
|
57
|
-
} finally {
|
|
58
|
-
span.end();
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Serialize a store to Turtle.
|
|
65
|
-
* @param {Store} store - The store to serialize
|
|
66
|
-
* @param {Object} [options] - Serialization options
|
|
67
|
-
* @param {Object} [options.prefixes] - Prefix mappings
|
|
68
|
-
* @param {string} [options.baseIRI] - Base IRI for the output
|
|
69
|
-
* @returns {Promise<string>} Promise resolving to the Turtle string
|
|
70
|
-
*
|
|
71
|
-
* @throws {Error} If serialization fails
|
|
72
|
-
*
|
|
73
|
-
* @example
|
|
74
|
-
* const turtle = await toTurtle(store, {
|
|
75
|
-
* prefixes: { ex: 'http://example.org/' },
|
|
76
|
-
* baseIRI: 'http://example.org/'
|
|
77
|
-
* });
|
|
78
|
-
*/
|
|
79
|
-
export async function toTurtle(store, options = {}) {
|
|
80
|
-
if (!store || typeof store.getQuads !== 'function') {
|
|
81
|
-
throw new TypeError('toTurtle: store must be a valid Store instance');
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
try {
|
|
85
|
-
// Use Oxigraph dump for serialization
|
|
86
|
-
let result = store.dump({ format: 'text/turtle' });
|
|
87
|
-
|
|
88
|
-
// Add @base directive if baseIRI is provided
|
|
89
|
-
if (options.baseIRI) {
|
|
90
|
-
result = `@base <${options.baseIRI}> .\n\n${result}`;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// Add prefixes if provided (prepend to output)
|
|
94
|
-
if (options.prefixes && Object.keys(options.prefixes).length > 0) {
|
|
95
|
-
const prefixLines = Object.entries(options.prefixes)
|
|
96
|
-
.map(([prefix, iri]) => `@prefix ${prefix}: <${iri}> .`)
|
|
97
|
-
.join('\n');
|
|
98
|
-
result = `${prefixLines}\n\n${result}`;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return result;
|
|
102
|
-
} catch (error) {
|
|
103
|
-
throw new Error(`Failed to serialize to Turtle: ${error.message}`);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Serialize a store to canonical N-Quads.
|
|
109
|
-
* @param {Store} store - The store to serialize
|
|
110
|
-
* @param {Object} [options] - Serialization options
|
|
111
|
-
* @returns {Promise<string>} Promise resolving to the N-Quads string
|
|
112
|
-
*
|
|
113
|
-
* @throws {Error} If serialization fails
|
|
114
|
-
*
|
|
115
|
-
* @example
|
|
116
|
-
* const nquads = await toNQuads(store);
|
|
117
|
-
*/
|
|
118
|
-
export async function toNQuads(store, options = {}) {
|
|
119
|
-
if (!store || typeof store.getQuads !== 'function') {
|
|
120
|
-
throw new TypeError('toNQuads: store must be a valid Store instance');
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
try {
|
|
124
|
-
// Use Oxigraph dump for N-Quads serialization
|
|
125
|
-
const result = store.dump({ format: 'application/n-quads', ...options });
|
|
126
|
-
return result;
|
|
127
|
-
} catch (error) {
|
|
128
|
-
throw new Error(`Failed to serialize to N-Quads: ${error.message}`);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Parse a JSON-LD string into a Store.
|
|
134
|
-
* @param {string} jsonld - The JSON-LD string to parse
|
|
135
|
-
* @param {Object} [options] - Parsing options
|
|
136
|
-
* @param {string} [options.baseIRI] - Base IRI for resolving relative URIs
|
|
137
|
-
* @returns {Promise<Store>} Promise resolving to a Store containing the parsed quads
|
|
138
|
-
*
|
|
139
|
-
* @throws {Error} If parsing fails
|
|
140
|
-
*
|
|
141
|
-
* @example
|
|
142
|
-
* const jsonld = `{
|
|
143
|
-
* "@context": {"ex": "http://example.org/"},
|
|
144
|
-
* "@id": "ex:alice",
|
|
145
|
-
* "ex:knows": {"@id": "ex:bob"}
|
|
146
|
-
* }`;
|
|
147
|
-
* const store = await parseJsonLd(jsonld);
|
|
148
|
-
*/
|
|
149
|
-
export async function parseJsonLd(jsonld, _options = {}) {
|
|
150
|
-
if (typeof jsonld !== 'string' && typeof jsonld !== 'object') {
|
|
151
|
-
throw new TypeError('parseJsonLd: jsonld must be a string or object');
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
try {
|
|
155
|
-
// Parse JSON string if needed
|
|
156
|
-
const jsonldData = typeof jsonld === 'string' ? JSON.parse(jsonld) : jsonld;
|
|
157
|
-
|
|
158
|
-
if (!jsonldData || (!Array.isArray(jsonldData) && typeof jsonldData !== 'object')) {
|
|
159
|
-
throw new TypeError('parseJsonLd: jsonld must be an object or array');
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// Convert JSON-LD to RDF quads
|
|
163
|
-
// This is a simplified implementation - in production you would use a proper JSON-LD to RDF converter
|
|
164
|
-
const store = createStore();
|
|
165
|
-
|
|
166
|
-
// Handle @graph array or single object
|
|
167
|
-
const items = jsonldData['@graph'] || (Array.isArray(jsonldData) ? jsonldData : [jsonldData]);
|
|
168
|
-
|
|
169
|
-
// Validate that we have valid JSON-LD structure
|
|
170
|
-
if (
|
|
171
|
-
items.length === 0 ||
|
|
172
|
-
!items.some(item => item && typeof item === 'object' && item['@id'])
|
|
173
|
-
) {
|
|
174
|
-
throw new Error('Invalid JSON-LD structure: no valid items with @id found');
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
for (const item of items) {
|
|
178
|
-
if (item && item['@id']) {
|
|
179
|
-
const subject = item['@id'];
|
|
180
|
-
|
|
181
|
-
// Process each property
|
|
182
|
-
for (const [key, value] of Object.entries(item)) {
|
|
183
|
-
if (key === '@id' || key === '@context') continue;
|
|
184
|
-
|
|
185
|
-
if (Array.isArray(value)) {
|
|
186
|
-
for (const val of value) {
|
|
187
|
-
if (typeof val === 'object' && val['@id']) {
|
|
188
|
-
store.add(
|
|
189
|
-
dataFactory.quad(
|
|
190
|
-
dataFactory.namedNode(subject),
|
|
191
|
-
dataFactory.namedNode(key),
|
|
192
|
-
dataFactory.namedNode(val['@id'])
|
|
193
|
-
)
|
|
194
|
-
);
|
|
195
|
-
} else if (typeof val === 'string' || typeof val === 'number') {
|
|
196
|
-
store.add(
|
|
197
|
-
dataFactory.quad(
|
|
198
|
-
dataFactory.namedNode(subject),
|
|
199
|
-
dataFactory.namedNode(key),
|
|
200
|
-
dataFactory.literal(String(val))
|
|
201
|
-
)
|
|
202
|
-
);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
} else if (typeof value === 'object' && value['@id']) {
|
|
206
|
-
store.add(
|
|
207
|
-
dataFactory.quad(
|
|
208
|
-
dataFactory.namedNode(subject),
|
|
209
|
-
dataFactory.namedNode(key),
|
|
210
|
-
dataFactory.namedNode(value['@id'])
|
|
211
|
-
)
|
|
212
|
-
);
|
|
213
|
-
} else if (typeof value === 'string' || typeof value === 'number') {
|
|
214
|
-
store.add(
|
|
215
|
-
dataFactory.quad(
|
|
216
|
-
dataFactory.namedNode(subject),
|
|
217
|
-
dataFactory.namedNode(key),
|
|
218
|
-
dataFactory.literal(String(value))
|
|
219
|
-
)
|
|
220
|
-
);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
return store;
|
|
227
|
-
} catch (error) {
|
|
228
|
-
throw new Error(`Failed to parse JSON-LD: ${error.message}`);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* Serialize a store to JSON-LD.
|
|
234
|
-
* @param {Store} store - The store to serialize
|
|
235
|
-
* @param {Object} [options] - Serialization options
|
|
236
|
-
* @param {Object} [options.context] - JSON-LD context
|
|
237
|
-
* @returns {Promise<Object>} Promise resolving to the JSON-LD object
|
|
238
|
-
*
|
|
239
|
-
* @throws {Error} If serialization fails
|
|
240
|
-
*
|
|
241
|
-
* @example
|
|
242
|
-
* const jsonld = await toJsonLd(store, {
|
|
243
|
-
* context: { ex: 'http://example.org/' }
|
|
244
|
-
* });
|
|
245
|
-
*/
|
|
246
|
-
export async function toJsonLd(store, options = {}) {
|
|
247
|
-
if (!store || typeof store.getQuads !== 'function') {
|
|
248
|
-
throw new TypeError('toJsonLd: store must be a valid Store instance');
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
try {
|
|
252
|
-
// This is a simplified implementation
|
|
253
|
-
// In production, you would use a proper RDF to JSON-LD converter
|
|
254
|
-
const quads = store.getQuads();
|
|
255
|
-
const result = {
|
|
256
|
-
'@context': options.context || {},
|
|
257
|
-
'@graph': [],
|
|
258
|
-
};
|
|
259
|
-
|
|
260
|
-
// Add @base to context if baseIRI is provided
|
|
261
|
-
if (options.baseIRI) {
|
|
262
|
-
result['@context']['@base'] = options.baseIRI;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
// Convert quads to JSON-LD format
|
|
266
|
-
for (const quad of quads) {
|
|
267
|
-
const subject = quad.subject.value;
|
|
268
|
-
const predicate = quad.predicate.value;
|
|
269
|
-
const object = quad.object.value;
|
|
270
|
-
|
|
271
|
-
// Find existing subject in graph or create new one
|
|
272
|
-
let subjectNode = result['@graph'].find(node => node['@id'] === subject);
|
|
273
|
-
if (!subjectNode) {
|
|
274
|
-
subjectNode = { '@id': subject };
|
|
275
|
-
result['@graph'].push(subjectNode);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
// Add predicate-object pair
|
|
279
|
-
if (!subjectNode[predicate]) {
|
|
280
|
-
subjectNode[predicate] = [];
|
|
281
|
-
}
|
|
282
|
-
subjectNode[predicate].push({ '@id': object });
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
// Return as object (users can call JSON.stringify if they need a string)
|
|
286
|
-
return result;
|
|
287
|
-
} catch (error) {
|
|
288
|
-
throw new Error(`Failed to serialize to JSON-LD: ${error.message}`);
|
|
289
|
-
}
|
|
290
|
-
}
|