eyemind 0.5.1 → 0.5.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/README.md +46 -16
- package/SPEC.md +11 -1
- package/package.json +1 -1
- package/src/cli.js +37 -5
- package/src/formats.js +149 -0
- package/src/n3-builtins.js +622 -0
- package/src/n3-documents.js +75 -0
- package/src/n3-parser.js +465 -0
- package/src/n3-writer.js +203 -0
- package/src/n3.js +881 -0
- package/src/node.js +37 -0
- package/src/rdf-lexer.js +2024 -0
- package/src/rdf-parser.js +827 -0
- package/src/rdf-prelude.js +808 -0
- package/src/rdf-rules.js +106 -0
- package/src/rdf-syntax.js +249 -0
- package/src/rdf.js +429 -0
- package/src/srl-analyze.js +703 -0
- package/src/srl-assignments.js +40 -0
- package/src/srl-builtin-names.js +78 -0
- package/src/srl-functions.js +584 -0
- package/src/srl-parser.js +816 -0
- package/src/srl-term.js +288 -0
- package/src/srl-tokenizer.js +341 -0
- package/src/srl.js +649 -0
- package/src/version.js +1 -1
package/src/n3-writer.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// eyemind conclusions written back as N3.
|
|
2
|
+
//
|
|
3
|
+
// The conclusions of a compiled program are rdf/3 terms, or result/3 terms
|
|
4
|
+
// when it has log:query rules. Terms map back as n3-to-eyemind wrote them; a
|
|
5
|
+
// Skolem term skolem(Rule, Variable, Arguments) is a blank node, the same one
|
|
6
|
+
// for the same rule firing.
|
|
7
|
+
import { readTerms } from './reader.js';
|
|
8
|
+
import { Struct, Var } from './terms.js';
|
|
9
|
+
import { write } from './writer.js';
|
|
10
|
+
import { LOG, RDF, XSD } from './n3-parser.js';
|
|
11
|
+
|
|
12
|
+
export class N3WriteError extends Error {}
|
|
13
|
+
|
|
14
|
+
// The triples of an eyemind conclusions document, in order and without
|
|
15
|
+
// duplicates. Also reports whether a contradiction halted the run.
|
|
16
|
+
export function triplesFromEyemind(text) {
|
|
17
|
+
const triples = [];
|
|
18
|
+
const seen = new Set();
|
|
19
|
+
let contradiction = false;
|
|
20
|
+
for (const [term] of readTerms(String(text))) {
|
|
21
|
+
if (term === 'false') { contradiction = true; continue; }
|
|
22
|
+
const t = tripleTerm(term);
|
|
23
|
+
if (!t) continue;
|
|
24
|
+
const k = write(t);
|
|
25
|
+
if (seen.has(k)) continue;
|
|
26
|
+
seen.add(k);
|
|
27
|
+
triples.push(t.args);
|
|
28
|
+
}
|
|
29
|
+
return { triples, contradiction };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function tripleTerm(term) {
|
|
33
|
+
if (!(term instanceof Struct)) return null;
|
|
34
|
+
if ((term.name === 'rdf' || term.name === 'result') && term.args.length === 3) return term;
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// N3 text for triples given as [S, P, O] eyemind terms. With strings
|
|
39
|
+
// 'auto', a result that has log:outputString triples is written as their
|
|
40
|
+
// strings, ordered by subject, as N3 reasoners do.
|
|
41
|
+
export function writeN3(triples, { prefixes = new Map(), strings = 'auto' } = {}) {
|
|
42
|
+
const w = new Writer(prefixes);
|
|
43
|
+
const outputs = triples.filter(([, p]) => isIri(p, `${LOG}outputString`));
|
|
44
|
+
if (strings === true || (strings === 'auto' && outputs.length > 0)) {
|
|
45
|
+
const keyed = outputs.map(([s, , o], i) => ({ s, o, i }));
|
|
46
|
+
keyed.sort((a, b) => compareKeys(a.s, b.s) || a.i - b.i);
|
|
47
|
+
return keyed.map(({ o }) => stringValue(o)).join('');
|
|
48
|
+
}
|
|
49
|
+
const body = triples.map(([s, p, o]) => `${w.term(s)} ${w.predicate(p)} ${w.term(o)} .`);
|
|
50
|
+
const used = [...prefixes].filter(([name]) => w.used.has(name));
|
|
51
|
+
const head = used.map(([name, ns]) => `@prefix ${name}: <${ns}> .`);
|
|
52
|
+
if (head.length && body.length) head.push('');
|
|
53
|
+
return [...head, ...body].map((line) => `${line}\n`).join('');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function eyemindToN3(text, options = {}) {
|
|
57
|
+
return writeN3(triplesFromEyemind(text).triples, options);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// A parsed N3 term as the eyemind term n3-to-eyemind writes for it.
|
|
61
|
+
export function termToEyemind(t) {
|
|
62
|
+
const st = (name, ...args) => new Struct(name, args);
|
|
63
|
+
switch (t.t) {
|
|
64
|
+
case 'iri': return new Struct('iri', [t.v]);
|
|
65
|
+
case 'var': return new Var(t.v);
|
|
66
|
+
case 'bnode': return st('bnode', 'n3', t.scope === 0 ? t.v : `${t.v}.${t.scope}`);
|
|
67
|
+
case 'int': return t.v;
|
|
68
|
+
case 'float': return t.v;
|
|
69
|
+
case 'literal': return st('literal', t.v, t.lang ? (t.dir ? st('lang', t.lang, t.dir) : st('lang', t.lang)) : st('datatype', t.dt));
|
|
70
|
+
case 'list': return t.items.reduceRight((tail, x) => st('.', termToEyemind(x), tail), '[]');
|
|
71
|
+
case 'graph': return st('graph', t.triples.reduceRight((tail, x) => st('.', st('rdf', ...tripleToEyemind(x)), tail), '[]'));
|
|
72
|
+
case 'triple': return st('triple', ...tripleToEyemind(t));
|
|
73
|
+
default: throw new N3WriteError(`unknown term ${t.t}`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
export const tripleToEyemind = (x) => [termToEyemind(x.s), termToEyemind(x.p), termToEyemind(x.o)];
|
|
77
|
+
|
|
78
|
+
const isIri = (t, v) => t instanceof Struct && t.name === 'iri' && t.args[0] === v;
|
|
79
|
+
|
|
80
|
+
function stringValue(o) {
|
|
81
|
+
if (o instanceof Struct && o.name === 'literal') return o.args[0];
|
|
82
|
+
if (typeof o === 'bigint' || typeof o === 'number') return String(o);
|
|
83
|
+
if (o instanceof Struct && o.name === 'iri') return o.args[0];
|
|
84
|
+
throw new N3WriteError('a log:outputString object must be a literal');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Subject order for strings: numbers by value before other terms, which
|
|
88
|
+
// compare by their text.
|
|
89
|
+
function compareKeys(a, b) {
|
|
90
|
+
const na = typeof a === 'bigint' || typeof a === 'number';
|
|
91
|
+
const nb = typeof b === 'bigint' || typeof b === 'number';
|
|
92
|
+
if (na && nb) return a < b ? -1 : a > b ? 1 : 0;
|
|
93
|
+
if (na !== nb) return na ? -1 : 1;
|
|
94
|
+
const ta = write(a);
|
|
95
|
+
const tb = write(b);
|
|
96
|
+
return ta < tb ? -1 : ta > tb ? 1 : 0;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const PN_LOCAL = /^(?:[A-Za-z_0-9À-�]|:)(?:[A-Za-z_0-9·À-�.:-]*[A-Za-z_0-9·À-�:-])?$/u;
|
|
100
|
+
|
|
101
|
+
class Writer {
|
|
102
|
+
constructor(prefixes) {
|
|
103
|
+
this.prefixes = [...prefixes].sort((a, b) => b[1].length - a[1].length);
|
|
104
|
+
this.used = new Set();
|
|
105
|
+
this.bnodes = new Map();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
iri(v) {
|
|
109
|
+
for (const [name, ns] of this.prefixes) {
|
|
110
|
+
if (v.startsWith(ns)) {
|
|
111
|
+
const local = v.slice(ns.length);
|
|
112
|
+
if (local === '' || PN_LOCAL.test(local)) {
|
|
113
|
+
this.used.add(name);
|
|
114
|
+
return `${name}:${local}`;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return `<${v.replace(/[\u0000- <>"{}|^`\\]/g, (c) => `\\u${c.charCodeAt(0).toString(16).padStart(4, '0')}`)}>`;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
predicate(p) {
|
|
122
|
+
if (isIri(p, `${RDF}type`)) return 'a';
|
|
123
|
+
if (isIri(p, `${LOG}implies`)) return '=>';
|
|
124
|
+
if (isIri(p, `${LOG}impliedBy`)) return '<=';
|
|
125
|
+
return this.term(p);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
bnode(key) {
|
|
129
|
+
if (!this.bnodes.has(key)) this.bnodes.set(key, `_:b${this.bnodes.size}`);
|
|
130
|
+
return this.bnodes.get(key);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
term(t) {
|
|
134
|
+
if (typeof t === 'bigint') return String(t);
|
|
135
|
+
if (typeof t === 'number') return floatText(t);
|
|
136
|
+
if (t instanceof Var) return `?${t.name.replace(/[^A-Za-z0-9_]/g, '_')}`;
|
|
137
|
+
if (t === '[]') return '()';
|
|
138
|
+
if (typeof t === 'string') throw new N3WriteError(`the atom ${JSON.stringify(t)} is not an N3 term`);
|
|
139
|
+
if (!(t instanceof Struct)) throw new N3WriteError('not an N3 term');
|
|
140
|
+
const [a, b, c] = t.args;
|
|
141
|
+
if (t.name === 'iri' && t.args.length === 1) return this.iri(a);
|
|
142
|
+
if (t.name === 'qvar' && t.args.length === 1 && typeof a === 'string') return `?${a.replace(/[^A-Za-z0-9_]/g, '_')}`;
|
|
143
|
+
if (t.name === 'qbnode' && t.args.length === 1 && typeof a === 'string') return `_:${a.replace(/[^A-Za-z0-9_]/g, '_')}`;
|
|
144
|
+
if (t.name === 'bnode' && t.args.length === 2) return this.bnode(`bnode ${write(t)}`);
|
|
145
|
+
if (t.name === 'skolem' && t.args.length === 3) return this.bnode(`skolem ${write(t)}`);
|
|
146
|
+
if (t.name === 'literal' && t.args.length === 2) return this.literal(a, b);
|
|
147
|
+
if (t.name === 'triple' && t.args.length === 3) return `<<( ${this.term(a)} ${this.predicate(b)} ${this.term(c)} )>>`;
|
|
148
|
+
if (t.name === 'graph' && t.args.length === 1) {
|
|
149
|
+
const items = listItems(a).map((x) => {
|
|
150
|
+
if (!(x instanceof Struct && x.name === 'rdf' && x.args.length === 3)) throw new N3WriteError('a formula holds rdf/3 triples');
|
|
151
|
+
return `${this.term(x.args[0])} ${this.predicate(x.args[1])} ${this.term(x.args[2])}`;
|
|
152
|
+
});
|
|
153
|
+
return items.length ? `{ ${items.join('. ')} }` : '{}';
|
|
154
|
+
}
|
|
155
|
+
if (t.name === '.' && t.args.length === 2) return `(${listItems(t).map((x) => this.term(x)).join(' ')})`;
|
|
156
|
+
throw new N3WriteError(`${write(t)} is not an N3 term`);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
literal(text, annotation) {
|
|
160
|
+
if (typeof text !== 'string') throw new N3WriteError(`a literal's text must be an atom, not ${write(text)}`);
|
|
161
|
+
const lex = quoteString(text);
|
|
162
|
+
if (annotation instanceof Struct && annotation.name === 'lang') {
|
|
163
|
+
const [lang, dir] = annotation.args;
|
|
164
|
+
return `${lex}@${lang}${dir ? `--${dir}` : ''}`;
|
|
165
|
+
}
|
|
166
|
+
if (annotation instanceof Struct && annotation.name === 'datatype') {
|
|
167
|
+
const dt = annotation.args[0];
|
|
168
|
+
if (dt === `${XSD}string`) return lex;
|
|
169
|
+
if (dt === `${XSD}boolean` && (text === 'true' || text === 'false')) return text;
|
|
170
|
+
if (dt === `${XSD}double` && /^[+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?$/.test(text) && Number.isFinite(Number(text))) {
|
|
171
|
+
const number = floatText(Number(text));
|
|
172
|
+
return /e/.test(number) ? number : `${number}e0`;
|
|
173
|
+
}
|
|
174
|
+
return `${lex}^^${this.iri(dt)}`;
|
|
175
|
+
}
|
|
176
|
+
throw new N3WriteError('a literal needs datatype(IRI) or lang(Tag)');
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function listItems(t) {
|
|
181
|
+
const items = [];
|
|
182
|
+
while (t instanceof Struct && t.name === '.' && t.args.length === 2) {
|
|
183
|
+
items.push(t.args[0]);
|
|
184
|
+
t = t.args[1];
|
|
185
|
+
}
|
|
186
|
+
if (t !== '[]') throw new N3WriteError('an open list is not an N3 term');
|
|
187
|
+
return items;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// A float as an N3 decimal, or a double when it needs an exponent.
|
|
191
|
+
function floatText(v) {
|
|
192
|
+
if (Object.is(v, -0)) return '-0.0';
|
|
193
|
+
const s = String(v);
|
|
194
|
+
if (/e/.test(s)) return s.replace('e+', 'e');
|
|
195
|
+
return s.includes('.') ? s : `${s}.0`;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function quoteString(text) {
|
|
199
|
+
if (text.includes('\n') && !text.includes('"""') && !text.endsWith('"')) {
|
|
200
|
+
return `"""${text.replace(/\\/g, '\\\\')}"""`;
|
|
201
|
+
}
|
|
202
|
+
return `"${text.replace(/[\\"\n\r\t]/g, (c) => ({ '\\': '\\\\', '"': '\\"', '\n': '\\n', '\r': '\\r', '\t': '\\t' })[c])}"`;
|
|
203
|
+
}
|