eyemind 0.9.17 → 0.9.18
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/LS-SPEC.md +1 -1
- package/README.md +5 -0
- package/package.json +1 -1
- package/src/messages.js +1 -1
- package/src/n3-writer.js +0 -4
- package/src/rdf-lexer.js +24 -516
- package/src/rdf-parser.js +33 -261
- package/src/rdf-prelude.js +0 -259
- package/src/rdf.js +4 -7
- package/src/srl-analyze.js +0 -22
- package/src/srl-term.js +0 -53
- package/src/version.js +1 -1
- package/src/rdf-rules.js +0 -106
package/src/rdf-prelude.js
CHANGED
|
@@ -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
|
};
|
package/src/rdf.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// RDF 1.2 in and out of eyemind: Turtle, TriG, N-Triples and N-Quads.
|
|
2
2
|
//
|
|
3
3
|
// Documents are read by the RDF lexer and parser (rdf-lexer.js,
|
|
4
|
-
// rdf-parser.js
|
|
5
|
-
//
|
|
4
|
+
// rdf-parser.js and rdf-prelude.js), with the RDF 1.2 surface checks
|
|
5
|
+
// below. Each quad becomes an eyemind term
|
|
6
6
|
// rdf(Subject, Predicate, Object, Graph):
|
|
7
7
|
//
|
|
8
8
|
// IRI iri('https://example.org/a')
|
|
@@ -46,9 +46,7 @@ export function parseRdf(text, { format = 'turtle', base = null, scope = 'rdf' }
|
|
|
46
46
|
try {
|
|
47
47
|
checkRdfSyntax(text, format);
|
|
48
48
|
assertRdf12SurfaceSyntax(text, format);
|
|
49
|
-
const parser = new Parser(lex(text, {
|
|
50
|
-
// RDF declares every prefix it uses; N3's predeclared ones do not apply.
|
|
51
|
-
parser.prefixes.map = {};
|
|
49
|
+
const parser = new Parser(lex(text, { trig: format === 'trig' }));
|
|
52
50
|
if (base) parser.prefixes.setBase(base);
|
|
53
51
|
const state = { scope, lists: 0, out: [] };
|
|
54
52
|
if (format === 'n-quads') {
|
|
@@ -62,8 +60,7 @@ export function parseRdf(text, { format = 'turtle', base = null, scope = 'rdf' }
|
|
|
62
60
|
quad(s, p, o, g, state);
|
|
63
61
|
}
|
|
64
62
|
} else {
|
|
65
|
-
const [, triples
|
|
66
|
-
if (forward.length || backward.length || queries.length) throw new SyntaxError('an RDF document has no rules');
|
|
63
|
+
const [, triples] = parser.parseDocument();
|
|
67
64
|
for (const t of triples) {
|
|
68
65
|
if (format === 'trig' && t.p instanceof Iri && t.p.value === `${LOG_NS}nameOf` && t.o instanceof GraphTerm) {
|
|
69
66
|
const g = convert(t.s, 'graph', DEFAULT_GRAPH, state);
|
package/src/srl-analyze.js
CHANGED
|
@@ -595,28 +595,6 @@ function boundVariables(clauses, initialBound = new Set()) {
|
|
|
595
595
|
return vars;
|
|
596
596
|
}
|
|
597
597
|
|
|
598
|
-
function bodyVariables(clauses) {
|
|
599
|
-
const vars = new Set();
|
|
600
|
-
for (const clause of clauses) {
|
|
601
|
-
if (clause.type === 'triple' || clause.type === 'path') collectTripleVars(clause.triple, vars);
|
|
602
|
-
if ((clause.type === 'set' || clause.type === 'bind')) {
|
|
603
|
-
vars.add(clause.variable);
|
|
604
|
-
for (const v of expressionVariables(clause.expr)) vars.add(v);
|
|
605
|
-
}
|
|
606
|
-
if (clause.type === 'filter') for (const v of expressionVariables(clause.expr)) vars.add(v);
|
|
607
|
-
if (clause.type === 'not') for (const v of bodyVariables(clause.body)) vars.add(v);
|
|
608
|
-
if (clause.type === 'group') {
|
|
609
|
-
for (const key of clause.keys) vars.add(key);
|
|
610
|
-
for (const aggregation of clause.aggregations) {
|
|
611
|
-
vars.add(aggregation.variable);
|
|
612
|
-
if (aggregation.function.argument !== '*') vars.add(aggregation.function.argument);
|
|
613
|
-
}
|
|
614
|
-
for (const v of bodyVariables(clause.pattern)) vars.add(v);
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
return vars;
|
|
618
|
-
}
|
|
619
|
-
|
|
620
598
|
function collectTripleVars(triple, vars) {
|
|
621
599
|
for (const term of [triple.s, triple.p, triple.o]) collectTermVars(term, vars);
|
|
622
600
|
}
|
package/src/srl-term.js
CHANGED
|
@@ -5,7 +5,6 @@ const RDF_FIRST = `${RDF_NS}first`;
|
|
|
5
5
|
const RDF_REST = `${RDF_NS}rest`;
|
|
6
6
|
const RDF_NIL = `${RDF_NS}nil`;
|
|
7
7
|
const RDF_REIFIES = `${RDF_NS}reifies`;
|
|
8
|
-
const XSD_STRING = 'http://www.w3.org/2001/XMLSchema#string';
|
|
9
8
|
const XSD_BOOLEAN = 'http://www.w3.org/2001/XMLSchema#boolean';
|
|
10
9
|
const XSD_INTEGER = 'http://www.w3.org/2001/XMLSchema#integer';
|
|
11
10
|
const XSD_DECIMAL = 'http://www.w3.org/2001/XMLSchema#decimal';
|
|
@@ -64,21 +63,6 @@ function termKey(term) {
|
|
|
64
63
|
return JSON.stringify(term);
|
|
65
64
|
}
|
|
66
65
|
|
|
67
|
-
function cloneTerm(term) {
|
|
68
|
-
if (!term) return term;
|
|
69
|
-
if (term.type === 'triple') return tripleTerm(cloneTerm(term.s), cloneTerm(term.p), cloneTerm(term.o));
|
|
70
|
-
return { ...term };
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function escapeString(value) {
|
|
74
|
-
return String(value)
|
|
75
|
-
.replace(/\\/g, '\\\\')
|
|
76
|
-
.replace(/\n/g, '\\n')
|
|
77
|
-
.replace(/\r/g, '\\r')
|
|
78
|
-
.replace(/\t/g, '\\t')
|
|
79
|
-
.replace(/"/g, '\\"');
|
|
80
|
-
}
|
|
81
|
-
|
|
82
66
|
function compactIRI(value, prefixes = {}) {
|
|
83
67
|
if (value === RDF_TYPE) return 'a';
|
|
84
68
|
const entries = Object.entries(prefixes)
|
|
@@ -94,42 +78,6 @@ function compactIRI(value, prefixes = {}) {
|
|
|
94
78
|
return `<${value}>`;
|
|
95
79
|
}
|
|
96
80
|
|
|
97
|
-
// A number may drop its datatype only when the shorthand reads back as the
|
|
98
|
-
// same datatype: an INTEGER carries neither `.` nor exponent, a DECIMAL a
|
|
99
|
-
// `.`, a DOUBLE an exponent ([95]-[97]). `72` written bare for an
|
|
100
|
-
// xsd:double would come back as an xsd:integer, so such a literal keeps
|
|
101
|
-
// its datatype; a whole-numbered decimal gains the `.0` that makes it one.
|
|
102
|
-
function numericShorthand(value, datatype) {
|
|
103
|
-
const text = String(value);
|
|
104
|
-
const hasExponent = /[eE]/.test(text);
|
|
105
|
-
const hasDot = text.includes('.');
|
|
106
|
-
if (!datatype || datatype === XSD_INTEGER) return !hasDot && !hasExponent ? text : null;
|
|
107
|
-
if (datatype === XSD_DECIMAL) return hasExponent ? null : (hasDot ? text : `${text}.0`);
|
|
108
|
-
if (datatype === XSD_DOUBLE) return hasExponent ? text : null;
|
|
109
|
-
return null;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function formatTerm(term, prefixes = {}) {
|
|
113
|
-
if (term.type === 'iri') return compactIRI(term.value, prefixes);
|
|
114
|
-
if (term.type === 'blank') return `_:${term.value}`;
|
|
115
|
-
if (term.type === 'var') return `?${term.value}`;
|
|
116
|
-
if (term.type === 'triple') return `<<(${formatTerm(term.s, prefixes)} ${formatTerm(term.p, prefixes)} ${formatTerm(term.o, prefixes)})>>`;
|
|
117
|
-
if (term.type === 'literal') {
|
|
118
|
-
const v = term.value;
|
|
119
|
-
if (typeof v === 'bigint' && !term.lang && (!term.datatype || term.datatype === XSD_INTEGER)) return String(v);
|
|
120
|
-
if (typeof v === 'number' && Number.isFinite(v) && !term.lang) {
|
|
121
|
-
const shorthand = numericShorthand(v, term.datatype);
|
|
122
|
-
if (shorthand !== null) return shorthand;
|
|
123
|
-
}
|
|
124
|
-
if (typeof v === 'boolean' && !term.lang && (!term.datatype || term.datatype === XSD_BOOLEAN)) return v ? 'true' : 'false';
|
|
125
|
-
const lexical = `"${escapeString(v)}"`;
|
|
126
|
-
if (term.lang) return `${lexical}@${term.lang}${term.langDir ? `--${term.langDir}` : ''}`;
|
|
127
|
-
if (term.datatype && term.datatype !== XSD_STRING) return `${lexical}^^${compactIRI(term.datatype, prefixes)}`;
|
|
128
|
-
return lexical;
|
|
129
|
-
}
|
|
130
|
-
return String(term.value ?? term);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
81
|
export {
|
|
134
82
|
termKey,
|
|
135
83
|
RDF_NS,
|
|
@@ -138,7 +86,6 @@ export {
|
|
|
138
86
|
RDF_REST,
|
|
139
87
|
RDF_NIL,
|
|
140
88
|
RDF_REIFIES,
|
|
141
|
-
XSD_STRING,
|
|
142
89
|
XSD_BOOLEAN,
|
|
143
90
|
XSD_INTEGER,
|
|
144
91
|
XSD_DECIMAL,
|
package/src/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// The release this checkout is; ./release keeps it in step with package.json.
|
|
2
|
-
export const VERSION = '0.9.
|
|
2
|
+
export const VERSION = '0.9.18';
|
package/src/rdf-rules.js
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* eyemind: RDF rule helpers
|
|
3
|
-
*
|
|
4
|
-
* Built-in rule helpers and utilities used by the engine. This is not the
|
|
5
|
-
* inference engine itself, but shared rule-related machinery.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
LOG_NS,
|
|
11
|
-
Iri,
|
|
12
|
-
Var,
|
|
13
|
-
Blank,
|
|
14
|
-
ListTerm,
|
|
15
|
-
OpenListTerm,
|
|
16
|
-
GraphTerm,
|
|
17
|
-
Triple,
|
|
18
|
-
copyQuotedGraphMetadata,
|
|
19
|
-
INTERNAL_BLANK_VAR_PREFIX,
|
|
20
|
-
} from './rdf-prelude.js';
|
|
21
|
-
|
|
22
|
-
function liftBlankRuleVars(premise, conclusion) {
|
|
23
|
-
function isLogIncludesLikePredicate(p) {
|
|
24
|
-
return p instanceof Iri && (p.value === LOG_NS + 'includes' || p.value === LOG_NS + 'notIncludes');
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// Map blank labels to stable rule-local variable names.
|
|
28
|
-
// This runs at rule construction time; keep it simple and allocation-light.
|
|
29
|
-
const mapping = Object.create(null);
|
|
30
|
-
let counter = 0;
|
|
31
|
-
|
|
32
|
-
// These variables are implementation details used to correlate one blank
|
|
33
|
-
// node across the lifted rule body. Their names must not be spellable by
|
|
34
|
-
// user input; otherwise a user variable such as ?_b1 can capture the
|
|
35
|
-
// lifted blank node and leak it into the rule head. The public custom
|
|
36
|
-
// builtin API maps these names back to ordinary-looking ?_bN variables.
|
|
37
|
-
function blankToVar(label) {
|
|
38
|
-
let name = mapping[label];
|
|
39
|
-
if (name === undefined) {
|
|
40
|
-
counter += 1;
|
|
41
|
-
name = `${INTERNAL_BLANK_VAR_PREFIX}${counter}`;
|
|
42
|
-
mapping[label] = name;
|
|
43
|
-
}
|
|
44
|
-
return new Var(name);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function copyQuotedTerm(t) {
|
|
48
|
-
// Quoted formulas are data terms with their own local blank scope.
|
|
49
|
-
// Copy them structurally so later in-place rewrites cannot mutate shared AST,
|
|
50
|
-
// but do not lift their blank nodes into rule-body variables.
|
|
51
|
-
if (t instanceof ListTerm) return new ListTerm(t.elems.map(copyQuotedTerm));
|
|
52
|
-
if (t instanceof OpenListTerm) return new OpenListTerm(t.prefix.map(copyQuotedTerm), t.tailVar);
|
|
53
|
-
if (t instanceof GraphTerm) {
|
|
54
|
-
const triples = t.triples.map(
|
|
55
|
-
(tr) => new Triple(copyQuotedTerm(tr.s), copyQuotedTerm(tr.p), copyQuotedTerm(tr.o)),
|
|
56
|
-
);
|
|
57
|
-
return copyQuotedGraphMetadata(t, new GraphTerm(triples));
|
|
58
|
-
}
|
|
59
|
-
return t;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function convertQuotedPatternTerm(t) {
|
|
63
|
-
if (t instanceof Blank) return blankToVar(t.label);
|
|
64
|
-
if (t instanceof ListTerm) return new ListTerm(t.elems.map(convertQuotedPatternTerm));
|
|
65
|
-
if (t instanceof OpenListTerm) return new OpenListTerm(t.prefix.map(convertQuotedPatternTerm), t.tailVar);
|
|
66
|
-
if (t instanceof GraphTerm) {
|
|
67
|
-
const triples = t.triples.map(
|
|
68
|
-
(tr) =>
|
|
69
|
-
new Triple(convertQuotedPatternTerm(tr.s), convertQuotedPatternTerm(tr.p), convertQuotedPatternTerm(tr.o)),
|
|
70
|
-
);
|
|
71
|
-
return copyQuotedGraphMetadata(t, new GraphTerm(triples));
|
|
72
|
-
}
|
|
73
|
-
return t;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function convertTerm(t, allowDirectQuotedPattern = false) {
|
|
77
|
-
if (t instanceof Blank) return blankToVar(t.label);
|
|
78
|
-
if (t instanceof ListTerm) return new ListTerm(t.elems.map((e) => convertTerm(e, false)));
|
|
79
|
-
if (t instanceof OpenListTerm)
|
|
80
|
-
return new OpenListTerm(
|
|
81
|
-
t.prefix.map((e) => convertTerm(e, false)),
|
|
82
|
-
t.tailVar,
|
|
83
|
-
);
|
|
84
|
-
if (t instanceof GraphTerm) return allowDirectQuotedPattern ? convertQuotedPatternTerm(t) : copyQuotedTerm(t);
|
|
85
|
-
return t;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const newPremise = premise.map((tr) => {
|
|
89
|
-
// In log:includes / log:notIncludes, quoted formula operands are formulas
|
|
90
|
-
// consumed by the builtin rather than ordinary triple patterns. Keep their
|
|
91
|
-
// local blank nodes as Blank terms so the builtin can treat them as local
|
|
92
|
-
// existentials, and bindings returned from an explicit scope are blank nodes
|
|
93
|
-
// instead of synthetic rule variables.
|
|
94
|
-
const keepFormulaBlanks = isLogIncludesLikePredicate(tr.p);
|
|
95
|
-
return new Triple(
|
|
96
|
-
keepFormulaBlanks && tr.s instanceof GraphTerm ? copyQuotedTerm(tr.s) : convertTerm(tr.s, true),
|
|
97
|
-
convertTerm(tr.p, true),
|
|
98
|
-
keepFormulaBlanks && tr.o instanceof GraphTerm ? copyQuotedTerm(tr.o) : convertTerm(tr.o, true),
|
|
99
|
-
);
|
|
100
|
-
});
|
|
101
|
-
return [newPremise, conclusion];
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export {
|
|
105
|
-
liftBlankRuleVars,
|
|
106
|
-
};
|