-
Notifications
You must be signed in to change notification settings - Fork 4
/
shacl-check.js
447 lines (414 loc) · 17.8 KB
/
shacl-check.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// SHACL RDF shape checker
//
// This is or was https://linkeddata.github.io/shacl-check
// MIT Licence
//
// This is probably incomplete implementation of SHACL
// See https://www.w3.org/TR/shacl/
const $rdf = require('rdflib')
const RDF = $rdf.Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
const sh = $rdf.Namespace('http://www.w3.org/ns/shacl#')
const a = RDF('type')
// see sh:nodeKind for these values
const termTypeMap = {'NamedNode': 'IRI'}
class ShapeChecker {
constructor (kb, shapeDoc, targetDoc, reportDoc, options) {
this.kb = kb
this.shapeDoc = shapeDoc
this.targetDoc = targetDoc
this.reportDoc = reportDoc
this.checked = [] // avoid dups
this.options = options || {} // eg noResultMessage
if (reportDoc) {
this.report = this.kb.bnode()
// this.report = this.kb.sym(reportDoc.uri + '#this')
this.kb.add(this.report, a, sh('ValidationReport'), this.reportDoc)
} else {
console.log('ShapeChecker: NO REPORT DOCUMENT GIVEN')
}
}
complain (message) {
console.log(message)
}
// See https://www.w3.org/TR/shacl/#validation-report
validationResult (issue) {
this.conforms = false
if (this.reportDoc) {
let result = this.kb.bnode()
this.kb.add(this.report, sh('result'), result, this.reportDoc)
this.kb.add(result, a, sh('ValidationResult'), this.reportDoc)
this.kb.add(result, sh('focusNode'), issue.node, this.reportDoc)
this.kb.add(result, sh('sourceShape'), issue.shape, this.reportDoc)
if (issue.path) this.kb.add(result, sh('resultPath'), issue.path, this.reportDoc)
// Extra - not in spec but valuable for diagnostics - which rule is broken?
if (issue.property && issue.property.uri) {
this.kb.add(result, sh('resultProperty'), issue.property, this.reportDoc)
}
if (issue.object !== undefined) {
this.kb.add(result, sh('value'), issue.object, this.reportDoc)
}
if (!this.options.noResultMessage) {
this.kb.add(result, sh('resultMessage'), issue.message, this.reportDoc)
}
let severity
if (issue.property) { // Property has precedence
severity = this.kb.any(issue.property, sh('severity'))
}
if (!severity) { // Then shape, then default.
severity = this.kb.any(issue.shape, sh('severity')) || sh('Violation')
}
this.kb.add(result, sh('resultSeverity'), severity, this.reportDoc)
this.kb.add(result, sh('sourceConstraintComponent'), sh(issue.cc + 'ConstraintComponent'), this.reportDoc)
}
}
// Find any shapes which should be applied to nodes
execute () {
const checker = this
const kb = this.kb
this.conforms = true
const post = function (issues) {
if (!issues) return
for (let i = 0; i < issues.length; i++) {
checker.validationResult(issues[i])
}
}
kb.statementsMatching(null, sh('targetClass'), null, this.shapeDoc)
.forEach(function (st) {
var targetClass = st.object
var shape = st.subject
console.log(' Target class ' + targetClass)
kb.each(null, a, targetClass).forEach(function (target) {
console.log(' Target class member ' + target)
post(checker.checkNodeShape(target, shape, true))
})
})
kb.statementsMatching(null, sh('targetObjectsOf'), null, this.shapeDoc)
.forEach(function (st) {
var targetProperty = st.object
var shape = st.subject
console.log(' Target objectsOf property ' + targetProperty)
kb.statementsMatching(null, targetProperty).forEach(function (st) {
let target = st.object
console.log(' targetObjectsOf target ' + target)
post(checker.checkNodeShape(target, shape, true))
})
})
kb.statementsMatching(null, sh('targetSubjectsOf'), null, this.shapeDoc)
.forEach(function (st) {
var targetProperty = st.object
var shape = st.subject
console.log(' Target subjectOf property ' + targetProperty)
kb.statementsMatching(null, targetProperty).forEach(function (st) {
let target = st.subject
// console.log(' Target subjectOf subject: ' + target)
post(checker.checkNodeShape(target, shape, true))
})
})
kb.statementsMatching(null, sh('targetNode'), null, this.shapeDoc)
.forEach(function (st) {
var target = st.object
var shape = st.subject
console.log('Target node ' + target)
post(checker.checkNodeShape(target, shape))
})
if (this.reportDoc) {
kb.add(this.report, sh('conforms'), this.conforms, this.reportDoc)
}
}
// Return an array of nodes at the end of the path
followPath (node, path) {
const checker = this
let pred = this.kb.any(path, sh('inversePath'))
if (pred) {
return this.kb.each(null, pred, node)
}
if (path.termType === 'Collection') { // A base list is a sequence
var results = []
var followSeq = function (node, array) {
if (array.length === 0) {
results.push(node)
}
checker.followPath(node, array[0]).forEach(function (next) {
followSeq(next, array.slice(1))
})
}
followSeq(node, path.elements)
return results
}
let alt = this.kb.any(path, a, sh('alternative'))
if (alt) {
let results = []
for (let i = 0; i < alt.length; i++) {
results = results.concat(checker.followPath(alt.elements[i]))
}
return results
}
let sub = this.kb.any(path, a, sh('oneOrMorePath'))
if (sub) {
let soFar = checker.followPath(node, sub)
if (soFar.length === 0) {
this.complain('Sould be at least one')
}
return soFar
}
sub = this.kb.any(path, a, sh('zeroOrMorePath'))
if (sub) {
let soFar = checker.followPath(node, sub)
// if (soFar.length ) // Semantics if this?
return soFar
}
sub = this.kb.any(path, a, sh('zeroOrOnePath'))
if (sub) {
let soFar = checker.followPath(node, sub)
if (soFar.length > 1) {
this.complain('Sould be no more than one')
}
return soFar
}
// default is this is just a forward predicate
return this.kb.each(node, path)
}
// Returns an array of issues else false
//
checkNodeShape (node, shape, avoidDuplicates) {
const kb = this.kb
const checker = this
avoidDuplicates = !!avoidDuplicates
console.log(' Applying shape ' + shape)
// console.log(' to node ' + node) // Obvious from previous output
const key = node.uri + ' - ' + shape.uri
if (avoidDuplicates && node.uri && shape.uri && this.checked[key]) {
console.log(' duplicate.')
return
}
this.checked[key] = true
var closed = kb.anyValue(shape, sh('closed'))
closed = {'true': true, '1': true}[closed] || false
// console.log(' closed: ' + closed)
var allowed = []
if (closed) {
var ignoredProperties = kb.any(shape, sh('ignoredProperties'))
if (ignoredProperties) {
ignoredProperties = ignoredProperties.elements
for (let k = 0; k < ignoredProperties.length; k++) {
// console.log(' Ignoreable: ' + ignoredProperties[k])
allowed[ignoredProperties[k].uri] = true
}
}
}
var issues = []
const noteIssue = function (issue) {
console.log(' -> ' + issue.message)
issues.push(issue)
}
kb.each(shape, sh('property')).forEach(function (property) {
// console.log(' @@ property ' + property)
let path = kb.the(property, sh('path')) // Assume simple predicate at this stage
let constraint
if (!path) {
console.log('@@ SHAPE ERROR: Property has no path! ' + kb.connectedStatements(property).length)
console.log('as subject: ' + kb.statementsMatching(property).length)
console.log('as object: ' + kb.statementsMatching(null, null, property).length)
console.log('property: ' + property)
console.log('shape: ' + shape)
console.log('about shape: ' + kb.connectedStatements(shape).map(x => x.toNT()))
console.log('node: ' + node)
throw new Error('@@ SHAPE ERROR: Property has no path! in shape ' + shape)
}
// console.log(' Checking property ' + property + ': ' + path)
let values = checker.followPath(node, path)
if (path.uri) { // Simple paths only
allowed[path.uri] = true
}
let minCount = kb.anyValue(property, sh('minCount'))
if (minCount && values.length < minCount) {
noteIssue({node, shape, property, path, cc: 'MinCount', message: 'Too few (' + values.length + ') ' + path + ' on ' + node})
}
let maxCount = kb.anyValue(property, sh('maxCount'))
if (maxCount && values.length > maxCount) {
noteIssue({node, shape, property, path, cc: 'MaxCount', message: 'Too many (' + values.length + ') ' + path + ' on ' + node})
}
for (var i = 0; i < values.length; i++) {
let object = values[i]
// Extra: checking for forward and backward links
constraint = kb.any(property, RDF('type'), sh('ForwardLink'))
if (constraint && !kb.holds(node, path, object, node.doc())) {
noteIssue({node, shape, property, path, cc: 'ForwardLink', message: 'ForwardLink to ' + object + ' not stored at subject ' + node})
}
constraint = kb.any(property, RDF('type'), sh('BackwardLink'))
if (constraint && !kb.holds(node, path, object, object.doc())) {
noteIssue({node, shape, property, path, cc: 'BackwardLink', message: 'BackwardLink subject ' + node + ' not stored at object ' + object})
}
// ///// Checking for object class membership
let constraints = kb.each(property, sh('class'))
if (constraints) {
constraints.forEach(function (constraint) {
if (constraint && !kb.holds(object, a, constraint)) {
noteIssue({node, shape, property, path, cc: 'Class', message: 'Error ' + object + ' should be in class ' + constraint})
}
})
}
// ////////////////////////////// Comparing with other predicates
constraint = kb.anyValue(property, sh('equals'))
if (constraint) {
let others = kb.each(node, constraint) // @@ extend to general paths??
for (let k = 0; k < others.length; k++) {
if (!object.sameTerm(others[k])) {
noteIssue({node, shape, property, path, object, cc: 'Equals', message: 'Error ' + object + ' should be equal to ' + constraint + ' that is, ' + others[k]})
}
}
}
constraint = kb.anyValue(property, sh('disjoint'))
if (constraint) {
let others = kb.each(node, constraint) // @@ extend to general paths??
for (let k = 0; k < others.length; k++) {
if (object.sameTerm(others[k])) {
noteIssue({node, shape, property, path, object, cc: 'Equals', message: 'Error ' + object + ' should be NOT equal to ' + constraint + ' that is, ' + others[k]})
}
}
}
// @@ todo: LessThan etc etc
// /////////////////////////// Range constraints
constraint = kb.anyValue(property, sh('minInclusive'))
if (constraint && !(object.value && object.value >= constraint)) { // @@@@ use som toJS to make work for any datatype
noteIssue({node, shape, property, path, object, cc: 'MinInclusive', message: 'Error ' + object + ' should more than or equal to ' + constraint})
}
constraint = kb.anyValue(property, sh('minExclusive'))
if (constraint && !(object.value && object.value > constraint)) { // @@@@ use som toJS to make work for any datatype
noteIssue({node, shape, property, path, object, cc: 'MinExclusive', message: 'Error ' + object + ' should more than ' + constraint})
}
constraint = kb.anyValue(property, sh('maxInclusive'))
if (constraint && !(object.value && object.value <= constraint)) { // @@@@ use som toJS to make work for any datatype
noteIssue({node, shape, property, path, object, cc: 'MaxInclusive', message: 'Error ' + object + ' should less than or equal to ' + constraint})
}
constraint = kb.anyValue(property, sh('maxExclusive'))
if (constraint && !(object.value && object.value < constraint)) { // @@@@ use som toJS to make work for any datatype
noteIssue({node, shape, property, path, object, cc: 'MaxExclusive', message: 'Error ' + object + ' should less than or equal to ' + constraint})
}
constraint = kb.anyValue(property, sh('minLength'))
if (constraint && !(object.value && object.value.length < constraint)) { // @@@@ use som toJS to make work for any datatype
noteIssue({node, shape, property, path, object, cc: 'MinLength', message: 'Error ' + object + ' should less than or equal to ' + constraint})
}
constraint = kb.anyValue(property, sh('maxLength'))
if (constraint && !(object.value && object.value.length > constraint)) { // @@@@ use som toJS to make work for any datatype
noteIssue({node, shape, property, path, object, cc: 'MexLength', message: 'Error ' + object + ' should less than or equal to ' + constraint})
}
// //////////////////////////////// Others
constraint = kb.any(property, sh('datatype'))
if (constraint && !(object.datatype && object.datatype.sameTerm(constraint))) {
noteIssue({node, shape, property, path, object, cc: 'Datatype', message: 'Error ' + object + ' should be datatype ' + constraint})
}
constraint = kb.anyValue(property, sh('languageIn'))
if (constraint) {
let langs = constraint.elements.map(function (x) { return x.value }).join(',')
if (!(object.lang && langs.includes(object.lang))) {
noteIssue({node, shape, property, path, object, cc: 'LanguageIn', message: 'Error ' + object + ' should be in one of languages: ' + langs})
}
}
constraint = kb.anyValue(property, sh('pattern'))
if (constraint) {
let regexp = new RegExp(constraint)
if (!(object.value && object.value.match(regexp))) {
noteIssue({node, shape, property, path, object, cc: 'Pattern', message: 'Error: "' + object + '" should match pattern ' + regexp})
}
}
constraint = kb.any(property, sh('nodeKind'))
if (constraint) {
let actual = termTypeMap[object.termType] || object.termType
let allowed = constraint.uri.split('#')[1] // eg 'BlankNodeOrIRI'
if (!allowed.includes(actual)) {
noteIssue({node, shape, property, path, object, cc: 'NodeKind', message: 'Error ' + object + ' should be of node kind: ' + allowed})
}
}
// //////////////////// Logical ones
constraint = kb.any(property, sh('node'))
if (constraint && !(checker.checkNodeShape(object, constraint))) {
noteIssue({node, shape, property, path, object, cc: 'Node', message: 'Error ' + object + ' should match shape ' + constraint})
}
constraint = kb.any(property, sh('not'))
if (constraint && (checker.checkNodeShape(object, constraint))) {
noteIssue({node, shape, property, path, object, cc: 'Not', message: 'Error ' + object + ' should NOT match shape ' + constraint})
}
constraint = kb.any(property, sh('and'))
if (constraint) {
let shapes = constraint.elements
let iss = []
for (let i = 0; i < shapes.length; i++) {
let res = checker.checkNodeShape(object, shapes[i])
if (res) {
iss = iss.concat(res)
}
}
if (iss.length) {
noteIssue({node,
shape,
property,
path,
object,
cc: 'And',
message: 'Error ' + object + ' does not match ALL shapes ' + constraint})
issues = issues.concat(iss) // @@@ Give all these details too? it is logical
}
}
constraint = kb.any(property, sh('or'))
var ok = true
if (constraint) {
let shapes = constraint.elements
let iss = []
ok = false
for (let i = 0; i < shapes.length; i++) {
let res = checker.checkNodeShape(object, shapes[i])
if (res) {
iss = iss.concat(res)
} else {
ok = true
}
}
if (!ok) {
noteIssue({node,
shape,
property,
path,
object,
cc: 'Or',
message: 'Error ' + object + ' does not match EITHER shape ' + constraint})
// issues = issues.concat(iss)
}
}
constraint = kb.any(property, sh('xone'))
if (constraint) {
let shapes = constraint.elements
// let iss = []
let matches = 0
for (let i = 0; i < shapes.length; i++) {
let res = checker.checkNodeShape(object, shapes[i])
if (res) {
matches += 1
// iss = iss.concat(res)
}
}
if (!ok) {
noteIssue({node,
shape,
property,
path,
object,
cc: 'Xone',
message: 'Error ' + object + ' does not match EXACTLY ONE shape ' + constraint})
// issues = issues.concat(iss)
}
}
} // next value
}) // next property
if (closed) {
kb.statementsMatching(node).forEach(function (st) {
if (!allowed[st.predicate.uri]) {
noteIssue({shape, node, object: st.object, cc: 'Closed', message: 'Closed node has extra data: ' + st})
}
})
}
return issues.length ? issues : false
} // method
} // End of class ShapeChecker
module.exports = ShapeChecker
// //////////////////////////////