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.
- package/LS-SPEC.md +1 -1
- package/README.md +25 -9
- package/package.json +1 -1
- package/src/cli.js +7 -7
- package/src/formats.js +18 -6
- package/src/index.js +1 -1
- package/src/jsonld.js +906 -0
- package/src/messages.js +104 -4
- package/src/n3-writer.js +0 -4
- package/src/node-messages.js +2 -1
- package/src/rdf-lexer.js +35 -520
- package/src/rdf-parser.js +36 -261
- package/src/rdf-prelude.js +0 -259
- package/src/rdf-xml.js +363 -0
- package/src/rdf.js +96 -14
- package/src/srl-analyze.js +0 -22
- package/src/srl-term.js +0 -53
- package/src/version.js +1 -1
- package/src/xml.js +174 -0
- package/src/rdf-rules.js +0 -106
package/src/messages.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
// RDF Message Logs: delimit datasets before parsing them, so blank nodes and
|
|
2
2
|
// assertions stay local to a message. The replay view is ordinary N3 data.
|
|
3
3
|
// See https://w3c-cg.github.io/rsp/spec/messages and its companion tests.
|
|
4
|
-
import { DEFAULT_GRAPH, parseRdf } from './rdf.js';
|
|
4
|
+
import { DEFAULT_GRAPH, parseRdf, parseRdfXmlLog, writeRdf } from './rdf.js';
|
|
5
5
|
import { readSources } from './n3.js';
|
|
6
6
|
import { tripleToEyemind } from './n3-writer.js';
|
|
7
7
|
import { EyemindError } from './terms.js';
|
|
8
8
|
|
|
9
|
-
export const MESSAGE_NS = 'https://eyereasoner.github.io/
|
|
9
|
+
export const MESSAGE_NS = 'https://eyereasoner.github.io/eyemind/vocab/message#';
|
|
10
10
|
const RDF_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
|
|
11
11
|
const LOG_NAME = 'http://www.w3.org/2000/10/swap/log#nameOf';
|
|
12
12
|
const GRAPH_CONTENT = 'https://eyereasoner.github.io/eyemind/graph#content';
|
|
@@ -15,6 +15,8 @@ const triple = (s, p, o) => ({ s, p: iri(p), o });
|
|
|
15
15
|
const int = (v) => ({ t: 'int', v: BigInt(v) });
|
|
16
16
|
const wordEnd = (c) => c === undefined || /[\s<>"'{}\[\]();,]/.test(c);
|
|
17
17
|
const iriText = (v) => v.replace(/[\u0000-\u0020<>"{}|^`\\]/g, (c) => `\\u${c.charCodeAt(0).toString(16).padStart(4, '0')}`);
|
|
18
|
+
// The start of an RDF/XML message log: its rdfm:MessageLog root.
|
|
19
|
+
const XML_LOG = /^\s*(?:<\?xml[^>]*\?>\s*)?(?:<!--[\s\S]*?-->\s*)*<(?:[\w.-]+:)?MessageLog[\s>/]/;
|
|
18
20
|
function trivia(s, at, final) {
|
|
19
21
|
while (at < s.length) {
|
|
20
22
|
if (/\s/.test(s[at])) at++;
|
|
@@ -30,9 +32,12 @@ function trivia(s, at, final) {
|
|
|
30
32
|
// Accept strings incrementally. Only the current message and active directive
|
|
31
33
|
// context are retained; onMessage runs before reading any further input.
|
|
32
34
|
export class RdfMessageReader {
|
|
33
|
-
constructor({ format = 'trig', base = null, scope = 'messages', onMessage = () => {}, probe = false } = {}) {
|
|
34
|
-
if (!['turtle', 'trig', 'n-triples', 'n-quads'].includes(format)) throw new EyemindError(`RDF Messages do not support the format ${format}`);
|
|
35
|
+
constructor({ format = 'trig', base = null, scope = 'messages', onMessage = () => {}, probe = false, loadDocument = null } = {}) {
|
|
36
|
+
if (!['turtle', 'trig', 'n-triples', 'n-quads', 'rdf-xml', 'ndjson-ld'].includes(format)) throw new EyemindError(`RDF Messages do not support the format ${format}`);
|
|
35
37
|
this.format = format;
|
|
38
|
+
this.loadDocument = loadDocument;
|
|
39
|
+
this.context = undefined;
|
|
40
|
+
this.line = 0;
|
|
36
41
|
this.base = base;
|
|
37
42
|
this.scope = scope;
|
|
38
43
|
this.onMessage = onMessage;
|
|
@@ -55,6 +60,20 @@ export class RdfMessageReader {
|
|
|
55
60
|
write(text) {
|
|
56
61
|
if (this.stopped) return;
|
|
57
62
|
this.buffer += text;
|
|
63
|
+
// An RDF/XML log is one XML document, read when it is complete.
|
|
64
|
+
if (this.format === 'rdf-xml') return;
|
|
65
|
+
if (this.format === 'ndjson-ld') {
|
|
66
|
+
for (let end = this.buffer.indexOf('\n'); end >= 0 && !this.stopped; end = this.buffer.indexOf('\n')) {
|
|
67
|
+
const line = this.buffer.slice(0, end);
|
|
68
|
+
this.buffer = this.buffer.slice(end + 1);
|
|
69
|
+
this.ndjsonLine(line.replace(/\r$/, ''));
|
|
70
|
+
}
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (this.probe && XML_LOG.test(this.buffer)) {
|
|
74
|
+
this.isLog = true;
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
58
77
|
this.scan(false);
|
|
59
78
|
if (this.probe) {
|
|
60
79
|
this.buffer = this.buffer.slice(this.at);
|
|
@@ -64,6 +83,24 @@ export class RdfMessageReader {
|
|
|
64
83
|
|
|
65
84
|
finish() {
|
|
66
85
|
if (this.stopped) { this.buffer = ''; return; }
|
|
86
|
+
if (this.format === 'ndjson-ld') {
|
|
87
|
+
if (this.buffer !== '') this.ndjsonLine(this.buffer.replace(/\r$/, ''));
|
|
88
|
+
this.buffer = '';
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (this.format === 'rdf-xml') {
|
|
92
|
+
try {
|
|
93
|
+
for (const message of parseRdfXmlLog(this.buffer, { base: this.base, scope: this.scope })) {
|
|
94
|
+
this.index = message.index;
|
|
95
|
+
if (this.onMessage(message) === false) break;
|
|
96
|
+
}
|
|
97
|
+
} catch (error) {
|
|
98
|
+
throw new EyemindError(`RDF Message Log: ${error.message}`);
|
|
99
|
+
} finally {
|
|
100
|
+
this.buffer = '';
|
|
101
|
+
}
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
67
104
|
this.scan(true);
|
|
68
105
|
if (!this.probe) {
|
|
69
106
|
if (!this.isLog) throw new EyemindError('RDF Message Log needs a VERSION "*-messages" directive');
|
|
@@ -72,6 +109,39 @@ export class RdfMessageReader {
|
|
|
72
109
|
this.buffer = '';
|
|
73
110
|
}
|
|
74
111
|
|
|
112
|
+
// A line of an NDJSON-LD log: a context message, whose @context is the
|
|
113
|
+
// active context for the messages that follow, an empty message {}, or a
|
|
114
|
+
// data message, read as the document {"@context": C, "@graph": message}.
|
|
115
|
+
// https://w3c-cg.github.io/rsp/spec/messages#ndjson-ld
|
|
116
|
+
ndjsonLine(line) {
|
|
117
|
+
this.line++;
|
|
118
|
+
let object;
|
|
119
|
+
try {
|
|
120
|
+
object = JSON.parse(line);
|
|
121
|
+
} catch {
|
|
122
|
+
object = null;
|
|
123
|
+
}
|
|
124
|
+
if (line.trim() === '') throw new EyemindError(`RDF Message Log line ${this.line}: NDJSON-LD allows no blank lines`);
|
|
125
|
+
if (object === null || typeof object !== 'object' || Array.isArray(object)) {
|
|
126
|
+
throw new EyemindError(`RDF Message Log line ${this.line}: each line of NDJSON-LD is one JSON object`);
|
|
127
|
+
}
|
|
128
|
+
const keys = Object.keys(object);
|
|
129
|
+
if (keys.length === 1 && keys[0] === '@context') {
|
|
130
|
+
this.context = object['@context'];
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const document = keys.length === 0 ? null : this.context === undefined ? object : { '@context': this.context, '@graph': object };
|
|
134
|
+
const number = this.index + 1;
|
|
135
|
+
let quads = [];
|
|
136
|
+
try {
|
|
137
|
+
if (document) quads = parseRdf(JSON.stringify(document), { format: 'json-ld', base: this.base, scope: `${this.scope}#message${number}`, loadDocument: this.loadDocument }).quads;
|
|
138
|
+
} catch (error) {
|
|
139
|
+
throw new EyemindError(`RDF Message Log message ${number}: ${error.message}`);
|
|
140
|
+
}
|
|
141
|
+
this.isLog = true;
|
|
142
|
+
this.stopped = this.onMessage({ quads, prefixes: {}, base: this.base, index: ++this.index }) === false;
|
|
143
|
+
}
|
|
144
|
+
|
|
75
145
|
emit(text, boundary) {
|
|
76
146
|
if (this.probe) return;
|
|
77
147
|
const line = this.format === 'n-triples' || this.format === 'n-quads';
|
|
@@ -216,6 +286,7 @@ export class RdfMessageReader {
|
|
|
216
286
|
}
|
|
217
287
|
|
|
218
288
|
export function isRdfMessageLog(text) {
|
|
289
|
+
if (XML_LOG.test(text)) return true;
|
|
219
290
|
const reader = new RdfMessageReader({ probe: true });
|
|
220
291
|
try {
|
|
221
292
|
reader.write(String(text));
|
|
@@ -261,3 +332,32 @@ export function parseRdfMessages(text, options = {}) {
|
|
|
261
332
|
reader.finish();
|
|
262
333
|
return { ...replayMessages(messages, options), messages };
|
|
263
334
|
}
|
|
335
|
+
|
|
336
|
+
// An RDF Message Log of messages, each its quads: VERSION "1.2-messages"
|
|
337
|
+
// and a MESSAGE delimiter between messages in Turtle, TriG, N-Triples and
|
|
338
|
+
// N-Quads, an rdf:RDF per message in RDF/XML, and a line per message in
|
|
339
|
+
// NDJSON-LD. A final empty message needs one more delimiter, as a trailing
|
|
340
|
+
// delimiter alone opens no message.
|
|
341
|
+
export function writeRdfMessages(messages, { format = 'trig', prefixes = {} } = {}) {
|
|
342
|
+
if (format === 'ndjson-ld') {
|
|
343
|
+
return messages.map((quads) => `${quads.length ? JSON.stringify({ '@graph': JSON.parse(writeRdf(quads, { format: 'json-ld' })) }) : '{}'}\n`).join('');
|
|
344
|
+
}
|
|
345
|
+
if (format === 'rdf-xml') {
|
|
346
|
+
let version = '1.0';
|
|
347
|
+
const parts = messages.map((quads) => {
|
|
348
|
+
const text = writeRdf(quads, { format: 'rdf-xml', prefixes });
|
|
349
|
+
if (text.startsWith('<?xml version="1.1"')) version = '1.1';
|
|
350
|
+
return text.replace(/^<\?xml[^>]*\?>\n/, '').replace(/^/gm, ' ').trimEnd();
|
|
351
|
+
});
|
|
352
|
+
return `<?xml version="${version}" encoding="utf-8"?>\n<rdfm:MessageLog xmlns:rdfm="https://w3c-cg.github.io/rsp/spec/messages#">\n` +
|
|
353
|
+
parts.map((p) => `${p}\n`).join('') + '</rdfm:MessageLog>\n';
|
|
354
|
+
}
|
|
355
|
+
if (!['turtle', 'trig', 'n-triples', 'n-quads'].includes(format)) throw new EyemindError(`RDF Messages do not support the format ${format}`);
|
|
356
|
+
const out = ['VERSION "1.2-messages"\n'];
|
|
357
|
+
messages.forEach((quads, i) => {
|
|
358
|
+
if (i > 0) out.push('MESSAGE\n');
|
|
359
|
+
out.push(writeRdf(quads, { format, prefixes }));
|
|
360
|
+
});
|
|
361
|
+
if (messages.length && !messages.at(-1).length) out.push('MESSAGE\n');
|
|
362
|
+
return out.join('');
|
|
363
|
+
}
|
package/src/n3-writer.js
CHANGED
|
@@ -60,10 +60,6 @@ export function writeN3(triples, { prefixes = new Map(), strings = 'auto', blank
|
|
|
60
60
|
return [...head, ...body].map((line) => `${line}\n`).join('');
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
export function eyemindToN3(text, options = {}) {
|
|
64
|
-
return writeN3(triplesFromEyemind(text).triples, options);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
63
|
// A parsed N3 term as the eyemind term n3-to-eyemind writes for it.
|
|
68
64
|
export function termToEyemind(t) {
|
|
69
65
|
const st = (name, ...args) => new Struct(name, args);
|
package/src/node-messages.js
CHANGED
|
@@ -11,6 +11,7 @@ const remote = (name) => /^https?:/.test(name);
|
|
|
11
11
|
async function messageFile(name) {
|
|
12
12
|
if (name === '-' || remote(name)) return true;
|
|
13
13
|
if (/\.js$/i.test(name)) return false;
|
|
14
|
+
if (rdfMessageFormat(name) === 'ndjson-ld') return true;
|
|
14
15
|
const probe = new RdfMessageReader({ probe: true });
|
|
15
16
|
for await (const text of createReadStream(name, { encoding: 'utf8' })) {
|
|
16
17
|
probe.write(text);
|
|
@@ -59,7 +60,7 @@ export async function streamMessages(request, io) {
|
|
|
59
60
|
if (await messageFile(name)) logs.push({ name, index });
|
|
60
61
|
else inputs.push([name, io.readFile(name)]);
|
|
61
62
|
}
|
|
62
|
-
if (!logs.length) throw new Error('--stream-messages needs an RDF Message Log
|
|
63
|
+
if (!logs.length) throw new Error('--stream-messages needs an RDF Message Log: VERSION "*-messages", an rdfm:MessageLog in RDF/XML, or NDJSON-LD (.ndjson)');
|
|
63
64
|
for (const { name, index } of logs) {
|
|
64
65
|
const source = await openSource(name);
|
|
65
66
|
const scope = request.scope(name, index);
|