UNPKG

lincd

Version:

LINCD is a JavaScript library for building user interfaces with linked data (also known as 'structured data', or RDF)

296 lines 9.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NodeSet = 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_js_1 = require("./CoreSet.js"); const models_js_1 = require("../models.js"); const QuadSet_js_1 = require("./QuadSet.js"); const QuadArray_js_1 = require("./QuadArray.js"); const Debug_js_1 = require("../utils/Debug.js"); const URI_js_1 = require("../utils/URI.js"); class NodeSet extends CoreSet_js_1.CoreSet { constructor(iterable) { super(iterable); } static fromValues(strings) { return new NodeSet(strings.map((s) => (URI_js_1.URI.isURI(s) ? models_js_1.NamedNode.getOrCreate(s) : new models_js_1.Literal(s)))); } //we cannot use NamedNodeSet here because of requirement loops getProperties(includeFromIncomingArcs = false) { var res = new NodeSet(); for (var node of this) { res = res.concat(node.getProperties(includeFromIncomingArcs)); } return res; } getInverseProperties() { var res = new NodeSet(); for (var node of this) { res = res.concat(node.getInverseProperties()); } return res; } getOne(property) { for (var node of this) { if (node.hasProperty(property)) { return node.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(); for (var node of this) { res = res.concat(node.getAll(property)); } return res; } getValues(property) { var res = []; for (var node of this) { res.push(node.getValue(property)); } return res; } /** * Returns an array of the URI's or literal values (for Literals) of the nodes in this set */ getNodeValues() { var res = []; for (var node of this) { res.push(node.value); } 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 node of this) { if (node.hasInverseProperty(property)) { return node.getOneInverse(property); } } return undefined; } getAllInverse(property) { var res = new NodeSet(); for (var node of this) { res = res.concat(node.getAllInverse(property)); } return res; } getMultipleInverse(properties) { var res = new 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(); for (var node of this) { for (var property of properties) { res = res.concat(node.getAll(property)); } } return res; } getDeep(property, maxDepth = Infinity) { var result = new NodeSet(); var stack = this; while (stack.size > 0 && maxDepth > 0) { var nextLevelStack = new NodeSet(); for (let node of stack) { for (var value of node.getAll(property)) { if (!result.has(value)) { result.add(value); nextLevelStack.add(value); } } } stack = nextLevelStack; maxDepth--; } return result; } getQuads(property) { var res = new QuadSet_js_1.QuadSet(); for (var node of this) { for (var quad of node.getQuads(property)) { res.add(quad); } } return res; } getInverseQuads(property) { var res = new QuadSet_js_1.QuadSet(); for (var node of this) { for (var quad of node.getInverseQuads(property)) { res.add(quad); } } return res; } getAllQuads(includeAsObject, includeImplicit = false) { var res = new QuadArray_js_1.QuadArray(); for (var node of this) { for (var item of node.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_js_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 node of this) { if (node.has(property, value)) { //as any apparently needed, strange that NamedNode is not seen as matching to R? res.add(node); } } return res; } getWhere(property, value) { for (var node of this) { if (node.has(property, value)) { return node; } } return undefined; } setEach(property, value) { let res = false; for (var node of this) { res = node.set(property, value) && res; } return res; } msetEach(property, values) { let res = false; for (var node of this) { res = node.mset(property, values) && res; } return res; } updateEach(property, value) { let res = false; for (var node of this) { res = node.overwrite(property, value) && res; } return res; } mupdateEach(property, values) { let res = false; for (var node of this) { res = node.moverwrite(property, values) && res; } return res; } unsetEach(property, value) { let res = false; for (var node of this) { res = node.unset(property, value) && res; } return res; } unsetAllEach(property) { let res = false; for (var node of this) { res = node.unsetAll(property) && res; } return res; } //@TODO: remove generic isLoaded now that we have Shape loading functionality? //@TODO: remove promiseLoaded now that we have Shape loading functionality? /** * @deprecated * @param loadInverseProperties */ promiseLoaded(loadInverseProperties = false) { return Promise.all(this.map((node) => node.promiseLoaded(loadInverseProperties))) .then((res) => { return res.every((result) => result === true); }) .catch(() => { return false; }); } // perhaps we need to add a new shapeIsLoaded() method? /** * @deprecated * @param includingInverseProperties */ isLoaded(includingInverseProperties = false) { return this.every((node) => node.isLoaded(includingInverseProperties)); } toString() { return ('NodeSet {\n' + [...this].map((node) => '\t' + node.toString()).join(',\n') + '\n}'); } print(includeIncomingProperties = true) { return Debug_js_1.Debug.print(this, includeIncomingProperties); } } exports.NodeSet = NodeSet; //# sourceMappingURL=NodeSet.js.map