Skip to content

Commit

Permalink
Merge pull request #1534 from zazuko/fix-observer
Browse files Browse the repository at this point in the history
Fix observer
  • Loading branch information
giacomociti authored Sep 24, 2024
2 parents 11082f2 + 0d8d510 commit e41d588
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 38 deletions.
7 changes: 7 additions & 0 deletions .changeset/loud-news-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@cube-creator/model": patch
"@cube-creator/core-api": patch
"@cube-creator/cli": patch
---

Fix: set correct `cube:observedBy` during transform
21 changes: 18 additions & 3 deletions cli/lib/commands/transform.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as Csvw from '@rdfine/csvw'
import { ThingMixin } from '@rdfine/schema'
import { Project, TransformJob } from '@cube-creator/model'
import { cube } from '@cube-creator/core/namespace'
import * as runner from './runner'

interface TransformRunOptions extends runner.RunOptions {
Expand All @@ -11,11 +13,24 @@ export default runner.create<TransformRunOptions>({
const { to } = command
return ['main', 'from-api', `to-${to}`, 'validate']
},
prepare(_, variable) {
variable.set('lastTransformed', {})

async prepare(options, variable) {
const Hydra = variable.get('apiClient')

const jobResource = await Hydra.loadResource<TransformJob>(options.job)
const job = jobResource.representation?.root
if (!job) {
throw new Error(`Did not find representation of job ${options.job}. Server responded ${jobResource.response?.xhr.status}`)
}

const projectResource = await Hydra.loadResource<Project>(job.project)
const project = projectResource.representation?.root
if (!project) {
throw new Error(`Did not find representation of project ${job.project}. Server responded ${projectResource.response?.xhr.status}`)
}
const observer = project.maintainer.pointer.out(cube.observedBy).term
variable.set('observer', observer?.value)
variable.set('lastTransformed', {})

Hydra.resources.factory.addMixin(...Object.values(Csvw))
Hydra.resources.factory.addMixin(ThingMixin)
},
Expand Down
10 changes: 5 additions & 5 deletions cli/lib/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ async function loadTransformJob(jobUri: string, log: Logger, variables: Params['
throw new Error(`Did not find representation of job ${jobUri}. Server responded ${response?.xhr.status}`)
}

if (!representation.root.cubeGraph) {
throw new Error('Cannot transform project. Missing output cube id')
if (representation.root.cubeGraph) {
log.info(`Will write output triples to graph <${representation.root.cubeGraph.value}>`)
variables.set('graph', representation.root.cubeGraph.value)
} else {
log.warn('Cannot transform project. Missing output cube id')
}

log.info(`Will write output triples to graph <${representation.root.cubeGraph.value}>`)
variables.set('graph', representation.root.cubeGraph.value)

return representation.root
}

Expand Down
24 changes: 1 addition & 23 deletions cli/lib/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Literal, NamedNode, Quad } from '@rdfjs/types'
import { obj } from 'through2'
import type { Literal, NamedNode } from '@rdfjs/types'
import $rdf from 'rdf-ext'
import { dcat, dcterms, rdf, schema, sh, _void, foaf, xsd } from '@tpluscode/rdf-ns-builders'
import { cc, cube } from '@cube-creator/core/namespace'
Expand Down Expand Up @@ -275,24 +274,3 @@ export async function loadCubeMetadata(this: Context, { jobUri, endpoint, user,
},
})
}

export async function injectObservedBy(this: Context, jobUri: string) {
const Hydra = this.variables.get('apiClient')

const { maintainer } = await loadDataset(jobUri, Hydra)

return obj(function (quad: Quad, _, callback) {
if (quad.predicate.equals(cube.observedBy)) {
const creatorTerms = maintainer.pointer.out(cube.observedBy).terms
for (const creator of creatorTerms) {
if (creator.termType === 'NamedNode') {
this.push($rdf.quad(quad.subject, cube.observedBy, creator))
}
}
} else {
this.push(quad)
}

callback()
})
}
1 change: 1 addition & 0 deletions cli/lib/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ declare module 'barnard59-core' {
originalValueQuads: DatasetExt
cubeCreatorVersion: string
cliVersion: string
observer: string
}
}
4 changes: 3 additions & 1 deletion cli/pipelines/main.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,14 @@
code:link <node:barnard59-cube/cube.js#toObservation>
] ;
code:arguments [
code:name "observer";
code:value "observer"^^:VariableName
], [
code:name "observations";
code:value [
a code:EcmaScript ;
code:link <file:../lib/cube#getObservationSetId>
]

], [
code:name "observation";
code:value "({ dataset, observations }) => ([...dataset][0].subject)"^^code:EcmaScript
Expand Down
1 change: 0 additions & 1 deletion cli/pipelines/publish.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
:steps [ :stepList ( <#loadCube>
<#countInputQuads>
<#injectCubeRevision>
<#injectObservedBy>
<#injectSoftwareVersions>
) ]
.
Expand Down
4 changes: 4 additions & 0 deletions cli/test/lib/commands/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@ describe('@cube-creator/cli/lib/commands/publish', function () {
minCount: 1,
},
})

