eyereasoner
Version:
Distributing the [EYE](https://github.com/eyereasoner/eye) reasoner for browser and node using WebAssembly.
54 lines (53 loc) • 1.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.N3Writer = void 0;
exports.write = write;
const n3_1 = require("n3");
function isQuoted(term, store) {
return term.termType === 'BlankNode' && store.getQuads(null, null, null, term).length > 0;
}
class N3Writer {
constructor() {
this._writer = new n3_1.Writer();
}
_encodePredicate(term) {
if (term.termType === 'NamedNode') {
switch (term.value) {
case 'http://www.w3.org/2000/10/swap/log#implies':
return '=>';
case 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type':
return 'a';
}
}
return this._writer._encodeIriOrBlank(term);
}
_encodeSubject(entity, store) {
if (isQuoted(entity, store)) {
return `{${this.quadsStoreToString(store, entity)}}`;
}
return this._writer._encodeSubject(entity);
}
_encodeObject(entity, store) {
if (isQuoted(entity, store)) {
return `{${this.quadsStoreToString(store, entity)}}`;
}
return this._writer._encodeObject(entity);
}
// ### `quadToString` serializes a quad as a string
quadToString(t, store) {
return `${this._encodeSubject(t.subject, store)} ${this._encodePredicate(t.predicate)} ${this._encodeObject(t.object, store)}`;
}
// ### `quadsToString` serializes an array of quads as a string
quadsStoreToString(store, graph = n3_1.DataFactory.defaultGraph()) {
return store.getQuads(null, null, null, graph)
.map(t => this.quadToString(t, store))
.join(' . ') + ' . ';
}
quadsToString(quads) {
return this.quadsStoreToString(new n3_1.Store(quads));
}
}
exports.N3Writer = N3Writer;
function write(quads) {
return (new N3Writer).quadsToString(quads);
}