Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use debug for logging #24

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { DataFactory } from "rdf-data-factory";
import { RdfStore } from "rdf-stores";
import { Quad, Term } from "@rdfjs/types";
import debug from "debug";

const log = debug("cbdExtracted");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d try to be more helpful here: cbdExtracted is ambiguous. Something like: Member being extracted using extract-cbd-shape


const df = new DataFactory();

Expand Down Expand Up @@ -39,10 +42,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 @@ -126,7 +128,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 @@ -138,13 +142,16 @@ export class CBDShapeExtractor {
target: string,
msg?: string,
) => Promise<Quad[]> = async (target: string, msg?: string) => {
const ms = msg ? ` (${msg})` : "";
if (dereferenced.indexOf(target) == -1) {
console.error("Dereferencing " + target + ms);
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 @@ -184,7 +191,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 @@ -223,7 +230,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 @@ -236,7 +243,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 @@ -254,7 +261,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 @@ -270,7 +277,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 @@ -281,7 +290,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 @@ -290,7 +299,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 @@ -319,7 +328,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 @@ -330,8 +341,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 @@ -344,7 +355,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 @@ -357,10 +368,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 @@ -373,9 +385,9 @@ export class CBDShapeExtractor {
const graph = this.options.cbdDefaultGraph ? df.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 @@ -388,12 +400,12 @@ export class CBDShapeExtractor {
q.object.termType === '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
17 changes: 17 additions & 0 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
Loading