eyemind 0.9.17 → 0.9.19

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.
@@ -11,22 +11,8 @@
11
11
  // ===========================================================================
12
12
 
13
13
  const RDF_NS = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
14
- const RDFS_NS = 'http://www.w3.org/2000/01/rdf-schema#';
15
- const OWL_NS = 'http://www.w3.org/2002/07/owl#';
16
14
  const XSD_NS = 'http://www.w3.org/2001/XMLSchema#';
17
- const MATH_NS = 'http://www.w3.org/2000/10/swap/math#';
18
- const TIME_NS = 'http://www.w3.org/2000/10/swap/time#';
19
- const LIST_NS = 'http://www.w3.org/2000/10/swap/list#';
20
15
  const LOG_NS = 'http://www.w3.org/2000/10/swap/log#';
21
- const STRING_NS = 'http://www.w3.org/2000/10/swap/string#';
22
- const DT_NS = 'https://eyereasoner.github.io/eyeling/datatype#';
23
- const SKOLEM_NS = 'https://eyereasoner.github.io/.well-known/genid/';
24
-
25
- // Private rule-lifting variable prefix used for blank nodes that appear in
26
- // rule-body patterns. The prefix is intentionally not spellable in N3 input,
27
- // but public extension APIs should present these variables with stable,
28
- // ordinary names and translate them back internally.
29
- const INTERNAL_BLANK_VAR_PREFIX = '\uE000eyeling_b';
30
16
 
31
17
  function parseUriReferenceForResolution(uri) {
32
18
  // RFC 3986 Appendix B-style component parser, with the scheme tightened to
@@ -373,13 +359,6 @@ class Literal extends Term {
373
359
  }
374
360
  }
375
361
 
