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

Improve structure of .well-known/void schema #34

Merged
merged 1 commit into from
May 3, 2021
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
"@fortawesome/free-solid-svg-icons": "^5.15.3",
"@fortawesome/vue-fontawesome": "^2.0.2",
"@rdfjs-elements/rdf-editor": "^0.4.1",
"@rdfjs/namespace": "^1.1.0",
"@rdfjs/serializer-jsonld-ext": "^2.0.0",
"@rdfjs/term-set": "1.0.2",
"@tpluscode/rdf-ns-builders": "^0.4.2",
"@tpluscode/rdf-string": "0.2.23",
"@zazuko/rdf-vocabularies": "2021.3.31",
"buefy": "^0.9.7",
Expand Down
2 changes: 1 addition & 1 deletion src/components/SettingsPane.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
Force introspection
</b-switch>
<p class="help">
By default, the explorer will use the endpoint schema provided at <code>endpoint/.well-known/schema</code>
By default, the explorer will use the endpoint schema provided at <code>endpoint/.well-known/void</code>
(if any). Use this option to force an introspection.
</p>
</b-field>
Expand Down
19 changes: 12 additions & 7 deletions src/endpoint.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import clownface from 'clownface'
import RDF from 'rdf-ext'
import ParsingClient from 'sparql-http-client/ParsingClient'
import TermSet from '@rdfjs/term-set'
import _prefixes from '@zazuko/rdf-vocabularies/prefixes'
import { shrink } from '@zazuko/rdf-vocabularies/shrink'
import { tablesFromSHACL } from '@/shacl'
import { rdf, schema, spex, prefixes as _prefixes } from './namespace'

const SCHEMA_URI = '.well-known/schema'
const SCHEMA_URI = '.well-known/void'

const initialPrefixes = [...Object.keys(_prefixes)]

Expand Down Expand Up @@ -54,9 +55,7 @@ export class Endpoint {

const tables = await this.fetchSHACL()

return tables.length > 0
? tables
: this.introspectTables()
return tables || this.introspectTables()
}

/**
Expand All @@ -71,9 +70,15 @@ export class Endpoint {
${fromClause}
`
const quads = await this.client.query.construct(query)
const dataset = RDF.dataset(quads)
const dataset = clownface({
dataset: RDF.dataset(quads),
term: RDF.namedNode(schemaURI),
})
const defaultShapes = dataset.out(spex.shapes).has(rdf.type, spex.DefaultShapes)

return tablesFromSHACL(dataset, this)
return defaultShapes.term
? tablesFromSHACL(defaultShapes.out(schema.hasPart), this)
: null
}

async introspectTables () {
Expand Down
13 changes: 13 additions & 0 deletions src/namespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import namespace from '@rdfjs/namespace'
import prefixes from '@zazuko/rdf-vocabularies/prefixes'

prefixes.spex = 'https://ld.zazuko.com/spex/'

export const rdf = namespace(prefixes.rdf)
export const schema = namespace(prefixes.schema)
export const sh = namespace(prefixes.sh)
export const spex = namespace(prefixes.spex)

export {
prefixes
}
36 changes: 24 additions & 12 deletions src/shacl.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import clownface from 'clownface'
import { rdf, sh } from '@tpluscode/rdf-ns-builders'
import _prefixes from '@zazuko/rdf-vocabularies/prefixes'

import { sh, prefixes } from './namespace'

/**
* Serializes a list of a tables as a SHACL graph.
*
* @param {Array} tables - List of tables
* @param {Endpoint} endpoint
* @returns Object - JSON-LD SHACL
*/
export function tablesToSHACL (tables, endpoint) {
const prefixes = endpoint.prefixes.reduce((acc, { prefix, url }) => ({ ...acc, [prefix]: url }), {})
const context = { ..._prefixes, ...prefixes }
const customPrefixes = endpoint.prefixes.reduce((acc, { prefix, url }) => ({ ...acc, [prefix]: url }), {})
const context = { ...prefixes, ...customPrefixes }

const graph = tables.map((table) => {
return {
Expand Down Expand Up @@ -33,16 +38,23 @@ export function tablesToSHACL (tables, endpoint) {

return {
'@context': context,
'@graph': graph,
'@graph': {
'@type': 'spex:DefaultShapes',
'schema:hasPart': graph,
},
}
}

export function tablesFromSHACL (dataset, endpoint) {
const cf = clownface({ dataset })

const shapes = cf.has(rdf.type, sh.NodeShape).toArray()

/**
* Extracts a list of tables from an RDF graph.
*
* @param {Clownface} shapes - Pointer to node shapes
* @param {Endpoint} endpoint
* @returns
*/
export function tablesFromSHACL (shapes, endpoint) {
return shapes
.toArray()
.map((shape) => {
const targetClass = shape.out(sh.targetClass).term

Expand Down
5 changes: 4 additions & 1 deletion src/views/ShaclEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ import { Splitpanes, Pane } from 'splitpanes'
import 'splitpanes/dist/splitpanes.css'
import '@rdfjs-elements/rdf-editor'
import { parsers } from '@rdf-esm/formats-common'
import clownface from 'clownface'
import OverviewTables from '@/components/OverviewTables.vue'
import { tablesFromSHACL } from '@/shacl'
import { rdf, sh } from '@/namespace'

const formats = [...parsers.keys()]

Expand Down Expand Up @@ -82,7 +84,8 @@ export default {
try {
const quads = await editor.quads
const dataset = RDF.dataset(quads)
this.tables = tablesFromSHACL(dataset, endpoint)
const shapes = clownface({ dataset }).has(rdf.type, sh.NodeShape)
this.tables = tablesFromSHACL(shapes, endpoint)
} catch (e) {
this.error = e.toString()
}
Expand Down