@prefix code: <https://code.described.at/> .
@prefix p: <https://pipeline.described.at/> .
@prefix shacl: <https://barnard59.zazuko.com/operations/shacl/> .
@prefix base: <https://barnard59.zazuko.com/operations/base/> .
@prefix getDataset: <https://barnard59.zazuko.com/operations/rdf/getDataset> .
@prefix splitDataset: <https://barnard59.zazuko.com/operations/rdf/splitDataset/> .
@prefix n3: <https://barnard59.zazuko.com/operations/formats/n3/> .
@prefix ntriples: <https://barnard59.zazuko.com/operations/formats/ntriples/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

@base <http://barnard59.zazuko.com/pipeline/cube-validation/> .


_:constraint a p:Variable ;
  p:name "constraint" ;
  rdfs:label "cube constraint file" ;
.

_:batchSize a p:Variable ;
  p:name "batchSize" ;
  rdfs:label "batch size" ;
  p:value 50 ;
.

_:maxViolations a p:Variable ;
  p:name "maxViolations" ;
  rdfs:label "max number of violations" ;
  p:value 500 ;
.

_:sortChunkSize a p:Variable ;
  p:name "sortChunkSize" ;
  rdfs:label "sort chunk size" ;
  p:value 100000 ;
.

<check-observations> a p:Pipeline , p:Readable;
  p:variables [ p:variable _:constraint , _:batchSize , _:maxViolations , _:sortChunkSize ] ;
  p:steps
    [
      p:stepList
      (
        [ base:stdin () ]
        [ n3:parse () ]
        _:sortBySubject
        [ splitDataset:bySubject () ]
        [ base:batch ("batchSize"^^p:VariableName) ]
        _:merge
        [ shacl:report
          [code:name "shape" ; code:value _:getConstraint ] ,
          [code:name "maxErrors" ; code:value "maxViolations"^^p:VariableName ]
        ]
        [ base:flatten () ]
        [ ntriples:serialize () ]
      )
    ]
.

_:getConstraint a p:Pipeline , p:ReadableObjectMode ;
  p:variables [ p:variable _:constraint ] ;
  p:steps
    [
      p:stepList
        (
          _:readConstraint
          [ getDataset: () ]
          _:addTarget
          [ base:flatten () ]
        )
    ]
.

_:readConstraint a p:Step ;
  code:implementedBy """
  function (path) {
    return this.env.fromFile(path)
  }
  """^^code:EcmaScript ;
  code:arguments ("constraint"^^p:VariableName)
.

_:merge base:map (
  [
    a code:EcmaScriptModule ;
    code:link <file:../lib/merge.js#default>
  ]
) .

_:addTarget base:map (
  [
    a code:EcmaScriptModule ;
    code:link <file:../lib/target.js#addTarget>
  ]
) .


_:sortBySubject a p:Step ;
  code:implementedBy [ a code:EcmaScriptModule ;
    code:link <file:../lib/sort.js#sortBySubject>
  ] ;
  code:arguments ("sortChunkSize"^^p:VariableName)
.