Skip to content

Commit

Permalink
Merge branch 'main' into cleaner-log
Browse files Browse the repository at this point in the history
  • Loading branch information
ajuvercr authored Mar 27, 2024
2 parents 4e682f3 + 5c0cfee commit b4a4c20
Show file tree
Hide file tree
Showing 7 changed files with 7,499 additions and 251 deletions.
50 changes: 36 additions & 14 deletions bin/extract.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
import * as process from 'process';
import { CBDShapeExtractor } from '../lib/CBDShapeExtractor';
import {Writer, NamedNode} from 'n3';
import { Writer } from 'n3';
import { DataFactory } from 'rdf-data-factory';
import { RdfStore } from 'rdf-stores';
import rdfDereference from 'rdf-dereference';

const df = new DataFactory();

// Check if at least one command line argument is provided
if (process.argv.length <= 2) {
console.error('Please provide an entity to describe in the first command line parameter, and optionally a shape IRI to fulfill, and a IRI');
process.exit(1); // Exit with an error code
}

async function loadShape(shapeURL: string, shapeStore: RdfStore) {
let readStream = (await rdfDereference.dereference(shapeURL)).data;
let requested : string[] = [shapeURL]
await new Promise ((resolve, reject) => {
shapeStore.import(readStream).on("end",resolve)
.on("error", reject);
});
//Now check whether there are one or more owl:imports on the shapesgraph
let importsURLs = shapeStore.getQuads(null, df.namedNode('http://www.w3.org/2002/07/owl#imports'),null,null).map((quad) => {
return quad.object.value ;
});
//If the import has not been requested before, let’s request it
for (let imp of importsURLs) {
if (!requested.includes(imp)) {
requested.push(imp);
let newReadStream = (await rdfDereference.dereference(imp)).data;
await new Promise ((resolve, reject) => {
shapeStore.import(newReadStream).on("end",resolve)
.on("error", reject);
});
//check for new URLs
importsURLs = shapeStore.getQuads(null, df.namedNode('http://www.w3.org/2002/07/owl#imports'),null,null).map((quad) => {
return quad.object.value ;
});
}
}
}

async function main () {
// Get the command line parameter at index 2 (index 0 is the node executable and index 1 is the script file)
const entity = process.argv[2];
Expand All @@ -19,25 +51,15 @@ async function main () {
if(process.argv[3] === 'shape') {
//Use our own shape extractor shape
shapeId = "https://raw.githubusercontent.com/pietercolpaert/extract-cbd-shape/main/extract-cbd-shape-ap.ttl#ShapeShape";

let readStream = (await rdfDereference.dereference("./extract-cbd-shape-ap.ttl", { localFiles: true })).data;
await new Promise ((resolve, reject) => {
shapeStore.import(readStream).on("end",resolve)
.on("error", reject);
});
} else if (process.argv[3]) {
} else {
shapeId = process.argv[3];
let readStream = (await rdfDereference.dereference(shapeId)).data;
await new Promise ((resolve, reject) => {
shapeStore.import(readStream).on("end",resolve)
.on("error", reject);
});
}
await loadShape(shapeId, shapeStore);
}
let extractor = new CBDShapeExtractor(shapeStore);
console.error('Processing shape ' + shapeId + ' from this shape: ', extractor.shapesGraph);
let writer = new Writer();
let quads = await extractor.extract(RdfStore.createDefault(), new NamedNode(entity), new NamedNode(shapeId));
let quads = await extractor.extract(RdfStore.createDefault(), df.namedNode(entity), df.namedNode(shapeId));
writer.addQuads(quads);
writer.end((err, res) => {console.log(res);});
}
Expand Down
8 changes: 5 additions & 3 deletions lib/CBDShapeExtractor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import rdfDereference, { RdfDereferencer } from "rdf-dereference";
import { NodeLink, RDFMap, ShapesGraph, ShapeTemplate } from "./Shape";
import { Path, PathResult } from "./Path";
import { BlankNode, DefaultGraph } from "n3";
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");

const df = new DataFactory();

class DereferenceNeeded {
target: string;
msg?: string;
Expand Down Expand Up @@ -380,7 +382,7 @@ export class CBDShapeExtractor {
graphsToIgnore: Array<string>,
) {
extractedStar.addCBDTerm(id);
const graph = this.options.cbdDefaultGraph ? new DefaultGraph() : null;
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
Expand All @@ -395,7 +397,7 @@ export class CBDShapeExtractor {

// Conditionally get more quads: if it’s a not yet extracted blank node
if (
q.object instanceof BlankNode &&
q.object.termType === 'BlankNode' &&
!extractedStar.cbdExtracted(q.object)
) {
// Only perform CBD again recursively on the blank node
Expand Down
3 changes: 1 addition & 2 deletions lib/Shape.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { BlankNode } from "n3";
import { RdfStore } from "rdf-stores";
import { Term } from "@rdfjs/types";
import { DataFactory } from 'rdf-data-factory';
Expand Down Expand Up @@ -178,7 +177,7 @@ export class ShapesGraph {
}

protected constructPathPattern(shapeStore: RdfStore, listItem: Term): Path {
if (listItem instanceof BlankNode) {
if (listItem.termType === 'BlankNode') {
//Look for special types
let zeroOrMorePathObjects = getObjects(shapeStore,
listItem,
Expand Down
Loading

0 comments on commit b4a4c20

Please sign in to comment.