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

Remove ENV dependency from Cube #205

Merged
merged 2 commits into from
Nov 17, 2023
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
5 changes: 5 additions & 0 deletions .changeset/soft-peaches-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"barnard59-env": minor
---

Added `cube` and `meta` namespaces
10 changes: 3 additions & 7 deletions package-lock.json

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

5 changes: 2 additions & 3 deletions packages/cube/lib/cbdCopy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import rdf from '@zazuko/env-node'

function cbdCopy(source, target, { ignore = rdf.termSet() } = {}) {
function cbdCopy(rdf, source, target, { ignore = rdf.termSet() } = {}) {
for (const quad of source.dataset.match(source.term)) {
if (ignore.has(quad.predicate)) {
continue
Expand All @@ -10,6 +8,7 @@ function cbdCopy(source, target, { ignore = rdf.termSet() } = {}) {

if (quad.object.termType === 'BlankNode') {
cbdCopy(
rdf,
{ dataset: source.dataset, term: quad.object },
{ dataset: target.dataset, term: quad.object },
)
Expand Down
44 changes: 21 additions & 23 deletions packages/cube/lib/cube/buildCubeShape/Cube.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import rdf from '@zazuko/env-node'
import addAll from 'rdf-dataset-ext/addAll.js'
import cbdCopy from '../../cbdCopy.js'
import * as ns from '../../namespaces.js'
import Dimension from './Dimension.js'

class Cube {
constructor({ metadata, observationSet, shape, term, propertyShapeId }) {
constructor({ rdf, metadata, observationSet, shape, term, propertyShapeId }) {
this.rdf = rdf
this.metadata = metadata
this.observationSet = observationSet
this.shape = shape
Expand All @@ -19,11 +17,11 @@ class Cube {

if (!dimension) {
const metadata = this.metadata
.out(ns.cube.observationConstraint)
.out(ns.sh.property)
.has(ns.sh.path, predicate)
.out(this.rdf.ns.cube.observationConstraint)
.out(this.rdf.ns.sh.property)
.has(this.rdf.ns.sh.path, predicate)

dimension = new Dimension({ metadata, predicate, object, shapeId: this.propertyShapeId })
dimension = new Dimension({ rdf: this.rdf, metadata, predicate, object, shapeId: this.propertyShapeId })

this.dimensions.set(predicate, dimension)
}
Expand All @@ -36,30 +34,30 @@ class Cube {
}

toDataset({ shapeGraph } = { shapeGraph: undefined }) {
const dataset = rdf.dataset()
const dataset = this.rdf.dataset()

const cube = rdf.clownface({ dataset, term: this.term })
.addOut(ns.rdf.type, ns.cube.Cube)
.addOut(ns.cube.observationSet, this.observationSet)
.addOut(ns.cube.observationConstraint, this.shape)
const cube = this.rdf.clownface({ dataset, term: this.term })
.addOut(this.rdf.ns.rdf.type, this.rdf.ns.cube.Cube)
.addOut(this.rdf.ns.cube.observationSet, this.observationSet)
.addOut(this.rdf.ns.cube.observationConstraint, this.shape)

cbdCopy(this.metadata, cube, { ignore: rdf.termSet([ns.cube.observationConstraint]) })
cbdCopy(this.rdf, this.metadata, cube, { ignore: this.rdf.termSet([this.rdf.ns.cube.observationConstraint]) })

rdf.clownface({ dataset, term: this.observationSet })
.addOut(ns.rdf.type, ns.cube.ObservationSet)
this.rdf.clownface({ dataset, term: this.observationSet })
.addOut(this.rdf.ns.rdf.type, this.rdf.ns.cube.ObservationSet)

const shapeDataset = rdf.dataset()
const shapeDataset = this.rdf.dataset()

rdf.clownface({ dataset: shapeDataset, term: this.shape })
.addOut(ns.rdf.type, [ns.sh.NodeShape, ns.cube.Constraint])
.addOut(ns.sh.closed, true)
this.rdf.clownface({ dataset: shapeDataset, term: this.shape })
.addOut(this.rdf.ns.rdf.type, [this.rdf.ns.sh.NodeShape, this.rdf.ns.cube.Constraint])
.addOut(this.rdf.ns.sh.closed, true)

for (const dimension of this.dimensions.values()) {
addAll(shapeDataset, dimension.toDataset({ cube: this, shape: this.shape }))
shapeDataset.addAll(dimension.toDataset({ cube: this, shape: this.shape }))
}
const setGraph = quad => rdf.quad(quad.subject, quad.predicate, quad.object, shapeGraph)
const setGraph = quad => this.rdf.quad(quad.subject, quad.predicate, quad.object, shapeGraph)

return addAll(dataset, [...shapeDataset].map(setGraph))
return dataset.addAll([...shapeDataset].map(setGraph))
}
}

Expand Down
69 changes: 22 additions & 47 deletions packages/cube/lib/cube/buildCubeShape/Dimension.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,18 @@
import rdf from '@zazuko/env-node'
import { fromRdf } from 'rdf-literal'
import cbdCopy from '../../cbdCopy.js'
import * as ns from '../../namespaces.js'

const datatypeParsers = rdf.termMap([
[ns.xsd.byte, fromRdf],
[ns.xsd.date, fromRdf],
[ns.xsd.dateTime, fromRdf],
[ns.xsd.decimal, fromRdf],
[ns.xsd.double, fromRdf],
[ns.xsd.float, fromRdf],
[ns.xsd.gDay, fromRdf],
[ns.xsd.gMonthDay, fromRdf],
[ns.xsd.gYear, fromRdf],
[ns.xsd.gYearMonth, fromRdf],
[ns.xsd.int, fromRdf],
[ns.xsd.integer, fromRdf],
[ns.xsd.long, fromRdf],
[ns.xsd.negativeInteger, fromRdf],
[ns.xsd.nonNegativeInteger, fromRdf],
[ns.xsd.nonPositiveInteger, fromRdf],
[ns.xsd.positiveInteger, fromRdf],
[ns.xsd.short, fromRdf],
[ns.xsd.unsignedByte, fromRdf],
[ns.xsd.unsignedInt, fromRdf],
[ns.xsd.unsignedLong, fromRdf],
[ns.xsd.unsignedShort, fromRdf],
])
import initDatatypeParsers from './datatypes.js'

class Dimension {
constructor({ metadata, predicate, object, shapeId = () => rdf.blankNode() }) {
constructor({ rdf, metadata, predicate, object, shapeId = () => rdf.blankNode() }) {
this.rdf = rdf
this.metadata = metadata
this.predicate = predicate
this.termType = object.termType
this.datatype = rdf.termSet()
this.shapeId = shapeId

if (object.datatype && datatypeParsers.has(object.datatype)) {
const datatypeParser = datatypeParsers.get(object.datatype)
this.datatypeParsers = initDatatypeParsers(rdf)
if (object.datatype && this.datatypeParsers.has(object.datatype)) {
const datatypeParser = this.datatypeParsers.get(object.datatype)

const value = datatypeParser(object)

Expand All @@ -55,8 +30,8 @@ class Dimension {
this.datatype.add(object.datatype)
}

if (object.datatype && datatypeParsers.has(object.datatype)) {
const datatypeParser = datatypeParsers.get(object.datatype)
if (object.datatype && this.datatypeParsers.has(object.datatype)) {
const datatypeParser = this.datatypeParsers.get(object.datatype)

const value = datatypeParser(object)

Expand All @@ -77,44 +52,44 @@ class Dimension {
}

toDataset({ cube, shape }) {
const dataset = rdf.dataset()
const dataset = this.rdf.dataset()

const graph = rdf.clownface({ dataset })
const graph = this.rdf.clownface({ dataset })
const ptr = graph.node(this.shapeId(cube, this))

ptr
.addIn(ns.sh.property, shape)
.addOut(ns.sh.path, this.predicate)
.addOut(ns.sh.nodeKind, this.termType === 'NamedNode' ? ns.sh.IRI : ns.sh.Literal)
.addOut(ns.sh.minCount, 1)
.addOut(ns.sh.maxCount, 1)
.addIn(this.rdf.ns.sh.property, shape)
.addOut(this.rdf.ns.sh.path, this.predicate)
.addOut(this.rdf.ns.sh.nodeKind, this.termType === 'NamedNode' ? this.rdf.ns.sh.IRI : this.rdf.ns.sh.Literal)
.addOut(this.rdf.ns.sh.minCount, 1)
.addOut(this.rdf.ns.sh.maxCount, 1)

if (this.datatype.size === 1) {
ptr.addOut(ns.sh.datatype, [...this.datatype][0])
ptr.addOut(this.rdf.ns.sh.datatype, [...this.datatype][0])
}

if (this.datatype.size > 1) {
ptr.addList(ns.sh.or, [...this.datatype].map(datatype => {
ptr.addList(this.rdf.ns.sh.or, [...this.datatype].map(datatype => {
return ptr
.blankNode()
.addOut(ns.sh.datatype, datatype)
.addOut(this.rdf.ns.sh.datatype, datatype)
}))
}

if (this.in) {
ptr.addList(ns.sh.in, [...this.in.values()])
ptr.addList(this.rdf.ns.sh.in, [...this.in.values()])
}

if (this.min) {
ptr.addOut(ns.sh.minInclusive, this.min)
ptr.addOut(this.rdf.ns.sh.minInclusive, this.min)
}

if (this.max) {
ptr.addOut(ns.sh.maxInclusive, this.max)
ptr.addOut(this.rdf.ns.sh.maxInclusive, this.max)
}

if (this.metadata.term) {
cbdCopy(this.metadata, ptr)
cbdCopy(this.rdf, this.metadata, ptr)
}

return dataset
Expand Down
30 changes: 30 additions & 0 deletions packages/cube/lib/cube/buildCubeShape/datatypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { fromRdf } from 'rdf-literal'

const datatypes = [
'byte',
'date',
'dateTime',
'decimal',
'double',
'float',
'gDay',
'gMonthDay',
'gYear',
'gYearMonth',
'int',
'integer',
'long',
'negativeInteger',
'nonNegativeInteger',
'nonPositiveInteger',
'positiveInteger',
'short',
'unsignedByte',
'unsignedInt',
'unsignedLong',
'unsignedShort',
]

export default function datatypeParsers(rdf) {
return rdf.termMap(datatypes.map(datatype => [rdf.ns.xsd[datatype], fromRdf]))
}
41 changes: 20 additions & 21 deletions packages/cube/lib/cube/buildCubeShape/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import once from 'lodash/once.js'
import $rdf from '@zazuko/env-node'
import { Transform } from 'readable-stream'
import fromStream from 'rdf-dataset-ext/fromStream.js'
import * as ns from '../../namespaces.js'
import urlJoin from '../../urlJoin.js'
import Cube from './Cube.js'

Expand All @@ -13,7 +10,7 @@ function defaultCube({ observationSet }) {
return null
}

return $rdf.namedNode(urlJoin(observationSetIri, '..'))
return this.rdf.namedNode(urlJoin(observationSetIri, '..'))
}

function defaultShape({ term }) {
Expand All @@ -23,19 +20,20 @@ function defaultShape({ term }) {
return null
}

return $rdf.namedNode(urlJoin(cubeIri, 'shape'))
return this.rdf.namedNode(urlJoin(cubeIri, 'shape'))
}

class CubeShapeBuilder extends Transform {
constructor({ excludeValuesOf, metadata, graph, propertyShapeId } = {}) {
constructor({ rdf, excludeValuesOf, metadata, graph, propertyShapeId } = {}) {
super({ objectMode: true })

this.rdf = rdf
this.options = {
cubes: $rdf.termMap(),
cube: defaultCube,
excludeValuesOf: $rdf.termSet(excludeValuesOf ? excludeValuesOf.map(v => $rdf.namedNode(v)) : []),
cubes: this.rdf.termMap(),
cube: defaultCube.bind({ rdf }),
excludeValuesOf: this.rdf.termSet(excludeValuesOf ? excludeValuesOf.map(v => this.rdf.namedNode(v)) : []),
metadataStream: metadata,
shape: defaultShape,
shape: defaultShape.bind({ rdf }),
graph,
propertyShapeId,
}
Expand All @@ -45,9 +43,9 @@ class CubeShapeBuilder extends Transform {

async _init() {
if (this.options.metadataStream) {
this.options.metadata = await fromStream($rdf.dataset(), this.options.metadataStream)
this.options.metadata = await this.rdf.dataset().import(this.options.metadataStream)
} else {
this.options.metadata = $rdf.dataset()
this.options.metadata = this.rdf.dataset()
}
}

Expand All @@ -58,22 +56,23 @@ class CubeShapeBuilder extends Transform {
return callback(err)
}

const dataset = $rdf.dataset([...chunk])
const dataset = this.rdf.dataset([...chunk])

const context = {
dataset,
ptr: $rdf.clownface({ dataset }).has(ns.rdf.type, ns.cube.Observation),
ptr: this.rdf.clownface({ dataset }).has(this.rdf.ns.rdf.type, this.rdf.ns.cube.Observation),
}

context.observationSet = context.ptr.in(ns.cube.observation).term
context.observationSet = context.ptr.in(this.rdf.ns.cube.observation).term
context.term = this.options.cube(context)
context.shape = this.options.shape(context)
context.cube = this.options.cubes.get(context.term)

if (!context.cube) {
context.cube = new Cube({
rdf: this.rdf,
term: context.term,
metadata: $rdf.clownface({ dataset: this.options.metadata, term: context.term }),
metadata: this.rdf.clownface({ dataset: this.options.metadata, term: context.term }),
observationSet: context.observationSet,
shape: context.shape,
propertyShapeId: this.options.propertyShapeId,
Expand All @@ -93,20 +92,20 @@ class CubeShapeBuilder extends Transform {

_flush(callback) {
for (const cube of this.options.cubes.values()) {
const dataset = cube.toDataset({ shapeGraph: toNamedNode(this.options.graph) })
const dataset = cube.toDataset({ shapeGraph: this.toNamedNode(this.options.graph) })
this.push(dataset)
}

callback()
}
}

function toNamedNode(item) {
return typeof item === 'string' ? $rdf.namedNode(item) : item
toNamedNode(item) {
return typeof item === 'string' ? this.rdf.namedNode(item) : item
}
}

function buildCubeShape({ excludeValuesOf, metadata, graph, propertyShapeId } = {}) {
return new CubeShapeBuilder({ excludeValuesOf, metadata, graph, propertyShapeId })
return new CubeShapeBuilder({ rdf: this.env, excludeValuesOf, metadata, graph, propertyShapeId })
}

export default buildCubeShape
Loading