UNPKG

@rdfc/sds-storage-writer-ts

Version:

An RDF-Connect processor to write SDS streams into a given storage system

56 lines (55 loc) 4.36 kB
import { RDF as RDFT, SDS } from "@treecg/types"; import { Parser } from "n3"; import { extractShapes, match, subject } from "rdf-lens"; const Shapes = extractShapes(new Parser().parse("@prefix cidoc: <http://www.cidoc-crm.org/cidoc-crm/>.\n@prefix rdfl: <https://w3id.org/rdf-lens/ontology#>.\n@prefix sosa: <http://www.w3.org/ns/sosa/>.\n@prefix dcterms: <http://purl.org/dc/terms/>.\n@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.\n@prefix sh: <http://www.w3.org/ns/shacl#>.\n@prefix sds: <https://w3id.org/sds#>.\n\n[ ] a sh:NodeShape;\n sh:targetClass <Record>;\n sh:property [\n sh:name \"stream\";\n sh:path sds:stream;\n sh:datatype xsd:string;\n sh:minCount 1;\n sh:maxCount 1;\n ], [\n sh:name \"payload\";\n sh:path sds:payload;\n sh:datatype xsd:string;\n sh:minCount 1;\n sh:maxCount 1;\n ], [\n sh:name \"buckets\";\n sh:path sds:bucket;\n sh:datatype xsd:string;\n ], [\n sh:name \"dataless\";\n sh:path sds:dataless;\n sh:datatype xsd:boolean;\n sh:minCount 0;\n sh:maxCount 1;\n ].\n\n[ ] a sh:NodeShape;\n sh:targetClass <Bucket>;\n sh:property [\n sh:name \"id\";\n sh:path ( );\n sh:datatype xsd:string;\n sh:minCount 1;\n sh:maxCount 1;\n ], [\n sh:name \"streamId\";\n sh:path sds:stream;\n sh:datatype xsd:string;\n sh:minCount 1;\n sh:maxCount 1;\n ], [\n sh:name \"immutable\";\n sh:path sds:immutable;\n sh:datatype xsd:boolean;\n sh:maxCount 1;\n ], [\n sh:name \"root\";\n sh:path sds:isRoot;\n sh:datatype xsd:boolean;\n sh:maxCount 1;\n ], [\n sh:name \"empty\";\n sh:path sds:empty;\n sh:datatype xsd:boolean;\n sh:maxCount 1;\n ].\n\n[ ] a sh:NodeShape;\n sh:targetClass <Relation>;\n sh:property [\n sh:name \"type\";\n sh:path sds:relationType;\n sh:datatype xsd:string;\n sh:minCount 1;\n sh:maxCount 1;\n ], [\n sh:name \"stream\";\n sh:path ( [ sh:inversePath sds:relation ] sds:stream );\n sh:datatype xsd:string;\n sh:minCount 1;\n sh:maxCount 1;\n ], [\n sh:name \"origin\";\n sh:path [ sh:inversePath sds:relation ];\n sh:datatype xsd:string;\n sh:minCount 1;\n sh:maxCount 1;\n ], [\n sh:name \"bucket\";\n sh:path sds:relationBucket;\n sh:datatype xsd:string;\n sh:minCount 1;\n sh:maxCount 1;\n ], [\n sh:name \"path\";\n sh:path sds:relationPath;\n sh:class <RdfThing>;\n sh:maxCount 1;\n ], [\n sh:name \"value\";\n sh:path sds:relationValue;\n sh:class <RdfThing>;\n sh:maxCount 1;\n ].\n\n[ ] a sh:NodeShape;\n sh:targetClass <RdfThing>;\n sh:property [\n sh:name \"id\";\n sh:path ( );\n sh:maxCount 1;\n sh:minCount 1;\n sh:datatype xsd:any;\n ], [\n sh:name \"quads\";\n sh:path ( );\n sh:maxCount 1;\n sh:minCount 1;\n sh:class rdfl:CBD;\n ].\n\n")); const RecordLens = match(undefined, SDS.terms.payload, undefined) .thenAll(subject) .thenSome(Shapes.lenses["Record"]); const BucketLens = match(undefined, RDFT.terms.type, SDS.terms.custom("Bucket")) .thenAll(subject) .thenSome(Shapes.lenses["Bucket"]); const RelationLens = match(undefined, RDFT.terms.type, SDS.terms.custom("Relation")) .thenAll(subject) .thenSome(Shapes.lenses["Relation"]); export class Extract { data = []; description = []; removeDescription = []; constructor(full) { full.forEach((q) => { if (q.graph.equals(SDS.terms.custom("DataDescription"))) { this.description.push(q); } else if (q.graph.equals(SDS.terms.custom("RemoveDataDescription"))) { this.removeDescription.push(q); } else { this.data.push(q); } }); } getData() { return this.data; } getRecords() { return RecordLens.execute(this.description); } getBuckets() { return BucketLens.execute(this.description); } getRelations() { return RelationLens.execute(this.description); } getRemoveRelations() { return RelationLens.execute(this.removeDescription); } } export class Extractor { constructor() { } extract_quads(quads) { return new Extract(quads); } extract(inp) { return new Extract(new Parser().parse(inp)); } }