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

feat: added transform for skolem-iris #5

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading