lincd
Version:
LINCD is a JavaScript library for building user interfaces with linked data (also known as 'structured data', or RDF)
274 lines • 9.35 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ShapeSet = void 0;
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
const CoreSet_1 = require("./CoreSet");
const QuadSet_1 = require("./QuadSet");
const QuadArray_1 = require("./QuadArray");
const NodeSet_1 = require("./NodeSet");
class ShapeSet extends CoreSet_1.CoreSet {
constructor(iterable) {
super(iterable);
}
/**
* Returns true if this set contains the exact same shape OR a shape that has same node and is an instance of the same class as the given shape
* Why? you can create two identical shapes of the same node, but they are not the same instance
* To avoid having to account for that, by default, ShapeSets return true if the node matches and the shapes are instances of the same class
* @param value the shape you want to check for
* @param matchOnNodes set to false if you only want to check for true matches of identical instances
*/
has(value, matchOnNodes = true) {
return super.has(value) || (matchOnNodes && this.some(shape => shape.node === value.node && Object.getPrototypeOf(shape) === Object.getPrototypeOf(value)));
}
delete(value) {
//if we can find a shape with the same node, delete that
return super.delete(this.find(shape => shape.node === value.node));
}
//we cannot use NamedNodeSet here because of requirement loops
getProperties(includeFromIncomingArcs = false) {
var res = new NodeSet_1.NodeSet();
for (var instance of this) {
res = res.concat(instance.getProperties(includeFromIncomingArcs));
}
return res;
}
concat(...sets) {
var res = this.createNew(this);
for (var set of sets) {
set.forEach(item => {
//for shape sets we need to manually check if an equivalent shape is already in the resulting shape set, to avoid duplicates
if (!res.has(item)) {
res.add(item);
}
});
}
return res;
}
getInverseProperties() {
var res = new NodeSet_1.NodeSet();
for (var instance of this) {
res = res.concat(instance.getInverseProperties());
}
return res;
}
getOne(property) {
for (var instance of this) {
if (instance.hasProperty(property)) {
return instance.getOne(property);
}
}
return undefined;
}
/**
* Returns a NodeSet containing the merged results of node.get(property) for each node in this set
* @param property
* @returns {NodeSet}
*/
getAll(property) {
var res = new NodeSet_1.NodeSet();
for (var instance of this) {
res = res.concat(instance.getAll(property));
}
return res;
}
getOneFromPath(...properties) {
//NOTE: same implementation as in NamedNode
//we just need one, so we do a depth-first algorithm which will be more performant, so:
//take first property
var property = properties.shift();
//if more properties left
if (properties.length > 0) {
var res;
//check if any of the values of that property for this node
//has a path to the rest of the properties, and if so return the found value
for (var value of this.getAll(property)) {
if ((res = value.getOneFromPath(...properties))) {
return res;
}
}
}
else {
//return the first value possible
return this.getOne(property);
}
}
getAllFromPath(...properties) {
//we just need all paths, so we can do a breadth first implementation
//take first property
var property = properties.shift();
if (properties.length > 0) {
//and ask the whole set of values to return all values of the rest of the path
return this.getAll(property).getAllFromPath(...properties);
}
else {
return this.getAll(property);
}
}
getOneInverse(property) {
for (var instance of this) {
if (instance.hasInverseProperty(property)) {
return instance.getOneInverse(property);
}
}
return undefined;
}
getAllInverse(property) {
var res = new NodeSet_1.NodeSet();
for (var instance of this) {
res = res.concat(instance.getAllInverse(property));
}
return res;
}
getMultipleInverse(properties) {
var res = new NodeSet_1.NodeSet();
for (var instance of this) {
for (var property of properties) {
res = res.concat(instance.getAllInverse(property));
}
}
return res;
}
getMultiple(properties) {
var res = new NodeSet_1.NodeSet();
for (var instance of this) {
for (var property of properties) {
res = res.concat(instance.getAll(property));
}
}
return res;
}
getDeep(property, maxDepth = Infinity) {
return this.getNodes().getDeep(property, maxDepth);
}
getQuads(property) {
var res = new QuadSet_1.QuadSet();
for (var instance of this) {
for (var quad of instance.getQuads(property)) {
res.add(quad);
}
}
return res;
}
getInverseQuads(property) {
var res = new QuadSet_1.QuadSet();
for (var instance of this) {
for (var quad of instance.getInverseQuads(property)) {
res.add(quad);
}
}
return res;
}
getAllQuads(includeAsObject, includeImplicit = false) {
var res = new QuadArray_1.QuadArray();
for (var instance of this) {
for (var item of instance.getAllQuads(includeAsObject, includeImplicit)) {
if (res.indexOf(item) === -1) {
res.push(item);
}
}
//res = res.concat(node.getAllQuads(includeAsObject));
}
return res;
}
getAllInverseQuads(includeImplicit) {
var res = new QuadArray_1.QuadArray();
for (var node of this) {
for (var item of node.getAllInverseQuads(includeImplicit)) {
if (res.indexOf(item) === -1) {
res.push(item);
}
}
}
return res;
}
where(property, value) {
//TODO: test performance with
//return this.filter(r => r.has(property,value));
var res = this.createNew();
for (var instance of this) {
if (instance.has(property, value)) {
//as any apparently needed, strange that NamedNode is not seen as matching to R?
res.add(instance);
}
}
return res;
}
getWhere(property, value) {
for (var instance of this) {
if (instance.has(property, value)) {
return instance;
}
}
return undefined;
}
setEach(property, value) {
let res = false;
for (var instance of this) {
res = instance.set(property, value) && res;
}
return res;
}
msetEach(property, values) {
let res = false;
for (var instance of this) {
res = instance.mset(property, values) && res;
}
return res;
}
updateEach(property, value) {
let res = false;
for (var instance of this) {
res = instance.overwrite(property, value) && res;
}
return res;
}
mupdateEach(property, values) {
let res = false;
for (var instance of this) {
res = instance.moverwrite(property, values) && res;
}
return res;
}
unsetEach(property, value) {
let res = false;
for (var instance of this) {
res = instance.unset(property, value) && res;
}
return res;
}
unsetAllEach(property) {
let res = false;
for (var instance of this) {
res = instance.unsetAll(property) && res;
}
return res;
}
getNodes() {
var res = new NodeSet_1.NodeSet();
for (var instance of this) {
res.add(instance.node);
}
return res;
}
promiseLoaded(loadInverseProperties = false) {
return Promise.all(this.map((instance) => instance.promiseLoaded(loadInverseProperties)))
.then((res) => {
return res.every((result) => result === true);
})
.catch(() => {
return false;
});
}
isLoaded(includingInverseProperties = false) {
return this.every((instance) => instance.isLoaded(includingInverseProperties));
}
toString() {
return 'ShapeSet {\n' + [...this].map((instance) => '\t' + instance.toString()).join(',\n') + '\n}';
}
}
exports.ShapeSet = ShapeSet;
//# sourceMappingURL=ShapeSet.js.map