-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.ts
81 lines (62 loc) · 2.92 KB
/
Parser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { ShaclParserInterface, Options, ParserOutput } from './types.ts'
import { N3Parser, RdfObjectLoader, Resource } from './deps.ts'
import { classAttributes, propertyAttributes } from './attributeHandlers.ts'
import postHandlers from './postHandlers.ts'
export class Parser {
shaclParser: ShaclParserInterface
constructor (options: Options = {}) {
this.shaclParser = options.shaclParserClass ?? new N3Parser()
}
/**
* Parsing of the SHACL string into Quads and then into the SHACL properties
*/
async parse (shaclTurtleString: string): Promise<ParserOutput> {
const quads = await this.shaclParser.parse(shaclTurtleString)
const context = { ...this.shaclParser._prefixes, sh: 'http://www.w3.org/ns/shacl#' }
const objectLoader = new RdfObjectLoader({ context })
await objectLoader.importArray(quads)
const shaclProperties: ParserOutput = {}
for (const [iri, resource] of Object.entries(objectLoader.resources)) {
const isNodeShape = resource.property['http://www.w3.org/1999/02/22-rdf-syntax-ns#type']?.term?.value === 'http://www.w3.org/ns/shacl#NodeShape'
if (!isNodeShape) continue
const shaclClassAttributes = this.processProperty(resource, classAttributes, {})
shaclProperties[iri] = {
attributes: shaclClassAttributes,
properties: []
}
for (const shaclProperty of resource.properties['sh:property']) {
const property = this.processProperty(shaclProperty)
if (Object.keys(property).length) shaclProperties[iri].properties.push(property)
}
}
return shaclProperties
}
/**
* One property process,
* First attribute handlers,
* Then post handlers.
*
* Post handlers use the data created by the attribute handlers.
*
* It is possible to skip post handlers, this is done for nested statements such as sh:or sh:and.
*/
processProperty (shaclProperty: Resource, attributeHandlers: any = propertyAttributes, afterHandlers: any = postHandlers) {
const property = {}
for (const [compactedPredicate, handler] of Object.entries(attributeHandlers)) {
const singular = shaclProperty.property[compactedPredicate]
const plural = shaclProperty.properties[compactedPredicate]
if (!(singular && plural)) continue
/** @ts-ignore */
const handlerOutput = handler.call(this, singular, plural)
if (!handlerOutput) continue
Object.assign(property, handlerOutput)
}
for (const handler of Object.values(afterHandlers)) {
/** @ts-ignore */
const handlerOutput = handler.call(this, property)
if (!handlerOutput) continue
Object.assign(property, handlerOutput)
}
return property
}
}