extract-cbd-shape
Version:
Extract an entity based on CBD and a SHACL shape
126 lines • 4.07 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RDFMap = exports.ShapeTemplate = exports.ShapeError = exports.NodeLink = void 0;
//TODO: split this file up between Shape functionality and SHACL to our Shape class conversion steps. Also introduce a ShEx to Shape Template
class NodeLink {
pathPattern;
link;
constructor(pathPattern, link) {
this.pathPattern = pathPattern;
this.link = link;
}
}
exports.NodeLink = NodeLink;
class ShapeError {
type;
errors = [];
constructor(type, errors = []) {
this.type = type;
this.errors = errors;
}
toString() {
if (this.errors.length === 1) {
return this.errors[0].toString();
}
else {
const sep = this.type == "and" ? " && " : " || ";
return "(" + this.errors.map((x) => x.toString()).join(sep) + ")";
}
}
}
exports.ShapeError = ShapeError;
class ShapeTemplate {
closed;
nodeLinks;
requiredPaths;
optionalPaths;
atLeastOneLists;
label;
constructor() {
//All properties will be added, but if a required property is not available, then we need to further look it up
this.requiredPaths = [];
//If there’s a nodelink through one of the properties, I want to know what other shape to look up in the shapes graph from there
this.nodeLinks = [];
this.atLeastOneLists = [];
this.optionalPaths = [];
this.closed = false; //default value
}
fillPathsAndLinks(extraPaths, extraNodeLinks) {
for (let list of this.atLeastOneLists) {
for (let item of list) {
extraPaths.push(...item.requiredPaths);
extraPaths.push(...item.optionalPaths);
// extraPaths.push(...item.nodeLinks.map((x) => x.pathPattern));
extraNodeLinks.push(...item.nodeLinks);
item.fillPathsAndLinks(extraPaths, extraNodeLinks);
}
}
}
invalidAtLeastOneLists(extract) {
const out = new ShapeError("and");
for (let list of this.atLeastOneLists) {
const sub = new ShapeError("or");
let atLeastOne = false;
for (let item of list) {
const error = item.requiredAreNotPresent(extract);
if (error) {
sub.errors.push(error);
}
else {
atLeastOne = true;
break;
}
}
if (!atLeastOne) {
out.errors.push(sub);
}
}
if (out.errors.length > 0) {
return out;
}
return;
}
requiredPathsAreNotPresent(extract) {
const errors = this.requiredPaths.filter((path) => !path.found(extract));
if (errors.length > 0) {
return new ShapeError("and", errors);
}
else {
return;
}
}
requiredAreNotPresent(extract) {
const required = this.requiredPathsAreNotPresent(extract);
const atLeastOne = this.invalidAtLeastOneLists(extract);
if (required && atLeastOne) {
return new ShapeError("and", [...required.errors, ...atLeastOne.errors]);
}
if (required)
return required;
if (atLeastOne)
return atLeastOne;
}
}
exports.ShapeTemplate = ShapeTemplate;
class RDFMap {
namedNodes = new Map();
blankNodes = new Map();
set(node, item) {
if (node.termType === "NamedNode") {
this.namedNodes.set(node.value, item);
}
if (node.termType === "BlankNode") {
this.blankNodes.set(node.value, item);
}
}
get(node) {
if (node.termType === "NamedNode") {
return this.namedNodes.get(node.value);
}
if (node.termType === "BlankNode") {
return this.blankNodes.get(node.value);
}
}
}
exports.RDFMap = RDFMap;
//# sourceMappingURL=Shape.js.map