Skip to content

Commit

Permalink
fix: blank node clashing
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Jan 26, 2024
1 parent 979056f commit 6bac13e
Show file tree
Hide file tree
Showing 20 changed files with 762 additions and 328 deletions.
6 changes: 6 additions & 0 deletions .changeset/nervous-moles-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@view-builder/publish-views": patch
"@view-builder/core": patch
---

Clashing blank nodes would cause invalid views being published
1 change: 0 additions & 1 deletion apps/www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"lit": "^2.3.0",
"n3": "^1.16.2",
"nanoid": "^4.0.0",
"rdf-ext": "^2.0.1",
"rdf-literal": "^1.3.0",
"rdf-validate-shacl": "^0.4.4",
"sparql-http-client": "^2.4.1",
Expand Down
6 changes: 3 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "0.0.0",
"type": "module",
"dependencies": {
"@rdfine/env": "^1.0.4",
"@rdfine/hydra": "^0.10.3",
"@rdfine/shacl": "^0.10.3",
"@rdfine/env": "^1.1.0",
"@rdfine/hydra": "^0.10.4",
"@rdfine/shacl": "^0.10.4",
"@rdfjs/namespace": "^2.0.0",
"@zazuko/vocabulary-extras": "^2.0.1"
}
Expand Down
16 changes: 7 additions & 9 deletions packages/publish-views/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import Runner from 'barnard59/runner.js'
import fromFile from 'rdf-utils-fs/fromFile.js'
import $rdf from 'rdf-ext'
import clownface from 'clownface'
import $rdf from 'barnard59-env'
import path from 'path'
import { fileURLToPath } from 'url'
import { PassThrough } from 'stream'
Expand All @@ -19,7 +17,7 @@ export function toNtriples({
loadViewsStepsPath = loadViewsFromStoreStepPath,
}) {
return startRun({
term: 'ToFile',
term: 'urn:pipeline:ToFile',
steps: [ntriplesStepsPath, fileStepsPath, loadViewsStepsPath],
variables: {
outFile,
Expand All @@ -30,19 +28,19 @@ export function toNtriples({

export async function toStore(variables = {}) {
return startRun({
term: 'ToStore',
term: 'urn:pipeline:ToStore',
steps: [storeStepsPath, loadViewsFromStoreStepPath],
variables,
})
}

async function startRun({ term, steps, variables }) {
const dataset = await $rdf.dataset().import(fromFile(path.resolve(__dirname, pipelinePath)))
await Promise.all(steps.map(src => dataset.import(fromFile(path.resolve(__dirname, src)))))
const pipeline = clownface({ dataset }).namedNode(term)
const dataset = await $rdf.dataset().import($rdf.fromFile(path.resolve(__dirname, pipelinePath)))
await Promise.all(steps.map(src => dataset.import($rdf.fromFile(path.resolve(__dirname, src)))))
const pipeline = $rdf.clownface({ dataset }).namedNode(term)

const outputStream = new PassThrough()
const run = await Runner(pipeline, {
const run = await Runner(pipeline, $rdf, {
basePath: __dirname,
variables: new Map(Object.entries(variables)),
outputStream,
Expand Down
10 changes: 4 additions & 6 deletions packages/publish-views/lib/ValidationError.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { rdf, sh } from '@tpluscode/rdf-ns-builders'
import $rdf from 'rdf-ext'
import clownface from 'clownface'
import $rdf from 'barnard59-env'

export class ValidationError extends Error {
constructor(reports) {
const total = reports.reduce((sum, { pointer }) => {
const violations = pointer.any().has(sh.resultSeverity).terms.length
const violations = pointer.any().has($rdf.ns.sh.resultSeverity).terms.length
return sum + violations
}, 0)

Expand All @@ -17,8 +15,8 @@ export class ValidationError extends Error {
})
Object.defineProperty(this, 'reports', {
enumerable: false,
value: clownface({ dataset: this.dataset })
.has(rdf.type, sh.ValidationReport),
value: $rdf.clownface({ dataset: this.dataset })
.has($rdf.ns.rdf.type, $rdf.ns.sh.ValidationReport),
})
}
}
Expand Down
16 changes: 7 additions & 9 deletions packages/publish-views/lib/loadViews.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { CONSTRUCT, SELECT } from '@tpluscode/sparql-builder'
import * as ns from '@view-builder/core/ns.js'
import through2 from 'through2'
import $rdf from 'rdf-ext'
import clownface from 'clownface'
import { schema } from '@tpluscode/rdf-ns-builders'
import $rdf from 'barnard59-env'
import * as shapeTo from '@hydrofoil/shape-to-query'
import toStream from 'rdf-dataset-ext/toStream.js'
import { viewShape } from './shapes.js'
import { getViewBuilderClient, getMetadataClient } from './sparql.js'

Expand All @@ -17,8 +14,8 @@ export default async function loadViewsToPublish() {
.WHERE`
?viewBuilderView
a ${ns.view.View} ;
${schema.sameAs} ?publishedView ;
${schema.isBasedOn} ?metaObject ;
${$rdf.ns.schema.sameAs} ?publishedView ;
${$rdf.ns.schema.isBasedOn} ?metaObject ;
${ns.viewBuilder.publish} true ;
`
.execute(client.query)
Expand All @@ -34,7 +31,7 @@ export default async function loadViewsToPublish() {

const dataset = $rdf.dataset()
await Promise.all([dataset.import(viewQuads), dataset.import(metaQuads)])
this.push(clownface({ dataset, term: viewBuilderView }))
this.push($rdf.clownface({ dataset, term: viewBuilderView }))

next()
}))
Expand All @@ -45,11 +42,12 @@ async function loadViewMeta(publishedView, metaObject, client) {

const subjectVariable = 'view'
const query = shapeTo.constructQuery(shape, { focusNode: metaObject, subjectVariable })
const dataset = await query.execute(client.query, {
const quads = await query.execute(client.query, {
operation: 'postDirect',
})

return toStream(dataset).pipe(through2.obj(viewIdTransform(metaObject, publishedView)))
return $rdf.dataset(quads).toStream()
.pipe(through2.obj(viewIdTransform(metaObject, publishedView)))
}

function viewIdTransform(from, to) {
Expand Down
12 changes: 5 additions & 7 deletions packages/publish-views/lib/shapes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import onetime from 'onetime'
import { fromFile } from 'rdf-utils-fs'
import $rdf from 'rdf-ext'
import clownface from 'clownface'
import $rdf from 'barnard59-env'
import { schema, sh } from '@tpluscode/rdf-ns-builders'
import { createRequire } from 'module'
import concat from 'barnard59-base/concat.js'
Expand All @@ -11,15 +9,15 @@ const metadataShapesPath = require.resolve('../shapes.ttl')
const viewShapesPath = require.resolve('@view-builder/core/shape/ViewValidationShape.ttl')

export const viewShape = onetime(async () => {
const dataset = await $rdf.dataset().import(fromFile(metadataShapesPath))
const dataset = await $rdf.dataset().import($rdf.fromFile(metadataShapesPath))

return clownface({ dataset })
return $rdf.clownface({ dataset })
.has(sh.targetClass, schema.Dataset)
})

export function validationShapes() {
const metadataShapes = fromFile(metadataShapesPath)
const viewShapes = fromFile(viewShapesPath)
const metadataShapes = $rdf.fromFile(metadataShapesPath)
const viewShapes = $rdf.fromFile(viewShapesPath)

return concat.object(metadataShapes, viewShapes)
}
27 changes: 11 additions & 16 deletions packages/publish-views/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,28 @@
"type": "module",
"main": "index.js",
"scripts": {
"run:pipeline": "npx --node-options '--loader node-loader-raw --inspect' barnard59 run --pipeline",
"run:pipeline": "node --loader node-loader-raw --inspect ../../node_modules/barnard59/bin/barnard59.js run --pipeline",
"postinstall": "mkdir -p .pipeline output",
"prestart:file": "cat pipeline/main.ttl pipeline/to-ntriples.ttl pipeline/to-file.ttl pipeline/steps/*.ttl > .pipeline/to-file.ttl",
"start:file": "dotenv -e ../../.env.local -e .env.local -- yarn run:pipeline ToFile --variable-all .pipeline/to-file.ttl",
"prestart:store": "mkdir .pipeline; cat pipeline/main.ttl pipeline/to-ntriples.ttl pipeline/to-store.ttl > .pipeline/to-store.ttl",
"start:store": "dotenv -e ../../.env.local -e .env.local -- yarn run:pipeline ToStore --variable-all .pipeline/to-store.ttl",
"start:file": "dotenv -e ../../.env -e ../../.env.local -e .env.local -- yarn run:pipeline urn:pipeline:ToFile --variable-all pipeline/to-file.ttl",
"start:store": "dotenv -e ../../.env.local -e .env.local -- yarn run:pipeline urn:pipeline:ToStore --variable-all pipeline/to-store.ttl",
"pretest": "yarn postinstall; yarn start:file",
"test": "shacl-cli validate --shapes test/shape*.ttl --shapes ../../packages/core/shape/ViewValidationShape.ttl --data output/views.nt"
},
"dependencies": {
"@hydrofoil/shape-to-query": "^0.8.1",
"@tpluscode/rdf-ns-builders": "^4.1.0",
"@tpluscode/rdf-ns-builders": "^4.1.1",
"@tpluscode/sparql-builder": "^1.1.0",
"@view-builder/core": "^0.0.0",
"@view-builder/view-util": "^0.0.1",
"barnard59": "^3.0.2",
"barnard59-base": "^1.2.2",
"barnard59-formats": "^1.4.2",
"barnard59-graph-store": "^1.1.1",
"barnard59-rdf": "^1.4.5",
"barnard59-validate-shacl": "^0.3.8",
"clownface": "^2",
"barnard59": "^4.5.3",
"barnard59-base": "^2.4.0",
"barnard59-env": "^1.2.3",
"barnard59-formats": "^2.1.0",
"barnard59-graph-store": "^5.1.1",
"barnard59-rdf": "^3.4.0",
"barnard59-shacl": "^1.4.1",
"node-loader-raw": "^0.0.0",
"onetime": "^6.0.0",
"rdf-dataset-ext": "^1.0.1",
"rdf-ext": "^2.1.0",
"rdf-utils-fs": "^2.2.0",
"rimraf": "^3.0.2",
"sparql-http-client": "^2.4.1",
"through2": "^4.0.2"
Expand Down
25 changes: 15 additions & 10 deletions packages/publish-views/pipeline/main.ttl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@prefix : <https://pipeline.described.at/> .
@prefix code: <https://code.described.at/> .

<Main>
<urn:pipeline:Main>
a :Pipeline, :ReadableObjectMode ;
:variables
[
Expand All @@ -16,12 +16,17 @@
[
:stepList
(
<#loadViews> <#prepareView> <#pointerToDataset> <#validate> <#checkShaclReports> <#flatten>
<urn:step:loadViews>
_:prepareView
_:pointerToDataset
_:validate
_:checkShaclReports
_:flatten
)
] ;
.

<#prepareView>
_:prepareView
a :Step ;
code:implementedBy
[
Expand All @@ -37,7 +42,7 @@
) ;
.

<#pointerToDataset>
_:pointerToDataset
a :Step ;
code:implementedBy
[
Expand All @@ -50,22 +55,22 @@
) ;
.

<#validate>
_:validate
a :Step ;
code:implementedBy
[
code:link <node:barnard59-validate-shacl/validate.js#shacl> ;
code:link <node:barnard59-shacl/validate.js#shacl> ;
a code:EcmaScriptModule ;
] ;
code:arguments
[ code:name "shape" ; code:value <#Shapes> ] ,
[ code:name "shape" ; code:value _:Shapes ] ,
[
code:name "onViolation" ;
code:value [ a code:EcmaScriptModule ; code:link <file:../lib/shacl.js#collectShaclReports> ] ;
];
.

<#checkShaclReports>
_:checkShaclReports
a :Step ;
code:implementedBy
[
Expand All @@ -74,7 +79,7 @@
] ;
.

<#Shapes>
_:Shapes
a :Pipeline, :ReadableObjectMode ;
:steps
[
Expand All @@ -91,7 +96,7 @@
]
.

<#flatten>
_:flatten
a :Step ;
code:implementedBy
[
Expand Down
2 changes: 1 addition & 1 deletion packages/publish-views/pipeline/steps/loadViews.ttl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PREFIX code: <https://code.described.at/>
prefix : <https://pipeline.described.at/>

<#loadViews>
<urn:step:loadViews>
a :Step ;
code:implementedBy
[
Expand Down
8 changes: 6 additions & 2 deletions packages/publish-views/pipeline/to-file.ttl
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
@prefix : <https://pipeline.described.at/> .
@prefix code: <https://code.described.at/> .

<ToFile>
[ code:imports <./main.ttl> ] .
[ code:imports <./to-ntriples.ttl> ] .
[ code:imports <./steps/loadViews.ttl> ] .

<urn:pipeline:ToFile>
a :Pipeline ;
:steps
[
:stepList
(
<Main> <#serialize> _:save
<urn:pipeline:Main> <urn:step:serialize> _:save
)
] ;
:variables
Expand Down
6 changes: 3 additions & 3 deletions packages/publish-views/pipeline/to-ntriples.ttl
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
@prefix : <https://pipeline.described.at/> .
@prefix code: <https://code.described.at/> .

<ToNtriples>
<urn:pipeline:ToNtriples>
a :Pipeline, :Readable ;
:steps
[
:stepList
(
<Main> <#serialize>
<urn:pipeline:Main> <urn:step:serialize>
)
] ;
.

<#serialize>
<urn:step:serialize>
a :Step ;
code:implementedBy
[
Expand Down
7 changes: 5 additions & 2 deletions packages/publish-views/pipeline/to-store.ttl
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
@prefix : <https://pipeline.described.at/> .
@prefix code: <https://code.described.at/> .

<ToStore>
[ code:imports <./main.ttl> ] .
[ code:imports <./steps/loadViews.ttl> ] .

<urn:pipeline:ToStore>
a :Pipeline ;
:steps
[
:stepList
(
<Main> _:setGraph _:upload
<urn:pipeline:Main> _:setGraph _:upload
)
] ;
.
Expand Down
4 changes: 3 additions & 1 deletion packages/publish-views/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { cube, view, viewBuilder } from '@view-builder/core/ns.js'
import { _void, dcat, foaf, rdfs, schema } from '@tpluscode/rdf-ns-builders'
import { MetaLookup } from '@view-builder/view-util/lib/metaLookup.js'
import sinon from 'sinon'
import $rdf from 'rdf-ext'
import $rdf from 'barnard59-env'
import { toNtriples } from '../index.js'
import { ValidationError } from '../lib/ValidationError.js'

Expand Down Expand Up @@ -42,6 +42,7 @@ describe('@view-builder/publish-views', () => {
${view.from} [
${view.path} ${foaf.name} ;
${view.source} [
a ${view.CubeSource} ;
${view.cube} <http://foo.cube> ;
] ;
] ;
Expand Down Expand Up @@ -74,6 +75,7 @@ describe('@view-builder/publish-views', () => {
${view.from} [
${view.path} ${schema.knows} ;
${view.source} [
a ${view.CubeSource} ;
${view.cube} <http://foo.cube> ;
] ;
] ;
Expand Down
Loading

0 comments on commit 6bac13e

Please sign in to comment.