lincd
Version:
LINCD is a JavaScript library for building user interfaces with linked data (also known as 'structured data', or RDF)
105 lines • 4.24 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Datafactory = void 0;
const models_1 = require("./models");
const NodeURIMappings_1 = require("./collections/NodeURIMappings");
const QuadSet_1 = require("./collections/QuadSet");
const CoreSet_1 = require("./collections/CoreSet");
const CoreMap_1 = require("./collections/CoreMap");
class Datafactory {
constructor(config) {
this.quads = new QuadSet_1.QuadSet();
this.emitEvents = true;
this.triggerStorage = false;
for (let key in config) {
this[key] = config[key];
}
if (!(config === null || config === void 0 ? void 0 : config.nodeMap)) {
this.nodeMap = new NodeURIMappings_1.NodeURIMappings();
}
this.quad = this.quad.bind(this);
this.blankNode = this.blankNode.bind(this);
this.namedNode = this.namedNode.bind(this);
this.literal = this.literal.bind(this);
if (config === null || config === void 0 ? void 0 : config.overwriteData) {
this.clearedProps = new CoreMap_1.CoreMap();
}
}
// startBlanknodeSpace() {
// this.nodeMap = new NodeURIMappings();
// }
// endBlanknodeSpace() {
// this.nodeMap = null;
// }
//TODO:
// Variable variable(DOMString value);
// Term fromTerm(Term original);
// Quad fromQuad(Quad original);
namedNode(uri) {
return this.nodeMap.getOrCreateNamedNode(uri);
}
literal(value, languageOrDatatype) {
if (languageOrDatatype instanceof models_1.NamedNode) {
return new models_1.Literal(value, languageOrDatatype);
}
else {
return new models_1.Literal(value, null, languageOrDatatype);
}
}
blankNode(value) {
//when using start/end blanknode space you can let the factory reuse the same blank nodes
// if (this.nodeMap) {
return this.nodeMap.getOrCreateBlankNode(value);
// }
// return BlankNode.getOrCreate(value);
}
defaultGraph() {
return models_1.defaultGraph;
}
quad(subject, predicate, object, graph) {
//if a target graph is given, we always use that, regardless of whether there was any graph present in the data
//else if a graph was in the data, use that, or fall back to default graph
if (this.targetGraph) {
graph = this.targetGraph;
}
else if (!graph) {
graph = models_1.defaultGraph;
}
//in LINCD we use Graph objects which extend NamedNode
//but when parsing with N3 we get NamedNode objects
if (graph instanceof models_1.NamedNode) {
graph = models_1.Graph.getOrCreate(graph.uri);
}
//sometimes we want to update the graph with new data coming in from JSONLD
//so if overwrite data is true, we clear old data for any subj/pred combination we find
if (this.overwriteData &&
(!this.clearedProps.has(subject) ||
!this.clearedProps.get(subject).has(predicate))) {
//remove without triggering storage events
subject.getQuads(predicate).removeAll(false);
if (!this.clearedProps.has(subject)) {
this.clearedProps.set(subject, new CoreSet_1.CoreSet());
}
this.clearedProps.get(subject).add(predicate);
}
let quad;
if (this.preventNewQuads) {
quad = models_1.Quad.get(subject, predicate, object, graph);
if (quad) {
this.quads.add(quad);
}
else {
//NOTE: this is not standard, so preventNewQuads will not work with other tools
//But it is useful for LINCD, where we want to prevent new quads from being created. Like when we share quads to be removed with other threads
return true;
}
}
else {
quad = models_1.Quad.getOrCreate(subject, predicate, object, graph, false, this.triggerStorage, this.emitEvents);
this.quads.add(quad);
}
return quad;
}
}
exports.Datafactory = Datafactory;
//# sourceMappingURL=Datafactory.js.map