376
- class Var extends Term {
377
- constructor(name) {
378
- super();
379
- this.name = name; // without leading '?'
380
- }
381
- }
382
-
383
362
  class Blank extends Term {
384
363
  constructor(label, blankNodeKind = 'labeled') {
385
364
  super();
@@ -401,14 +380,6 @@ class ListTerm extends Term {
401
380
  }
402
381
  }
403
382
 
404
- class OpenListTerm extends Term {
405
- constructor(prefix, tailVar) {
406
- super();
407
- this.prefix = prefix; // Term[]
408
- this.tailVar = tailVar; // string
409
- }
410
- }
411
-
412
383
  class GraphTerm extends Term {
413
384
  constructor(triples) {
414
385
  super();
@@ -424,17 +395,6 @@ class Triple {
424
395
  }
425
396
  }
426
397
 
427
- class Rule {
428
- constructor(premise, conclusion, isForward, isFuse, headBlankLabels) {
429
- this.premise = premise; // Triple[]
430
- this.conclusion = conclusion; // Triple[]
431
- this.isForward = isForward; // boolean
432
- this.isFuse = isFuse; // boolean
433
- // Set<string> of blank-node labels that occur explicitly in the rule head
434
- this.headBlankLabels = headBlankLabels || new Set();
435
- }
436
- }
437
-
438
398
  // ===========================================================================
439
399
  // Term interning
440
400
  // ===========================================================================
@@ -485,79 +445,18 @@ function internLiteral(value) {
485
445
  // Special predicate helpers (kept here because PrefixEnv needs them)
486
446
  // ===========================================================================
487
447
 
488
- function isRdfTypePred(p) {
489
- return p instanceof Iri && p.value === RDF_NS + 'type';
490
- }
491
-
492
- function isLogImplies(p) {
493
- return p instanceof Iri && p.value === LOG_NS + 'implies';
494
- }
495
-
496
- function isLogImpliedBy(p) {
497
- return p instanceof Iri && p.value === LOG_NS + 'impliedBy';
498
- }
499
-
500
- function isLogQuery(p) {
501
- return p instanceof Iri && p.value === LOG_NS + 'query';
502
- }
503
-
504
448
  // ===========================================================================
505
449
  // PREFIX ENVIRONMENT
506
450
  // ===========================================================================
507
451
 
508
- // Conservative check for whether a candidate local part can be safely serialized as a prefixed name.
509
- // If false, we fall back to <IRI> to guarantee syntactically valid N3/Turtle output.
510
- function isValidQNameLocal(local) {
511
- if (typeof local !== 'string' || local.length === 0) return false;
512
- // Disallow characters that would break PN_LOCAL unless escaped (we keep this conservative).
513
- if (/[#:/?\s]/.test(local)) return false;
514
- // Allow a safe ASCII subset.
515
- if (/[^A-Za-z0-9._-]/.test(local)) return false;
516
- // Avoid edge cases that typically require escaping.
517
- if (local.endsWith('.')) return false;
518
- if (/^[.-]/.test(local)) return false;
519
- return true;
520
- }
521
-
522
452
  class PrefixEnv {
523
453
  constructor(map, baseIri) {
524
454
  this.map = map || {}; // prefix -> IRI (including "" for @prefix :)
525
455
  this.baseIri = baseIri || ''; // base IRI for resolving <relative>
526
456
  }
527
457
 
528
- static newDefault() {
529
- const m = {};
530
- m['rdf'] = RDF_NS;
531
- m['rdfs'] = RDFS_NS;
532
- m['xsd'] = XSD_NS;
533
- m['log'] = LOG_NS;
534
- m['math'] = MATH_NS;
535
- m['string'] = STRING_NS;
536
- m['list'] = LIST_NS;
537
- m['time'] = TIME_NS;
538
- m['dt'] = DT_NS;
539
- m['genid'] = SKOLEM_NS;
540
- m[''] = ''; // empty prefix default namespace
541
- return new PrefixEnv(m, ''); // base IRI starts empty
542
- }
543
-
544
- // shrinkIri and prefixesUsedForOutput run once per IRI of output, and both used
545
- // to rebuild Object.entries(this.map) on every call. These caches hold the
546
- // entry list and the per-IRI answer instead; every write to the map goes
547
- // through set(), so invalidating there is enough.
548
- __invalidatePrefixCaches() {
549
- this.__entries = null;
550
- this.__shrinkCache = null;
551
- }
552
-
553
- __nonEmptyEntries() {
554
- if (!this.__entries) this.__entries = Object.entries(this.map).filter(([, base]) => !!base);
555
- return this.__entries;
556
- }
557
-
558
458
  set(pref, base) {
559
459
  this.map[pref] = base;
560
- this.__invalidatePrefixCaches();
561
460
  }
562
461
 
563
462
  setBase(baseIri) {
@@ -575,179 +474,21 @@ class PrefixEnv {
575
474
  }
576
475
  return q;
577
476
  }
578
-
579
- shrinkIri(iri) {
580
- if (!this.__shrinkCache) this.__shrinkCache = new Map();
581
- const cached = this.__shrinkCache.get(iri);
582
- if (cached !== undefined) return cached;
583
- const out = this.__shrinkIriUncached(iri);
584
- this.__shrinkCache.set(iri, out);
585
- return out;
586
- }
587
-
588
- __shrinkIriUncached(iri) {
589
- let best = null; // [prefix, local]
590
- for (const [p, base] of this.__nonEmptyEntries()) {
591
- if (iri.startsWith(base)) {
592
- const local = iri.slice(base.length);
593
- if (!local) continue;
594
- // Only emit a QName when the local part is safe to serialize without escaping.
595
- if (!isValidQNameLocal(local)) continue;
596
- const cand = [p, local];
597
- if (best === null || cand[1].length < best[1].length) best = cand;
598
- }
599
- }
600
- if (best === null) return null;
601
- const [p, local] = best;
602
- if (p === '') return `:${local}`;
603
- return `${p}:${local}`;
604
- }
605
-
606
- prefixesUsedForOutput(triples) {
607
- const used = new Set();
608
- const entries = this.__nonEmptyEntries();
609
- // One accumulator reused across every term, and stop the moment every prefix
610
- // is accounted for: the answer cannot grow after that.
611
- const iris = [];
612
- for (const t of triples) {
613
- if (used.size === entries.length) break;
614
- iris.length = 0;
615
- collectIrisInTerm(t.s, iris);
616
- if (!isRdfTypePred(t.p)) {
617
- collectIrisInTerm(t.p, iris);
618
- }
619
- collectIrisInTerm(t.o, iris);
620
- for (const iri of iris) {
621
- for (const [p, base] of entries) {
622
- if (iri.startsWith(base)) used.add(p);
623
- }
624
- }
625
- }
626
- const v = [];
627
- for (const p of used) {
628
- if (Object.prototype.hasOwnProperty.call(this.map, p)) v.push([p, this.map[p]]);
629
- }
630
- v.sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0));
631
- return v;
632
- }
633
- }
634
-
635
- // out is optional so the old "returns a fresh array" contract still holds, but
636
- // callers in a loop can pass one accumulator instead of allocating per term.
637
- function collectIrisInTerm(t, out = []) {
638
- if (t instanceof Iri) {
639
- out.push(t.value);
640
- } else if (t instanceof Literal) {
641
- const [, dt] = literalParts(t.value);
642
- if (dt) out.push(dt); // so rdf/xsd prefixes are emitted when only used in ^^...
643
- } else if (t instanceof ListTerm) {
644
- for (const x of t.elems) collectIrisInTerm(x, out);
645
- } else if (t instanceof OpenListTerm) {
646
- for (const x of t.prefix) collectIrisInTerm(x, out);
647
- } else if (t instanceof GraphTerm) {
648
- for (const tr of t.triples) {
649
- collectIrisInTerm(tr.s, out);
650
- collectIrisInTerm(tr.p, out);
651
- collectIrisInTerm(tr.o, out);
652
- }
653
- }
654
- return out;
655
- }
656
-
657
- function collectVarsInTerm(t, acc) {
658
- if (t instanceof Var) {
659
- acc.add(t.name);
660
- } else if (t instanceof ListTerm) {
661
- for (const x of t.elems) collectVarsInTerm(x, acc);
662
- } else if (t instanceof OpenListTerm) {
663
- for (const x of t.prefix) collectVarsInTerm(x, acc);
664
- acc.add(t.tailVar);
665
- } else if (t instanceof GraphTerm) {
666
- for (const tr of t.triples) {
667
- collectVarsInTerm(tr.s, acc);
668
- collectVarsInTerm(tr.p, acc);
669
- collectVarsInTerm(tr.o, acc);
670
- }
671
- }
672
- }
673
-
674
- function collectBlankLabelsInTerm(t, acc) {
675
- if (t instanceof Blank) {
676
- acc.add(t.label);
677
- } else if (t instanceof ListTerm) {
678
- for (const x of t.elems) collectBlankLabelsInTerm(x, acc);
679
- } else if (t instanceof OpenListTerm) {
680
- for (const x of t.prefix) collectBlankLabelsInTerm(x, acc);
681
- } else if (t instanceof GraphTerm) {
682
- for (const tr of t.triples) {
683
- collectBlankLabelsInTerm(tr.s, acc);
684
- collectBlankLabelsInTerm(tr.p, acc);
685
- collectBlankLabelsInTerm(tr.o, acc);
686
- }
687
- }
688
- }
689
-
690
- function collectBlankLabelsInTriples(triples) {
691
- const acc = new Set();
692
- for (const tr of triples) {
693
- collectBlankLabelsInTerm(tr.s, acc);
694
- collectBlankLabelsInTerm(tr.p, acc);
695
- collectBlankLabelsInTerm(tr.o, acc);
696
- }
697
- return acc;
698
- }
699
-
700
- function annotateQuotedGraphTerm(graph) {
701
- if (!(graph instanceof GraphTerm)) return graph;
702
- const labels = collectBlankLabelsInTriples(graph.triples);
703
- Object.defineProperty(graph, '__quotedLocalBlankLabels', {
704
- value: labels,
705
- enumerable: false,
706
- writable: true,
707
- configurable: true,
708
- });
709
- return graph;
710
- }
711
-
712
- function copyQuotedGraphMetadata(src, dst) {
713
- if (!(src instanceof GraphTerm) || !(dst instanceof GraphTerm)) return dst;
714
- if (!Object.prototype.hasOwnProperty.call(src, '__quotedLocalBlankLabels')) return dst;
715
-
716
- const labels = src.__quotedLocalBlankLabels;
717
- Object.defineProperty(dst, '__quotedLocalBlankLabels', {
718
- value: labels instanceof Set ? new Set(labels) : labels,
719
- enumerable: false,
720
- writable: true,
721
- configurable: true,
722
- });
723
- return dst;
724
477
  }
725
478
 
726
479
  export {
727
480
  RDF_NS,
728
- OWL_NS,
729
481
  LOG_NS,
730
- INTERNAL_BLANK_VAR_PREFIX,
731
482
  resolveIriRef,
732
483
  literalParts,
733
- Term,
734
484
  Iri,
735
485
  Literal,
736
- Var,
737
486
  Blank,
738
487
  ListTerm,
739
- OpenListTerm,
740
488
  GraphTerm,
741
489
  Triple,
742
- Rule,
743
490
  internIri,
744
491
  internLiteral,
745
492
  clearParserCaches,
746
- isLogImplies,
747
- isLogImpliedBy,
748
- isLogQuery,
749
493
  PrefixEnv,
750
- collectBlankLabelsInTriples,
751
- annotateQuotedGraphTerm,
752
- copyQuotedGraphMetadata,
753
494
  };