Skip to content

Commit

Permalink
Merge pull request #15 from TREEcg/feat-rdfstore
Browse files Browse the repository at this point in the history
RdfStore instead of N3store, and changed types to RDFJS types
  • Loading branch information
pietercolpaert authored Feb 17, 2024
2 parents caa8185 + 475a22e commit 3694c01
Show file tree
Hide file tree
Showing 20 changed files with 166 additions and 127 deletions.
7 changes: 4 additions & 3 deletions bin/extract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as process from 'process';
import { CBDShapeExtractor } from '../lib/CBDShapeExtractor';
import {Store, Writer, NamedNode} from 'n3';
import {Writer, NamedNode} from 'n3';
import { RdfStore } from 'rdf-stores';
import rdfDereference from 'rdf-dereference';

// Check if at least one command line argument is provided
Expand All @@ -11,7 +12,7 @@ if (process.argv.length <= 2) {
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];
let shapeStore:Store = new Store();
let shapeStore:RdfStore = RdfStore.createDefault();
let shapeId = "";
if (process.argv[3]) {
//A shape type has been set!
Expand All @@ -36,7 +37,7 @@ async function main () {
let extractor = new CBDShapeExtractor(shapeStore);
console.error('Processing shape ' + shapeId + ' from this shape: ', extractor.shapesGraph);
let writer = new Writer();
let quads = await extractor.extract(new Store(), new NamedNode(entity), new NamedNode(shapeId));
let quads = await extractor.extract(RdfStore.createDefault(), new NamedNode(entity), new NamedNode(shapeId));
writer.addQuads(quads);
writer.end((err, res) => {console.log(res);});
}
Expand Down
21 changes: 11 additions & 10 deletions lib/CBDShapeExtractor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import rdfDereference, { RdfDereferencer } from "rdf-dereference";
import { NodeLink, RDFMap, ShapesGraph, ShapeTemplate } from "./Shape";
import { Path, PathResult } from "./Path";
import { BlankNode, DefaultGraph, Store } from "n3";
import { BlankNode, DefaultGraph } from "n3";
import { RdfStore } from "rdf-stores";
import { Quad, Term } from "@rdfjs/types";

class DereferenceNeeded {
Expand Down Expand Up @@ -31,7 +32,7 @@ export class CBDShapeExtractor {
options: CBDShapeExtractorOptions;

constructor(
shapesGraphStore?: Store,
shapesGraphStore?: RdfStore,
dereferencer?: RdfDereferencer<Quad>,
options: Partial<CBDShapeExtractorOptions> = {},
) {
Expand All @@ -50,14 +51,14 @@ export class CBDShapeExtractor {
}
}

loadQuadStreamInStore(store: Store, quadStream: any) {
loadQuadStreamInStore(store: RdfStore, quadStream: any) {
return new Promise((resolve, reject) => {
store.import(quadStream).on("end", resolve).on("error", reject);
});
}

public async bulkExtract(
store: Store,
store: RdfStore,
ids: Array<Term>,
shapeId?: Term,
graphsToIgnore?: Array<Term>,
Expand All @@ -70,12 +71,12 @@ export class CBDShapeExtractor {
for (let id of ids) {
memberSpecificQuads[id.value] = [];
}
const newStore = new Store();
const newStore = RdfStore.createDefault();
for (let quad of store.readQuads(null, null, null, null)) {
if (quad.graph.termType == "NamedNode" && idSet.has(quad.graph.value)) {
memberSpecificQuads[quad.graph.value].push(quad);
} else {
newStore.add(quad);
newStore.addQuad(quad);
}
}

Expand Down Expand Up @@ -109,14 +110,14 @@ export class CBDShapeExtractor {
* * all quads in the namedgraph of this entity,
* * all quads of required paths found in the shape
* * the same algorithm on top of all found node links
* @param store The N3 Store loaded with a set of initial quads
* @param store The RdfStore loaded with a set of initial quads
* @param id The entity to be described/extracted
* @param shapeId The optional SHACL NodeShape identifier
* @param graphsToIgnore The optional parameter of graph to ignore when other entities are mentioned in the current context
* @returns Promise of a quad array of the described entity
*/
public async extract(
store: Store,
store: RdfStore,
id: Term,
shapeId?: Term,
graphsToIgnore?: Array<Term>,
Expand Down Expand Up @@ -210,7 +211,7 @@ export class CBDShapeExtractor {
}

private async extractRecursively(
store: Store,
store: RdfStore,
id: Term,
extracted: CbdExtracted,
graphsToIgnore: Array<string>,
Expand Down Expand Up @@ -360,7 +361,7 @@ export class CBDShapeExtractor {
public async CBD(
result: Quad[],
extractedStar: CbdExtracted,
store: Store,
store: RdfStore,
id: Term,
graphsToIgnore: Array<string>,
) {
Expand Down
14 changes: 7 additions & 7 deletions lib/Path.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Quad, Term } from "@rdfjs/types";
import { Store } from "n3";
import { RdfStore } from "rdf-stores";
import { CbdExtracted, Extracted } from "./CBDShapeExtractor";

export interface Path {
Expand All @@ -8,7 +8,7 @@ export interface Path {
found(cbd: CbdExtracted, inverse?: boolean): CbdExtracted | undefined;

match(
store: Store,
store: RdfStore,
extracted: CbdExtracted,
focusNode: Term,
graphsToIgnore?: Array<string>,
Expand All @@ -32,7 +32,7 @@ export class PredicatePath implements Path {
}

match(
store: Store,
store: RdfStore,
extracted: CbdExtracted,
focusNode: Term,
graphsToIgnore: Array<string>,
Expand Down Expand Up @@ -79,7 +79,7 @@ export class SequencePath implements Path {
}

match(
store: Store,
store: RdfStore,
extracted: CbdExtracted,
focusNode: Term,
graphsToIgnore: Array<string>,
Expand Down Expand Up @@ -132,7 +132,7 @@ export class AlternativePath implements Path {
}

match(
store: Store,
store: RdfStore,
extracted: CbdExtracted,
focusNode: Term,
graphsToIgnore: Array<string>,
Expand Down Expand Up @@ -160,7 +160,7 @@ export class InversePath implements Path {
}

match(
store: Store,
store: RdfStore,
extracted: CbdExtracted,
focusNode: Term,
graphsToIgnore: Array<string>,
Expand Down Expand Up @@ -190,7 +190,7 @@ export abstract class MultiPath implements Path {
abstract found(cbd: CbdExtracted): CbdExtracted | undefined;

match(
store: Store,
store: RdfStore,
extracted: CbdExtracted,
focusNode: Term,
graphsToIgnore: Array<string>,
Expand Down
Loading

0 comments on commit 3694c01

Please sign in to comment.