1"""Validate sparse typed SH.value occurrences before constructing report terms."""
2
3from rdflib.namespace import RDF, SH, XSD
4
5
6def boolean_occurrences(records, quads, entries):
7 def iri(index, value):
8 return (
9 type(index) is int
10 and 0 <= index < len(records)
11 and tuple(records[index]) == (0, str(value), None, None)
12 )
13
14 results = {(s, g) for s, p, o, g in quads if iri(p, RDF.type) and iri(o, SH.ValidationResult)}
15 result = {}
16 for entry in entries:
17 if not isinstance(entry, (list, tuple)) or len(entry) != 2:
18 raise ValueError("Invalid Boolean report occurrence")
19 index, code = entry
20 if type(index) is not int or not 0 <= index < len(quads):
21 raise ValueError("Invalid Boolean report quad index")
22 if type(code) is not int or not 1 <= code <= 6 or index in result:
23 raise ValueError("Invalid or duplicate Boolean report state")
24 subject, predicate, value, context = quads[index]
25 if (
26 not iri(predicate, SH.value)
27 or records[value][0] != 2
28 or records[value][1] not in ("false", "true")
29 or tuple(records[value][2:]) != (str(XSD.boolean), None)
30 or (subject, context) not in results
31 ):
32 raise ValueError("Boolean occurrence is not a direct ValidationResult value")
33 result[index] = (code >= 4, (None, False, True)[(code - 1) % 3])
34 return result