extract-cbd-shape
Version:
Extract an entity based on CBD and a SHACL shape
216 lines • 6.59 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZeroOrOnePath = exports.ZeroOrMorePath = exports.OneOrMorePath = exports.MultiPath = exports.InversePath = exports.AlternativePath = exports.SequencePath = exports.PredicatePath = void 0;
class PredicatePath {
literalType;
predicate;
constructor(predicate, literalType) {
this.literalType = literalType;
this.predicate = predicate;
}
toString() {
return `<${this.predicate.value}>`;
}
found(cbd, inverse = false) {
return cbd.enter(this.predicate, inverse);
}
match(store, extracted, focusNode, graphsToIgnore, inverse = false) {
let quads = (inverse
? store.getQuads(null, this.predicate, focusNode, null)
: store.getQuads(focusNode, this.predicate, null, null)).filter((q) => !graphsToIgnore.includes(q.graph.value));
if (quads.length > 0) {
let cbd = extracted.push(this.predicate, inverse);
return quads.map((quad) => {
const newFocusNode = inverse ? quad.subject : quad.object;
return { path: [quad], target: newFocusNode, cbdExtracted: cbd };
});
}
else {
return [];
}
}
}
exports.PredicatePath = PredicatePath;
class SequencePath {
literalType;
sequence;
constructor(sequence, literalType) {
this.literalType = literalType;
this.sequence = sequence;
}
found(cbd, inverse) {
let current = cbd;
for (const seq of this.sequence) {
if (current) {
current = seq.found(current, inverse);
}
}
return current;
}
toString() {
return this.sequence.map((x) => x.toString()).join("/");
}
match(store, extracted, focusNode, graphsToIgnore, inverse = false) {
let results = [
{
path: [],
target: focusNode,
cbdExtracted: extracted,
},
];
for (const path of this.sequence) {
results = results.flatMap((res) => {
const nexts = path.match(store, res.cbdExtracted, res.target, graphsToIgnore, inverse);
return nexts.map((n) => ({
path: [...res.path, ...n.path],
cbdExtracted: n.cbdExtracted,
target: n.target,
}));
});
}
return results;
}
}
exports.SequencePath = SequencePath;
class AlternativePath {
literalType;
alternatives;
constructor(alternatives, literalType) {
this.literalType = literalType;
this.alternatives = alternatives;
}
found(cbd, inverse) {
for (const option of this.alternatives) {
const maybe = option.found(cbd, inverse);
if (maybe)
return maybe;
}
return;
}
toString() {
return this.alternatives.map((x) => x.toString()).join("|");
}
match(store, extracted, focusNode, graphsToIgnore, inverse = false) {
return this.alternatives.flatMap((path) => path.match(store, extracted, focusNode, graphsToIgnore, inverse));
}
}
exports.AlternativePath = AlternativePath;
class InversePath {
literalType;
path;
constructor(path, literalType) {
this.literalType = literalType;
this.path = path;
}
found(cbd, inverse) {
return this.path.found(cbd, !inverse);
}
toString() {
return "^" + this.path.toString();
}
match(store, extracted, focusNode, graphsToIgnore, inverse = false) {
return this.path.match(store, extracted, focusNode, graphsToIgnore, !inverse);
}
}
exports.InversePath = InversePath;
class MultiPath {
literalType;
path;
maxCount;
constructor(path, maxCount, literalType) {
this.literalType = literalType;
this.path = path;
this.maxCount = maxCount;
}
match(store, extracted, focusNode, graphsToIgnore, inverse = false) {
const out = [];
let targets = [
{
path: [],
target: focusNode,
cbdExtracted: extracted,
},
];
for (let i = 0; true; i++) {
if (this.maxCount && i > this.maxCount)
break;
if (targets.length === 0)
break;
const newTargets = [];
for (const t of targets) {
if (this.filter(i, t)) {
out.push(t);
}
for (const found of this.path.match(store, t.cbdExtracted, t.target, graphsToIgnore, inverse)) {
newTargets.push({
path: [...t.path, ...found.path],
cbdExtracted: t.cbdExtracted,
target: found.target,
});
}
}
targets = newTargets;
}
return out;
}
}
exports.MultiPath = MultiPath;
class OneOrMorePath extends MultiPath {
constructor(path, literalType) {
super(path, undefined, literalType);
}
filter(times, _res) {
return times >= 1;
}
toString() {
return this.path.toString() + "+";
}
found(cbd, inverse) {
let newCbd = this.path.found(cbd, inverse);
if (!newCbd)
return;
let next = this.path.found(newCbd, inverse);
while (next) {
newCbd = next;
next = this.path.found(newCbd, inverse);
}
return newCbd;
}
}
exports.OneOrMorePath = OneOrMorePath;
class ZeroOrMorePath extends MultiPath {
constructor(path, literalType) {
super(path, undefined, literalType);
}
filter(_times, _res) {
return true;
}
toString() {
return this.path.toString() + "*";
}
found(cbd, inverse) {
let next = this.path.found(cbd, inverse);
while (next) {
cbd = next;
next = this.path.found(cbd, inverse);
}
return cbd;
}
}
exports.ZeroOrMorePath = ZeroOrMorePath;
class ZeroOrOnePath extends MultiPath {
constructor(path, literalType) {
super(path, 1, literalType);
}
filter(times, _res) {
return times < 2;
}
toString() {
return this.path.toString() + "?";
}
found(cbd, inverse) {
return this.path.found(cbd, inverse) || cbd;
}
}
exports.ZeroOrOnePath = ZeroOrOnePath;
//# sourceMappingURL=Path.js.map