Skip to content

Commit

Permalink
Fix #27
Browse files Browse the repository at this point in the history
  • Loading branch information
pietercolpaert committed Apr 3, 2024
1 parent f75ddd5 commit fd9aede
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 39 deletions.
52 changes: 32 additions & 20 deletions lib/CBDShapeExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,14 @@ export class CBDShapeExtractor {
graphsToIgnore?: Array<Term>,
): Promise<Array<Quad>> {
// First extract everything except for something within the graphs to ignore, or within the graph of the current entity, as that’s going to be added anyway later on
let dontExtractFromGraph: Array<string> = (
graphsToIgnore ? graphsToIgnore : []
).map((item) => {
return item.value;
});
if (!graphsToIgnore) {
graphsToIgnore = [];
}

const extractInstance = new ExtractInstance(
store,
this.dereferencer,
dontExtractFromGraph,
graphsToIgnore,
this.options,
this.shapesGraph,
);
Expand Down Expand Up @@ -239,21 +237,32 @@ class ExtractInstance {

dereferencer: RdfDereferencer;
options: CBDShapeExtractorOptions;
graphsToIgnore: string[];
graphs: Term [];

shapesGraph?: ShapesGraph;

constructor(
store: RdfStore,
dereferencer: RdfDereferencer,
graphsToIgnore: string[],
graphsToIgnore: Term[],
options: CBDShapeExtractorOptions,
shapesGraph?: ShapesGraph,
) {
this.store = store;
this.dereferencer = dereferencer;
this.shapesGraph = shapesGraph;
this.graphsToIgnore = graphsToIgnore;
//Turn graphs To Ignore into graphs
this.graphs = store.getQuads()
//only interested in the graph
.map((quad) => { return quad.graph })
// distinct graphs
.filter((graph, index, array) => {
return array.indexOf(graph) === index;
})
// Now filter on graphs that are not in the graphsToIgnore list
.filter((graph) => {
return graphsToIgnore.find((graphToIgnore) => { return graphToIgnore.equals(graph) }) === undefined;
});
this.options = options;
}

Expand Down Expand Up @@ -339,7 +348,7 @@ class ExtractInstance {
}

if (!shape?.closed) {
this.CBD(id, result, extracted, this.graphsToIgnore);
this.CBD(id, result, extracted, this.graphs);
}

// Next, on our newly fetched data,
Expand All @@ -358,7 +367,7 @@ class ExtractInstance {
)) {
if (!path.found(extracted) || shape.closed) {
let pathQuads = path
.match(this.store, extracted, id, this.graphsToIgnore)
.match(this.store, extracted, id, this.graphs)
.flatMap((pathResult) => {
return pathResult.path;
});
Expand All @@ -372,7 +381,7 @@ class ExtractInstance {
this.store,
extracted,
id,
this.graphsToIgnore,
this.graphs,
);

// I don't know how to do this correctly, but this is not the way
Expand Down Expand Up @@ -422,17 +431,20 @@ class ExtractInstance {
id: Term,
result: Quad[],
extractedStar: CbdExtracted,
graphsToIgnore: Array<string>,
graphs: Array<Term>,
) {
extractedStar.addCBDTerm(id);
const graph = this.options.cbdDefaultGraph ? df.defaultGraph() : null;
const quads = this.store.getQuads(id, null, null, graph);
let quads : Quad[] = [];
if (graphs) {
for (const graph of graphs) {
quads = quads.concat(this.store.getQuads(id, null, null, graph));
}
} else {
//search all graphs if graphs is not set
quads = this.store.getQuads(id, null, null, null)
}

for (const q of quads) {
// Ignore quads in the graphs to ignore
if (graphsToIgnore?.includes(q.graph.value)) {
continue;
}
result.push(q);

const next = extractedStar.push(q.predicate, false);
Expand All @@ -442,7 +454,7 @@ class ExtractInstance {
q.object.termType === "BlankNode" &&
!extractedStar.cbdExtracted(q.object)
) {
await this.CBD(q.object, result, next, graphsToIgnore);
await this.CBD(q.object, result, next, graphs);
}
}
}
Expand Down
40 changes: 25 additions & 15 deletions lib/Path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface Path {
store: RdfStore,
extracted: CbdExtracted,
focusNode: Term,
graphsToIgnore?: Array<string>,
graphs?: Array<Term>,
inverse?: boolean,
): PathResult[];
}
Expand All @@ -35,14 +35,24 @@ export class PredicatePath implements Path {
store: RdfStore,
extracted: CbdExtracted,
focusNode: Term,
graphsToIgnore: Array<string>,
graphs: Array<Term>,
inverse: boolean = false,
): PathResult[] {
let quads = (
inverse
? store.getQuads(null, this.predicate, focusNode, null)
: store.getQuads(focusNode, this.predicate, null, null)
).filter((q) => !graphsToIgnore.includes(q.graph.value));
let quads: Quad[] = [];
if (graphs) {
for (let graph of graphs) {
quads = quads.concat(
inverse?
store.getQuads(null, this.predicate, focusNode, graph) :
store.getQuads(focusNode, this.predicate, null, graph)
);
}
} else {
//search all graphs if graphs is not set
quads = inverse?
store.getQuads(null, this.predicate, focusNode, null) :
store.getQuads(focusNode, this.predicate, null, null);
}

if (quads.length > 0) {
let cbd: CbdExtracted = extracted.push(this.predicate, inverse);
Expand Down Expand Up @@ -82,7 +92,7 @@ export class SequencePath implements Path {
store: RdfStore,
extracted: CbdExtracted,
focusNode: Term,
graphsToIgnore: Array<string>,
graphs: Array<Term>,
inverse: boolean = false,
): PathResult[] {
let results: PathResult[] = [
Expand All @@ -98,7 +108,7 @@ export class SequencePath implements Path {
store,
res.cbdExtracted,
res.target,
graphsToIgnore,
graphs,
inverse,
);
return nexts.map((n) => ({
Expand Down Expand Up @@ -135,11 +145,11 @@ export class AlternativePath implements Path {
store: RdfStore,
extracted: CbdExtracted,
focusNode: Term,
graphsToIgnore: Array<string>,
graphs: Array<Term>,
inverse: boolean = false,
): PathResult[] {
return this.alternatives.flatMap((path) =>
path.match(store, extracted, focusNode, graphsToIgnore, inverse),
path.match(store, extracted, focusNode, graphs, inverse),
);
}
}
Expand All @@ -163,14 +173,14 @@ export class InversePath implements Path {
store: RdfStore,
extracted: CbdExtracted,
focusNode: Term,
graphsToIgnore: Array<string>,
graphs: Array<Term>,
inverse: boolean = false,
): PathResult[] {
return this.path.match(
store,
extracted,
focusNode,
graphsToIgnore,
graphs,
!inverse,
);
}
Expand All @@ -193,7 +203,7 @@ export abstract class MultiPath implements Path {
store: RdfStore,
extracted: CbdExtracted,
focusNode: Term,
graphsToIgnore: Array<string>,
graphs: Array<Term>,
inverse: boolean = false,
): PathResult[] {
const out: PathResult[] = [];
Expand All @@ -215,7 +225,7 @@ export abstract class MultiPath implements Path {
store,
t.cbdExtracted,
t.target,
graphsToIgnore,
graphs,
inverse,
)) {
if (this.filter(i, found)) {
Expand Down
9 changes: 5 additions & 4 deletions tests/06 - shapes and named graphs/extraction.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { assert } from "chai";
import { NamedNode, Parser, StreamParser, Term, Writer } from "n3";
import { CBDShapeExtractor } from "../../lib/CBDShapeExtractor";
import rdfDereference from "rdf-dereference";
import { RdfStore } from "rdf-stores";
import { DataFactory } from "rdf-data-factory";

describe("Check whether paths trigger the right extraction process", function () {
let shapeStore = RdfStore.createDefault();
let extractor: CBDShapeExtractor;
let dataStore = RdfStore.createDefault();
let df = new DataFactory();
before(async () => {
let readStream = (
await rdfDereference.dereference("./tests/06 - shapes and named graphs/shape.ttl", {
Expand All @@ -30,9 +31,9 @@ describe("Check whether paths trigger the right extraction process", function ()
it("Named Graphs should not conflict with the shape extraction", async () => {
let result = await extractor.extract(
dataStore,
new NamedNode("http://example.org/M1v1"),
new NamedNode("http://example.org/Shape"),
[new NamedNode("http://example.org/M1v2")] //Other members in the current context
df.namedNode("http://example.org/M1v1"),
df.namedNode("http://example.org/Shape"),
[df.namedNode("http://example.org/M1v2")] //Other members in the current context
);
// It should only have 2 quads: one outside of the named graph, and one in the named graph that is not part of the other named graphs
assert.equal(result.length, 2);
Expand Down

0 comments on commit fd9aede

Please sign in to comment.