Skip to content

Commit

Permalink
feat: added transform fro skolem-iris
Browse files Browse the repository at this point in the history
  • Loading branch information
bergos committed Mar 6, 2024
1 parent da22a01 commit 7ce9a8f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
10 changes: 10 additions & 0 deletions bin/rdf-ext-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import createInputStream from '../lib/createInputStream.js'
import createOutputStream from '../lib/createOutputStream.js'
import createShaclStream from '../lib/createShaclStream.js'
import transformMapNamespaceFunc from '../lib/transformMapNamespace.js'
import transformSkolemIrisFunc from '../lib/transformSkolemIris.js'
import transformToTripleFunc from '../lib/transformToTriple.js'

const program = new Command()
Expand Down Expand Up @@ -42,6 +43,7 @@ program
.option('--shacl-details', 'generate nested result details')
.option('--shacl-trace', 'generate results for path traversing')
.option('--transform-map-namespace <mapping>', 'map the given namespaces', collectMappings, rdf.termMap())
.option('--transform-skolem-iris <baseIri>', 'map blank nodes to Skolem IRIs')
.option('--transform-to-triples', 'set graph to default graph')
.option('--output-prefix <prefix>', 'output prefix', collectPrefixes, new Map())
.option('--output-type <type>', 'output content type', 'text/turtle')
Expand All @@ -58,6 +60,7 @@ program
shaclDetails,
shaclTrace,
transformMapNamespace,
transformSkolemIris,
transformToTriple,
outputPrefix,
outputType,
Expand Down Expand Up @@ -104,6 +107,13 @@ program
stream = toTripleStream
}

if (transformSkolemIris) {
const toTripleStream = transformSkolemIrisFunc(transformSkolemIris)

stream.pipe(toTripleStream)
stream = toTripleStream
}

if (transformToTriple) {
const toTripleStream = transformToTripleFunc()

Expand Down
8 changes: 8 additions & 0 deletions examples/url-to-skolem-iri-turtle.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

./bin/rdf-ext-cli.js \
--pretty \
--transform-skolem-iris http://example.com/.well-known/genid/ \
--output-prefix houseplace=https://housemd.rdf-ext.org/place/ \
--output-prefix schema=http://schema.org/ \
https://housemd.rdf-ext.org/place/221b-baker-street
37 changes: 37 additions & 0 deletions lib/transformSkolemIris.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { randomUUID } from 'node:crypto'
import rdf from 'rdf-ext'
import { Transform } from 'readable-stream'

function transformSkolemIris (baseIri) {
const nodeMap = rdf.termMap()

const mapTerm = term => {
if (term.termType === 'BlankNode') {
if (!nodeMap.has(term)) {
nodeMap.set(term, rdf.namedNode(`${baseIri}${randomUUID().split('-').join('')}`))
}

return nodeMap.get(term)
}

return term
}

const mapQuad = quad => {
return rdf.quad(
mapTerm(quad.subject),
mapTerm(quad.predicate),
mapTerm(quad.object),
mapTerm(quad.graph)
)
}

return new Transform({
objectMode: true,
transform: (quad, encoding, callback) => {
callback(null, mapQuad(quad))
}
})
}

export default transformSkolemIris

0 comments on commit 7ce9a8f

Please sign in to comment.