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

Shape to mermaid #30

Merged
merged 13 commits into from
May 6, 2024
85 changes: 85 additions & 0 deletions bin/mermaid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { assert } from "chai";
import { RdfStore } from "rdf-stores";
import { ShapesGraph } from "../lib/ShapesGraph";
import { DataFactory } from "rdf-data-factory";
import rdfDereference from "rdf-dereference";
import fs from "fs/promises";
import * as process from 'process';
import { Term } from "rdf-js";
import { deflate} from "pako";
import { fromUint8Array } from 'js-base64';

const df = new DataFactory();


// Check if at least one command line argument is provided
if (process.argv.length <= 2) {
console.error('Please provide an IRI to a dereferenceable SHACL NodeShape or an LDES or tree:Collection with with tree:shape property in it');
process.exit(1); // Exit with an error code
}

let iri = process.argv[2];

async function main () {
let df = new DataFactory();
let shapeStore = RdfStore.createDefault();
let shapesGraph: ShapesGraph;
let shapeTerm: Term = df.namedNode(iri);
let readStream = (
await rdfDereference.dereference(iri, {
localFiles: true,
})
).data;

await new Promise((resolve, reject) => {
shapeStore.import(readStream).on("end", resolve).on("error", reject);
});
let tmpShapeTerm: Term[] = shapeStore.getQuads(null, df.namedNode('https://w3id.org/tree#shape'), null).map((quad) => quad.object);
if (tmpShapeTerm[0]) {
shapeTerm = tmpShapeTerm[0];
iri = shapeTerm.value;
}
if (tmpShapeTerm[0] && tmpShapeTerm[0].termType==='NamedNode') {
//Dereference the shape and add it here. The iri is not this IRI
console.error('GET ' + shapeTerm.value);
//Try to dereference this one as well. If it works, nice, if it doesn’t, too bad, we’ll continue without notice.
let readStream2 = (
await rdfDereference.dereference(shapeTerm.value, {
localFiles: true,
})
).data;
await new Promise((resolve, reject) => {
shapeStore.import(readStream2).on("end", resolve).on("error", () => {
console.error('Warning: couldn’t fetch ' + iri + ' but continuing');
resolve(null);
});
});
}

shapesGraph = new ShapesGraph(shapeStore);
const actualMermaid = shapesGraph.toMermaid(shapeTerm);
console.log('```mermaid');
console.log(actualMermaid);
console.log('```');


const formatJSON = (data: unknown): string => JSON.stringify(data, undefined, 2);
const serialize = (state: string): string => {
const data = new TextEncoder().encode(state);
const compressed = deflate(data, { level: 9 }); // zlib level 9
return fromUint8Array(compressed, true); // url safe base64 encoding
}
const defaultState = {
code: actualMermaid,
mermaid: formatJSON({
theme: 'default'
}),
autoSync: true,
updateDiagram: true
};
const json = JSON.stringify(defaultState);
const serialized = serialize(json);
console.log();
console.log('Mermaid Live: https://mermaid.live/edit#pako:'+ serialized);
}
main();
3 changes: 2 additions & 1 deletion lib/CBDShapeExtractor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import rdfDereference, { RdfDereferencer } from "rdf-dereference";
import { NodeLink, RDFMap, ShapesGraph, ShapeTemplate } from "./Shape";
import { NodeLink, RDFMap, ShapeTemplate } from "./Shape";
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";
import {ShapesGraph} from "./ShapesGraph";

const log = debug("extract-cbd-shape");

Expand Down
Loading
Loading