Skip to content

Commit

Permalink
use debug for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ajuvercr committed Mar 27, 2024
1 parent b11046e commit 4e682f3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 29 deletions.
66 changes: 39 additions & 27 deletions lib/CBDShapeExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { Path, PathResult } from "./Path";
import { BlankNode, DefaultGraph } from "n3";
import { RdfStore } from "rdf-stores";
import { Quad, Term } from "@rdfjs/types";
import debug from "debug";

const log = debug("cbdExtracted");

class DereferenceNeeded {
target: string;
Expand Down Expand Up @@ -37,10 +40,9 @@ export class CBDShapeExtractor {
dereferencer?: RdfDereferencer<Quad>,
options: Partial<CBDShapeExtractorOptions> = {},
) {
this.options = {
cbdDefaultGraph: options.cbdDefaultGraph || false,
fetch: options.fetch,
};
// Assign with default options
this.options = Object.assign({ cbdDefaultGraph: false }, options);

if (!dereferencer) {
this.dereferencer = rdfDereference;
} else {
Expand Down Expand Up @@ -124,7 +126,9 @@ export class CBDShapeExtractor {
shapeId?: Term,
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
const logger = log.extend("extract");

// 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) => {
Expand All @@ -136,13 +140,16 @@ export class CBDShapeExtractor {
target: string,
msg?: string,
) => Promise<Quad[]> = async (target: string, msg?: string) => {
const ms = msg ? ` (${msg})` : "";
console.error("Maybe dereferencing " + target + ms);
if (dereferenced.indexOf(target) == -1) {
logger(`Dereferencing ${target} ${msg ?? ""}`);
dereferenced.push(target);
await this.loadQuadStreamInStore(
store,
(await this.dereferencer.dereference(target, {fetch: this.options.fetch})).data,
(
await this.dereferencer.dereference(target, {
fetch: this.options.fetch,
})
).data,
);

return await tryExtract();
Expand Down Expand Up @@ -182,7 +189,7 @@ export class CBDShapeExtractor {

const result = await tryExtract();

//When returning the quad array, remove duplicate triples as CBD, required properties, etc. could have added multiple times the same triple
// When returning the quad array, remove duplicate triples as CBD, required properties, etc. could have added multiple times the same triple
return result.filter((value: Quad, index: number, array: Quad[]) => {
return index === array.findIndex((x) => x.equals(value));
});
Expand Down Expand Up @@ -221,7 +228,7 @@ export class CBDShapeExtractor {
offline: boolean,
shapeId?: Term | ShapeTemplate,
): Promise<void> {
//If it has already been extracted, don’t extract it again: prevents cycles
// If it has already been extracted, don’t extract it again: prevents cycles
if (extracted.cbdExtracted(id)) {
return;
}
Expand All @@ -234,7 +241,7 @@ export class CBDShapeExtractor {
shape = this.shapesGraph.shapes.get(shapeId);
}

//Perform CBD and we’re done, except on the condition there’s a shape defined and it’s closed
// Perform CBD and we’re done, except on the condition there’s a shape defined and it’s closed
if (!(shape && shape.closed)) {
this.CBD(result, extracted, store, id, graphsToIgnore);
}
Expand All @@ -252,7 +259,7 @@ export class CBDShapeExtractor {
let extraPaths: Path[] = [];
let extraNodeLinks: NodeLink[] = [];

//Process atLeastOneLists in extraPaths and extra NodeLinks
// Process atLeastOneLists in extraPaths and extra NodeLinks
this.recursivelyProcessAtLeastOneLists(
extracted,
shape,
Expand All @@ -268,7 +275,9 @@ export class CBDShapeExtractor {
let pathQuads = path
.match(store, extracted, id, graphsToIgnore)
.map((pathResult: PathResult) => {
//if the shape is open and thus CBD is going to take place, remove the first element from the quads list of the matches, if the subject of that first item is the focusnode (otherwise the first element was a reverse path)
// if the shape is open and thus CBD is going to take place,
// and the subject of that first item is the focusnode (otherwise the first element was a reverse path)
// remove the first element from the quads list of the matches,
if (
!shape!.closed &&
pathResult.path[0].subject.value === id.value
Expand All @@ -279,7 +288,7 @@ export class CBDShapeExtractor {
})
.flat()
.filter((quad) => {
//Make sure we don’t add quads multiple times
// Make sure we don’t add quads multiple times
if (!visited.find((x) => x.equals(quad))) {
visited.push(quad);
return true;
Expand All @@ -288,7 +297,7 @@ export class CBDShapeExtractor {
return false;
});

result.push(...pathQuads); //concat all quad paths in the results
result.push(...pathQuads); // concat all quad paths in the results
}
}

Expand Down Expand Up @@ -317,7 +326,9 @@ export class CBDShapeExtractor {
nodeLink.pathPattern.match(store, extracted, id, graphsToIgnore),
)
.map((pathResult: PathResult) => {
//if the shape is open and thus CBD is going to take place, remove the first element from the quads list of the matches, if the subject of that first item is the focusnode (otherwise the first element was a reverse path)
// if the shape is open and thus CBD is going to take place
// and if the subject of that first item is the focusnode (otherwise the first element was a reverse path)
// remove the first element from the quads list of the matches,
if (
!shape?.closed &&
pathResult.path[0].subject.value === id.value
Expand All @@ -328,8 +339,8 @@ export class CBDShapeExtractor {
})
.flat()
.filter((quad) => {
//Make sure we don’t add quads multiple times
//There must be a more efficient solution to making sure there’s only one of each triple...
// Make sure we don’t add quads multiple times
// There must be a more efficient solution to making sure there’s only one of each triple...
if (!visited.find((x) => x.equals(quad))) {
visited.push(quad);
return true;
Expand All @@ -342,7 +353,7 @@ export class CBDShapeExtractor {
}

if (!offline && id.termType === "NamedNode" && shape) {
//Check required paths and lazy evaluate the atLeastOneLists
// Check required paths and lazy evaluate the atLeastOneLists
const problems = shape.requiredAreNotPresent(extracted);
if (problems) {
throw new DereferenceNeeded(
Expand All @@ -355,10 +366,11 @@ export class CBDShapeExtractor {

/**
* Performs Concise Bounded Description: extract star-shape and recurses over the blank nodes
* @param result
* @param store
* @param id
* @param extracted
* @param result list of quads
* @param extractedStar topology object to keep track of already found properties
* @param store store to use for cbd
* @param id starting subject
* @param graphsToIgnore
*/
public async CBD(
result: Quad[],
Expand All @@ -371,9 +383,9 @@ export class CBDShapeExtractor {
const graph = this.options.cbdDefaultGraph ? new DefaultGraph() : null;
const quads = store.getQuads(id, null, null, graph);

//Iterate over the quads, add them to the result and check whether we should further get other quads based on blank nodes or the SHACL shape
// Iterate over the quads, add them to the result and check whether we should further get other quads based on blank nodes or the SHACL shape
for (const q of quads) {
//ignore quads in the graphs to ignore
// Ignore quads in the graphs to ignore
if (graphsToIgnore?.includes(q.graph.value)) {
continue;
}
Expand All @@ -386,12 +398,12 @@ export class CBDShapeExtractor {
q.object instanceof BlankNode &&
!extractedStar.cbdExtracted(q.object)
) {
//only perform CBD again recursively on the blank node
// Only perform CBD again recursively on the blank node
await this.CBD(result, next, store, q.object, graphsToIgnore);
}
}

//Should we also take into account RDF* and/or RDF reification systems here?
// Should we also take into account RDF* and/or RDF reification systems here?
}
}

Expand Down
21 changes: 19 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"license": "MIT",
"dependencies": {
"@treecg/types": "^0.4.5",
"debug": "^4.3.4",
"jsdom": "^23.0.1",
"n3": "^1.17.0",
"rdf-data-factory": "^1.1.2",
Expand All @@ -29,6 +30,7 @@
"@rdfjs/types": "*",
"@types/benchmark": "^2.1.5",
"@types/chai": "^4.3.5",
"@types/debug": "^4.1.12",
"@types/mocha": "^10.0.1",
"@types/n3": "^1.16.1",
"@types/sinon": "^10.0.16",
Expand Down

0 comments on commit 4e682f3

Please sign in to comment.