const observedByIns = Array.from(props.has(sh.path, cube.observedBy).out(sh.in).list() ?? [])
expect(observedByIns).to.have.length(1)
expect(observedByIns[0].value).to.equal('https://ld.admin.ch/office/VII.1.7')
})

it('removes all csvw triples', async () => {
Expand Down
3 changes: 3 additions & 0 deletions cli/test/lib/commands/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ describe('@cube-creator/cli/lib/commands/transform', function () {
path: rdf.type,
hasValue: cube.Observation,
minCount: 1,
maxCount: 1,
}, {
path: cube.observedBy,
hasValue: $rdf.namedNode('https://ld.admin.ch/office/VII.1.7'),
minCount: 1,
maxCount: 1,
}, {
path: $rdf.namedNode('https://environment.ld.admin.ch/foen/ubd/28/dimension/year'),
hasValue: $rdf.literal('2000', xsd.gYear),
Expand Down
10 changes: 5 additions & 5 deletions fuseki/sample-ubd.trig
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ graph <cube-project/ubd/cube-data> {

<https://environment.ld.admin.ch/foen/ubd/28/observation/blBAS-2003-annualmean>
a cube:Observation ;
cube:observedBy <urn:will:replace> ;
cube:observedBy <https://ld.admin.ch/office/VII.1.7> ;
<https://environment.ld.admin.ch/foen/ubd/28/aggregation> <https://environment.ld.admin.ch/foen/ubd/28/aggregation/annualmean> ;
<https://environment.ld.admin.ch/foen/ubd/28/dimension/value> "6.2"^^xsd:decimal ;
<https://environment.ld.admin.ch/foen/ubd/28/dimension/year> "2003"^^xsd:gYear ;
Expand All @@ -219,7 +219,7 @@ graph <cube-project/ubd/cube-data> {

<https://environment.ld.admin.ch/foen/ubd/28/observation/blBAS-2002-annualmean>
a cube:Observation ;
cube:observedBy <urn:will:replace> ;
cube:observedBy <https://ld.admin.ch/office/VII.1.7> ;
<https://environment.ld.admin.ch/foen/ubd/28/aggregation> <https://environment.ld.admin.ch/foen/ubd/28/aggregation/annualmean> ;
<https://environment.ld.admin.ch/foen/ubd/28/dimension/value> "6.1"^^xsd:decimal ;
<https://environment.ld.admin.ch/foen/ubd/28/dimension/year> "2002"^^xsd:gYear ;
Expand All @@ -230,7 +230,7 @@ graph <cube-project/ubd/cube-data> {

<https://environment.ld.admin.ch/foen/ubd/28/observation/blBAS-2004-annualmean>
a cube:Observation ;
cube:observedBy <urn:will:replace> ;
cube:observedBy <https://ld.admin.ch/office/VII.1.7> ;
<https://environment.ld.admin.ch/foen/ubd/28/aggregation> <https://environment.ld.admin.ch/foen/ubd/28/aggregation/annualmean> ;
<https://environment.ld.admin.ch/foen/ubd/28/dimension/value> "6.1"^^xsd:decimal ;
<https://environment.ld.admin.ch/foen/ubd/28/dimension/year> "2002"^^xsd:gYear ;
Expand All @@ -242,7 +242,7 @@ graph <cube-project/ubd/cube-data> {

<https://environment.ld.admin.ch/foen/ubd/28/observation/blBAS-1999-annualmean>
a cube:Observation ;
cube:observedBy <urn:will:replace> ;
cube:observedBy <https://ld.admin.ch/office/VII.1.7> ;
<https://environment.ld.admin.ch/foen/ubd/28/aggregation> <https://environment.ld.admin.ch/foen/ubd/28/aggregation/annualmean> ;
<https://environment.ld.admin.ch/foen/ubd/28/dimension/value> "5.4"^^xsd:decimal ;
<https://environment.ld.admin.ch/foen/ubd/28/dimension/year> "1999"^^xsd:gYear ;
Expand Down Expand Up @@ -282,7 +282,7 @@ graph <cube-project/ubd/cube-data> {
sh:path <https://environment.ld.admin.ch/foen/ubd/28/aggregation> .

_:observedBy
sh:in ( <https://ld.stadt-zuerich.ch/> ) ;
sh:in ( <https://ld.admin.ch/office/VII.1.7> ) ;
sh:maxCount 1 ;
sh:minCount 1 ;
sh:nodeKind sh:IRI ;
Expand Down
4 changes: 4 additions & 0 deletions packages/model/Job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface Job extends Action, Rdfs.Resource, RdfResource {
}

export interface TransformJob extends Job {
project: NamedNode
cubeGraph: NamedNode
tableCollection: Link<TableCollection>
dimensionMetadata: Link<DimensionMetadataCollection>
Expand Down Expand Up @@ -95,6 +96,9 @@ JobMixin.appliesTo = cc.Job

export function TransformJobMixin<Base extends Constructor<RdfResource>>(base: Base): Mixin {
class Impl extends ResourceMixin(ActionMixin(base)) implements Partial<TransformJob> {
@property({ path: cc.project })
project!: NamedNode

@property.resource({ path: cc.tables })
tableCollection!: Link<TableCollection>

Expand Down

0 comments on commit e41d588

Please sign in to comment.