rete-graph 0.1.0 → 0.2.0

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 CHANGED
File without changes
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! rete-graph v0.1.0 | Apache-2.0 | https://github.com/caviri/rete */
1
+ /*! rete-graph v0.2.0 | Apache-2.0 | https://github.com/caviri/rete */
2
2
  var __defProp = Object.defineProperty;
3
3
  var __getOwnPropNames = Object.getOwnPropertyNames;
4
4
  var __esm = (fn, res) => function __init() {
@@ -598,6 +598,10 @@ var RemoteGraph = class {
598
598
  }
599
599
  /**
600
600
  * See [`sparql_url`] — same query, but over the resident, cached handle.
601
+ * The incompleteness verdict is PER QUERY: reset the sticky failure flags
602
+ * first, so one transient fetch failure fails only the query it happened
603
+ * in — not every later query on this session (failed tiles/chunks are
604
+ * never cached, so they retry here).
601
605
  * @param {string} query
602
606
  * @param {string} format
603
607
  * @returns {string}
@@ -1196,6 +1200,257 @@ async function __wbg_init(module_or_path) {
1196
1200
  return __wbg_finalize_init(instance, module2);
1197
1201
  }
1198
1202
 
1203
+ // src/comunica.js
1204
+ var BaseTerm = class {
1205
+ equals(other) {
1206
+ return !!other && other.termType === this.termType && other.value === this.value && (this.termType !== "Literal" || other.language === this.language && other.datatype.value === this.datatype.value);
1207
+ }
1208
+ };
1209
+ var NamedNode = class extends BaseTerm {
1210
+ constructor(value) {
1211
+ super();
1212
+ this.termType = "NamedNode";
1213
+ this.value = value;
1214
+ }
1215
+ };
1216
+ var BlankNode = class extends BaseTerm {
1217
+ constructor(label) {
1218
+ super();
1219
+ this.termType = "BlankNode";
1220
+ this.value = label;
1221
+ }
1222
+ };
1223
+ var Literal = class extends BaseTerm {
1224
+ constructor(value, languageOrDatatype) {
1225
+ super();
1226
+ this.termType = "Literal";
1227
+ this.value = value;
1228
+ if (typeof languageOrDatatype === "string") {
1229
+ this.language = languageOrDatatype;
1230
+ this.datatype = new NamedNode(
1231
+ "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString"
1232
+ );
1233
+ } else {
1234
+ this.language = "";
1235
+ this.datatype = languageOrDatatype ?? new NamedNode("http://www.w3.org/2001/XMLSchema#string");
1236
+ }
1237
+ }
1238
+ };
1239
+ var DefaultGraph = class extends BaseTerm {
1240
+ constructor() {
1241
+ super();
1242
+ this.termType = "DefaultGraph";
1243
+ this.value = "";
1244
+ }
1245
+ };
1246
+ var Quad = class {
1247
+ constructor(subject, predicate, object, graph) {
1248
+ this.termType = "Quad";
1249
+ this.value = "";
1250
+ this.subject = subject;
1251
+ this.predicate = predicate;
1252
+ this.object = object;
1253
+ this.graph = graph ?? new DefaultGraph();
1254
+ }
1255
+ equals(other) {
1256
+ return !!other && other.termType === "Quad" && this.subject.equals(other.subject) && this.predicate.equals(other.predicate) && this.object.equals(other.object) && this.graph.equals(other.graph);
1257
+ }
1258
+ };
1259
+ function toRdfjs(t) {
1260
+ const term = t instanceof Term ? t : Term.parse(t);
1261
+ switch (term.kind) {
1262
+ case "iri":
1263
+ return new NamedNode(term.value);
1264
+ case "bnode":
1265
+ return new BlankNode(term.value.replace(/^_:/, ""));
1266
+ case "triple": {
1267
+ const [s, p, o] = splitQuoted(term.value);
1268
+ return new Quad(toRdfjs(s), toRdfjs(p), toRdfjs(o));
1269
+ }
1270
+ default:
1271
+ if (term.lang) return new Literal(term.value, term.lang);
1272
+ if (term.datatype) return new Literal(term.value, new NamedNode(term.datatype));
1273
+ return new Literal(term.value);
1274
+ }
1275
+ }
1276
+ function splitQuoted(token) {
1277
+ const inner = token.slice(2, -2).trim();
1278
+ const parts = [];
1279
+ let depth = 0, inString = false, start = 0;
1280
+ for (let i = 0; i < inner.length; i += 1) {
1281
+ const c = inner[i];
1282
+ if (inString) {
1283
+ if (c === "\\") i += 1;
1284
+ else if (c === '"') inString = false;
1285
+ continue;
1286
+ }
1287
+ if (c === '"') inString = true;
1288
+ else if (inner.startsWith("<<", i)) {
1289
+ depth += 1;
1290
+ i += 1;
1291
+ } else if (inner.startsWith(">>", i)) {
1292
+ depth -= 1;
1293
+ i += 1;
1294
+ } else if (c === " " && depth === 0) {
1295
+ if (i > start) parts.push(inner.slice(start, i));
1296
+ start = i + 1;
1297
+ if (parts.length === 2) {
1298
+ parts.push(inner.slice(start));
1299
+ break;
1300
+ }
1301
+ }
1302
+ }
1303
+ if (parts.length < 3) parts.push(inner.slice(start));
1304
+ return parts.map((p) => p.trim()).filter(Boolean);
1305
+ }
1306
+ var XSD_STRING = "http://www.w3.org/2001/XMLSchema#string";
1307
+ function toN3(term) {
1308
+ switch (term.termType) {
1309
+ case "NamedNode":
1310
+ return `<${term.value}>`;
1311
+ case "Literal": {
1312
+ const dt = term.language || !term.datatype || term.datatype.value === XSD_STRING ? null : term.datatype.value;
1313
+ return new Term("literal", term.value, dt, term.language || null).n3;
1314
+ }
1315
+ case "Quad":
1316
+ return `<< ${toN3(term.subject)} ${toN3(term.predicate)} ${toN3(term.object)} >>`;
1317
+ default:
1318
+ throw new TypeError(`cannot serialize ${term.termType} into a pattern`);
1319
+ }
1320
+ }
1321
+ var isVarish = (t) => t == null || t.termType === "Variable";
1322
+ var QuadStream = class {
1323
+ #quads;
1324
+ #i = 0;
1325
+ #handlers = { readable: [], end: [], error: [], data: [] };
1326
+ #error;
1327
+ constructor(quads, error = null) {
1328
+ this.#quads = quads;
1329
+ this.#error = error;
1330
+ queueMicrotask(() => {
1331
+ if (this.#error) return this.#emit("error", this.#error);
1332
+ this.#emit("readable");
1333
+ if (this.#i >= this.#quads.length) this.#emit("end");
1334
+ });
1335
+ }
1336
+ read() {
1337
+ if (this.#i < this.#quads.length) {
1338
+ const q = this.#quads[this.#i];
1339
+ this.#i += 1;
1340
+ if (this.#i >= this.#quads.length) queueMicrotask(() => this.#emit("end"));
1341
+ return q;
1342
+ }
1343
+ return null;
1344
+ }
1345
+ on(event, handler) {
1346
+ (this.#handlers[event] ??= []).push(handler);
1347
+ return this;
1348
+ }
1349
+ once(event, handler) {
1350
+ const stream = this;
1351
+ const wrap = function(...a) {
1352
+ stream.removeListener(event, wrap);
1353
+ handler.call(this, ...a);
1354
+ };
1355
+ return this.on(event, wrap);
1356
+ }
1357
+ addListener(event, handler) {
1358
+ return this.on(event, handler);
1359
+ }
1360
+ removeListener(event, handler) {
1361
+ const list = this.#handlers[event] ?? [];
1362
+ const at = list.indexOf(handler);
1363
+ if (at >= 0) list.splice(at, 1);
1364
+ return this;
1365
+ }
1366
+ emit() {
1367
+ return false;
1368
+ }
1369
+ // external emits are ignored
1370
+ destroy() {
1371
+ this.#i = this.#quads.length;
1372
+ return this;
1373
+ }
1374
+ #emit(event, ...args) {
1375
+ for (const h of [...this.#handlers[event] ?? []]) h.call(this, ...args);
1376
+ }
1377
+ };
1378
+ var ReteSource = class {
1379
+ /** @param {import("./index.js").Graph} graph */
1380
+ constructor(graph) {
1381
+ this.graph = graph;
1382
+ }
1383
+ /** All quads matching the pattern, as an RDF/JS quad stream. */
1384
+ match(subject, predicate, object, graph) {
1385
+ try {
1386
+ return new QuadStream(this.#matchArray(subject, predicate, object, graph));
1387
+ } catch (error) {
1388
+ return new QuadStream([], error);
1389
+ }
1390
+ }
1391
+ /** Number of matching quads (used by query planners for join ordering). */
1392
+ countQuads(subject, predicate, object, graph) {
1393
+ return this.#matchArray(subject, predicate, object, graph).length;
1394
+ }
1395
+ #matchArray(subject, predicate, object, graph) {
1396
+ const wantBnode = (term) => term != null && term.termType === "BlankNode" ? `_:${term.value}` : null;
1397
+ const wantS = wantBnode(subject), wantO = wantBnode(object);
1398
+ const free = {
1399
+ s: isVarish(subject) || subject.termType === "BlankNode",
1400
+ p: isVarish(predicate),
1401
+ o: isVarish(object) || object.termType === "BlankNode"
1402
+ };
1403
+ const pattern = [
1404
+ free.s ? "?s" : toN3(subject),
1405
+ free.p ? "?p" : toN3(predicate),
1406
+ free.o ? "?o" : toN3(object)
1407
+ ].join(" ");
1408
+ const fullyBound = !free.s && !free.p && !free.o;
1409
+ const rows = [];
1410
+ const defaultGraph = new DefaultGraph();
1411
+ const runDefault = () => {
1412
+ if (fullyBound) {
1413
+ if (this.graph.query(`ASK { ${pattern} }`)) rows.push({ row: {}, graphTerm: defaultGraph });
1414
+ return;
1415
+ }
1416
+ for (const row of this.graph.query(`SELECT * WHERE { ${pattern} }`)) {
1417
+ rows.push({ row, graphTerm: defaultGraph });
1418
+ }
1419
+ };
1420
+ const runNamed = (g) => {
1421
+ if (g && fullyBound) {
1422
+ if (this.graph.query(`ASK { GRAPH <${g.value}> { ${pattern} } }`))
1423
+ rows.push({ row: {}, graphTerm: g });
1424
+ return;
1425
+ }
1426
+ const clause = g ? `GRAPH <${g.value}>` : "GRAPH ?g";
1427
+ for (const row of this.graph.query(`SELECT * WHERE { ${clause} { ${pattern} } }`)) {
1428
+ rows.push({ row, graphTerm: g ?? toRdfjs(row.g) });
1429
+ }
1430
+ };
1431
+ if (graph == null || graph.termType === "Variable") {
1432
+ runDefault();
1433
+ runNamed(null);
1434
+ } else if (graph.termType === "DefaultGraph") {
1435
+ runDefault();
1436
+ } else if (graph.termType === "NamedNode") {
1437
+ runNamed(graph);
1438
+ } else {
1439
+ throw new TypeError(`cannot match graph term ${graph.termType}`);
1440
+ }
1441
+ const out = [];
1442
+ for (const { row, graphTerm } of rows) {
1443
+ const s = row.s ? toRdfjs(row.s) : subject;
1444
+ const p = row.p ? toRdfjs(row.p) : predicate;
1445
+ const o = row.o ? toRdfjs(row.o) : object;
1446
+ if (wantS && (s.termType !== "BlankNode" || `_:${s.value}` !== wantS)) continue;
1447
+ if (wantO && (o.termType !== "BlankNode" || `_:${o.value}` !== wantO)) continue;
1448
+ out.push(new Quad(s, p, o, graphTerm));
1449
+ }
1450
+ return out;
1451
+ }
1452
+ };
1453
+
1199
1454
  // src/index.js
1200
1455
  var XSD = "http://www.w3.org/2001/XMLSchema#";
1201
1456
  var INT_TYPES = new Set(
@@ -1428,6 +1683,7 @@ async function build2(text, format = "nt") {
1428
1683
  }
1429
1684
  export {
1430
1685
  Graph2 as Graph,
1686
+ ReteSource,
1431
1687
  Term,
1432
1688
  __setWasmSource,
1433
1689
  build2 as build,