Skip to content

Commit

Permalink
Added owl:imports in the bin - fixing #23
Browse files Browse the repository at this point in the history
  • Loading branch information
pietercolpaert committed Mar 23, 2024
1 parent 006300b commit 5c0cfee
Showing 1 changed file with 36 additions and 14 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

0 comments on commit 5c0cfee

Please sign in to comment.