Skip to content

Commit

Permalink
Shape to mermaid (#30)
Browse files Browse the repository at this point in the history
* Add a shapeGraph to mermaid functionality
  * optional paths, nodeLinks and atLeastOneLists
  * inverse path

* Add error test + docs

* Extract label for Node Shapes from rdfs:label or IRI

* Added a simple binary for mermaid

* bump version

---------

Co-authored-by: Julian Rojas <[email protected]>
Co-authored-by: Pieter Colpaert <[email protected]>
Co-authored-by: ajuvercr <[email protected]>
  • Loading branch information
4 people authored May 6, 2024
1 parent f75ddd5 commit 66e3978
Show file tree
Hide file tree
Showing 29 changed files with 1,046 additions and 360 deletions.
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

0 comments on commit 66e3978

Please sign in to comment.